Commit | Line | Data |
772c89c1 |
1 | use strict; |
2 | use warnings; |
3 | use Test::More; |
4 | |
5 | use Scalar::Util; |
6 | |
d87edac9 |
7 | BEGIN { |
8 | eval "use DBD::SQLite"; |
9 | plan $@ |
10 | ? ( skip_all => 'needs DBD::SQLite for testing' ) |
11 | : ( tests => 19 ); |
12 | } |
13 | |
772c89c1 |
14 | |
15 | use lib qw(t/lib); |
16 | |
d87edac9 |
17 | use_ok('DBIx::Class::Schema::RestrictWithObject'); |
18 | use_ok('RestrictByUserTest'); |
772c89c1 |
19 | my $schema = RestrictByUserTest->init_schema; |
20 | ok($schema, "Connected successfully"); |
21 | |
22 | my $user1 = $schema->resultset('Users')->create({name => 'user1'}); |
23 | my $user2 = $schema->resultset('Users')->create({name => 'user2'}); |
24 | ok(ref $user1 && ref $user2, "Successfully created mock users"); |
25 | |
26 | ok($user1->notes->create({name => 'note 1-1'}), "Successfully created 1-1 note"); |
27 | ok($user1->notes->create({name => 'note 1-2'}), "Successfully created 1-2 note"); |
28 | |
29 | ok($user2->notes->create({name => 'note 2-1'}), "Successfully created 2-1 note"); |
30 | ok($user2->notes->create({name => 'note 2-2'}), "Successfully created 2-2 note"); |
31 | ok($user2->notes->create({name => 'note 2-3'}), "Successfully created 2-3 note"); |
32 | ok($user2->notes->create({name => 'note 2-4'}), "Successfully created 2-4 note"); |
33 | |
d87edac9 |
34 | my $u1_schema = $schema->restrict_with_object($user1); |
35 | my $u2_schema = $schema->restrict_with_object($user2, "MY"); |
36 | my $u3_schema = $schema->restrict_with_object($user2, "BUNK"); |
772c89c1 |
37 | |
d87edac9 |
38 | is($u1_schema->restricting_object->id, $user1->id, "Correct restriction for user 1"); |
39 | is($u2_schema->restricting_object->id, $user2->id, "Correct restriction for user 2"); |
772c89c1 |
40 | is($u2_schema->restricted_prefix, "MY", "Correct prefix for user 2"); |
41 | |
d87edac9 |
42 | ok(Scalar::Util::refaddr($u1_schema) ne Scalar::Util::refaddr($u2_schema), |
772c89c1 |
43 | "Successful clones"); |
44 | |
45 | is($schema->resultset('Notes')->count, 6, 'Correct un resticted count'); |
46 | is($u1_schema->resultset('Notes')->count, 2, 'Correct resticted count'); |
47 | is($u2_schema->resultset('Notes')->count, 4, 'Correct resticted count using prefix'); |
d87edac9 |
48 | is($u2_schema->resultset('Notes')->count, 4, |
772c89c1 |
49 | 'Correct resticted count using prefix and fallback'); |
50 | |
51 | is($u2_schema->resultset('Users')->count, 2, 'Unrestricted resultsets work'); |
52 | |
53 | |
54 | 1; |