Revision: 580
Author: perlstalker
Date: Fri Nov 27 22:27:35 2009
Log: Add code and tests for RetrieveUsers() and RetrieveAllUsers().
http://code.google.com/p/vuser/source/detail?r=580
Modified:
/VUser-Google-ProvisioningAPI/trunk/lib/VUser/Google/ApiProtocol/V2_0.pm
/VUser-Google-ProvisioningAPI/trunk/lib/VUser/Google/ApiProtocol.pm
/VUser-Google-ProvisioningAPI/trunk/lib/VUser/Google/Provisioning/V2_0.pm
/VUser-Google-ProvisioningAPI/trunk/t/tests/My/Test/Class.pm
/VUser-Google-ProvisioningAPI/trunk/t/tests/Test/VUser/Google/Provisioning/V2_0.pm
=======================================
---
/VUser-Google-ProvisioningAPI/trunk/lib/VUser/Google/ApiProtocol/V2_0.pm
Sat Sep 19 20:08:50 2009
+++
/VUser-Google-ProvisioningAPI/trunk/lib/VUser/Google/ApiProtocol/V2_0.pm
Fri Nov 27 22:27:35 2009
@@ -138,6 +138,9 @@
$self->dprint( "*** REQUEST ***" );
+ # relogin if needed
+ $self->Login;
+
# clear last results
$self->_set_reply_headers('');
$self->_set_reply_content('');
=======================================
--- /VUser-Google-ProvisioningAPI/trunk/lib/VUser/Google/ApiProtocol.pm Sat
Sep 19 20:08:50 2009
+++ /VUser-Google-ProvisioningAPI/trunk/lib/VUser/Google/ApiProtocol.pm Fri
Nov 27 22:27:35 2009
@@ -125,7 +125,12 @@
my $text = shift;
my @args = @_;
if( $self->debug and defined ($text) ) {
- print STDERR sprintf ("$text\n", @args);
+ if (@_) {
+ print STDERR sprintf ("$text\n", @args);
+ }
+ else {
+ print STDERR "$text\n";
+ }
}
}
=======================================
---
/VUser-Google-ProvisioningAPI/trunk/lib/VUser/Google/Provisioning/V2_0.pm
Tue Nov 17 23:17:24 2009
+++
/VUser-Google-ProvisioningAPI/trunk/lib/VUser/Google/Provisioning/V2_0.pm
Fri Nov 27 22:27:35 2009
@@ -90,7 +90,7 @@
else {
## ERROR!
$self->dprint('CreateUser failed: '.$self->google->result->{reason});
- return undef;
+ die "Error creating user: ".$self->google->result->{'reason'}."\n";
}
}
@@ -101,14 +101,94 @@
my $url = $self->base_url.$self->google->domain.'/user/2.0/'.$username;
if ($self->google->Request('GET', $url)) {
- return $self->_build_user_entry($self->google->get_result);
+ return $self->_build_user_entry($self->google->result);
}
else {
- return undef;
+ if ($self->google->result->{'reason'} =~ 'EntityDoesNotExist') {
+ return undef;
+ }
+ else {
+ die "Error retrieving user: ".$self->google->result->{'reason'}."\n";
+ }
}
}
+# Retrieve one page of users.
+# How to return the next page?
+# Returns (
+# entries => \@entries, # list of UserEntry objects
+# next => $next # the next username if another page exists
+# # undef otherwise
+# )
+sub RetrieveUsers {
+ my $self = shift;
+ my $start_user = shift;
+
+ my @entries = ();
+ my $next_user;
+
+ my $url = $self->base_url.$self->google->domain.'/user/2.0';
+ if ($start_user) {
+ $url .= "?startUsername=$start_user";
+ }
+
+ if ($self->google->Request('GET', $url)) {
+ foreach my $entry (@{ $self->google->result->{'entry'} }) {
+ ## Create UserEntry object
+ my $user = $self->_build_user_entry($entry);
+ push @entries, $user;
+ }
+ }
+ else {
+ ## There was an error
+ die "Error fetching users: ".$self->google->result->{'reason'}."\n";
+ }
+
+ # Look for the a link tag that says there should be more results
+ # A link tag with rel=next means there is another page
+ foreach my $link (@{ $self->google->result->{'link'} }) {
+ if ($link->{'rel'} eq 'next') {
+ $url = $link->{'href'};
+ if ($url =~ /startUsername=([^\"]+)/) {
+ $next_user = $1;
+ }
+ }
+ }
+
+ return ( entries => \@entries, next => $next_user );
+}
+
+# Alias for RetrieveUsers
+sub RetrievePageOfUsers {
+ $_[0]->RetrieveUsers(@_);
+}
+
+# Returns a list of UserEntry objects
sub RetrieveAllUsers {
+ my $self = shift;
+
+ my @entries = ();
+ my $next;
+
+ my %results;
+
+ eval {
+ %results = $self->RetrieveUsers;
+ push @entries, @{ $results{'entries'} };
+ $next = $results{'next'};
+ };
+ die $@ if $@;
+
+ while ($next) {
+ eval {
+ %results = $self->RetrieveUsers($next);
+ push @entries, @{ $results{'entries'} };
+ $next = $results{'next'};
+ };
+ die $@ if $@;
+ }
+
+ return @entries;
}
sub UpdateUser {
=======================================
--- /VUser-Google-ProvisioningAPI/trunk/t/tests/My/Test/Class.pm Tue Nov 17
23:17:24 2009
+++ /VUser-Google-ProvisioningAPI/trunk/t/tests/My/Test/Class.pm Fri Nov 27
22:27:35 2009
@@ -27,7 +27,7 @@
domain => $ENV{GAPPS_DOMAIN},
admin => $ENV{GAPPS_ADMIN},
password => $ENV{GAPPS_PASSWD},
-# debug => 1
+ debug => 1
);
return $google;
=======================================
---
/VUser-Google-ProvisioningAPI/trunk/t/tests/Test/VUser/Google/Provisioning/V2_0.pm
Tue Nov 17 23:17:24 2009
+++
/VUser-Google-ProvisioningAPI/trunk/t/tests/Test/VUser/Google/Provisioning/V2_0.pm
Fri Nov 27 22:27:35 2009
@@ -5,7 +5,7 @@
use Test::Most;
use base 'Test::VUser::Google::Provisioning';
-sub CreateUser : Tests(3) {
+sub CreateUser : Tests(12) {
my $test = shift;
my $class = $test->class;
@@ -28,8 +28,88 @@
is $res->UserName, $user, "... and the username is $user";
- # clean up
- $api->DeleteUser($res->UserName);
+ ## Retrieve Test
+ can_ok $api, 'RetrieveUser';
+ $res = $api->RetrieveUser($user);
+ isa_ok $res, 'VUser::Google::Provisioning::UserEntry',
+ '... and the account was retrieved';
+
+ is $res->GivenName, 'Test',
+ '... and retrieved given name matches';
+
+ is $res->FamilyName, 'User',
+ '... and retrieved family name matches';
+
+ TODO: {
+ local $TODO = 'How to check if quota updates are disabled?';
+ is $res->Quota, '2048',
+ '... and retrieved quota matches';
+ }
+
+ is $res->ChangePasswordAtNextLogin, 1,
+ '... and retrieved change pw matches';
+
+ ## clean up
+ can_ok $api, 'DeleteUser';
+ my $rc = $api->DeleteUser($res->UserName);
+ is $rc, 1, '... and delete reports successful';
+
+ $res = $api->RetrieveUser($user);
+ ok !defined $res,
+ '... and there\'s nothing to retrieve';
+}
+
+sub RetrieveUsers : Tests(5) {
+ my $test = shift;
+ my $class = $test->class;
+
+ my $api = $class->new(google => $test->create_google);
+
+ can_ok $api, 'RetrieveUsers';
+
+ can_ok $api, 'RetrieveAllUsers';
+
+ my $num_users = 110;
+
+ ## Create 110 test users
+ note "Creating $num_users test users. This will take a while.";
+ my $user = $test->get_test_user;
+ foreach my $i (1 .. $num_users) {
+ my $res = $api->CreateUser(
+ userName => $user.".$i",
+ givenName => 'Test',
+ familyName => 'User',
+ password => 'testing',
+ quota => 2048,
+ changePasswordAtNextLogin => 1,
+ );
+ }
+
+ ## Fetch first page of users
+ my %results = $api->RetrieveUsers;
+ is @{ $results{'entries'} }, 100,
+ '... and we have 100 users';
+ my $next = $results{next};
+
+
+ ## Fetch second page of users
+ %results = $api->RetrieveUsers($next);
+ is $results{'entries'}[0]->UserName, $next,
+ '... and the first result of the second page is the "next" from the first
page';
+
+ ## Retrieve all users
+ my @entries = $api->RetrieveAllUsers;
+ TODO: {
+ local $TODO = 'How many users already existed?';
+ ok @entries >= $num_users+1,
+ '... and there are the expected number of users';
+ }
+
+ ## Delete test users
+ note "Deleting $num_users test users. This will also take a while.";
+ foreach my $i (1 .. $num_users) {
+ my $rc = $api->DeleteUser($user.".$i");
+ }
}
1;