diff options
author | Jannis M. Hoffmann <jannis.hoffmann@rwth-aachen.de> | 2021-08-24 11:10:39 +0200 |
---|---|---|
committer | Jannis M. Hoffmann <jannis.hoffmann@rwth-aachen.de> | 2021-08-24 11:10:39 +0200 |
commit | 2388747fbf954de990862a6c01713d50823e8725 (patch) | |
tree | c3b3a7dc80f1d6a978d25e3048f7a6afb9fa08e0 /lib/JWebmail/Model/Driver/MockMaildir.pm | |
parent | bf5554febae6f299c716b5a9582c6bfd6980b728 (diff) |
overhauled testing
Diffstat (limited to 'lib/JWebmail/Model/Driver/MockMaildir.pm')
-rw-r--r-- | lib/JWebmail/Model/Driver/MockMaildir.pm | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/JWebmail/Model/Driver/MockMaildir.pm b/lib/JWebmail/Model/Driver/MockMaildir.pm new file mode 100644 index 0000000..e8956ed --- /dev/null +++ b/lib/JWebmail/Model/Driver/MockMaildir.pm @@ -0,0 +1,57 @@ +package JWebmail::Model::Driver::MockMaildir; + +use Mojo::Base -base; + +use Mojo::JSON 'decode_json'; + + +has user => sub { $ENV{USER} }; +has maildir => 't/'; +has extractor => 'perl'; + + +our %EXTRACTORS = ( + perl => 'perl lib/JWebmail/Model/Driver/QMailAuthuser/Extract.pm', + rust => 'extract/target/debug/jwebmail-extract', +); + +use constant { + VALID_USER => 'me@mockmaildir.com', + VALID_PW => '12345', +}; + +sub communicate { + my $self = shift; + my %args = @_; + + if ($args{mode} eq 'auth') { + return ("", 0) if $args{user} eq VALID_USER && $args{password} eq VALID_PW; + return ("", 1); + } + + my $mail_user = 'maildir'; + my $exec = $EXTRACTORS{$self->extractor} . ' ' . join(' ', map { $_ =~ s/(['\\])/\\$1/g; "'$_'" } ($self->maildir, $self->user, $mail_user, $args{mode}, @{$args{args}})); + + my $pid = open(my $reader, '-|', $exec) + or die 'failed to create subprocess'; + + my $input = <$reader>; + + waitpid($pid, 0); + my $rc = $? >> 8; + + my $resp; + if ($rc == 3 || $rc == 0) { + eval { $resp = decode_json $input; }; + if (my $err = $@) { $resp = {error => "decoding error '$err'"}; $rc ||= 1; }; + } + elsif ($rc) { + $resp = {error => "qmail-authuser returned code: $rc"}; + } + + return ($resp, $rc); +} + + +1 + |