User store functionality actually in Directory; migrate changes there
[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;
b77f6c1b 8use TryCatch;
2006bd3f 9
cf7e4e7b 10use_ok('Text::Tradition::Directory');
2006bd3f 11
12my $fh = File::Temp->new();
13my $file = $fh->filename;
14$fh->close;
15my $dsn = "dbi:SQLite:dbname=$file";
2006bd3f 16
cf7e4e7b 17my $user_store = Text::Tradition::Directory->new('dsn' => $dsn,
ef02228c 18 'extra_args' => { 'create' => 1 } );
19
770f7a2b 20my $scope = $user_store->new_scope;
21
ef02228c 22## passwords
23my $shortpass = 'bloggs';
24ok(!$user_store->validate_password($shortpass), '"bloggs" is too short for a password');
b77f6c1b 25try {
26 my $dud_user = $user_store->add_user({ username => 'joe',
27 password => $shortpass });
28 ok( 0, "User with short password should not have been created" );
29} catch ( Text::Tradition::Error $e ) {
30 is( $e->message, "Invalid password - must be at least "
31 . $user_store->MIN_PASS_LEN . " characters long",
32 "Attempt to add user with too-short password threw correct error" );
33}
2006bd3f 34
d1ba091f 35## create user
ef02228c 36my $new_user = $user_store->add_user({ username => 'fred',
37 password => 'bloggspass'});
2006bd3f 38isa_ok($new_user, 'Text::Tradition::User');
570cf8ba 39is($new_user->active, 1, 'New user created and active');
d5033c7a 40is($new_user->email, 'fred', 'Email value set to username');
7cb56251 41ok(!$new_user->is_admin, 'New user is not an admin');
2006bd3f 42
d1ba091f 43## find user
44my $find_user = $user_store->find_user({ username => 'fred'});
45isa_ok($find_user, 'Text::Tradition::User');
ef02228c 46ok($find_user->check_password('bloggspass'), 'Stored & retrieved with correct password');
47
48## modify user
49my $changed_user = $user_store->modify_user({ username => 'fred',
50 password => 'passbloggs' });
51isa_ok($changed_user, 'Text::Tradition::User');
52my $changed = $user_store->find_user({ username => 'fred'});
53ok($changed->check_password('passbloggs'), 'Modified & retrieved with correct new password');
d1ba091f 54
570cf8ba 55{
56## deactivate user
57## Sets all traditions to non-public, deactivates
58 my $user = $user_store->add_user({ username => 'testactive',
59 password => 'imanactiveuser' });
60 ok($user->active, 'Deactivate test user starts active');
61
62 my $d_user = $user_store->deactivate_user({ username => 'testactive' });
63 is($d_user->active, 0, 'Deactivated user');
df8c12f0 64 is($user_store->find_user({ username => 'testactive' }), undef, 'Deactivated user not returned by find_user');
570cf8ba 65
66## TODO - add test where user has traditions to start with
67}
68
69{
70## reactivate user
71## reactivates user, does not mess with their traditions (as we don't know which were public to start with)
72
73 my $user = $user_store->add_user({ username => 'testinactive',
74 password => 'imaninactiveuser' });
df8c12f0 75 my $d_user = $user_store->deactivate_user({ username => 'testinactive' });
570cf8ba 76 ok(!$d_user->active, 'Deactivate test user starts active');
77
78 my $a_user = $user_store->reactivate_user({ username => 'testinactive' });
79 is($a_user->active, 1, 'Re-activated user');
df8c12f0 80 ok($user_store->find_user({ username => 'testinactive' }), 'Re-activated user returned by find_user again');
570cf8ba 81}
82
83{
84## delete user (admin only?)
85 my $user = $user_store->add_user({ username => 'testdelete',
86 password => 'imgoingtobedeleted' });
87
88 my $gone = $user_store->delete_user({ username => 'testdelete' });
89
90 my $d_user = $user_store->find_user({ username => 'testdelete' });
91
92 ok($gone && !$d_user, 'Deleted user completely from store');
93}
d1ba091f 94
fefeeeda 95{
96## add_tradition
97 use Text::Tradition;
98 my $t = Text::Tradition->new(
99 'name' => 'inline',
100 'input' => 'Tabular',
101 'file' => 't/data/simple.txt',
102 );
103
104 my $uuid = $user_store->save($t);
105 my $user = $user_store->add_user({ username => 'testadd',
106 password => 'testingtraditions' });
107 $user->add_tradition($t);
108 $user_store->update($user);
fefeeeda 109
110 is( scalar @{$user->traditions}, 1, 'Added one tradition');
111
7d52d62b 112 my @tlist = $user_store->traditionlist($user);
113 is($tlist[0]->{name}, $t->name, 'Traditionlist returns same named user->tradition');
114 is($tlist[0]->{id}, $uuid, 'Traditionlist returns actual tradition with same uuid we put in earlier');
7cb56251 115 my $fetched_t = $user_store->tradition($tlist[0]->{id});
116 is($fetched_t->user->id, $user->id, 'Traditionlist returns item belonging to this user');
117
118 ## add a second, not owned by this user, we shouldn't return it from
119 ## traditionslist
120 my $t2 = Text::Tradition->new(
121 'name' => 'inline',
122 'input' => 'Tabular',
123 'file' => 't/data/simple.txt',
124 );
125 $user_store->save($t2);
126 my @tlist2 = $user_store->traditionlist($user);
127 is(scalar @tlist2, 1, 'With 2 stored traditions, we only fetch one');
128 my $fetched_t2 = $user_store->tradition($tlist[0]->{id});
129 is($fetched_t2->user->id, $user->id, 'Traditionlist returns item belonging to this user');
130
131
132}
133
134
135TODO: {
136 local $TODO = 'searching on public attr not implemented yet';
137 ## Fetch public traditions, not user traditions, when not fetching with a user
138 use Text::Tradition;
139 my $t = Text::Tradition->new(
140 'name' => 'inline',
141 'input' => 'Tabular',
142 'file' => 't/data/simple.txt',
143 );
144
145 $user_store->save($t);
146 my $user = $user_store->add_user({ username => 'testpublic',
147 password => 'testingtraditions' });
148 $user->add_tradition($t);
149 $user_store->update($user);
150
151 ## add a second, not owned by this user, we shouldn't return it from
152 ## traditionslist
153 my $t2 = Text::Tradition->new(
154 'name' => 'inline',
155 'input' => 'Tabular',
156 'file' => 't/data/simple.txt',
157 );
158 $t2->public(1);
159 my $uuid = $user_store->save($t2);
160
161 my @tlist = $user_store->traditionlist('public');
162 is(scalar @tlist, 1, 'Got one public tradition');
163 is($tlist[0]->{name}, $t2->name, 'Traditionlist returns same named user->tradition');
164 is($tlist[0]->{id}, $uuid, 'Traditionlist returns actual tradition with same uuid we put in earlier');
165 my $fetched_t = $user_store->tradition($tlist[0]->{id});
166 ok($fetched_t->public, 'Traditionlist returns public item');
167
fefeeeda 168}
169
ec7ea4e6 170{
171## remove_tradition
172 use Text::Tradition;
173 my $t = Text::Tradition->new(
174 'name' => 'inline',
175 'input' => 'Tabular',
176 'file' => 't/data/simple.txt',
177 );
178
179 my $uuid = $user_store->save($t);
180 my $user = $user_store->add_user({ username => 'testremove',
181 password => 'testingtraditions' });
182 $user->add_tradition($t);
183 $user_store->update($user);
184
185 $user->remove_tradition($t);
186 $user_store->update($user);
187 my $changed_t = $user_store->tradition($uuid);
188
189 is( scalar @{$user->traditions}, 0, 'Added and removed one tradition');
190 ok(!$changed_t->has_user, 'Removed user from tradition');
191
192 my @tlist = $user_store->traditionlist($user);
193 is(scalar @tlist, 0, 'Traditionlist now empty');
194}
195
7cb56251 196{
197 ## Add admin user
198 my $admin = $user_store->add_user({
199 username => 'adminuser',
200 password => 'adminpassword',
201 role => 'admin' });
202
203 ok($admin->is_admin, 'Got an admin user');
204
205 ## test admins get all traditions
206 use Text::Tradition;
207 my $t = Text::Tradition->new(
208 'name' => 'inline',
209 'input' => 'Tabular',
210 'file' => 't/data/simple.txt',
211 );
212
213 $user_store->save($t);
214
215 my @tlist = $user_store->traditionlist(); ## all traditions
216 my @admin_tlist = $user_store->traditionlist($admin);
217
218 is(scalar @admin_tlist, scalar @tlist, 'Got all traditions for admin user');
219
220}
10ef7653 221
222{
223 ## Add/find simple openid user with OpenIDish parameters:
224
225 my $openid_user = $user_store->create_user({
226 url => 'http://username.myopenid.com',
d5033c7a 227 email => 'username.myopenid.com',
10ef7653 228 });
229 ok($openid_user, 'Created user from OpenID params');
230
231 my $get_openid_user = $user_store->find_user({
232 url => 'http://username.myopenid.com',
d5033c7a 233 email => 'username.myopenid.com',
10ef7653 234 });
235
236 ok($openid_user == $get_openid_user, 'Found OpenID user again');
237 is($get_openid_user->id, 'http://username.myopenid.com', 'Set id to unique url from openid');
d5033c7a 238 is($get_openid_user->email, 'username.myopenid.com', 'Kept original email value');
10ef7653 239}
240
241{
242 ## Add/find openid user with email attribute:
243 my $openid_user = $user_store->create_user({
244 url => 'http://blahblah.com/foo/bar/baz/lotsofjunk',
d5033c7a 245 email => 'http://blahblah.com/foo/bar/baz/lotsofjunk',
10ef7653 246 extensions => {
247 'http://openid.net/srv/ax/1.0' => {
248 'value.email' => 'fredbloggs@blahblah.com',
249 'type.email' => 'http://axschema.org/contact/email',
250 'mode' => 'fetch_response',
251 },
252 },
253 });
254 ok($openid_user, 'Created user from OpenID params');
255
256 my $get_openid_user = $user_store->find_user({
257 url => 'http://blahblah.com/foo/bar/baz/lotsofjunk',
d5033c7a 258 email => 'http://blahblah.com/foo/bar/baz/lotsofjunk',
10ef7653 259 extensions => {
260 'http://openid.net/srv/ax/1.0' => {
261 'value.email' => 'fredbloggs@blahblah.com',
262 'type.email' => 'http://axschema.org/contact/email',
263 'mode' => 'fetch_response',
264 },
265 },
266 });
267
268 ok($openid_user == $get_openid_user, 'Found OpenID user again');
269 is($get_openid_user->id, 'http://blahblah.com/foo/bar/baz/lotsofjunk', 'Set id to unique url from openid');
d5033c7a 270 is($get_openid_user->email, 'fredbloggs@blahblah.com', 'Set email value to email from extension');
10ef7653 271}