#!perl -w

use strict;
use warnings;

use Mail::Header;
use Qpsmtpd::Constants;

my $r;

sub register_tests {
    my $self = shift;

    $self->register_test('test_get_dspam_results', 6);
    $self->register_test('test_log_and_return', 6);
    $self->register_test('test_reject_type', 3);
}

sub test_log_and_return {
    my $self = shift;

    my $transaction = $self->qp->transaction;

    # reject not set
    $self->{_args}{reject} = undef;
    $transaction->notes('dspam', { class=> 'Spam', probability => .99, confidence=>1 } );
    ($r) = $self->log_and_return( $transaction );
    cmp_ok( $r, '==', DECLINED, "($r)");

    # reject exceeded
    $self->{_args}{reject} = .95;
    $transaction->notes('dspam', { class=> 'Spam', probability => .99, confidence=>1 } );
    ($r) = $self->log_and_return( $transaction );
    cmp_ok( $r, '==', DENY, "($r)");

    # below reject threshold
    $transaction->notes('dspam', { class=> 'Spam', probability => .94, confidence=>1 } );
    ($r) = $self->log_and_return( $transaction );
    cmp_ok( $r, '==', DECLINED, "($r)");

    # requires agreement
    $self->{_args}{reject} = 'agree';
    $transaction->notes('spamassassin', { is_spam => 'Yes', score => 25 } );
    $transaction->notes('dspam', { class=> 'Spam', probability => .90, confidence=>1 } );
    ($r) = $self->log_and_return( $transaction );
    cmp_ok( $r, '==', DENY, "($r)");

    # requires agreement
    $transaction->notes('spamassassin', { is_spam => 'No', score => 15 } );
    $transaction->notes('dspam', { class=> 'Spam', probability => .96, confidence=>1 } );
    ($r) = $self->log_and_return( $transaction );
    cmp_ok( $r, '==', DECLINED, "($r)");

    # requires agreement
    $transaction->notes('spamassassin', { is_spam => 'Yes', score => 10 } );
    $transaction->notes('dspam', { class=> 'Innocent', probability => .96, confidence=>1 } );
    ($r) = $self->log_and_return( $transaction );
    cmp_ok( $r, '==', DECLINED, "($r)");
};

sub test_get_dspam_results {
    my $self = shift;

    my $transaction = $self->qp->transaction;
    my $header = Mail::Header->new(Modify => 0, MailFrom => "COERCE");
    $transaction->header( $header );

    my @dspam_sample_headers = (
        'Innocent, probability=0.0000, confidence=0.69',
        'Innocent, probability=0.0000, confidence=0.85',
        'Innocent, probability=0.0023, confidence=1.00',
        'Spam, probability=1.0000, confidence=0.87',
        'Spam, probability=1.0000, confidence=0.99',
        'Whitelisted',
    );

    foreach my $header ( @dspam_sample_headers ) {
        $transaction->header->delete('X-DSPAM-Result');
        $transaction->header->add('X-DSPAM-Result', $header);
        my $r = $self->get_dspam_results($transaction);
        ok( ref $r, "r: ($header)" );
        #warn Data::Dumper::Dumper($r);
    };
};

sub test_reject_type {
    my $self = shift;

    $self->{_args}{reject_type} = undef;
    cmp_ok( $self->get_reject_type(), '==', DENY, "default");

    $self->{_args}{reject_type} = 'temp';
    cmp_ok( $self->get_reject_type(), '==', DENYSOFT, "defer");

    $self->{_args}{reject_type} = 'disconnect';
    cmp_ok( $self->get_reject_type(), '==', DENY_DISCONNECT, "disconnect");
};