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