AUR/Login.pm

filter: Statementstmt 35.2% 42 / 119 Branchbran 4.7% 2 / 42 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 45.0% 9 / 20 Podpod 50.0% 2 / 4 totaltotal 27.7% 55 / 198 SCAR 55.5
CC
24
Cov
26%
new
42.0
_do_pkg_action
30.0
comment
30.0
CRAP 257.1
« AUR/Package/File.pm ↑ Summary AUR/Package.pm »
1 package WWW::AUR::Login;
2  
3 1 ▶use warnings 'FATAL' => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use HTTP::Cookies qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use WWW::AUR::Maintainer qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use WWW::AUR::URI qw( pkg_uri pkgsubmit_uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use WWW::AUR qw( _category_index _useragent );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our @ISA = qw(WWW::AUR::Maintainer);
14  
15 1 my $COOKIE_NAME = 'AURSID';
16 1 my $BAD_LOGIN_MSG = 'Bad username or password.';
17 1 my $PKG_EXISTS_MSG = ( 'You are not allowed to overwrite the '
18 . '<b>.*?</b> package.' );
19 1 my $PKG_EXISTS_ERR = 'You tried to submit a package you do not own';
20 1 my $COMMADD_MSG = quotemeta '<b>Comment has been added.</b>';
21  
22 1 my $PKGOUTPUT_MATCH = qr{ <p [ ] class="pkgoutput"> ( [^<]+ ) </p> }xms;
23  
24 sub _new_cookie_jar
25 {
26 0 ▶ my $jar = HTTP::Cookies->new();
Subroutine _new_cookie_jar: not called CC=2 SCAR=17.9
27  
28 0 my ($domain, $port) = split /:/, $WWW::AUR::HOST;
29 0 ▶ $port ||= 443; # we use https for logins
Condition0%0/2
$port ||= 443
l!l
00
MC/DC0%0/1
$port ||= 443
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
30  
31 # This REALLY should take a hash as argument...
32 0 $jar->set_cookie( 0, 'AURLANG' => 'en', # version, key, val
33 '/', $domain, $port, # path, domain, port
34 0, 0, # path_spec, secure
35 0, 0, # maxage, discard
36 {} ); # rest
37  
38 0 return $jar;
39 }
40  
41 sub new
42 {
43 0 ▶ my $class = shift;
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
44  
45 0 ▶ Carp::croak 'You must supply a name and password as argument'
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 2 0 0 0
46 unless @_ >= 2;
47 0 my ($name, $password) = @_;
48  
49 0 my $ua = _useragent( 'cookie_jar' => _new_cookie_jar());
50 0 $ua->InitTLS;
51 0 my $resp = $ua->post( "https://$WWW::AUR::HOST/login",
52 [ user => $name, passwd => $password ] );
53  
54 0 ▶ Carp::croak 'Failed to login to AUR: bad username or password'
Branch: 1 branch
BranchTrueFalseTotal
if $resp->content =~ /$BAD_LOGIN_MSG/ 0 0 0
55 if $resp->content =~ /$BAD_LOGIN_MSG/;
56  
57 0 ▶ unless ( $resp->code == 302 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->code == 302) 0 0 0
58 0 ▶ Carp::croak 'Failed to login to AUR: ' . $resp->status_line
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
59 unless $resp->is_success;
60 }
61  
62 0 my $self = $class->SUPER::new( $name );
63 0 $self->{'useragent'} = $ua;
64 0 ▶ $self->{'sid'} = _sidcookie($ua)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sid"} = _sidcookie($ua) or Carp::croak("Failed to read session cookie from login") 0 0 0
65 or Carp::croak 'Failed to read session cookie from login';
66  
67 0 return $self;
68 }
69  
70 sub _sidcookie
71 {
72 0 ▶ my ($ua) = @_;
Subroutine _sidcookie: not called CC=1 SCAR=6.9
73 0 my $jar = $ua->cookie_jar;
74 0 my $sid;
75 0 ▶ $jar->scan(sub { $sid = $_[2] if($_[1] eq 'AURSID') });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq "AURSID" 0 0 0
76 0 return $sid;
77 }
78  
79 1 my %_PKG_ACTIONS = map { ( lc $_ => "do_$_" ) }
80 qw{ Adopt Disown Vote UnVote Notify UnNotify Flag UnFlag Delete };
81  
82 sub _do_pkg_action
83 {
84 0 ▶ my ($self, $act, $pkg, @params) = @_;
Subroutine _do_pkg_action: not called CC=5 SCAR=34.0
85  
86 0 ▶ Carp::croak 'Please provide a proper package ID/name/obj argument'
Branch: 1 branch
BranchTrueFalseTotal
unless $pkg 0 0 0
87 unless $pkg;
88  
89 0 ▶ my $action = $_PKG_ACTIONS{ $act }
Branch: 1 branch
BranchTrueFalseTotal
my $action = $_PKG_ACTIONS{$act} or Carp::croak("$act is not a valid action for a package") 0 0 0
90 or Carp::croak "$act is not a valid action for a package";
91  
92 0 my $id = _pkgid( $pkg );
93 0 my $ua = $self->{'useragent'};
94 0 my $uri = pkg_uri( 'https' => 1, 'ID' => $id );
95 my $resp = $ua->post( $uri, [ "IDs[$id]" => 1,
96 'ID' => $id,
97 0 'token' => $self->{'sid'},
98 $action => 1,
99 @params ] );
100  
101 0 ▶ Carp::croak 'Failed to send package action: ' . $resp->status_line
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
102 unless $resp->is_success;
103  
104 0 my ($pkgoutput) = $resp->content =~ /$PKGOUTPUT_MATCH/;
105 0 ▶ Carp::confess 'Failed to parse package action response'
Branch: 1 branch
BranchTrueFalseTotal
unless $pkgoutput 0 0 0
106 unless $pkgoutput;
107  
108 0 return $pkgoutput;
109 }
110  
111 #---HELPER FUNCTION---
112 sub _pkgid
113 {
114 0 ▶ my $pkg = shift;
Subroutine _pkgid: not called CC=4 SCAR=30.0
115  
116 0 ▶ if ( ! ref $pkg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $pkg) 0 0 0
117 0 ▶ return $pkg if $pkg =~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $pkg =~ /\A\d+\z/ 0 0 0
118  
119 0 require WWW::AUR::Package;
120 0 my $pkgobj = WWW::AUR::Package->new( $pkg );
121 0 return $pkgobj->id;
122 }
123  
124 Carp::croak 'You must provide a package name, id, or object'
125 0 ▶ unless eval { $pkg->isa( 'WWW::AUR::Package' ) };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $pkg->isa("WWW::AUR::Package") } } 0 0 0
126  
127 0 return $pkg->id;
128 }
129  
130 #---HELPER FUNCTION---
131 # If provided pkg is an object, call its name method, otherwise pass through.
132 sub _pkgdesc
133 {
134 0 ▶ my ($pkg) = @_;
Subroutine _pkgdesc: not called CC=2 SCAR=17.9
135 0 my $name;
136 0 ▶ return $name if $name = eval { $pkg->name };
Branch: 1 branch
BranchTrueFalseTotal
if $name = eval { do { $pkg->name } } 0 0 0
137 0 return $pkg;
138 }
139  
140 sub _def_action_method
141 {
142 8 ▶ my ($name, $goodmsg) = @_;
Subroutine _def_action_method: called CC=1 SCAR=1.2
143
144 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
145 8 *{ $name } = sub {
146 0 ▶ my ($self, $pkg) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
147  
148 0 my $txt = $self->_do_pkg_action( $name => $pkg );
149 0 ▶ unless ( $txt =~ /\A$goodmsg/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($txt =~ /\A$goodmsg/) 0 0 0
150 0 Carp::confess sprintf qq{%s action on "%s" failed:\n%s\n},
151 ucfirst $name, _pkgdesc( $pkg ), $txt;
152 }
153 0 return $txt;
154 8 };
155  
156 8 return;
157 }
158  
159 1 my %_ACTIONS = ( 'adopt' => 'The selected packages have been adopted.',
160 'disown' => 'The selected packages have been disowned.',
161  
162 'vote' => ( 'Your votes have been cast for the selected '
163 . 'packages.' ),
164 'unvote' => ( 'Your votes have been removed from the '
165 . 'selected packages.' ),
166  
167 'notify' => ( 'You have been added to the comment '
168 . 'notification list for' ),
169 'unnotify' => ( 'You have been removed from the comment '
170 . 'notification list for' ),
171  
172 'flag' => ( 'The selected packages have been flagged '
173 . 'out-of-date.' ),
174 'unflag' => 'The selected packages have been unflagged.',
175 );
176  
177 1 ▶while ( my ($name, $goodmsg) = each %_ACTIONS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %_ACTIONS) 8 1 9
178 8 _def_action_method( $name, $goodmsg );
179 }
180  
181 sub delete
182 {
183 0 ▶ my ($self, $pkg) = @_;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
184  
185 0 my $txt = $self->_do_pkg_action( 'delete' => $pkg,
186 'confirm_Delete' => 1 );
187  
188 0 ▶ unless ( $txt =~ /\AThe selected packages have been deleted[.]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($txt =~ /\AThe selected packages have been deleted[.]/) 0 0 0
189 0 my $msg = sprintf q{Failed to perform the delete action on }
190 . q{package "%s"}, _pkgdesc( $pkg );
191 0 Carp::croak $msg;
192 }
193  
194 0 return $txt;
195  
196 }
197  
198 sub upload
199 {
200 0 ▶ my ($self, $path, $catname) = @_;
Subroutine upload: not called CC=3 SCAR=24.8
Pod documented
201 0 ▶ unless ( -f $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $path) 0 0 0
202 0 Carp::croak "Given file path ($path) does not exist";
203 }
204  
205 0 my $catidx = _category_index( $catname );
206 my $form = [
207 'category' => $catidx,
208 'submit' => 'Upload',
209 0 'token' => $self->{'sid'},
210 'pkgsubmit' => 1,
211  
212 # The AUR does not use the provided filename or mimetype.
213 # Specify dummy values to prevent LWP from detecting them.
214 'pfile' => [ $path, 'ignored-filename', 'ignored-mimetype' ],
215 ];
216 0 my $resp = $self->{'useragent'}->post(
217 pkgsubmit_uri(),
218 'Content-Type' => 'form-data',
219 'Content' => $form
220 );
221  
222 0 ▶ Carp::croak $PKG_EXISTS_ERR if $resp->content =~ /$PKG_EXISTS_MSG/;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->content =~ /$PKG_EXISTS_MSG/ 0 0 0
223 0 return;
224 }
225  
226 sub comment
227 {
228 0 ▶ my ($self, $pkg, $com) = @_;
Subroutine comment: not called CC=5 SCAR=34.0
Pod undocumented
229  
230 0 ▶ Carp::croak 'comment text cannot be empty' unless
Branch: 1 branch
BranchTrueFalseTotal
unless defined $com and length $com 0 0 0
Condition0%0/3
defined $com and length $com
!ll&&!rl&&r
000
MC/DC0%0/2
defined $com and length $com
defined $com length $com
Truth table0%0/3
A and B
A= defined $comB= length $com
ABresult
0X0
100
111
231 ( defined $com && length $com );
232  
233 0 my $id = _pkgid($pkg);
234 0 my $ua = $self->{'useragent'};
235 0 my $uri = pkg_uri('https' => 1, 'ID' => $id); # GET & POST params... meh
236 my $prms = [ 'ID' => $id, 'comment' => $com, 'submit' => 'Submit',
237 0 'token' => $self->{'sid'}, ];
238 0 my $resp = $ua->post($uri, $prms);
239  
240 0 ▶ Carp::croak "failed to post comment to package #$id"
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success and $resp->content =~ /$COMMADD_MSG/ 0 0 0
Condition0%0/3
$resp->is_success and $resp->content =~ /$COMMADD_MSG/
!ll&&!rl&&r
000
MC/DC0%0/2
$resp->is_success and $resp->content =~ /$COMMADD_MSG/
$resp->is_success $resp->content =~ /$COMMADD_MSG/
Truth table0%0/3
A and B
A= $resp->is_successB= $resp->content =~ /$COMMADD_MSG/
ABresult
0X0
100
111
241 unless $resp->is_success && $resp->content =~ /$COMMADD_MSG/;
242  
243 0 return;
244 }
245  
246 # Create a nifty alias, to match the "My Packages" AUR link...
247 1 *my_packages = \&WWW::AUR::Maintainer::packages;
248  
249 1 1;
250  
« AUR/Package/File.pm ↑ Summary AUR/Package.pm »