#!perl
use Cassandane::Tiny;

sub test_basic ($self)
{
    xlog $self, "Test the cyr_deny utility with the imap service";

    # Data thanks to hipsteripsum.me
    my @cases = ({
            # test default options
            user => 'helvetica',
            opts => [ ],
            can_login => 0,
        },{
            # test the -s option with our service
            user => 'portland',
            opts => [ '-s', 'imap' ],
            can_login => 0,
        },{
            # test the -s option with another service
            user => 'stumptown',
            opts => [ '-s', 'godard' ],
            can_login => 1,
        },{
            # test the -m option
            user => 'mustache',
            opts => [ '-m', 'Bugger off, you' ],
            can_login => 0,
        },{
            # control case - no cyr_deny command run
            user => 'vegan',
            can_login => 1,
        });


    xlog $self, "Create all users";
    foreach my $case (@cases)
    {
        $self->{instance}->create_user($case->{user});
    }

    xlog $self, "Running cyr_deny for some users";
    foreach my $case (@cases)
    {
        next unless defined $case->{opts};
        $self->{instance}->run_command({ cyrus => 1 },
                'cyr_deny', @{$case->{opts}}, $case->{user});
    }

    my $svc = $self->{instance}->get_service('imap');
    foreach my $case (@cases)
    {
        xlog $self, "Trying to log in as user $case->{user}";
        my $store = $svc->create_store(username => $case->{user});
        if ($case->{can_login})
        {
            xlog $self, "Expecting this to succeed";
            my $talk = $store->get_client();
            my $r = $talk->status('inbox', [ 'messages' ]);
            $self->assert_deep_equals({ messages => 0 }, $r);
            $talk = undef;
        }
        else
        {
            xlog $self, "Expecting this to fail";
            eval { $store->get_client(); };
            my $exception = $@;
            $self->assert_matches(qr/no - login failed: authorization failure/i, $exception);
        }
    }
}
