[LON-CAPA-cvs] cvs: modules /jerf/tests ApacheRequest.pm Utils.pm

bowersj2 lon-capa-cvs@mail.lon-capa.org
Tue, 01 Jul 2003 20:20:16 -0000


bowersj2		Tue Jul  1 16:20:16 2003 EDT

  Modified files:              
    /modules/jerf/tests	ApacheRequest.pm Utils.pm 
  Log:
  Can now also delete courses and revoke at least the cc in a course, so
  running the test suite should now be capable of running without majorly 
  polluting the lonUsers/ directory.
  
  
Index: modules/jerf/tests/ApacheRequest.pm
diff -u modules/jerf/tests/ApacheRequest.pm:1.7 modules/jerf/tests/ApacheRequest.pm:1.8
--- modules/jerf/tests/ApacheRequest.pm:1.7	Mon Jun 30 16:08:07 2003
+++ modules/jerf/tests/ApacheRequest.pm	Tue Jul  1 16:20:16 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Apache Request Simulator
 #
-# $Id: ApacheRequest.pm,v 1.7 2003/06/30 20:08:07 bowersj2 Exp $
+# $Id: ApacheRequest.pm,v 1.8 2003/07/01 20:20:16 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -570,6 +570,7 @@
     for my $handler (@{$self->{handler_list}}) {
 	$self->realDoHandler($handler);
     }
+    $self->resetRequest(); # if those other handlers output anything, don't send it back
     return $self->realDoHandler($handlerPackageName);
 }
 
@@ -684,8 +685,8 @@
 =item * I<execInEnv>(code): Executes the code in the request's environment and
     saves the changes the code makes in the environment to the request. Useful
     for things like logging in, where we bypass the handler but we really need
-    the request's environment fiddled with. Returns the (scalar) result of the
-    code called.
+    the request's environment fiddled with, or things that end up calling
+    Apache::lonnet::allowed. Returns the (scalar) result of the code called.
 
 =cut
 
Index: modules/jerf/tests/Utils.pm
diff -u modules/jerf/tests/Utils.pm:1.3 modules/jerf/tests/Utils.pm:1.4
--- modules/jerf/tests/Utils.pm:1.3	Mon Jun 30 16:08:07 2003
+++ modules/jerf/tests/Utils.pm	Tue Jul  1 16:20:16 2003
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # testing utilities
 #
-# $Id: Utils.pm,v 1.3 2003/06/30 20:08:07 bowersj2 Exp $
+# $Id: Utils.pm,v 1.4 2003/07/01 20:20:16 bowersj2 Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -199,7 +199,7 @@
 sub delete {
     my $self = shift;
     
-    my $dirname = "/home/httpd/lonTabs/";
+    my $dirname = "/home/httpd/lonUsers/";
     $dirname .= $self->{domain} . '/';
     $dirname .= substr($self->{name}, 0, 1) . '/';
     $dirname .= substr($self->{name}, 1, 1) . '/';
@@ -225,6 +225,9 @@
     # Now we run lonacc to get the environment set up
     $r->resetRequest();
     $r->doHandler("Apache::lonacc");
+
+    # FIXME: Detect if we logged in correctly (account may not exist?)
+    return 1;
 }
 
 # FIXME: Document; selects the DC role for the user, if any
@@ -241,6 +244,9 @@
 			        headers => $request->{headers},
 			        uri => "/adm/roles"});
     $r->doHandler("Apache::lonroles");
+    
+    # FIXME: Return 0 if the role does not exist.
+    return 1;
 }
 
 1;
@@ -280,6 +286,7 @@
 
 use Data;
 use ApacheRequest;
+use Apache::lonnet;
 
 sub new {
     my $class = shift;
@@ -290,6 +297,7 @@
     $self->{courseMap} = shift;
     if (!defined($self->{courseMap})) { $self->{courseMap} = ''; }
     $self->{courseCoordinator} = shift;
+    $self->{domain} = $Data::testDomain;
 
     my $postcontent = {};
 
@@ -315,7 +323,110 @@
 				headers => $self->{request}->{headers}});
     $r->doHandler("Apache::loncreatecourse");
 
+    # Now extract the course ID 
+    my $output = $r->getOutputString();
+    ($self->{courseId}) = $output =~ m/Course ID: ([^<]+)/;
+
+    # Prepare a Domain Coordinator login that can be used to manipulate
+    # the course
+    
+    $self->{dc} = Utils::User->new($Data::testDomain, $Data::dcName,
+				   $Data::dcPassword);
+    $self->{r} = ApacheRequest->new({user=>$user});
+    if (!$self->{dc}->login($self->{r}) || !$self->{dc}->selectRoleDC($self->{r})) {
+	# will cause later calls to blow up.
+	$self->{dc} = undef;
+	$self->{r} = undef; 
+    } 
+    
     bless($self, $class);
+}
+
+=pod
+
+=item * I<revokeRole>(userName, roleType, delete): Revokes the given role
+    in the course, where user is the login name of the user who 
+    owns the role you wish to revoke, and roleType is the two-letter
+    code of the role you wish to revoke. If delete is true, the role
+    is actually deleted, otherwise it is just revoked.
+
+=cut
+
+sub revokeRole {
+    my $self = shift;
+    (my $userName, my $role, my $delete) = @_;
+
+    my $call = sub { return &Apache::lonnet::assignrole($self->{domain}, $userName, 
+					   $self->{courseId}, $role, time(), 0, $delete); };
+    my $result = $self->{r}->execInEnv($call);
+
+    return $result eq 'ok';
+}
+
+=pod
+
+=item * I<addRole>(username, roleType): Adds the given role in the course,
+    where username is the login name of the user who should get the role, and
+    roleType is the two-letter role code representing the role to be added.
+
+    Since the role is intended to be transient, there's no way to specify end
+    or start date with this call.
+
+=cut
+
+
+sub addRole {
+    my $self = shift;
+    (my $userName, my $role) = @_;
+
+    my $call = sub { &Apache::lonnet::assignrole($self->{domain}, $userName,
+					     $self->{courseId}, $role, time() - 1,
+						 ''); };
+    my $result = $self->{r}->execInEnv($call);
+
+    return $result eq 'ok';
+}
+
+
+=pod
+
+=item * I<courseDirectory>: Return the absolute path to the directory where
+    the course information lives.
+
+=cut 
+
+sub courseDirectory {
+    my $self = shift;
+    my $id = $self->{courseId};
+    ($id) = $id =~ m!/\w+/(.*)!;
+
+    my $dirname = "/home/httpd/lonUsers/";
+    $dirname .= $self->{domain} . '/';
+    $dirname .= substr($id, 0, 1) . '/';
+    $dirname .= substr($id, 1, 1) . '/';
+    $dirname .= substr($id, 2, 1) . '/';
+    $dirname .= $id;
+
+    return $dirname;
+}
+
+=pod
+
+=item I<delete>(): Violently deletes the course (we're talking rm -rf here), and
+    to emphasize the point that you deleted the course, replaces your reference with
+    undef. Generally it would be a good idea to not leave any roles lying around 
+    that are attached to this course, because they'll be pointing to a non-existant 
+    course and that has the potential to annoy LON-CAPA.
+
+=cut
+
+sub delete {
+    my $courseDir = $_[0]->courseDirectory();
+    my $domain = $_[0]->{domain};
+    if ($courseDir =~ m!^/home/httpd/lonUsers/$domain/./././\w*$!) { # little extra protection
+	system("rm -rf $courseDir");
+    }
+    $_[0] = undef;
 }
 
 1;