blib/lib/GitLab/API/v4/WWWClient.pm untested

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