[Jifty-commit] r3998 - in apps/CASPlus/trunk: t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed Aug 29 12:49:04 EDT 2007


Author: sterling
Date: Wed Aug 29 12:49:03 2007
New Revision: 3998

Modified:
   apps/CASPlus/trunk/   (props changed)
   apps/CASPlus/trunk/t/40-many-to-one_relationships-explicit.t

Log:
 r8964 at riddle:  andrew | 2007-08-25 19:33:50 -0500
 Adding a test for role calculation in many-parents/one-child relationships.


Modified: apps/CASPlus/trunk/t/40-many-to-one_relationships-explicit.t
==============================================================================
--- apps/CASPlus/trunk/t/40-many-to-one_relationships-explicit.t	(original)
+++ apps/CASPlus/trunk/t/40-many-to-one_relationships-explicit.t	Wed Aug 29 12:49:03 2007
@@ -0,0 +1,652 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+use List::Util qw/ sum /;
+use Jifty::Test tests => 128;
+
+my $system_user = CASPlus::CurrentUser->superuser;
+
+my $role_profile = CASPlus::Model::Profile->new(current_user => $system_user);
+$role_profile->create(
+    name         => 'generic role',
+    profile_type => 'role',
+);
+ok($role_profile->id, 'created a role profile');
+
+my $other_profile = CASPlus::Model::Profile->new(current_user => $system_user);
+$other_profile->create(
+    name         => 'generic other',
+    profile_type => 'other',
+);
+ok($other_profile->id, 'created an other profile');
+
+my $user_profile = CASPlus::Model::Profile->new(current_user => $system_user);
+$user_profile->create(
+    name         => 'generic user',
+    profile_type => 'user',
+);
+ok($user_profile->id, 'created a user profile');
+
+my $ur_relationship = CASPlus::Model::ProfileRelationship->new(current_user => $system_user);
+$ur_relationship->create(
+    name                        => 'user-role',
+    parent_name                 => 'my_roles',
+    child_name                  => 'my_user',
+    relation_parent             => $role_profile,
+    relation_child              => $user_profile,
+    many_parents                => 1,
+    many_children               => 0,
+    roles_propagate_to_children => 1,
+);
+ok($ur_relationship->id, 'created a user-role relationship');
+
+my $uo_relationship = CASPlus::Model::ProfileRelationship->new(current_user => $system_user);
+$uo_relationship->create(
+    name                        => 'user-other',
+    parent_name                 => 'my_others',
+    child_name                  => 'my_user',
+    relation_parent             => $other_profile,
+    relation_child              => $user_profile,
+    many_parents                => 1,
+    many_children               => 0,
+    roles_propagate_to_children => 1,
+);
+ok($ur_relationship->id, 'created a user-other relationship');
+
+my $rr_relationship = CASPlus::Model::ProfileRelationship->new(current_user => $system_user);
+$rr_relationship->create(
+    name                        => 'role-role',
+    parent_name                 => 'my_parent_roles',
+    child_name                  => 'my_child_role',
+    relation_parent             => $role_profile,
+    relation_child              => $role_profile,
+    many_parents                => 1,
+    many_children               => 0,
+    roles_propagate_to_children => 1,
+);
+ok($rr_relationship->id, 'created a role-role relationship');
+
+my $or_relationship = CASPlus::Model::ProfileRelationship->new(current_user => $system_user);
+$or_relationship->create(
+    name                        => 'other-role',
+    parent_name                 => 'my_roles',
+    child_name                  => 'my_other',
+    relation_parent             => $role_profile,
+    relation_child              => $other_profile,
+    many_parents                => 1,
+    many_children               => 0,
+    roles_propagate_to_children => 1,
+);
+ok($or_relationship->id, 'created a other-role relationship');
+
+my $user = CASPlus::Model::User->new(current_user => $system_user);
+my $role = CASPlus::Model::Role->new(current_user => $system_user);
+my $permission = CASPlus::Model::ProfilePermission->new(current_user => $system_user);
+
+my $user_class  = $user_profile->record_class;
+my $other_class = $other_profile->record_class;
+my $role_class  = $role_profile->record_class;
+
+my $ur_class = $ur_relationship->record_class;
+my $uo_class = $uo_relationship->record_class;
+my $or_class = $or_relationship->record_class;
+my $rr_class = $rr_relationship->record_class;
+
+# Create some test objects
+
+{ # user-1 : unique ID 1
+    $user->create(
+        username => 'user-1',
+        password => 'test',
+    );
+    ok($user->id, 'created user-1');
+    my $user_obj = $user_class->new(current_user => $system_user);
+    $user_obj->create(
+        user_object => $user,
+    );
+    ok($user->id, 'created user-1 profile');
+    is($user_obj->unique_id, 1, 'created user-1 unique ID');
+}
+
+{ # role-1 : unique ID 2
+    $role->create(
+        name => 'role-1',
+    );
+    ok($role->id, 'created role-1');
+    my $role_obj = $role_class->new(current_user => $system_user);
+    $role_obj->create(
+        role_object => $role,
+    );
+    ok($role_obj->id, 'created role-1 profile');
+    is($role_obj->unique_id, 2, 'created role-1 unique ID');
+}
+
+{ # role-2 : unique ID 3
+    $role->create(
+        name => 'role-2',
+    );
+    ok($role->id, 'created role-2');
+    my $role_obj = $role_class->new(current_user => $system_user);
+    $role_obj->create(
+        role_object => $role,
+    );
+    ok($role_obj->id, 'created role-2 profile');
+    is($role_obj->unique_id, 3, 'created role-2 unique ID');
+}
+
+{ # first <other> : unique ID 4
+    my $other_obj = $other_class->new(current_user => $system_user);
+    $other_obj->create();
+    ok($other_obj->id, 'created first <other> profile');
+    is($other_obj->unique_id, 4, 'created first <other> unique ID');
+}
+
+{ # user-2 : unique ID 5
+    $user->create(
+        username => 'user-2',
+        password => 'test',
+    );
+    ok($user->id, 'created user-2');
+    my $user_obj = $user_class->new(current_user => $system_user);
+    $user_obj->create(
+        user_object => $user,
+    );
+    ok($user_obj->id, 'created user-2 profile');
+    is($user_obj->unique_id, 5, 'created user-2 unique ID');
+}
+
+{ # second <other> : unique ID 6
+    my $other_obj = $other_class->new(current_user => $system_user);
+    $other_obj->create();
+    ok($other_obj->id, 'created second <other> profile');
+    is($other_obj->unique_id, 6, 'created second <other> unique ID');
+}
+
+{ # role-3 : unique ID 7
+    $role->create(
+        name => 'role-3',
+    );
+    ok($role->id, 'created role-3');
+    my $role_obj = $role_class->new(current_user => $system_user);
+    $role_obj->create(
+        role_object => $role,
+    );
+    ok($role_obj->id, 'created role-3 profile');
+    is($role_obj->unique_id, 7, 'created role-3 unique ID');
+}
+
+{ # user-3 : unique ID 8
+    $user->create(
+        username => 'user-3',
+        password => 'test',
+    );
+    ok($user->id, 'created user-3');
+    my $user_obj = $user_class->new(current_user => $system_user);
+    $user_obj->create(
+        user_object => $user,
+    );
+    ok($user_obj->id, 'created user-3 profile');
+    is($user_obj->unique_id, 8, 'created user-3 unique ID');
+}
+
+{ # role-4 : unique ID 9
+    $role->create(
+        name => 'role-4',
+    );
+    ok($role->id, 'created role-4');
+    my $role_obj = $role_class->new(current_user => $system_user);
+    $role_obj->create(
+        role_object => $role,
+    );
+    ok($role_obj->id, 'created role-4 profile');
+    is($role_obj->unique_id, 9, 'created role-4 unique ID');
+}
+
+{ # user-4 : unique ID 10
+    $user->create(
+        username => 'user-4',
+        password => 'test',
+    );
+    ok($user->id, 'created user-4');
+    my $user_obj = $user_class->new(current_user => $system_user);
+    $user_obj->create(
+        user_object => $user,
+    );
+    ok($user_obj->id, 'created user-4 profile');
+    is($user_obj->unique_id, 10, 'created user-4 unique ID');
+}
+
+{ # role-5 : unique ID 11
+    $role->create(
+        name => 'role-5',
+    );
+    ok($role->id, 'created role-5');
+    my $role_obj = $role_class->new(current_user => $system_user);
+    $role_obj->create(
+        role_object => $role,
+    );
+    ok($role_obj->id, 'created role-5 profile');
+    is($role_obj->unique_id, 11, 'created role-5 unique ID');
+}
+
+# Create the relationships
+
+{ # user-2 - first <other> relationshifirstp
+    my $user_obj = $user_class->new(current_user => $system_user);
+    $user_obj->load(2);
+    ok($user_obj->id, 'loaded user-2');
+    
+    my $other_obj = $other_class->new(current_user => $system_user);
+    $other_obj->load(1);
+    ok($other_obj->id, 'loaded first <other>');
+
+    is($user_obj->unique_id, 5);
+    is($other_obj->unique_id, 4);
+
+    $other_obj->set_my_user($user_obj);
+
+    $other_obj->load(1);
+    $user_obj->load(2);
+
+    is($other_obj->my_user->id, 2, 'other side of user-2 - first <other> relationship');
+    is_deeply(
+        [ sort map { $_->id } @{ $user_obj->my_others->items_array_ref } ],
+        [ 1 ],
+        'user side of user-2 - first <other> relationship'
+    );
+}
+
+{ # first <other> - role-2 relationship
+    my $role_obj = $role_class->new(current_user => $system_user);
+    $role_obj->load(2);
+    ok($role_obj->id, 'loaded role-2');
+    
+    my $other_obj = $other_class->new(current_user => $system_user);
+    $other_obj->load(1);
+    ok($other_obj->id, 'loaded first <other>');
+
+    $role_obj->set_my_other($other_obj);
+
+    $role_obj->load(2);
+    $other_obj->load(1);
+
+    is($role_obj->my_other->id, 1, 'other side of first <other> - role-2 relationship');
+    is_deeply(
+        [ sort map { $_->id } @{ $other_obj->my_roles->items_array_ref } ],
+        [ 2 ],
+        'user side of first <other> - role-2 relationship'
+    );
+}
+
+{ # user-4 - second <other> relationship
+    my $user_obj = $user_class->new(current_user => $system_user);
+    $user_obj->load(4);
+    ok($user_obj->id, 'loaded user-4');
+    
+    my $other_obj = $other_class->new(current_user => $system_user);
+    $other_obj->load(2);
+    ok($other_obj->id, 'loaded second <other>');
+
+    $other_obj->set_my_user($user_obj);
+
+    $user_obj->load(4);
+    $other_obj->load(2);
+
+    is($other_obj->my_user->id, 4, 'other side of user-4 - second <other> relationship');
+    is_deeply(
+        [ sort map { $_->id } @{ $user_obj->my_others->items_array_ref } ],
+        [ 2 ],
+        'user side of user-4 - second <other> relationship'
+    );
+}
+
+{ # role-1 - role-3 relationship
+    my $parent_role_obj = $role_class->new(current_user => $system_user);
+    $parent_role_obj->load(3);
+    ok($parent_role_obj->id, 'loaded role-3');
+    
+    my $child_role_obj = $role_class->new(current_user => $system_user);
+    $child_role_obj->load(1);
+    ok($child_role_obj->id, 'loaded role-1');
+
+    $parent_role_obj->set_my_child_role($child_role_obj);
+
+    $parent_role_obj->load(3);
+    $child_role_obj->load(1);
+
+    is($parent_role_obj->my_child_role->id, 1, 'parent side of role-1 - role-3 relationship');
+    is_deeply(
+        [ sort map { $_->id } @{ $child_role_obj->my_parent_roles->items_array_ref } ],
+        [ 3 ],
+        'user side of user-1 - role-3 relationship'
+    );
+}
+
+{ # user-3 - role-1 relationship
+    my $user_obj = $user_class->new(current_user => $system_user);
+    $user_obj->load(3);
+    ok($user_obj->id, 'loaded user-3');
+    
+    my $role_obj = $role_class->new(current_user => $system_user);
+    $role_obj->load(1);
+    ok($role_obj->id, 'loaded role-1');
+
+    $role_obj->set_my_user($user_obj);
+
+    $user_obj->load(3);
+    $role_obj->load(1);
+
+    is($role_obj->my_user->id, 3, 'role side of user-3 - role-1 relationship');
+    is_deeply(
+        [ sort map { $_->id } @{ $user_obj->my_roles->items_array_ref } ],
+        [ 1 ],
+        'user side of user-3 - role-1 relationship'
+    );
+}
+
+{ # first <other> - role-4 relationship
+    my $role_obj = $role_class->new(current_user => $system_user);
+    $role_obj->load(4);
+    ok($role_obj->id, 'loaded role-4');
+    
+    my $other_obj = $other_class->new(current_user => $system_user);
+    $other_obj->load(1);
+    ok($other_obj->id, 'loaded first <other>');
+
+    $role_obj->set_my_other($other_obj);
+
+    $role_obj->load(4);
+    $other_obj->load(1);
+
+    is($role_obj->my_other->id, 1, 'other side of first <other> - role-4 relationship');
+    is_deeply(
+        [ sort map { $_->id } @{ $other_obj->my_roles->items_array_ref } ],
+        [ 2, 4 ],
+        'user side of first <other> - role-4 relationship'
+    );
+}
+
+{ # user-1 - role-1 relationship
+    my $user_obj = $user_class->new(current_user => $system_user);
+    $user_obj->load(1);
+    ok($user_obj->id, 'loaded user-1');
+    
+    my $role_obj = $role_class->new(current_user => $system_user);
+    $role_obj->load(1);
+    ok($role_obj->id, 'loaded role-1');
+
+    $role_obj->set_my_user($user_obj);
+
+    $role_obj->load(1);
+    $user_obj->load(1);
+
+    is($role_obj->my_user->id, 1, 'role side of user-1 - role-1 relationship');
+    is_deeply(
+        [ sort map { $_->id } @{ $user_obj->my_roles->items_array_ref } ],
+        [ 1 ],
+        'user side of user-1 - role-1 relationship'
+    );
+}
+
+{ # user-3 - first <other> relationship
+    my $user_obj = $user_class->new(current_user => $system_user);
+    $user_obj->load(3);
+    ok($user_obj->id, 'loaded user-3');
+    
+    my $other_obj = $other_class->new(current_user => $system_user);
+    $other_obj->load(1);
+    ok($other_obj->id, 'loaded first <other>');
+
+    $other_obj->set_my_user($user_obj);
+
+    $user_obj->load(3);
+    $other_obj->load(1);
+
+    is($other_obj->my_user->id, 3, 'other side of user-3 - first <other> relationship');
+    is_deeply(
+        [ sort map { $_->id } @{ $user_obj->my_others->items_array_ref } ],
+        [ 1 ],
+        'user side of user-4 - second <other> relationship'
+    );
+}
+
+{ # role-1 - role-2 relationship
+    my $parent_role_obj = $role_class->new(current_user => $system_user);
+    $parent_role_obj->load(2);
+    ok($parent_role_obj->id, 'loaded role-2');
+    
+    my $child_role_obj = $role_class->new(current_user => $system_user);
+    $child_role_obj->load(1);
+    ok($child_role_obj->id, 'loaded role-1');
+
+    $parent_role_obj->set_my_child_role($child_role_obj);
+
+    $parent_role_obj->load(2);
+    $child_role_obj->load(1);
+
+    is($parent_role_obj->my_child_role->id, 1, 'parent side of role-1 - role-2 relationship');
+    is_deeply(
+        [ sort map { $_->id } @{ $child_role_obj->my_parent_roles->items_array_ref } ],
+        [ 2, 3 ],
+        'user side of user-1 - role-2 relationship'
+    );
+}
+
+# Create the permissions
+
+{ # permissions for role-1
+    $role->load(1);
+    is($role->id, 1, 'loaded role-1');
+
+    my $profile_obj = CASPlus->get_profile_object_by_unique_id(int(rand(11)) + 1);
+    ok($profile_obj->id, 'loaded a random profile object');
+
+    $permission->create(
+        the_role          => $role,
+        profile           => $profile_obj,
+        may_create        => int(rand(1)),
+        may_create_my_own => int(rand(1)),
+        may_read          => int(rand(1)),
+        may_read_my_own   => int(rand(1)),
+        may_write         => int(rand(1)),
+        may_write_my_own  => int(rand(1)),
+        may_delete        => int(rand(1)),
+        may_delete_my_own => int(rand(1)),
+    );
+    ok($permission->id, "created a permission for role-1");
+}
+
+{ # permissions for role-2
+    $role->load(2);
+    is($role->id, 2, 'loaded role-2');
+
+    for ( 1 .. 3 ) {
+        my $profile_obj = CASPlus->get_profile_object_by_unique_id(int(rand(11)) + 1);
+        ok($profile_obj->id, 'loaded a random profile object');
+
+        $permission->create(
+            the_role          => $role,
+            profile           => $profile_obj,
+            may_create        => int(rand(1)),
+            may_create_my_own => int(rand(1)),
+            may_read          => int(rand(1)),
+            may_read_my_own   => int(rand(1)),
+            may_write         => int(rand(1)),
+            may_write_my_own  => int(rand(1)),
+            may_delete        => int(rand(1)),
+            may_delete_my_own => int(rand(1)),
+        );
+        ok($permission->id, "created a permission for role-2");
+    }
+}
+
+{ # permissions for role-3
+    $role->load(3);
+    is($role->id, 3, 'loaded role-3');
+
+    for ( 1 .. 3 ) {
+        my $profile_obj = CASPlus->get_profile_object_by_unique_id(int(rand(11)) + 1);
+        ok($profile_obj->id, 'loaded a random profile object');
+
+        $permission->create(
+            the_role          => $role,
+            profile           => $profile_obj,
+            may_create        => int(rand(1)),
+            may_create_my_own => int(rand(1)),
+            may_read          => int(rand(1)),
+            may_read_my_own   => int(rand(1)),
+            may_write         => int(rand(1)),
+            may_write_my_own  => int(rand(1)),
+            may_delete        => int(rand(1)),
+            may_delete_my_own => int(rand(1)),
+        );
+        ok($permission->id, "created a permission for role-3");
+    }
+}
+
+{ # permissions for role-4
+    $role->load(4);
+    is($role->id, 4, 'loaded role-4');
+
+    for ( 1 .. 3 ) {
+        my $profile_obj = CASPlus->get_profile_object_by_unique_id(int(rand(11)) + 1);
+        ok($profile_obj->id, 'loaded a random profile object');
+
+        $permission->create(
+            the_role          => $role,
+            profile           => $profile_obj,
+            may_create        => int(rand(1)),
+            may_create_my_own => int(rand(1)),
+            may_read          => int(rand(1)),
+            may_read_my_own   => int(rand(1)),
+            may_write         => int(rand(1)),
+            may_write_my_own  => int(rand(1)),
+            may_delete        => int(rand(1)),
+            may_delete_my_own => int(rand(1)),
+        );
+        ok($permission->id, "created a permission for role-4");
+    }
+}
+
+{ # permissions for role-5
+    $role->load(5);
+    is($role->id, 5, 'loaded role-4');
+
+    for ( 1 .. 3 ) {
+        my $profile_obj = CASPlus->get_profile_object_by_unique_id(int(rand(11) + 1));
+        ok($profile_obj->id, 'loaded a random profile object');
+
+        $permission->create(
+            the_role          => $role,
+            profile           => $profile_obj,
+            may_create        => int(rand(1)),
+            may_create_my_own => int(rand(1)),
+            may_read          => int(rand(1)),
+            may_read_my_own   => int(rand(1)),
+            may_write         => int(rand(1)),
+            may_write_my_own  => int(rand(1)),
+            may_delete        => int(rand(1)),
+            may_delete_my_own => int(rand(1)),
+        );
+        ok($permission->id, "created a permission for role-5");
+    }
+}
+
+my %user_results = (
+    1 => {
+        roles => [ 1, 2, 3 ],
+        perms => [ 1, 2, 3, 4, 5, 6, 7 ],
+    },
+    2 => {
+        roles => [ 2, 4 ],
+        perms => [ 2, 3, 4, 8, 9, 10 ],
+    },
+    3 => {
+        roles => [ 1, 2, 3, 4 ],
+        perms => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ],
+    },
+    4 => {
+        roles => [ ],
+        perms => [ ],
+    },
+);
+
+my %role_members = (
+    '' => [
+        ':5:@2:4:',
+        ':4:@4:3:',
+        ':10:@2:6:',
+        ':2:@3:7:',
+        ':4:@4:9:',
+        ':8:@2:4:',
+        ':2:@3:3:',
+    ],
+    1 => [
+        ':5:@2:4:@4:3:',
+    ],
+    2 => [
+        ':8:@1:2:',
+    ],
+    3 => [
+        ':8:@1:2:@3:7:',
+    ],
+    4 => [
+        ':5:@2:4:@4:9:',
+    ],
+    5 => [
+        ':1:@1:2:',
+    ],
+    6 => [
+        ':1:@1:2:@3:7:',
+    ],
+    7 => [
+        ':8:@2:4:@4:3:',
+        ':8:@1:2:@3:3:',
+    ],
+    8 => [
+        ':8:@2:4:@4:9:',
+    ],
+    9 => [
+        ':1:@1:2:@3:3:',
+    ],
+);
+
+# Run the tests on the role cache built on-the-fly
+my $role_members = CASPlus::Model::RoleMemberCollection->new(current_user => $system_user);
+$role_members->unlimit;
+is($role_members->count_all, (scalar keys %role_members) - 1, 'there are the right number of role members');
+
+my $cache_paths = CASPlus::Model::RoleMemberPathCacheCollection->new(current_user => $system_user);
+$cache_paths->unlimit;
+is($cache_paths->count_all, (sum map { scalar @$_ } values %role_members), 'there are the right number of cache paths');
+
+while (my $path_cache = $cache_paths->next) {
+    diag($path_cache->cache_path);
+}
+
+my $profiles = CASPlus::Model::ProfileCollection->new(current_user => $system_user);
+$profiles->unlimit;
+while (my $profile = $profiles->next) {
+    my $collection_class = $profile->record_class->collection_class;
+    my $objects = $collection_class->new(current_user => $system_user);
+    $objects->unlimit;
+    while (my $object = $objects->next) {
+        $object->recalculate_role_cache;
+
+        # Run the tests on the role cache built by total recalculation
+        $role_members = CASPlus::Model::RoleMemberCollection->new(current_user => $system_user);
+        $role_members->unlimit;
+        is($role_members->count_all, (scalar keys %role_members) - 1, 'there are the right number of role members');
+
+        $cache_paths = CASPlus::Model::RoleMemberPathCacheCollection->new(current_user => $system_user);
+        $cache_paths->unlimit;
+        is($cache_paths->count_all, (sum map { scalar @$_ } values %role_members), 'there are the right number of cache paths');
+    }
+
+}
+
+
+#while (my $path_cache = $cache_paths->next) {
+#    diag($path_cache->cache_path);
+#}


More information about the Jifty-commit mailing list