Remove /unsubscribe from htaccess config
[blerg.git] / lib / perl / Blerg-Database / t / auth.t
1 use File::Path qw/remove_tree/;
2 use strict;
3 use warnings;
4
5 use Test::More tests => 21;
6 BEGIN { use_ok('Blerg::Database') };
7
8 ### Setup
9 my $test_user = 'barfy';
10 my $test_dir = "/tmp/blerg_test_$$";
11 mkdir $test_dir;
12 chdir $test_dir;
13 mkdir 'data';
14 mkdir 'hash_tags';
15 mkdir 'ref_tags';
16
17 $ENV{BLERG_PATH} = $test_dir;
18 Blerg::Database::init();
19
20 my ($ptr, $password, $n);
21 $password = 'blargh';
22 $n = 0;
23
24 $ptr = Blerg::Database->open($test_user);
25 $ptr->close;
26
27 ### Auth (C interface)
28 is(   Blerg::Database::auth_set_password($test_user, $password), 1 );
29 isnt( Blerg::Database::auth_set_password('fakeuser', $password), 1 );
30
31 is(   Blerg::Database::auth_check_password($test_user, $password), 1 );
32 isnt( Blerg::Database::auth_check_password('fakeuser', $password), 1 );
33 isnt( Blerg::Database::auth_check_password($test_user, 'wrongpass'), 1 );
34
35 my $token = Blerg::Database::auth_login($test_user, $password);
36 ok( defined $token );
37 ok( length($token) == 32 );
38 ok( Blerg::Database::auth_check_token($test_user, $token) );
39 isnt( Blerg::Database::auth_check_token($test_user, 'badtoken'), 1 );
40 isnt( Blerg::Database::auth_check_token('fakeuser', $token), 1 );
41 ok( Blerg::Database::auth_logout($test_user, $token) );
42
43 $token = Blerg::Database::auth_login($test_user, 'wrongpass');
44 ok( not defined $token );
45
46 $token = Blerg::Database::auth_login('wronguser', $password);
47 ok( not defined $token );
48
49 isnt( Blerg::Database::auth_logout($test_user, 'badtoken'), 1 );
50 isnt( Blerg::Database::auth_logout('fakeuser', 'badtoken'), 1 );
51
52 my $counter1 = Blerg::Database::auth_get_counter('fakeuser');
53 ok( !defined $counter1 );
54
55 $counter1 = Blerg::Database::auth_get_counter($test_user);
56 ok( defined $counter1 );
57 isnt( $counter1, 0);
58
59 Blerg::Database::auth_set_password($test_user, $password . 'X');
60 ok( Blerg::Database::auth_check_password($test_user, $password . 'X') );
61
62 my $counter2 = Blerg::Database::auth_get_counter($test_user);
63 ok( $counter1 != $counter2 );
64
65 END {
66         chdir;
67         remove_tree "/tmp/blerg_test_$$";
68 }