v4/WWWClient.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 102 CC 7 SCAR 24.6
CC
7
Cov
0%
sign_in
30.0
_croak_res
6.0
get
6.0
CRAP 11.8
1 package GitLab::API::v4::WWWClient;
2 our $VERSION = '0.27';
3  
4 =encoding utf8
5  
6 =head1 NAME
7  
8 GitLab::API::v4::WWWClient - A client that works against the GitLab web site.
9  
10 =head1 SYNOPSIS
11  
12 use GitLab::API::v4::WWWClient;
13
14 my $client = GitLab::API::v4::WWWClient->new(
15 url => 'https://git.example.com/',
16 );
17
18 $client->sign_in( $username, $password );
19
20 my $res = $client->get( $path );
21  
22 =head1 DESCRIPTION
23  
24 This class makes it possible to interact with the GitLab web site.
25  
26 =cut
27  
28 use Carp qw( croak );
29 use HTTP::Tiny;
30 use List::Util qw( first );
31 use Types::Common::String qw( NonEmptySimpleStr );
32  
33 use Moo;
34 use strictures 2;
35 use namespace::clean;
36  
37 has _session => (
38 is => 'rw',
39 init_arg => undef,
40 );
41  
42 sub _croak_res {
43 my ($verb, $url, $res) = @_;
44
45 return if $res->{status} !~ m{^5};
46  
47 local $Carp::Internal{ 'GitLab::API::v4::WWWClient' } = 1;
48  
49 croak sprintf(
50 'Error %sing %s (HTTP %s): %s',
51 uc($verb), $url,
52 $res->{status}, ($res->{reason} || 'Unknown'),
53 );
54 }
55  
56 =head1 REQUIRED ARGUMENTS
57  
58 =head2 url
59  
60 This is the base URL to your GitLab web site.
61  
62 =cut
63  
64 has url => (
65 is => 'ro',
66 isa => NonEmptySimpleStr,
67 required => 1,
68 );
69  
70 =head1 METHODS
71  
72 =head2 sign_in
73  
74 $client->sign_in( $username, $password );
75  
76 Signs in the client given the username and password.
77  
78 =cut
79  
80 sub sign_in {
81 my ($self, $username, $password) = @_;
82  
83 my $tiny = HTTP::Tiny->new(
84 verify_SSL => 1,
85 max_redirect => 0,
86 );
87  
88 my $base_url = $self->url();
89 $base_url =~ s{/$}{};
90 my $sign_in_url = "$base_url/users/sign_in";
91  
92 my $load_res = $tiny->get( $sign_in_url );
93  
94 _croak_res( 'get', $sign_in_url, $load_res );
95  
96 my $token = (
97 $load_res->{content} =~
98 m{name="authenticity_token" value="(.+?)"}
99 )[0];
100  
101
102 my ($first_session) = do{
103 my $set_cookie_headers = $load_res->{headers}->{ 'set-cookie' };
104 $set_cookie_headers = [ $set_cookie_headers ] if !ref $set_cookie_headers;
105 my $value = first { $_ =~ m{^_gitlab_session=(.*)} } @$set_cookie_headers;
106 $value =~ s{^_gitlab_session=}{}r;
107 };
108  
109 my $submit_res = $tiny->post_form(
110 $sign_in_url,
111 {
112 'utf8' => '✓',
113 'authenticity_token' => $token,
114 'user[login]' => $username,
115 'user[password]' => $password,
116 'user[remember_me]' => 0,
117 },
118 {
119 headers => {
120 'Referer' => $sign_in_url,
121 'Cookie' => "_gitlab_session=$first_session",
122 'Cookie2' => '$Version="1"',
123 },
124 },
125 );
126  
127 _croak_res( 'post', $sign_in_url, $submit_res );
128  
129 my ($second_session) = do{
130 my $set_cookie_headers = $submit_res->{headers}->{ 'set-cookie' };
131 $set_cookie_headers = [ $set_cookie_headers ] if !ref $set_cookie_headers;
132 my $value = first { $_ =~ m{^_gitlab_session=(.*)} } @$set_cookie_headers;
133 $value =~ s{^_gitlab_session=}{}r;
134 };
135  
136 my $home_res = $tiny->get(
137 $base_url,
138 {
139 headers => {
140 'Referer' => $sign_in_url,
141 'Cookie' => "_gitlab_session=$second_session",
142 'Cookie2' => '$Version="1"',
143 },
144 },
145 );
146  
147 _croak_res( 'get', $base_url, $home_res );
148  
149 my $ok = ( $home_res->{content} =~ m{sign-out-link} ) ? 1 : 0;
150 croak 'Failed to sign in' if !$ok;
151  
152 $self->_session( $second_session );
153  
154 return;
155 }
156  
157 =head2 get
158  
159 my $res = $client->get( $path );
160  
161 Gets the path and returns the L<HTTP::Tiny> response hash.
162  
163 =cut
164  
165 sub get {
166 my ($self, $path) = @_;
167  
168 my $tiny = HTTP::Tiny->new(
169 verify_SSL => 1,
170 max_redirect => 0,
171 );
172  
173 my $base_url = $self->url();
174 $base_url =~ s{/$}{};
175 $path =~ s{^/}{};
176 my $url = "$base_url/$path";
177  
178 my $session = $self->_session();
179 my $headers = $session ? {
180 'Cookie' => "_gitlab_session=$session",
181 'Cookie2' => '$Version="1"',
182 } : {};
183  
184 my $res = $tiny->get(
185 $url,
186 { headers=>$headers },
187 );
188  
189 _croak_res( 'get', $url, $res );
190  
191 return $res;
192 }
193  
194 1;
195 __END__