import rt 3.8.7
[freeside.git] / rt / t / web / rights1.t
1 #!/usr/bin/perl -w
2 use strict;
3 use HTTP::Cookies;
4
5 use RT::Test tests => 35;
6 my ($baseurl, $agent) = RT::Test->started_ok;
7
8 # Create a user with basically no rights, to start.
9 my $user_obj = RT::User->new($RT::SystemUser);
10 my ($ret, $msg) = $user_obj->LoadOrCreateByEmail('customer-'.$$.'@example.com');
11 ok($ret, 'ACL test user creation');
12 $user_obj->SetName('customer-'.$$);
13 $user_obj->SetPrivileged(1);
14 ($ret, $msg) = $user_obj->SetPassword('customer');
15 ok($ret, "ACL test password set. $msg");
16
17 # Now test the web interface, making sure objects come and go as
18 # required.
19
20
21 my $cookie_jar = HTTP::Cookies->new;
22
23 # give the agent a place to stash the cookies
24
25 $agent->cookie_jar($cookie_jar);
26
27 no warnings 'once';
28 # get the top page
29 login($agent, $user_obj);
30
31 # Test for absence of Configure and Preferences tabs.
32 ok(!$agent->find_link( url => "$RT::WebPath/Admin/",
33                        text => 'Configuration'), "No config tab" );
34 ok(!$agent->find_link( url => "$RT::WebPath/User/Prefs.html",
35                        text => 'Preferences'), "No prefs pane" );
36
37 # Now test for their presence, one at a time.  Sleep for a bit after
38 # ACL changes, thanks to the 10s ACL cache.
39 my ($grantid,$grantmsg) =$user_obj->PrincipalObj->GrantRight(Right => 'ShowConfigTab', Object => $RT::System);
40
41 ok($grantid,$grantmsg);
42
43 $agent->reload;
44
45 like($agent->{'content'} , qr/Logout/i, "Reloaded page successfully");
46 ok($agent->find_link( url => "$RT::WebPath/Admin/",
47                        text => 'Configuration'), "Found config tab" );
48 my ($revokeid,$revokemsg) =$user_obj->PrincipalObj->RevokeRight(Right => 'ShowConfigTab');
49 ok ($revokeid,$revokemsg);
50 ($grantid,$grantmsg) =$user_obj->PrincipalObj->GrantRight(Right => 'ModifySelf');
51 ok ($grantid,$grantmsg);
52 $agent->reload();
53 like($agent->{'content'} , qr/Logout/i, "Reloaded page successfully");
54 ok($agent->find_link( 
55                        text => 'Preferences'), "Found prefs pane" );
56 ($revokeid,$revokemsg) = $user_obj->PrincipalObj->RevokeRight(Right => 'ModifySelf');
57 ok ($revokeid,$revokemsg);
58 # Good.  Now load the search page and test Load/Save Search.
59 $agent->follow_link( url => "$RT::WebPath/Search/Build.html",
60                      text => 'Tickets');
61 is($agent->{'status'}, 200, "Fetched search builder page");
62 ok($agent->{'content'} !~ /Load saved search/i, "No search loading box");
63 ok($agent->{'content'} !~ /Saved searches/i, "No saved searches box");
64
65 ($grantid,$grantmsg) = $user_obj->PrincipalObj->GrantRight(Right => 'LoadSavedSearch');
66 ok($grantid,$grantmsg);
67 $agent->reload();
68 like($agent->{'content'} , qr/Load saved search/i, "Search loading box exists");
69 ok($agent->{'content'} !~ /input\s+type=['"]submit['"][^>]+name=['"]SavedSearchSave['"]/i, 
70    "Still no saved searches box");
71
72 ($grantid,$grantmsg) =$user_obj->PrincipalObj->GrantRight(Right => 'CreateSavedSearch');
73 ok ($grantid,$grantmsg);
74 $agent->reload();
75 like($agent->{'content'} , qr/Load saved search/i, 
76    "Search loading box still exists");
77 like($agent->{'content'} , qr/input\s+type=['"]submit['"][^>]+name=['"]SavedSearchSave['"]/i, 
78    "Saved searches box exists");
79
80 # Create a group, and a queue, so we can test limited user visibility
81 # via SelectOwner.
82
83 my $queue_obj = RT::Queue->new($RT::SystemUser);
84 ($ret, $msg) = $queue_obj->Create(Name => 'CustomerQueue-'.$$, 
85                                   Description => 'queue for SelectOwner testing');
86 ok($ret, "SelectOwner test queue creation. $msg");
87 my $group_obj = RT::Group->new($RT::SystemUser);
88 ($ret, $msg) = $group_obj->CreateUserDefinedGroup(Name => 'CustomerGroup-'.$$,
89                               Description => 'group for SelectOwner testing');
90 ok($ret, "SelectOwner test group creation. $msg");
91
92 # Add our customer to the customer group, and give it queue rights.
93 ($ret, $msg) = $group_obj->AddMember($user_obj->PrincipalObj->Id());
94 ok($ret, "Added customer to its group. $msg");
95 ($grantid,$grantmsg) =$group_obj->PrincipalObj->GrantRight(Right => 'OwnTicket',
96                                      Object => $queue_obj);
97                                      
98 ok($grantid,$grantmsg);
99 ($grantid,$grantmsg) =$group_obj->PrincipalObj->GrantRight(Right => 'SeeQueue',
100                                      Object => $queue_obj);
101 ok ($grantid,$grantmsg);
102 # Now.  When we look at the search page we should be able to see
103 # ourself in the list of possible owners.
104
105 $agent->reload();
106 ok($agent->form_name('BuildQuery'), "Yep, form is still there");
107 my $input = $agent->current_form->find_input('ValueOfActor');
108 ok(grep(/customer-$$/, $input->value_names()), "Found self in the actor listing");
109
110 sub login {
111     my $agent = shift;
112
113     my $url = "http://localhost:" . RT->Config->Get('WebPort') . RT->Config->Get('WebPath') . "/";
114     $agent->get($url);
115     is( $agent->{'status'}, 200,
116         "Loaded a page - http://localhost" . RT->Config->Get('WebPath') );
117
118     # {{{ test a login
119
120     # follow the link marked "Login"
121
122     ok( $agent->{form}->find_input('user') );
123
124     ok( $agent->{form}->find_input('pass') );
125     like( $agent->{'content'} , qr/username:/i );
126     $agent->field( 'user' => $user_obj->Name );
127     $agent->field( 'pass' => 'customer' );
128
129     # the field isn't named, so we have to click link 0
130     $agent->click(0);
131     is( $agent->{'status'}, 200, "Fetched the page ok" );
132     like( $agent->{'content'} , qr/Logout/i, "Found a logout link" );
133 }
134 1;