summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Model/ReadMails/MockJSON.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/JWebmail/Model/ReadMails/MockJSON.pm')
-rw-r--r--lib/JWebmail/Model/ReadMails/MockJSON.pm119
1 files changed, 119 insertions, 0 deletions
diff --git a/lib/JWebmail/Model/ReadMails/MockJSON.pm b/lib/JWebmail/Model/ReadMails/MockJSON.pm
new file mode 100644
index 0000000..bb105d1
--- /dev/null
+++ b/lib/JWebmail/Model/ReadMails/MockJSON.pm
@@ -0,0 +1,119 @@
+package JWebmail::Model::ReadMails::MockJSON;
+
+use v5.24;
+use warnings;
+use utf8;
+
+use List::Util 'sum';
+
+use Role::Tiny::With;
+
+use Mojo::JSON qw(decode_json);
+
+use constant {
+ VALID_USER => 'mockjson@example.com',
+ VALID_PW => 'vwxyz',
+};
+
+use namespace::clean;
+
+with 'JWebmail::Model::ReadMails::Role';
+
+
+sub new { bless {} }
+
+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 verify_user {
+ my $self = shift;
+ my $auth = shift;
+
+ return $auth->{user} eq VALID_USER && $auth->{password} eq VALID_PW;
+}
+
+sub read_headers_for {
+ my $self = shift;
+ my $auth = shift;
+ my %args = @_;
+
+ my ($start, $end, $sort, $folder) = @args{qw(start end sort folder)};
+
+ unless ($sort) {
+ return [@{ $self->list_reply }[$start..$end]];
+ }
+ if ($folder eq 'test') {
+ return [];
+ }
+ my $s = sub {
+ my $sort_by = $sort;
+ my $rev = $sort_by !~ m/^![[:lower:]]+/ ? 1 : -1;
+ $sort_by =~ s/^!//;
+ return (($a->{$sort_by}||$a->{head}{$sort_by}) cmp ($b->{$sort_by}||$b->{head}{$sort_by})) * $rev;
+ };
+ return [sort { &$s } @{ $self->list_reply }[$start..$end]];
+}
+
+sub count {
+ my $self = shift;
+ my $auth = shift;
+ my $_folder = shift;
+
+ return (
+ sum(map {$_->{size}} @{ $self->list_reply }), # size
+ scalar(@{ $self->list_reply }), # count
+ 0, # new
+ );
+}
+
+sub show {
+ my $self = shift;
+ my $auth = shift;
+ my $mid = shift;
+
+ my $mail = $self->read_reply->{$mid};
+ if ($mail) {
+ return $mail;
+ }
+ else {
+ die 'unkown mail-id';
+ }
+}
+
+sub folders { ['', qw(cur test devel debug)] }
+
+sub search { ... }
+sub move { ... }
+
+
+1
+
+__END__
+
+=head1 NAME
+
+Mock - Simple file based mock for the L<JWebmail::Model::ReadMails> module.
+
+=cut