blib/script/mock_onfido.pl untested

filter: Statementstmt 0.0% 0 / 203 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 245 CC 4 SCAR 16.4
CC
4
Cov
0%
clone_and_remove_private
12.0
create_report
6.0
END
2.0
CRAP 5.2
1 ## no critic (RequireExplicitPackage RequireEndWithOne)
2 # Automatically enables "strict", "warnings", "utf8" and Perl 5.10 features
3 use Mojolicious::Lite;
4 use Clone 'clone';
5 use Date::Utility;
6 use Data::UUID;
7 use File::Basename;
8 use Path::Tiny;
9  
10 # In this script we think the key like '_xxxxx' in hash as private keys. Will not send them
11 plugin 'RenderFile';
12  
13 # Route with placeholder
14 my $applicant_template;
15 my $check_template;
16  
17 # $applicants{$app_id}
18 my %applicants;
19 my %deleted_applicants;
20  
21 # $documents{$doc_id}
22 my %documents;
23  
24 # $photos{$photo_id}
25 my %photos;
26  
27 # $files{$doc_id}
28 # $files{$photo_id}
29 my %files;
30 my %reports;
31 my %checks;
32  
33 ################################################################################
34 # applicants
35 # create applicant
36 post '/v3.4/applicants' => sub {
37 my $c = shift;
38 my $data = $c->req->json;
39 my $id = Data::UUID->new->create_str();
40 my $applicant = {
41 id => $id,
42 created_at => Date::Utility->new()->datetime_iso8601,
43 href => "v3.4/applicants/$id",
44 };
45 for my $k (keys %$data) {
46 $applicant->{$k} = $data->{$k};
47 }
48 $applicants{$id} = $applicant;
49 $c->render(json => $applicant);
50 };
51  
52 get '/v3.4/applicants' => sub {
53 my $c = shift;
54  
55 # TODO page
56 return $c->render(json => {applicants => [sort { $b->{created_at} cmp $a->{created_at} } values %applicants]});
57 };
58  
59 put '/v3.4/applicants/:id' => sub {
60 my $c = shift;
61 my $id = $c->stash('id');
62 my $applicant = $applicants{$id};
63 my $data = $c->req->json;
64 for my $k (keys %$data) {
65 $applicant->{$k} = $data->{$k};
66 }
67 $c->render(json => $applicant);
68 };
69  
70 get '/v3.4/applicants/:id' => sub {
71 my $c = shift;
72 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 my $applicant = $applicants{$id} // {status => 'Not Found'};
77 $c->render(json => $applicant);
78 };
79  
80 del '/v3.4/applicants/:id' => sub {
81 my $c = shift;
82 my $id = $c->stash('id');
83 if (exists $applicants{$id}) {
84 $deleted_applicants{$id} = delete $applicants{$id};
85 $deleted_applicants{$id}->{delete_at} =
86 Date::Utility->new()->datetime_iso8601;
87 }
88 $c->render(
89 status => 204,
90 json => {status => 'delete ok'});
91 };
92  
93 ################################################################################
94 # documents
95  
96 post '/v3.4/documents' => sub {
97 my $c = shift;
98 my $applicant_id = $c->param('applicant_id');
99 my $document_id = Data::UUID->new->create_str();
100 my $file = $c->param('file');
101 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 for my $param (qw(type side issuing_country)) {
112 $document->{$param} = $c->param($param);
113 }
114 $files{$document_id} = Path::Tiny->tempfile;
115 $file->move_to($files{$document_id}->stringify);
116 $documents{$document_id} = $document;
117 return $c->render(json => clone_and_remove_private($document));
118 };
119  
120 get '/v3.4/documents' => sub {
121 my $c = shift;
122 my $applicant_id = $c->param('applicant_id');
123  
124 my @documents =
125 sort { $b->{created_at} cmp $a->{created_at} }
126 map { clone_and_remove_private($_) }
127 grep { $_->{_applicant_id} eq $applicant_id } values %documents;
128  
129 return $c->render(json => {documents => \@documents});
130 };
131  
132 get '/v3.4/documents/:document_id' => sub {
133 my $c = shift;
134 my $document_id = $c->stash('document_id');
135 unless (exists($documents{$document_id})) {
136 return $c->render(json => {status => 'Not Found'});
137 }
138 return $c->render(json => clone_and_remove_private($documents{$document_id}));
139 };
140  
141 get '/v3.4/documents/:document_id/download' => sub {
142 my $c = shift;
143 my $document_id = $c->stash('document_id');
144 unless (exists($documents{$document_id})) {
145 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 );
152 };
153  
154 post '/v3.4/live_photos' => sub {
155 my $c = shift;
156 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 my $photo_id = Data::UUID->new->create_str();
161 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 my $file = $c->param('file');
168 $photo->{file_name} = basename($file->filename);
169 $photo->{file_size} = $file->size;
170 $photo->{file_type} = $file->headers->content_type;
171 $files{$photo_id} = Path::Tiny->tempfile;
172 $file->move_to($files{$photo_id}->stringify);
173 $photo->{_applicant_id} = $applicant_id;
174 $photos{$photo_id} = $photo;
175 return $c->render(json => clone_and_remove_private($photo));
176 };
177  
178 get '/v3.4/live_photos' => sub {
179 my $c = shift;
180 my $applicant_id = $c->param('applicant_id');
181 my @photos =
182 sort { $b->{created_at} cmp $a->{created_at} }
183 map { clone_and_remove_private($_) }
184 grep { $_->{_applicant_id} eq $applicant_id } values %photos;
185 return $c->render(json => {live_photos => \@photos});
186 };
187  
188 get '/v3.4/live_photos/:photo_id' => sub {
189 my $c = shift;
190 my $photo_id = $c->stash('photo_id');
191 my $photo = clone_and_remove_private($photos{$photo_id});
192 return $c->render(json => {status => 'Not Found'}) unless $photo;
193 return $c->render(json => $photo);
194 };
195  
196 get '/v3.4/live_photos/:photo_id/download' => sub {
197 my $c = shift;
198 my $photo_id = $c->stash('photo_id');
199 unless (exists($photos{$photo_id})) {
200 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 );
207  
208 };
209  
210 sub create_report {
211 my ($c, $check_id, $applicant_id) = @_;
212 my $params = $c->req->json;
213 my @reports;
214 my @document_ids =
215 map { $_->{id} }
216 grep { $_->{_applicant_id} eq $applicant_id } values %documents;
217 for my $req ($params->{report_names}->@*) {
218 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 ? (documents => [map { {id => $_} } @document_ids])
230 : (),
231 };
232 $reports{$report_id} = $report;
233 push @reports, $report_id;
234 }
235 return [map { clone_and_remove_private($_) } @reports];
236 }
237  
238 post '/v3.4/checks' => sub {
239 my $c = shift;
240 my $data = $c->req->json;
241  
242 my $applicant_id = $data->{applicant_id};
243 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 applicant_id => $applicant_id,
255 };
256 $checks{$check_id} = $check;
257  
258 return $c->render(json => clone_and_remove_private($check));
259 };
260  
261 get '/v3.4/checks/:check_id' => sub {
262 my $c = shift;
263 my $check_id = $c->stash('check_id');
264  
265 unless (exists($checks{$check_id})) {
266 return $c->render(json => {status => 'Not Found'});
267 }
268 $checks{$check_id}{status} = 'complete';
269 return $c->render(json => clone_and_remove_private($checks{$check_id}));
270 };
271  
272 get '/v3.4/checks' => sub {
273 my $c = shift;
274 my $applicant_id = $c->param('applicant_id');
275  
276 my @checks =
277 sort { $b->{created_at} cmp $a->{created_at} }
278 map { clone_and_remove_private($_) }
279 grep { $_->{applicant_id} eq $applicant_id } values %checks;
280 return $c->render(json => {checks => \@checks});
281 };
282  
283 get '/v3.4/reports' => sub {
284 my $c = shift;
285 my $check_id = $c->param('check_id');
286 my @reports =
287 sort { $b->{created_at} cmp $a->{created_at} }
288 map { clone_and_remove_private($_) }
289 grep { $_->{_check_id} eq $check_id } values %reports;
290  
291 return $c->render(json => {reports => \@reports});
292 };
293  
294 get '/v3.4/reports/:report_id' => sub {
295 my $c = shift;
296 my $report_id = $c->stash('report_id');
297 unless (exists($reports{$report_id})) {
298 return $c->render(json => {status => 'Not Found'});
299 }
300 return $c->render(json => clone_and_remove_private($reports{$report_id}));
301 };
302  
303 my @sdk_tokens;
304 post '/v3.4/sdk_token' => sub {
305 my $c = shift;
306 my $data = $c->req->json;
307 my $applicant_id = $data->{applicant_id};
308 my $referrer = $data->{referrer};
309 unless (exists($applicants{$applicant_id}) && $referrer) {
310 return $c->render(json => {status => 'Not Found'});
311 }
312 my $sdk_token = {
313 token => Data::UUID->new->create_str(),
314 applicant_id => $applicant_id,
315 referrer => $referrer,
316 };
317 push @sdk_tokens, $sdk_token;
318 return $c->render(json => $sdk_token);
319 };
320  
321 sub clone_and_remove_private {
322 my $result = shift;
323  
324 return $result unless $result && ref($result) eq 'HASH';
325 $result = clone($result);
326 for my $k (keys %$result) {
327 if ($k =~ /^_/) {
328 delete $result->{$k};
329 }
330 }
331  
332 return $result;
333 }
334  
335 # Start the Mojolicious command system
336 app->start('daemon', '-l', "http://*:3000");
337  
338 sub END {
339 for my $f (values %files) {
340 print "removing $f\n";
341 $f->remove;
342 }
343 }