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