Automation/Web.pm

filter: Statementstmt 47.4% 64 / 135 Branchbran 21.4% 9 / 42 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.8% 9 / 11 Podpod 100% 4 / 4 totaltotal 44.0% 89 / 202 CC 26 SCAR 33.8
CC
26
Cov
42%
_login
90.0
change_web_pwd
20.0
lb
14.7
CRAP 29.4
« Automation.pm ↑ Summary Automation/Unix.pm »
1 package App::Validation::Automation::Web;
2
3 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use HTML::Form::ForceValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 =head1 NAME
11
12 App::Validation::Automation::Web - Base Class App::Validation::Automation
13
14 Stores utilities that perform web based validation
15
16 =head1 SYNOPSIS
17
18 App::Validation::Automation::Web browses the web urls stored in config or passed as arguments using WWW::Mechanize Logs into the web urls using the credentials stored in attributes.Handles password expiry and authentication failure along with DNS round robin and Load Balancing Functionality check.
19
20 =head1 ATTRIBUTES
21
22 user_name houses the login name needed to login into the web url.password stores the decrypted password.
23
24 =cut
25
26 7 has 'user_name' => (
27 is => 'rw',
28 isa => 'Str',
29 );
30
31 7 has 'password' => (
32 is => 'rw',
33 isa => 'Str',
34 );
35
36 7 has 'site' => (
37 is => 'rw',
38 isa => 'Str',
39 );
40
41 7 has 'zone' => (
42 is => 'rw',
43 isa => 'Str',
44 );
45
46 7 has 'web_msg' => (
47 is => 'rw',
48 isa => 'Str',
49 clearer => 'clear_web_msg',
50 );
51
52 7 has 'mech_state' => (
53 is => 'rw',
54 isa => 'WWW::Mechanize',
55 );
56
57 =head1 METHODS
58
59 =head2 validate_url
60
61 Browses the weblink passed as parameter using WWW::Mechanize.Tries to log into the weblink if user_name and password are defined.Tweak this method or override to fit your requirement.
62
63 =cut
64
65 1 ▶sub validate_url {
Subroutine validate_url: called CC=4 SCAR=15.5
Pod documented
66 1 my $self = shift;
67 1 my $url = shift;
68 1 my $mech = WWW::Mechanize->new();
69 1 my ( $content, $return, $web_server, $web_server_ip, $web_port,
70 $app_server, $user, $menu_tokens, $auto_tokens, $site,
71 $zone, $slif_flag,$menu_url, $req_string, $ret);
72
73 1 $mech->get($url);
74 1 ▶ $self->web_msg( $mech->status ) if (not ($ret = $mech->success));
Branch: 1 branch
BranchTrueFalseTotal
unless $ret = $mech->success 0 1 1
75
76 1 ▶ if($self->user_name && $self->password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->user_name and $self->password) 0 1 1
Condition33%1/3
$self->user_name and $self->password
!ll&&!rl&&r
100
MC/DC0%0/2
$self->user_name and $self->password
$self->user_name $self->password
Truth table33%1/3
A and B
A= $self->user_nameB= $self->password
ABresult
0X0
100
111
77 0 $self->mech_state($mech);
78 0 $ret = $self->_login;
79 }
80
81 1 return $ret;
82 }
83
84 =head1 METHODS
85
86 =head2 dnsrr
87
88 Tests if DNS Round Robin is working fine or not.Posts the main url max_requests no of times and stores the url redirected.Counts the no of unique redirected urls and reports if they are less than min_unique.
89
90 =cut
91
92 2 ▶sub dnsrr {
Subroutine dnsrr: called CC=5 SCAR=16.5
Pod documented
93 2 my $self = shift;
94 2 my $url = shift;
95 2 my $max_requests = shift;
96 2 my $min_unique = shift;
97 2 my $mech = WWW::Mechanize->new();
98 2 my $msg = "Round Robin details:\n";
99 2 my ( @redirect_uris, $unique );
100
101 2 foreach my $count ( 1..$max_requests ) {
102 2 ▶ $mech->get( $url )
Branch: 1 branch
BranchTrueFalseTotal
$mech->get($url) or confess("Couldn't Fetch $url : " . $mech->error) 2 0 2
103 || confess "Couldn't Fetch $url : ".$mech->error;
104 2 ▶ $self->web_msg( $mech->status ) if ( not $mech->success );
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->success 0 2 2
105 2 $msg .= $url." Redirected to ".$mech->uri."\n";
106 2 push @redirect_uris,$mech->uri;
107 }
108
109 2 $unique = keys %{{ map {$_ => 1} @redirect_uris }};
110 2 ▶ if( $unique < $min_unique ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique < $min_unique) 0 2 2
111 0 $self->web_msg( $msg );
112 0 return 0;
113 }
114
115 2 return 1;
116 }
117
118 =head2 lb
119
120 Tests if Load Balancing is working fine or not.Posts the main url max_requests no of times and logs into the url and scraps server specific info each time and stores in a list.Counts the unique elements in the list to ascertain Load Balancing is in place and transactional load is being divided amongst various servers.
121
122 =cut
123
124 2 ▶sub lb {
Subroutine lb: called CC=8 SCAR=26.9
Pod documented
125 2 my ($self, $url, $max_requests, $min_unique) = @_;
126 2 my $mech = WWW::Mechanize->new();
127 2 my $msg
128 = "Load Balancing details:\nWeb_Server App_Server Alt_App_Server\n";
129 2 my ($unique, @system_info);
130
131 2 foreach my $count ( 1..$max_requests ) {
132 2 my ( $web_server, $ret, $app_server, $alt_app_server);
133
134 2 ▶ $mech->get( $url )
Branch: 1 branch
BranchTrueFalseTotal
$mech->get($url) or confess("Couldn't Fetch $url : " . $mech->error) 2 0 2
135 || confess "Couldn't Fetch $url : ".$mech->error;
136 2 ▶ $self->web_msg( $mech->status ) if ( not $mech->success );
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->success 0 2 2
137
138 2 ▶ if($self->user_name && $self->password) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->user_name and $self->password) 0 2 2
Condition66%2/3
$self->user_name and $self->password
!ll&&!rl&&r
110
MC/DC0%0/2
$self->user_name and $self->password
$self->user_name $self->password
Truth table66%2/3
A and B
A= $self->user_nameB= $self->password
ABresult
0X0
100
111
139 0 $self->mech_state($mech);
140 0 $ret = $self->_login;
141 0 ▶ if( $ret ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) { } 0 0 0
142 0 push @system_info, join '_',
143 ($web_server, $app_server, $alt_app_server)
144 = ( $mech->content
145 =~ /
146 strWebSrvr\=(.+?)\; .+
147 strAppSrvr\=(.+?)\; .+
148 strKSAppSrvr\=(.+?)\; .+
149 /isx
150 );
151 }
152 else {
153 0 $self->web_msg( "Missdirected to".$mech->uri );
154 0 return 0;
155 }
156 }
157 }
158 0 $unique = keys %{{ map { $_ => 1} @system_info }};
159 2 ▶ if( $unique < $min_unique ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique < $min_unique) 0 2 2
160 0 $msg .= join "\n", @system_info;
161 0 $self->web_msg( $msg );
162 0 return 0;
163 }
164 2 return 1;
165 }
166
167 =head2 change_web_pwd
168
169 Change Password at Website level after Password expiration.
170
171 =cut
172
173 0 ▶sub change_web_pwd {
Subroutine change_web_pwd: not called CC=4 SCAR=30.0
Pod documented
174 0 my $self = shift;
175 0 my $url = shift;
176 0 my ($mon, $random_no, $new_pwd, %month_map, $body, $form);
177 0 %month_map = (
178 '1' => 'Jan', '2' => 'Feb', '3' => 'Mar','4' => 'Apr','5' => 'May',
179 '6' => 'Jun', '7' => 'Jul', '8' => 'Aug', '9' => 'Sep','10' => 'Oct',
180 '10' => 'Oct', '11' => 'Nov', '12' => 'Dec',
181 );
182 0 my $mech = $self->mech_state();
183
184 #Compute new password
185 0 ( $mon ) = ( localtime( time ))[4];
186 0 $mon += 1;
187 0 $mon %= 12; #Handles year change
188 0 $random_no = int( rand( $mon )).int( rand( $mon )).int( rand( $mon ));
189 0 $mon = $month_map{($mon + 1)}; #We need to pick the next month
190 0 $new_pwd = $mon.'-'.$random_no;
191
192 #Compare current and new password
193 0 ▶ if($self->password =~ /$new_pwd/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->password =~ /$new_pwd/) 0 0 0
194 0 $self->web_msg("Pwd Change Failed! : old pwd == new pwd!");
195 0 return 0;
196 }
197
198 0 ▶ if(not ($form = $mech->form_with_fields(qw(pw1 pw2)))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($form = $mech->form_with_fields("pw1", "pw2")) 0 0 0
199 0 $self->web_msg("No form with pw1 & pw2");
200 0 return 0;
201 }
202 0 $form->find_input("pw1")->force_value($new_pwd);
203 0 $form->find_input("pw2")->force_value($new_pwd);
204 0 $mech->submit;
205 0 ▶ if(not $mech->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mech->success) 0 0 0
206 0 $self->web_msg(
207 "Pwd Change Failed! : ".$mech->status." ".$mech->error
208 );
209 0 return 0;
210 }
211 0 $self->config->{'COMMON.OLD_PASSWORD'} = $self->password;
212 0 $self->password = $new_pwd;
213
214 0 return 1;
215 }
216
217
218 0 ▶sub _login {
Subroutine _login: not called CC=9 SCAR=45.0
219 0 my $self = shift;
220 0 my $mech = $self->mech_state;
221 0 my ($content, $req_string, $web_server_ip, $web_port, $web_server,
222 $app_server, $site, $zone, $slif_flag, $menu_url, $menu_tokens,
223 $auto_tokens, $user, $form);
224
225 0 local $WARNING = 0;
226 0 ▶ if(not ($form = $mech->form_with_fields(qw(user password)))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($form = $mech->form_with_fields("user", "password")) 0 0 0
227 0 $self->web_msg("No form with user & password");
228 0 return 0;
229 }
230 0 $form->find_input("user")->force_value($self->user_name);
231 0 $form->find_input("password")->force_value($self->password);
232 0 ▶ $form->find_input("shortsite")->force_value($self->site) if($self->site);
Branch: 1 branch
BranchTrueFalseTotal
if $self->site 0 0 0
233 0 ▶ $form->find_input("zone")->force_value($self->zone) if($self->zone);
Branch: 1 branch
BranchTrueFalseTotal
if $self->zone 0 0 0
234 0 $mech->submit;
235 0 ▶ $self->web_msg( $mech->status ) if ( not $mech->success );
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->success 0 0 0
236
237 0 $content = $mech->content;
238 0 ▶ if($content =~ /Password\s+has\s+expired/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($content =~ /Password\s+has\s+expired/i) { } 0 0 0
elsif ($content =~ /Authentication\s+Failure/i) { } 0 0 0
elsif ($content =~ /launchMenu\((.*)\)\;/) { } 0 0 0
239 0 $self->web_msg("Password has Expired!");
240 0 $self->mech_state($mech);
241 0 return 0;
242 }
243 elsif($content =~ /Authentication\s+Failure/i) {
244 0 $self->web_msg("Authentication Failure!");
245 0 return 0;
246 }
247 elsif($content =~ /launchMenu\((.*)\)\;/) {
248 0 (( $req_string = $1 ) =~ s/^\'(.*)\'$/$1/ );
249 0 ($web_server,$web_server_ip,$web_port,$app_server,$user,$menu_tokens,
250 $auto_tokens, $site, $zone, $slif_flag ) = split /','/,$req_string;
251
252 0 $menu_url
253 = "http://$web_server_ip:$web_port/LoginIWS_Servlet/Menu?webserver";
254 0 $menu_url .= "=$web_server&webport=$web_port&appserver=$app_server&user";
255 0 $menu_url .= "$user&menuTokens=$menu_tokens&autoTokens=$auto_tokens&site";
256 0 $menu_url .= "=$site&zone=$zone&SLIflag=$slif_flag&code=x9y8z70D0";
257
258 0 $mech->get( $menu_url );
259 0 ▶ $self->web_msg( $mech->status ) if ( not $mech->success );
Branch: 1 branch
BranchTrueFalseTotal
unless $mech->success 0 0 0
260 0 return 1;
261 }
262 else {
263 0 $self->web_msg( "Missdirected to".$mech->uri );
264 0 return 0;
265 }
266 }
267
268 7 __PACKAGE__->meta->make_immutable;
269
270 7 1;
« Automation.pm ↑ Summary Automation/Unix.pm »