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