Debian Codegraph @

t/021_DBD_Mock_Session.t

raw
1 use 5.008;
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 BEGIN {
9 use_ok('DBD::Mock');
10 }
11
12 use DBI;
13
14 {
15 package Login::Test;
16
17 my $MAX_LOGIN_FAILURES = 3;
18
19 sub login {
20 my ($dbh, $u, $p) = @_;
21 # look for the right username and password
22 my ($user_id) = $dbh->selectrow_array("SELECT user_id FROM users WHERE username = '$u' AND password = '$p'");
23 # if we find one, then ...
24 if ($user_id) {
25 # log the event and return true
26 $dbh->do("INSERT INTO event_log (event) VALUES('User $user_id logged in')");
27 return 'LOGIN SUCCESSFUL';
28 }
29 # if we don't find one then ...
30 else {
31 # see if the username exists ...
32 my ($user_id, $login_failures) = $dbh->selectrow_array("SELECT user_id, login_failures FROM users WHERE username = '$u'");
33 # if we do have a username, and the password doesnt match then ...
34 if ($user_id) {
35 # if we have not reached the max allowable login failures then ...
36 if ($login_failures < $MAX_LOGIN_FAILURES) {
37 # update the login failures
38 $dbh->do("UPDATE users SET login_failures = (login_failures + 1) WHERE user_id = $user_id");
39 return 'BAD PASSWORD';
40 }
41 # otherwise ...
42 else {
43 # we must update the login failures, and lock the account
44 $dbh->do("UPDATE users SET login_failures = (login_failures + 1), locked = 1 WHERE user_id = $user_id"); return 'USER ACCOUNT LOCKED';
45 }
46 }
47 else {
48 return 'USERNAME NOT FOUND';
49 }
50 }
51 }
52 }
53
54 {
55 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
56
57 my $session = DBD::Mock::Session->new({ statement => '', results => []});
58 isa_ok($session, 'DBD::Mock::Session');
59
60 is($session->name(), 'Session 1', '... got the first default session name');
61
62 $dbh->{mock_session} = $session;
63
64 my $fetched_session = $dbh->{mock_session};
65 is($fetched_session, $session, '... it is the same session we put in');
66
67 $dbh->{mock_session} = undef;
68 ok(!defined($dbh->{mock_session}), '... we no longer have a session in there');
69
70 my $session2 = DBD::Mock::Session->new({ statement => '', results => []});
71 isa_ok($session2, 'DBD::Mock::Session');
72
73 is($session2->name(), 'Session 2', '... got the second default session name');
74 }
75
76 {
77 my $successful_login = DBD::Mock::Session->new('successful_login' => (
78 {
79 statement => "SELECT user_id FROM users WHERE username = 'user' AND password = '****'",
80 results => [[ 'user_id' ], [ 1 ]]
81 },
82 {
83 statement => "INSERT INTO event_log (event) VALUES('User 1 logged in')",
84 results => []
85 }
86 ));
87 isa_ok($successful_login, 'DBD::Mock::Session');
88
89 is($successful_login->name(), 'successful_login', '... got the right name');
90
91 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
92 $dbh->{mock_session} = $successful_login;
93
94 is(Login::Test::login($dbh, 'user', '****'), 'LOGIN SUCCESSFUL', '... logged in successfully');
95
96 # check the reusablity
97
98 # it is not reusable now
99 eval {
100 Login::Test::login($dbh, 'user', '****')
101 };
102
103 ok($@, '... got the exception');
104 like($@, qr/Session Error\: Session states exhausted/, '... got the exception we expected');
105
106 # reset the DBD::Mock::Session object
107 $successful_login->reset;
108
109 # and it is re-usable now
110 is(Login::Test::login($dbh, 'user', '****'), 'LOGIN SUCCESSFUL', '... logged in successfully');
111 }
112
113 {
114
115 my $bad_username = DBD::Mock::Session->new('bad_username' => (
116 {
117 statement => qr/SELECT user_id FROM users WHERE username = \'.*?\' AND password = \'.*?\'/, #'
118 results => [[ 'user_id' ], [ undef ]]
119 },
120 {
121 statement => qr/SELECT user_id, login_failures FROM users WHERE username = \'.*?\'/, #'
122 results => [[ 'user_id', 'login_failures' ], [ undef, undef ]]
123 }
124 ));
125 isa_ok($bad_username, 'DBD::Mock::Session');
126
127 is($bad_username->name(), 'bad_username', '... got the right name');
128
129 my $dbh = DBI->connect('dbi:Mock:', '', '');
130 $dbh->{mock_session} = $bad_username;
131
132 is(Login::Test::login($dbh, 'user', '****'), 'USERNAME NOT FOUND', '... username is not found');
133 }
134
135 {
136 my $bad_password = DBD::Mock::Session->new('bad_password' => (
137 {
138 statement => sub { $_[0] eq "SELECT user_id FROM users WHERE username = 'user' AND password = '****'" },
139 results => [[ 'user_id' ], [ undef]]
140 },
141 {
142 statement => sub { $_[0] eq "SELECT user_id, login_failures FROM users WHERE username = 'user'" },
143 results => [[ 'user_id', 'login_failures' ], [ 1, 0 ]]
144 },
145 {
146 statement => sub { $_[0] eq "UPDATE users SET login_failures = (login_failures + 1) WHERE user_id = 1" },
147 results => []
148 }
149 ));
150 isa_ok($bad_password, 'DBD::Mock::Session');
151
152 is($bad_password->name(), 'bad_password', '... got the right name');
153
154 my $dbh = DBI->connect('dbi:Mock:', '', '');
155 $dbh->{mock_session} = $bad_password;
156
157 is(Login::Test::login($dbh, 'user', '****'), 'BAD PASSWORD', '... username is found, but the password is wrong');
158 }
159
160 {
161 my $lock_user_account = DBD::Mock::Session->new('lock_user_account' => (
162 {
163 statement => "SELECT user_id FROM users WHERE username = 'user' AND password = '****'",
164 results => [[ 'user_id' ], [ undef]]
165 },
166 {
167 statement => qr/SELECT user_id, login_failures FROM users WHERE username = \'.*?\'/, #'
168 results => [[ 'user_id', 'login_failures' ], [ 1, 4 ]]
169 },
170 {
171 statement => sub { $_[0] eq "UPDATE users SET login_failures = (login_failures + 1), locked = 1 WHERE user_id = 1" },
172 results => []
173 }
174 ));
175 isa_ok($lock_user_account, 'DBD::Mock::Session');
176
177 is($lock_user_account->name(), 'lock_user_account', '... got the right name');
178
179 my $dbh = DBI->connect('dbi:Mock:', '', '');
180 $dbh->{mock_session} = $lock_user_account;
181
182 is(Login::Test::login($dbh, 'user', '****'), 'USER ACCOUNT LOCKED', '... username is found, and the password is wrong, and the user account is now locked');
183 }
184
185 # now check some errors
186
187 {
188 my $not_enough_statements = DBD::Mock::Session->new((
189 {
190 statement => "SELECT user_id FROM users WHERE username = 'user' AND password = '****'",
191 results => [[ 'user_id' ], [ undef]]
192 },
193 {
194 statement => qr/SELECT user_id, login_failures FROM users WHERE username = \'.*?\'/, #'
195 results => [[ 'user_id', 'login_failures' ], [ 1, 4 ]]
196 },
197 # ... removed one statement here which DBI will be looking for
198 ));
199 isa_ok($not_enough_statements, 'DBD::Mock::Session');
200
201 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
202 $dbh->{mock_session} = $not_enough_statements;
203
204 eval {
205 Login::Test::login($dbh, 'user', '****');
206 };
207 ok(defined($@), '... got an error, as expected');
208 like($@, qr/Session Error\: Session states exhausted\, /, '... got the error we expected');
209
210 }
211
212 {
213 eval {
214 DBD::Mock::Session->new()
215 };
216 ok(defined($@), '... got an error, as expected');
217 like($@, qr/^You must specify at least one session state/, '... got the error we expected');
218
219 eval {
220 DBD::Mock::Session->new([])
221 };
222 ok(defined($@), '... got an error, as expected');
223 like($@, qr/^You must specify session states as HASH refs/, '... got the error we expected');
224
225 eval {
226 DBD::Mock::Session->new('session')
227 };
228 ok(defined($@), '... got an error, as expected');
229 like($@, qr/^You must specify at least one session state/, '... got the error we expected');
230
231 eval {
232 DBD::Mock::Session->new('session', [])
233 };
234 ok(defined($@), '... got an error, as expected');
235 like($@, qr/^You must specify session states as HASH refs/, '... got the error we expected');
236
237 eval {
238 DBD::Mock::Session->new('session', { statement => '', results => [] }, [])
239 };
240 ok(defined($@), '... got an error, as expected');
241 like($@, qr/^You must specify session states as HASH refs/, '... got the error we expected');
242
243 }
244
245 {
246 eval {
247 my $session = DBD::Mock::Session->new('session' => {});
248 $session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
249 };
250 ok(defined($@), '... got an error, as expected');
251 like($@, qr/^Bad state \'0\' in DBD::Mock::Session \(session\)/, '... got the error we expected');
252
253 }
254
255 {
256 eval {
257 my $session = DBD::Mock::Session->new('session' => { statement => "" });
258 $session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
259 };
260 ok(defined($@), '... got an error, as expected');
261 like($@, qr/^Bad state \'0\' in DBD::Mock::Session \(session\)/, '... got the error we expected');
262
263 }
264
265 {
266 eval {
267 my $session = DBD::Mock::Session->new('session' => { results => [] });
268 $session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
269 };
270 ok(defined($@), '... got an error, as expected');
271 like($@, qr/^Bad state \'0\' in DBD::Mock::Session \(session\)/, '... got the error we expected');
272
273 }
274
275 {
276 eval {
277 my $session = DBD::Mock::Session->new('session' => {
278 statement => [],
279 results => []
280 });
281 $session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
282 };
283 ok(defined($@), '... got an error, as expected');
284 like($@, qr/^Bad \'statement\' value \'ARRAY\(0x[a-f0-9]+\)\' in DBD::Mock::Session \(session\)/, '... got the error we expected');
285
286 }
287
288 {
289 my $session = DBD::Mock::Session->new('session' =>
290 {
291 statement => "SELECT foo FROM baz",
292 results => []
293 }
294 );
295 isa_ok($session, 'DBD::Mock::Session');
296
297 eval {
298 $session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
299 };
300 ok(defined($@), '... got an error, as expected');
301 like($@, qr/^Statement does not match current state in DBD::Mock::Session \(session\)/, '... got the error we expected');
302
303 }
304
305 {
306 my $session = DBD::Mock::Session->new('session' =>
307 {
308 statement => qr/SELECT foo FROM baz/,
309 results => []
310 }
311 );
312 isa_ok($session, 'DBD::Mock::Session');
313
314 eval {
315 $session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
316 };
317 ok(defined($@), '... got an error, as expected');
318 like($@, qr/^Statement does not match current state \(with Regexp\) in DBD::Mock::Session \(session\)/, '... got the error we expected');
319
320 }
321
322 {
323 my $session = DBD::Mock::Session->new('session' =>
324 {
325 statement => sub { 0 },
326 results => []
327 }
328 );
329 isa_ok($session, 'DBD::Mock::Session');
330
331 eval {
332 $session->verify_statement(DBI->connect('dbi:Mock:', '', ''), 'SELECT foo FROM bar');
333 };
334 ok(defined($@), '... got an error, as expected');
335 like($@, qr/^Statement does not match current state \(with CODE ref\) in DBD::Mock::Session \(session\)/, '... got the error we expected');
336
337 }
338
339 {
340 my $dbh = DBI->connect('dbi:Mock:', '', '', { RaiseError => 1, PrintError => 0 });
341 my $session = DBD::Mock::Session->new('session' =>
342 {
343 statement => 'Some SQL',
344 results => []
345 }
346 );
347 isa_ok($session, 'DBD::Mock::Session');
348 $dbh->{mock_session} = $session;
349 eval {
350 $dbh->disconnect;
351 };
352
353 ok(defined($@), '... got an error, as expected');
354 like($@, qr/^DBH->finish called when session still has states left/, '... got the error we expected');
355
356 # Shuts up warning when object is destroyed
357 undef $dbh->{mock_session};
358 }
359
360 done_testing();