1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package JWebmail::Model::Driver::Mock;
use Mojo::Base -base;
use List::Util 'sum';
use Mojo::JSON qw(decode_json);
use constant {
VALID_USER => 'me@example.de',
VALID_PW => 'vwxyz',
};
use constant {
LIST_START => 0,
LIST_END => 1,
LIST_SORT => 2,
LIST_FOLDER => 3,
};
sub _read_json_file {
my ($file_name) = @_;
use constant PREFIX => 't/private/';
open(my $body_file, '<', PREFIX . $file_name);
local $/;
my $body = <$body_file>;
close $body_file;
return decode_json($body);
}
sub list_reply {
state $init = _read_json_file('msgs.json');
}
sub read_reply {
state $init = {
'SC-ORD-MAIL54526c63b751646618a793be3f8329cca@sc-ord-mail5' => _read_json_file('msg2.json'),
'example' => _read_json_file('msg.json'),
};
}
sub communicate {
no warnings 'experimental::smartmatch';
my $self = shift;
my %args = @_;
given ($args{mode}) {
when ('auth') {
return (undef, 0) if $args{user} eq VALID_USER && $args{password} eq VALID_PW;
return (undef, 2);
}
when ('list') {
return ([@{ $self->list_reply }[$args{args}->[LIST_START]..$args{args}->[LIST_END]]], 0) if !$args{args}->[LIST_SORT];
return ([], 0) if $args{args}->[LIST_FOLDER] eq 'test';
my $s = sub {
my $sort_by = $args{args}->[LIST_SORT];
my $rev = $sort_by !~ m/^![[:lower:]]+/ ? 1 : -1;
$sort_by =~ s/!//;
return ($a->{$sort_by} cmp $b->{$sort_by}) * $rev;
};
return ([sort { &$s } @{ $self->list_reply }[$args{args}->[LIST_START]..$args{args}->[LIST_END]]], 0);
}
when ('count') {
return ({
count => scalar(@{ $self->list_reply }),
size => sum(map {$_->{size}} @{ $self->list_reply }),
new => 0,
}, 0);
}
when ('read-mail') {
my $mid = $args{args}->[0];
my $mail = $self->read_reply->{$mid};
return ($mail, 0) if $mail;
return ({error => 'unkown mail-id'}, 3);
}
when ('folders') {
return ([qw(cur test devel debug)], 0);
}
when ('move') {
local $, = ' ';
say "@{ $args{args} }";
return (undef, 0);
}
default { return ({error => 'unkown mode'}, 3); }
}
}
1
__END__
=head1 NAME
Mock - Simple file based mock for the L<JWebmail::Model::ReadMails> module.
=cut
|