Update password storage to a more compact form
[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 => 17;
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 Blerg::Database::auth_set_password($test_user, $password . 'X');
53 ok( Blerg::Database::auth_check_password($test_user, $password . 'X') );
54
55 END {
56         chdir;
57         remove_tree "/tmp/blerg_test_$$";
58 }