Add simple "admin" user support
[scpubgit/stemmatology.git] / t / text_tradition_user.t
CommitLineData
2006bd3f 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More 'no_plan';
2006bd3f 7use File::Temp;
8
cf7e4e7b 9use_ok('Text::Tradition::Directory');
2006bd3f 10
11my $fh = File::Temp->new();
12my $file = $fh->filename;
13$fh->close;
14my $dsn = "dbi:SQLite:dbname=$file";
2006bd3f 15
cf7e4e7b 16my $user_store = Text::Tradition::Directory->new('dsn' => $dsn,
ef02228c 17 'extra_args' => { 'create' => 1 } );
18
770f7a2b 19my $scope = $user_store->new_scope;
20
ef02228c 21## passwords
22my $shortpass = 'bloggs';
23ok(!$user_store->validate_password($shortpass), '"bloggs" is too short for a password');
2006bd3f 24
d1ba091f 25## create user
ef02228c 26my $new_user = $user_store->add_user({ username => 'fred',
27 password => 'bloggspass'});
2006bd3f 28isa_ok($new_user, 'Text::Tradition::User');
570cf8ba 29is($new_user->active, 1, 'New user created and active');
7cb56251 30ok(!$new_user->is_admin, 'New user is not an admin');
2006bd3f 31
d1ba091f 32## find user
33my $find_user = $user_store->find_user({ username => 'fred'});
34isa_ok($find_user, 'Text::Tradition::User');
ef02228c 35ok($find_user->check_password('bloggspass'), 'Stored & retrieved with correct password');
36
37## modify user
38my $changed_user = $user_store->modify_user({ username => 'fred',
39 password => 'passbloggs' });
40isa_ok($changed_user, 'Text::Tradition::User');
41my $changed = $user_store->find_user({ username => 'fred'});
42ok($changed->check_password('passbloggs'), 'Modified & retrieved with correct new password');
d1ba091f 43
570cf8ba 44{
45## deactivate user
46## Sets all traditions to non-public, deactivates
47 my $user = $user_store->add_user({ username => 'testactive',
48 password => 'imanactiveuser' });
49 ok($user->active, 'Deactivate test user starts active');
50
51 my $d_user = $user_store->deactivate_user({ username => 'testactive' });
52 is($d_user->active, 0, 'Deactivated user');
df8c12f0 53 is($user_store->find_user({ username => 'testactive' }), undef, 'Deactivated user not returned by find_user');
570cf8ba 54
55## TODO - add test where user has traditions to start with
56}
57
58{
59## reactivate user
60## reactivates user, does not mess with their traditions (as we don't know which were public to start with)
61
62 my $user = $user_store->add_user({ username => 'testinactive',
63 password => 'imaninactiveuser' });
df8c12f0 64 my $d_user = $user_store->deactivate_user({ username => 'testinactive' });
570cf8ba 65 ok(!$d_user->active, 'Deactivate test user starts active');
66
67 my $a_user = $user_store->reactivate_user({ username => 'testinactive' });
68 is($a_user->active, 1, 'Re-activated user');
df8c12f0 69 ok($user_store->find_user({ username => 'testinactive' }), 'Re-activated user returned by find_user again');
570cf8ba 70}
71
72{
73## delete user (admin only?)
74 my $user = $user_store->add_user({ username => 'testdelete',
75 password => 'imgoingtobedeleted' });
76
77 my $gone = $user_store->delete_user({ username => 'testdelete' });
78
79 my $d_user = $user_store->find_user({ username => 'testdelete' });
80
81 ok($gone && !$d_user, 'Deleted user completely from store');
82}
d1ba091f 83
fefeeeda 84{
85## add_tradition
86 use Text::Tradition;
87 my $t = Text::Tradition->new(
88 'name' => 'inline',
89 'input' => 'Tabular',
90 'file' => 't/data/simple.txt',
91 );
92
93 my $uuid = $user_store->save($t);
94 my $user = $user_store->add_user({ username => 'testadd',
95 password => 'testingtraditions' });
96 $user->add_tradition($t);
97 $user_store->update($user);
fefeeeda 98
99 is( scalar @{$user->traditions}, 1, 'Added one tradition');
100
7d52d62b 101 my @tlist = $user_store->traditionlist($user);
102 is($tlist[0]->{name}, $t->name, 'Traditionlist returns same named user->tradition');
103 is($tlist[0]->{id}, $uuid, 'Traditionlist returns actual tradition with same uuid we put in earlier');
7cb56251 104 my $fetched_t = $user_store->tradition($tlist[0]->{id});
105 is($fetched_t->user->id, $user->id, 'Traditionlist returns item belonging to this user');
106
107 ## add a second, not owned by this user, we shouldn't return it from
108 ## traditionslist
109 my $t2 = Text::Tradition->new(
110 'name' => 'inline',
111 'input' => 'Tabular',
112 'file' => 't/data/simple.txt',
113 );
114 $user_store->save($t2);
115 my @tlist2 = $user_store->traditionlist($user);
116 is(scalar @tlist2, 1, 'With 2 stored traditions, we only fetch one');
117 my $fetched_t2 = $user_store->tradition($tlist[0]->{id});
118 is($fetched_t2->user->id, $user->id, 'Traditionlist returns item belonging to this user');
119
120
121}
122
123
124TODO: {
125 local $TODO = 'searching on public attr not implemented yet';
126 ## Fetch public traditions, not user traditions, when not fetching with a user
127 use Text::Tradition;
128 my $t = Text::Tradition->new(
129 'name' => 'inline',
130 'input' => 'Tabular',
131 'file' => 't/data/simple.txt',
132 );
133
134 $user_store->save($t);
135 my $user = $user_store->add_user({ username => 'testpublic',
136 password => 'testingtraditions' });
137 $user->add_tradition($t);
138 $user_store->update($user);
139
140 ## add a second, not owned by this user, we shouldn't return it from
141 ## traditionslist
142 my $t2 = Text::Tradition->new(
143 'name' => 'inline',
144 'input' => 'Tabular',
145 'file' => 't/data/simple.txt',
146 );
147 $t2->public(1);
148 my $uuid = $user_store->save($t2);
149
150 my @tlist = $user_store->traditionlist('public');
151 is(scalar @tlist, 1, 'Got one public tradition');
152 is($tlist[0]->{name}, $t2->name, 'Traditionlist returns same named user->tradition');
153 is($tlist[0]->{id}, $uuid, 'Traditionlist returns actual tradition with same uuid we put in earlier');
154 my $fetched_t = $user_store->tradition($tlist[0]->{id});
155 ok($fetched_t->public, 'Traditionlist returns public item');
156
fefeeeda 157}
158
ec7ea4e6 159{
160## remove_tradition
161 use Text::Tradition;
162 my $t = Text::Tradition->new(
163 'name' => 'inline',
164 'input' => 'Tabular',
165 'file' => 't/data/simple.txt',
166 );
167
168 my $uuid = $user_store->save($t);
169 my $user = $user_store->add_user({ username => 'testremove',
170 password => 'testingtraditions' });
171 $user->add_tradition($t);
172 $user_store->update($user);
173
174 $user->remove_tradition($t);
175 $user_store->update($user);
176 my $changed_t = $user_store->tradition($uuid);
177
178 is( scalar @{$user->traditions}, 0, 'Added and removed one tradition');
179 ok(!$changed_t->has_user, 'Removed user from tradition');
180
181 my @tlist = $user_store->traditionlist($user);
182 is(scalar @tlist, 0, 'Traditionlist now empty');
183}
184
7cb56251 185{
186 ## Add admin user
187 my $admin = $user_store->add_user({
188 username => 'adminuser',
189 password => 'adminpassword',
190 role => 'admin' });
191
192 ok($admin->is_admin, 'Got an admin user');
193
194 ## test admins get all traditions
195 use Text::Tradition;
196 my $t = Text::Tradition->new(
197 'name' => 'inline',
198 'input' => 'Tabular',
199 'file' => 't/data/simple.txt',
200 );
201
202 $user_store->save($t);
203
204 my @tlist = $user_store->traditionlist(); ## all traditions
205 my @admin_tlist = $user_store->traditionlist($admin);
206
207 is(scalar @admin_tlist, scalar @tlist, 'Got all traditions for admin user');
208
209}