webgui/t/User.t
Colin Kuskie 4b891f22f8 We're getting occasional failures of tests for timings. Using
the lastUpdated method on a user, for example.  I've made two changes
to try and fix these:

1) Changed the tests to use cmp_ok instead of ok, so that future failures
will print the measured times as a debug, like is and isn't do.

2) Reversed the order of the method and time measurement.  This might help
the difference stay smaller since the two statements are closer.
2006-08-01 18:28:58 +00:00

307 lines
12 KiB
Perl

#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2006 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use FindBin;
use strict;
use lib "$FindBin::Bin/lib";
use WebGUI::Test;
use WebGUI::Session;
use WebGUI::Utility;
use WebGUI::Cache;
use WebGUI::User;
use Test::More tests => 81; # increment this value for each test you create
my $session = WebGUI::Test->session;
my $testCache = WebGUI::Cache->new($session, 'myTestKey');
$testCache->flush;
my $user;
my $lastUpdate;
#Let's try to create a new user and make sure we get an object back
my $userCreationTime = time();
ok(defined ($user = WebGUI::User->new($session,"new")), 'new("new") -- object reference is defined');
#New does not return undef if something breaks, so we'll see if the _profile hash was set.
ok(scalar %{$user->{_profile}} > 0, 'new("new") -- profile property contains at least one key');
#Let's assign a username
$user->username("bill_lumberg");
$lastUpdate = time();
is($user->username, "bill_lumberg", 'username() method');
cmp_ok(abs($user->lastUpdated-$lastUpdate), '<=', 1, 'lastUpdated() -- username change');
#Let's check the UID and make sure it's sane
ok($user->userId =~ m/[A-Za-z0-9\-\_]{22}/, 'userId() returns sane value');
#Let's make sure the user was added to the correct groups;
foreach my $groupId (2,7) {
ok($user->isInGroup($groupId), "User added to group $groupId by default");
}
#Let's check the status method
$user->status('Active');
$lastUpdate = time();
is($user->status, "Active", 'status("Active")');
cmp_ok(abs($user->lastUpdated-$lastUpdate), '<=', 1, 'lastUpdated() -- status change');
$user->status('Selfdestructed');
is($user->status, "Selfdestructed", 'status("Selfdestructed")');
$user->status('Deactivated');
is($user->status, "Deactivated", 'status("Deactivated")');
#Let's get/set a profile field
$user->profileField("firstName", "Bill");
$lastUpdate = time();
is($user->profileField("firstName"), "Bill", 'profileField() get/set');
cmp_ok(abs($user->lastUpdated-$lastUpdate), '<=', 1, 'lastUpdated() -- profileField');
#Let's check the auth methods
#Default should be WebGUI
is($user->authMethod, "WebGUI", 'authMethod() -- default value is WebGUI');
#Try changing to LDAP
$lastUpdate = time();
$user->authMethod("LDAP");
is($user->authMethod, "LDAP", 'authMethod() -- set to LDAP');
$user->authMethod("WebGUI");
is($user->authMethod, "WebGUI", 'authMethod() -- set back to WebGUI');
ok(abs($user->lastUpdated-$lastUpdate) < 1, 'lastUpdated() -- authmethod change');
#See if datecreated is correct
is($user->dateCreated, $userCreationTime, 'dateCreated()');
#get/set karma
my $oldKarma = $user->karma;
$user->karma('69', 'peter gibbons', 'test karma');
is($user->karma, $oldKarma+69, 'karma() -- get/set add amount');
my ($source, $description) = $session->db->quickArray("select source, description from karmaLog where userId=?",[$user->userId]);
is($source, 'peter gibbons', 'karma() -- get/set source');
is($description, 'test karma', 'karma() -- get/set description');
$oldKarma = $user->karma;
$user->karma('-69', 'peter gibbons', 'lumberg took test karma away');
is($user->karma, $oldKarma-69, 'karma() -- get/set subtract amount');
#Let's test referringAffiliate
$lastUpdate = time();
$user->referringAffiliate(10);
is($user->referringAffiliate, '10', 'referringAffiliate() -- get/set');
ok(abs($user->lastUpdated-$lastUpdate) < 1, 'lastUpdated() -- referringAffiliate');
#Let's try adding this user to some groups. Note, users are auto-added to 2 and 7 on creation
my @groups = qw|6 4|;
$user->addToGroups(\@groups);
my ($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [6, $user->userId]);
ok($result, 'addToGroups() -- added to first test group');
($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [4, $user->userId]);
ok($result, 'addToGroups() -- added to second test group');
#Let's delete this user from our test groups
$user->deleteFromGroups(\@groups);
my ($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [6, $user->userId]);
is($result, '0', 'deleteFromGroups() -- removed from first test group');
my ($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [4, $user->userId]);
is($result, '0', 'deleteFromGroups() -- removed from second test group');
#Let's delete this user
my $userId = $user->userId;
$user->delete;
my ($count) = $session->db->quickArray("select count(*) from users where userId=?",[$userId]);
is($count, '0', 'delete() -- users table');
($count) = $session->db->quickArray("select count(*) from userProfileData where userId=?",[$userId]);
is($count, '0', 'delete() -- userProfileData table');
($count) = $session->db->quickArray("select count(*) from inbox where userId=?",[$userId]);
is($count, '0', 'delete() -- inbox table');
#Let's test new with an override uid
$user = WebGUI::User->new($session, "new", "ROYSUNIQUEUSERID000001");
is($user->userId, "ROYSUNIQUEUSERID000001", 'new() -- override user id');
$user->delete;
#Let's test new to retrieve an existing user
$user = WebGUI::User->new($session,3);
is($user->username, "Admin", 'new() -- retrieve existing user');
$user = "";
#Let's test new to retrieve default user visitor with no params passed in
$user = WebGUI::User->new($session);
is($user->userId, '1', 'new() -- returns visitor with no args');
$user = "";
$user = WebGUI::User->new($session, "new", "ROYSUNIQUEUSERID000002");
is($user->userId, "ROYSUNIQUEUSERID000002", 'new() -- override user id');
$user->authMethod("LDAP");
is($user->authMethod, "LDAP", 'authMethod() -- set to LDAP');
#get/set karma
my $oldKarma = $user->karma;
$user->karma('69', 'peter gibbons', 'test karma');
is($user->karma, $oldKarma+69, 'karma() -- get/set add amount');
my ($source, $description) = $session->db->quickArray("select source, description from karmaLog where userId=?",[$user->userId]);
is($source, 'peter gibbons', 'karma() -- get/set source');
is($description, 'test karma', 'karma() -- get/set description');
$oldKarma = $user->karma;
$user->karma('-69', 'peter gibbons', 'lumberg took test karma away');
is($user->karma, $oldKarma-69, 'karma() -- get/set subtract amount');
#Let's test referringAffiliate
$lastUpdate = time();
$user->referringAffiliate(10);
is($user->referringAffiliate, '10', 'referringAffiliate() -- get/set');
ok(abs($user->lastUpdated-$lastUpdate) < 1, 'lastUpdated() -- referringAffiliate');
#Let's try adding this user to some groups
my @groups = qw|2 4|;
$user->addToGroups(\@groups);
my ($result) = $session->db->quickArray("select count(*) from groupings where groupId=".$session->db->quote('2')." and userId=".$session->db->quote($user->userId));
ok($result, 'addToGroups() -- added to first test group');
($result) = $session->db->quickArray("select count(*) from groupings where groupId=".$session->db->quote('4')." and userId=".$session->db->quote($user->userId));
ok($result, 'addToGroups() -- added to second test group');
#Let's delete this user from our test groups
$user->deleteFromGroups(\@groups);
($result) = $session->db->quickArray("select count(*) from groupings where groupId=".$session->db->quote('2')." and userId=".$session->db->quote($user->userId));
is($result, '0', 'deleteFromGroups() -- removed from first test group');
($result) = $session->db->quickArray("select count(*) from groupings where groupId=".$session->db->quote('4')." and userId=".$session->db->quote($user->userId));
is($result, '0', 'deleteFromGroups() -- removed from second test group');
#Let's delete this user
my $userId = $user->userId;
$user->delete;
my ($count) = $session->db->quickArray("select count(*) from users where userId=".$session->db->quote($userId));
is($count, '0', 'delete() -- users table');
($count) = $session->db->quickArray("select count(*) from userProfileData where userId=".$session->db->quote($userId));
is($count, '0', 'delete() -- userProfileData table');
($count) = $session->db->quickArray("select count(*) from inbox where userId=".$session->db->quote($userId));
is($count, '0', 'delete() -- inbox table');
ok(WebGUI::User->validUserId($session, 1), 'Visitor has a valid userId');
ok(WebGUI::User->validUserId($session, 3), 'Admin has a valid userId');
ok(!WebGUI::User->validUserId($session, 'eeee'), 'random illegal Id #1');
ok(!WebGUI::User->validUserId($session, 37), 'random illegal Id #2');
#identifier() and uncache()
SKIP: {
skip("identifier() -- deprecated",1);
ok(undef, "identifier()");
}
SKIP: {
skip("uncache() -- Don't know how to test uncache()",1);
ok(undef, "uncache");
}
my $cm = WebGUI::Group->new($session, 4);
is( $cm->name, "Content Managers", "content manager name check");
is( $cm->getId, 4, "content manager groupId check");
my $admin = WebGUI::User->new($session, 3);
my $visitor = WebGUI::User->new($session, 1);
$session->db->write('update userSession set lastIP=? where sessionId=?',['192.168.0.101', $session->getId]);
my ($result) = $session->db->quickArray('select lastIP,sessionId from userSession where sessionId=?',[$session->getId]);
is ($result, '192.168.0.101', "userSession setup correctly");
ok (!$visitor->isInGroup($cm->getId), "Visitor is not member of group");
ok ($admin->isInGroup($cm->getId), "Admin is member of group");
my $origFilter = $cm->ipFilter;
$cm->ipFilter('192.168.0.0/24');
is( $cm->ipFilter, "192.168.0.0/24", "ipFilter assignment to local net, 192.168.0.0/24");
$session->errorHandler->warn("Begin IP lookup");
ok ($visitor->isInGroup($cm->getId), "Visitor is allowed in via IP");
$session->db->write('update userSession set lastIP=? where sessionId=?',['193.168.0.101', $session->getId]);
$cm->clearCaches;
ok (!$visitor->isInGroup($cm->getId), "Visitor is not allowed in via IP");
##Restore original filter
$cm->ipFilter(defined $origFilter ? $origFilter : '');
##Test for group membership
$user = WebGUI::User->new($session, "new");
$user->addToGroups([3]);
$session->errorHandler->warn('userId: '. $user->userId);
ok($user->isInGroup(3), "addToGroups: New user is in group 3(Admin)");
ok($user->isInGroup(11), "New user is in group 11(Secondary Admins)");
ok($user->isInGroup(12), "New user is in group 12(Turn On Admin)");
ok($user->isInGroup(13), "New user is in group 13(Export Managers)");
ok($user->isInGroup(14), "New user is in group 14(Product Managers)");
$user->deleteFromGroups([3]);
ok(!$user->isInGroup(3), "deleteFromGroups: New user is not in group 3(Admin)");
ok(!$user->isInGroup(11), "Flush cache, new user not in group 11 (Secondary Admins)");
ok(!$user->isInGroup(12), "Flush cache, new user not in group 12 (Turn On Admin)");
ok(!$user->isInGroup(13), "Flush cache, new user not in group 13 (Export Managers)");
ok(!$user->isInGroup(14), "Flush cache, new user not in group 14 (Product Managers)");
$user->delete;
ok($visitor->isInGroup(1), "Visitor is a member of group Visitor");
ok($visitor->isInGroup(7), "Visitor is a member of group Everyone");
##remove Visitor from those groups, and make sure we can add him back in.
WebGUI::Group->new($session, '1')->deleteUsers([1]);
($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [1, $user->userId]);
is($result, 0, 'deleteFromGroups() -- Visitor removed from Visitor group');
WebGUI::Group->new($session, '7')->deleteUsers([1]);
($result) = $session->db->quickArray("select count(*) from groupings where groupId=? and userId=?", [7, $user->userId]);
is($result, 0, 'deleteFromGroups() -- Visitor removed from Everyone group');
ok($visitor->isInGroup(1), "isInGroup: Visitor is in group Visitor, hardcoded");
ok($visitor->isInGroup(7), "isInGroup: Everyone is in group Everyone, hardcoded");
##Add Visitor back to those groups
WebGUI::Group->new($session, '1')->addUsers([1]);
WebGUI::Group->new($session, '7')->addUsers([1]);
ok($visitor->isInGroup(1), "Visitor added back to group Visitor");
ok($visitor->isInGroup(7), "Visitor added back to group Everyone");
END {
(defined $user and ref $user eq 'WebGUI::User') and $user->delete;
$testCache->flush;
}