bin/mock_onfido.pl

filter: Statementstmt 94.9% 189 / 199 Branchbran 63.6% 14 / 22 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 28 / 28 Podpod n/a totaltotal 90.0% 236 / 262 CC 21 SCAR 7.9
CC
21
Cov
90%
clone_and_remove_private
5.0
__ANON__
3.2
create_report
3.0
CRAP 2.2
« blib/lib/WebService/Async/Onfido/Base/Address.pm ↑ Summary blib/lib/WebService/Async/Onfido/Check.pm »
1 ## no critic (RequireExplicitPackage RequireEndWithOne)
2 # Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features
3 1 ▶use Mojolicious::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Clone 'clone';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Date::Utility;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # In this script we think the key like '_xxxxx' in hash as private keys. Will not send them
11 1 plugin 'RenderFile';
12  
13 # Route with placeholder
14 1 my $applicant_template;
15 my $check_template;
16  
17 # $applicants{$app_id}
18 1 my %applicants;
19 1 my %deleted_applicants;
20  
21 # $documents{$doc_id}
22 1 my %documents;
23  
24 # $photos{$photo_id}
25 1 my %photos;
26  
27 # $files{$doc_id}
28 # $files{$photo_id}
29 1 my %files;
30 1 my %reports;
31 1 my %checks;
32  
33 ################################################################################
34 # applicants
35 # create applicant
36 post '/v3.4/applicants' => sub {
37 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
38 1 my $data = $c->req->json;
39 1 my $id = Data::UUID->new->create_str();
40 1 my $applicant = {
41 id => $id,
42 created_at => Date::Utility->new()->datetime_iso8601,
43 href => "v3.4/applicants/$id",
44 };
45 1 for my $k (keys %$data) {
46 8 $applicant->{$k} = $data->{$k};
47 }
48 1 $applicants{$id} = $applicant;
49 1 $c->render(json => $applicant);
50 1 };
51  
52 get '/v3.4/applicants' => sub {
53 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.4
54  
55 # TODO page
56 1 return $c->render(json => {applicants => [sort { $b->{created_at} cmp $a->{created_at} } values %applicants]});
57 1 };
58  
59 put '/v3.4/applicants/:id' => sub {
60 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
61 1 my $id = $c->stash('id');
62 1 my $applicant = $applicants{$id};
63 1 my $data = $c->req->json;
64 1 for my $k (keys %$data) {
65 2 $applicant->{$k} = $data->{$k};
66 }
67 1 $c->render(json => $applicant);
68 1 };
69  
70 get '/v3.4/applicants/:id' => sub {
71 2 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
72 2 my $id = $c->stash('id');
73  
74 # There is no description that what result should be if there is no such applicant.
75 # So return 'Not Found' temporarily
76 2 ▶ my $applicant = $applicants{$id} // {status => 'Not Found'};
Condition50%1/2
$applicants{$id} // {"status", "Not Found"}
l!l
20
MC/DC0%0/1
$applicants{$id} // {"status", "Not Found"}
$applicants{$id}
Truth table50%1/2
A
A= $applicants{$id}
Aresult
11
00
77 2 $c->render(json => $applicant);
78 1 };
79  
80 del '/v3.4/applicants/:id' => sub {
81 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
82 1 my $id = $c->stash('id');
83 1 ▶ if (exists $applicants{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $applicants{$id}) 1 0 1
84 1 $deleted_applicants{$id} = delete $applicants{$id};
85 $deleted_applicants{$id}->{delete_at} =
86 1 Date::Utility->new()->datetime_iso8601;
87 }
88 $c->render(
89 1 status => 204,
90 json => {status => 'delete ok'});
91 1 };
92  
93 ################################################################################
94 # documents
95  
96 post '/v3.4/documents' => sub {
97 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
98 1 my $applicant_id = $c->param('applicant_id');
99 1 my $document_id = Data::UUID->new->create_str();
100 1 my $file = $c->param('file');
101 1 my $document = {
102 id => $document_id,
103 created_at => Date::Utility->new()->datetime_iso8601,
104 href => "/v3.4/documents/$document_id",
105 download_href => "/v3.4/documents/$document_id/download",
106 _applicant_id => $applicant_id,
107 file_name => basename($file->filename),
108 file_size => $file->size,
109 file_type => $file->headers->content_type,
110 };
111 1 for my $param (qw(type side issuing_country)) {
112 3 $document->{$param} = $c->param($param);
113 }
114 1 $files{$document_id} = Path::Tiny->tempfile;
115 1 $file->move_to($files{$document_id}->stringify);
116 1 $documents{$document_id} = $document;
117 1 return $c->render(json => clone_and_remove_private($document));
118 1 };
119  
120 get '/v3.4/documents' => sub {
121 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
122 1 my $applicant_id = $c->param('applicant_id');
123  
124 my @documents =
125 0 sort { $b->{created_at} cmp $a->{created_at} }
126 1 map { clone_and_remove_private($_) }
127 1 grep { $_->{_applicant_id} eq $applicant_id } values %documents;
128  
129 1 return $c->render(json => {documents => \@documents});
130 1 };
131  
132 get '/v3.4/documents/:document_id' => sub {
133 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.4
134 1 my $document_id = $c->stash('document_id');
135 1 ▶ unless (exists($documents{$document_id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $documents{$document_id}) 0 1 1
136 0 return $c->render(json => {status => 'Not Found'});
137 }
138 1 return $c->render(json => clone_and_remove_private($documents{$document_id}));
139 1 };
140  
141 get '/v3.4/documents/:document_id/download' => sub {
142 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.4
143 1 my $document_id = $c->stash('document_id');
144 1 ▶ unless (exists($documents{$document_id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $documents{$document_id}) 0 1 1
145 0 return $c->render(json => {status => 'Not Found'});
146 }
147 return $c->render_file(
148 'filepath' => $files{$document_id}->stringify,
149 'filename' => $documents{$document_id}{file_name},
150 'content_type' => $documents{$document_id}{file_type},
151 1 );
152 1 };
153  
154 post '/v3.4/live_photos' => sub {
155 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
156 1 my $applicant_id = $c->param('applicant_id');
157  
158 # don't know how to used yet
159 #my $advanced_validation = $c->param('advanced_validation');
160 1 my $photo_id = Data::UUID->new->create_str();
161 1 my $photo = {
162 id => $photo_id,
163 created_at => Date::Utility->new()->datetime_iso8601,
164 href => "/v3.4/live_photos/$photo_id",
165 download_href => "/v3.4/live_photos/$photo_id/download",
166 };
167 1 my $file = $c->param('file');
168 1 $photo->{file_name} = basename($file->filename);
169 1 $photo->{file_size} = $file->size;
170 1 $photo->{file_type} = $file->headers->content_type;
171 1 $files{$photo_id} = Path::Tiny->tempfile;
172 1 $file->move_to($files{$photo_id}->stringify);
173 1 $photo->{_applicant_id} = $applicant_id;
174 1 $photos{$photo_id} = $photo;
175 1 return $c->render(json => clone_and_remove_private($photo));
176 1 };
177  
178 get '/v3.4/live_photos' => sub {
179 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
180 1 my $applicant_id = $c->param('applicant_id');
181 my @photos =
182 0 sort { $b->{created_at} cmp $a->{created_at} }
183 1 map { clone_and_remove_private($_) }
184 1 grep { $_->{_applicant_id} eq $applicant_id } values %photos;
185 1 return $c->render(json => {live_photos => \@photos});
186 1 };
187  
188 get '/v3.4/live_photos/:photo_id' => sub {
189 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
190 1 my $photo_id = $c->stash('photo_id');
191 1 my $photo = clone_and_remove_private($photos{$photo_id});
192 1 ▶ return $c->render(json => {status => 'Not Found'}) unless $photo;
Branch: 1 branch
BranchTrueFalseTotal
unless $photo 0 1 1
193 1 return $c->render(json => $photo);
194 1 };
195  
196 get '/v3.4/live_photos/:photo_id/download' => sub {
197 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.4
198 1 my $photo_id = $c->stash('photo_id');
199 1 ▶ unless (exists($photos{$photo_id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $photos{$photo_id}) 0 1 1
200 0 return $c->render(json => {status => 'Not Found'});
201 }
202 return $c->render_file(
203 'filepath' => $files{$photo_id}->stringify,
204 'filename' => $photos{$photo_id}{file_name},
205 'content_type' => $photos{$photo_id}{file_type},
206 1 );
207  
208 1 };
209  
210 sub create_report {
211 1 ▶ my ($c, $check_id, $applicant_id) = @_;
Subroutine create_report: called CC=3 SCAR=11.0
212 1 my $params = $c->req->json;
213 1 my @reports;
214 my @document_ids =
215 1 map { $_->{id} }
216 1 grep { $_->{_applicant_id} eq $applicant_id } values %documents;
217 1 for my $req ($params->{report_names}->@*) {
218 2 my $report_id = Data::UUID->new->create_str();
219 my $report = {
220 id => $report_id,
221 _check_id => $check_id,
222 created_at => Date::Utility->new()->datetime_iso8601,
223 name => $req,
224 status => 'complete',
225 result => 'clear',
226 breakdown => {},
227 properties => {document_type => 'passport'},
228 $req eq 'document'
229 2 ▶ ? (documents => [map { {id => $_} } @document_ids])
Branch: 1 branch
BranchTrueFalseTotal
$req eq 'document' ? : 1 1 2
230 : (),
231 };
232 2 $reports{$report_id} = $report;
233 2 push @reports, $report_id;
234 }
235 1 return [map { clone_and_remove_private($_) } @reports];
236 }
237  
238 post '/v3.4/checks' => sub {
239 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
240 1 my $data = $c->req->json;
241  
242 1 my $applicant_id = $data->{applicant_id};
243 1 my $check_id = Data::UUID->new->create_str();
244 my $check = {
245 id => $check_id,
246 created_at => Date::Utility->new()->datetime_iso8601,
247 href => "/v3.4/checks/$check_id",
248 status => 'in_progress',
249 result => 'clear',
250 redirect_uri => 'https://somewhere.else',
251 results_uri => "https://onfido.com/dashboard/information_requests/<REQUEST_ID>",
252 reports_ids => create_report($c, $check_id, $applicant_id),
253 tags => $data->{tags},
254 1 applicant_id => $applicant_id,
255 };
256 1 $checks{$check_id} = $check;
257  
258 1 return $c->render(json => clone_and_remove_private($check));
259 1 };
260  
261 get '/v3.4/checks/:check_id' => sub {
262 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.2
263 1 my $check_id = $c->stash('check_id');
264  
265 1 ▶ unless (exists($checks{$check_id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $checks{$check_id}) 0 1 1
266 0 return $c->render(json => {status => 'Not Found'});
267 }
268 1 $checks{$check_id}{status} = 'complete';
269 1 return $c->render(json => clone_and_remove_private($checks{$check_id}));
270 1 };
271  
272 get '/v3.4/checks' => sub {
273 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
274 1 my $applicant_id = $c->param('applicant_id');
275  
276 my @checks =
277 0 sort { $b->{created_at} cmp $a->{created_at} }
278 1 map { clone_and_remove_private($_) }
279 1 grep { $_->{applicant_id} eq $applicant_id } values %checks;
280 1 return $c->render(json => {checks => \@checks});
281 1 };
282  
283 get '/v3.4/reports' => sub {
284 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
285 1 my $check_id = $c->param('check_id');
286 my @reports =
287 1 sort { $b->{created_at} cmp $a->{created_at} }
288 2 map { clone_and_remove_private($_) }
289 1 grep { $_->{_check_id} eq $check_id } values %reports;
290  
291 1 return $c->render(json => {reports => \@reports});
292 1 };
293  
294 get '/v3.4/reports/:report_id' => sub {
295 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=2 SCAR=7.4
296 1 my $report_id = $c->stash('report_id');
297 1 ▶ unless (exists($reports{$report_id})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $reports{$report_id}) 0 1 1
298 0 return $c->render(json => {status => 'Not Found'});
299 }
300 1 return $c->render(json => clone_and_remove_private($reports{$report_id}));
301 1 };
302  
303 1 my @sdk_tokens;
304 post '/v3.4/sdk_token' => sub {
305 1 ▶ my $c = shift;
Subroutine __ANON__: called CC=3 SCAR=11.7
306 1 my $data = $c->req->json;
307 1 my $applicant_id = $data->{applicant_id};
308 1 my $referrer = $data->{referrer};
309 1 ▶ unless (exists($applicants{$applicant_id}) && $referrer) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $applicants{$applicant_id} and $referrer) 0 1 1
Condition33%1/3
exists $applicants{$applicant_id} and $referrer
!ll&&!rl&&r
001
MC/DC0%0/2
exists $applicants{$applicant_id} and $referrer
exists $applicants{$applicant_id} $referrer
Truth table33%1/3
A and B
A= exists $applicants{$applicant_id}B= $referrer
ABresult
0X0
100
111
310 0 return $c->render(json => {status => 'Not Found'});
311 }
312 1 my $sdk_token = {
313 token => Data::UUID->new->create_str(),
314 applicant_id => $applicant_id,
315 referrer => $referrer,
316 };
317 1 push @sdk_tokens, $sdk_token;
318 1 return $c->render(json => $sdk_token);
319 1 };
320  
321 sub clone_and_remove_private {
322 14 ▶ my $result = shift;
Subroutine clone_and_remove_private: called CC=5 SCAR=16.1
323  
324 14 ▶ return $result unless $result && ref($result) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $result and ref $result eq "HASH" 2 12 14
Condition66%2/3
$result and ref $result eq "HASH"
!ll&&!rl&&r
0212
MC/DC50%1/2
$result and ref $result eq "HASH"
$result ref $result eq "HASH"
Truth table66%2/3
A and B
A= $resultB= ref $result eq "HASH"
ABresult
0X0
100
111
325 12 $result = clone($result);
326 12 for my $k (keys %$result) {
327 113 ▶ if ($k =~ /^_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^_/u) 9 104 113
328 9 delete $result->{$k};
329 }
330 }
331  
332 12 return $result;
333 }
334  
335 # Start the Mojolicious command system
336 1 app->start('daemon', '-l', "http://*:3000");
337  
338 sub END {
339 1 ▶ for my $f (values %files) {
Subroutine END: called CC=2 SCAR=6.9
340 2 print "removing $f\n";
341 2 $f->remove;
342 }
343 }
« blib/lib/WebService/Async/Onfido/Base/Address.pm ↑ Summary blib/lib/WebService/Async/Onfido/Check.pm »