#!/usr/bin/perl -w
use strict; 

use lib qw(./ ./DADA/perllib ../ ../DADA/perllib ../../ ../../DADA/perllib); 

use Test::More qw(no_plan); 


use DADA::Config; 
use DADA::App::Guts; 
use DADA::MailingList; 
use DADA::MailingList::Settings; 



my $list = 'dadatest'; 

my $list_params = { 

        list             =>  $list, 
        list_name        => 'Justin!', 
        list_owner_email => 'user@example.com',  
        password         => 'abcd', 
        retype_password  => 'abcd', 
        info             => 'info', 
        privacy_policy   => 'privacy_policy',
        physical_address => 'physical_address', 

};

my ($list_errors,$flags) = check_list_setup(-fields => $list_params);
ok($list_errors == 0); 

# ok ok ok! We'll actually make the list...
my $ls = DADA::MailingList::Create({ -name => $list }); 
    my $test_list_params = $list_params; 
    delete($test_list_params->{retype_password}); 
   $ls->save($test_list_params);

#undef($ls); 


#my $ls = DADA::MailingList::Settings->new(-List => $list); 

my $li = $ls->get(); 

foreach(keys %$list_params){ 
    ok(($li->{$_} eq $list_params->{$_}), $_); 
}


# These have been problematic variables...
ok(length($li->{cipher_key}) >= 1, "we have a cipher key!");
my $orig_cipher_key = $li->{cipher_key};
ok(length($li->{admin_menu}) >= 1, "we have an admin menu");  
my $orig_admin_menu = $li->{admin_menu};

# Gotta do this, since the backup names are based on time - gah, should fix that soon; 
sleep(5); 
# do a bs save...
$ls->save({list_name => "New List Name"}); 

undef $li; 

my $li = $ls->get(); 

ok($li->{list_name} eq 'New List Name', "New List Name was saved.");
ok($orig_cipher_key eq $li->{cipher_key}, "Cipher Key didn't change for any odd reason.");
# This test is sort of dumb, but whatever...
ok($orig_admin_menu eq $li->{admin_menu}, "Admin Menu didn't change for any odd reason.");









        
eval { $ls->save({bad => "bad"}); };
ok(defined($@), "Error when attempting to save a non-existant setting: $@"); 
 




my $Remove = DADA::MailingList::Remove({ -name => $list});
ok($Remove == 1, "Remove returned a status of, '1'");

