Coverage for app/backend/src/tests/test_strong_verification.py: 99%
384 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-10 09:48 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-10 09:48 +0000
1import json
2from datetime import date, timedelta
3from unittest.mock import ANY, patch
4from urllib.parse import urlencode
6import grpc
7import pytest
8from google.protobuf import empty_pb2
9from sqlalchemy import select, update
10from sqlalchemy.sql import or_
12from couchers.config import config
13from couchers.crypto import asym_decrypt, b64encode_unpadded
14from couchers.db import session_scope
15from couchers.jobs.handlers import update_badges
16from couchers.jobs.worker import process_job
17from couchers.materialized_views import refresh_materialized_views_rapid
18from couchers.models import (
19 PassportSex,
20 StrongVerificationAttempt,
21 StrongVerificationAttemptStatus,
22 StrongVerificationCallbackEvent,
23 User,
24)
25from couchers.proto import account_pb2, admin_pb2, api_pb2
26from couchers.proto.google.api import httpbody_pb2
27from tests.fixtures.db import generate_user
28from tests.fixtures.feature_flags import FeatureFlags
29from tests.fixtures.misc import PushCollector
30from tests.fixtures.sessions import account_session, api_session, real_admin_session, real_iris_session
33def _emulate_iris_callback(session_id, session_state, reference):
34 assert session_state in ["CREATED", "INITIATED", "FAILED", "ABORTED", "COMPLETED", "REJECTED", "APPROVED"]
35 with real_iris_session() as iris:
36 data = json.dumps(
37 {"session_id": session_id, "session_state": session_state, "session_reference": reference}
38 ).encode("ascii")
39 iris.Webhook(httpbody_pb2.HttpBody(content_type="application/json", data=data))
42default_expiry = date.today() + timedelta(days=5 * 365)
45def do_and_check_sv(
46 user,
47 token,
48 verification_id,
49 sex,
50 dob,
51 document_type,
52 document_number,
53 document_expiry,
54 nationality,
55 return_after=None,
56):
57 iris_token_data = {
58 "merchant_id": 5731012934821982,
59 "session_id": verification_id,
60 "seed": 1674246339,
61 "face_verification": False,
62 "host": "https://passportreader.app",
63 }
64 iris_token = b64encode_unpadded(json.dumps(iris_token_data).encode("utf8"))
66 with account_session(token) as account:
67 # start by initiation
68 with patch("couchers.servicers.account.requests.post") as mock:
69 json_resp1 = {
70 "id": verification_id,
71 "token": iris_token,
72 }
73 mock.return_value = type(
74 "__MockResponse",
75 (),
76 {
77 "status_code": 200,
78 "text": json.dumps(json_resp1),
79 "json": lambda: json_resp1,
80 },
81 )
82 res = account.InitiateStrongVerification(empty_pb2.Empty())
83 mock.assert_called_once_with(
84 "https://passportreader.app/api/v1/session.create",
85 auth=("dummy_pubkey", "dummy_secret"),
86 json={
87 "callback_url": "http://localhost:8888/iris/webhook",
88 "face_verification": False,
89 "passport_only": True,
90 "reference": ANY,
91 },
92 timeout=10,
93 verify="/etc/ssl/certs/ca-certificates.crt",
94 )
95 reference_data = mock.call_args.kwargs["json"]["reference"]
96 verification_attempt_token = res.verification_attempt_token
97 return_url = f"http://localhost:3000/complete-strong-verification?verification_attempt_token={verification_attempt_token}"
98 assert res.redirect_url == "https://passportreader.app/open?" + urlencode(
99 {"token": iris_token, "redirect_url": return_url}
100 )
102 assert (
103 account.GetStrongVerificationAttemptStatus(
104 account_pb2.GetStrongVerificationAttemptStatusReq(verification_attempt_token=verification_attempt_token)
105 ).status
106 == account_pb2.STRONG_VERIFICATION_ATTEMPT_STATUS_IN_PROGRESS_WAITING_ON_USER_TO_OPEN_APP
107 )
109 # ok, now the user downloads the app, scans their id, and Iris ID sends callbacks to the server
110 _emulate_iris_callback(verification_id, "INITIATED", reference_data)
112 with account_session(token) as account:
113 assert (
114 account.GetStrongVerificationAttemptStatus(
115 account_pb2.GetStrongVerificationAttemptStatusReq(verification_attempt_token=verification_attempt_token)
116 ).status
117 == account_pb2.STRONG_VERIFICATION_ATTEMPT_STATUS_IN_PROGRESS_WAITING_ON_USER_IN_APP
118 )
120 if return_after == "INITIATED":
121 return reference_data
123 _emulate_iris_callback(verification_id, "COMPLETED", reference_data)
125 with account_session(token) as account:
126 assert (
127 account.GetStrongVerificationAttemptStatus(
128 account_pb2.GetStrongVerificationAttemptStatusReq(verification_attempt_token=verification_attempt_token)
129 ).status
130 == account_pb2.STRONG_VERIFICATION_ATTEMPT_STATUS_IN_PROGRESS_WAITING_ON_BACKEND
131 )
133 if return_after == "COMPLETED": 133 ↛ 134line 133 didn't jump to line 134 because the condition on line 133 was never true
134 return reference_data
136 _emulate_iris_callback(verification_id, "APPROVED", reference_data)
138 with account_session(token) as account:
139 assert (
140 account.GetStrongVerificationAttemptStatus(
141 account_pb2.GetStrongVerificationAttemptStatusReq(verification_attempt_token=verification_attempt_token)
142 ).status
143 == account_pb2.STRONG_VERIFICATION_ATTEMPT_STATUS_IN_PROGRESS_WAITING_ON_BACKEND
144 )
146 if return_after == "APPROVED":
147 return reference_data
149 with patch("couchers.jobs.handlers.requests.post") as mock:
150 json_resp2 = {
151 "id": verification_id,
152 "created": "2024-05-11T15:46:46Z",
153 "expires": "2024-05-11T16:17:26Z",
154 "state": "APPROVED",
155 "reference": reference_data,
156 "user_ip": "10.123.123.123",
157 "user_agent": "Iris%20ID/168357896 CFNetwork/1494.0.7 Darwin/23.4.0",
158 "given_names": "John Wayne",
159 "surname": "Doe",
160 "nationality": nationality,
161 "sex": sex,
162 "date_of_birth": dob,
163 "document_type": document_type,
164 "document_number": document_number,
165 "expiry_date": document_expiry.isoformat(),
166 "issuing_country": nationality,
167 "issuer": "Department of State, U.S. Government",
168 "portrait": "dGVzdHRlc3R0ZXN0...",
169 }
170 mock.return_value = type(
171 "__MockResponse",
172 (),
173 {
174 "status_code": 200,
175 "text": json.dumps(json_resp2),
176 "json": lambda: json_resp2,
177 },
178 )
179 while process_job():
180 pass
182 mock.assert_called_once_with(
183 "https://passportreader.app/api/v1/session.get",
184 auth=("dummy_pubkey", "dummy_secret"),
185 json={"id": verification_id},
186 timeout=10,
187 verify="/etc/ssl/certs/ca-certificates.crt",
188 )
190 with account_session(token) as account:
191 assert (
192 account.GetStrongVerificationAttemptStatus(
193 account_pb2.GetStrongVerificationAttemptStatusReq(verification_attempt_token=verification_attempt_token)
194 ).status
195 == account_pb2.STRONG_VERIFICATION_ATTEMPT_STATUS_SUCCEEDED
196 )
198 with session_scope() as session:
199 verification_attempt = session.execute(
200 select(StrongVerificationAttempt).where(
201 StrongVerificationAttempt.verification_attempt_token == verification_attempt_token
202 )
203 ).scalar_one()
204 assert verification_attempt.user_id == user.id
205 assert verification_attempt.status == StrongVerificationAttemptStatus.succeeded
206 assert verification_attempt.has_full_data
207 assert verification_attempt.passport_encrypted_data
208 # assert verification_attempt.passport_date_of_birth == date(1988, 1, 1)
209 # assert verification_attempt.passport_sex == PassportSex.male
210 assert verification_attempt.has_minimal_data
211 assert verification_attempt.passport_expiry_date == document_expiry
212 assert verification_attempt.passport_nationality == nationality
213 assert verification_attempt.passport_last_three_document_chars == document_number[-3:]
214 assert verification_attempt.iris_token == iris_token
215 assert verification_attempt.iris_session_id == verification_id
217 private_key = bytes.fromhex("e6c2fbf3756b387bc09a458a7b85935718ef3eb1c2777ef41d335c9f6c0ab272")
218 decrypted_data = json.loads(asym_decrypt(private_key, verification_attempt.passport_encrypted_data))
219 assert decrypted_data == json_resp2
221 callbacks = (
222 session.execute(
223 select(StrongVerificationCallbackEvent.iris_status)
224 .where(StrongVerificationCallbackEvent.verification_attempt_id == verification_attempt.id)
225 .order_by(StrongVerificationCallbackEvent.created.asc())
226 )
227 .scalars()
228 .all()
229 )
230 assert callbacks == ["INITIATED", "COMPLETED", "APPROVED"]
233@pytest.fixture
234def sv_config() -> None:
235 # An autouse fixture runs before non-autouse fixtures of the same scope.
236 config.IRIS_ID_PUBKEY = "dummy_pubkey"
237 config.IRIS_ID_SECRET = "dummy_secret"
238 config.VERIFICATION_DATA_PUBLIC_KEY = bytes.fromhex(
239 "dd740a2b2a35bf05041a28257ea439b30f76f056f3698000b71e6470cd82275f"
240 )
243def test_strong_verification_happy_path(db, sv_config):
244 user, token = generate_user(birthdate=date(1988, 1, 1), gender="Man")
245 _, superuser_token = generate_user(is_superuser=True)
247 update_badges(empty_pb2.Empty())
248 refresh_materialized_views_rapid(empty_pb2.Empty())
250 with api_session(token) as api:
251 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
252 assert "strong_verification" not in res.badges
253 assert not res.has_strong_verification
254 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_UNVERIFIED
255 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_UNVERIFIED
256 assert (
257 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
258 == res.has_strong_verification
259 )
261 do_and_check_sv(
262 user,
263 token,
264 verification_id=5731012934821983,
265 sex="MALE",
266 dob="1988-01-01",
267 document_type="PASSPORT",
268 document_number="31195855",
269 document_expiry=default_expiry,
270 nationality="US",
271 )
273 with session_scope() as session:
274 verification_attempt = session.execute(
275 select(StrongVerificationAttempt).where(StrongVerificationAttempt.user_id == user.id)
276 ).scalar_one()
277 assert verification_attempt.status == StrongVerificationAttemptStatus.succeeded
278 assert verification_attempt.passport_date_of_birth == date(1988, 1, 1)
279 assert verification_attempt.passport_sex == PassportSex.male
280 assert verification_attempt.passport_expiry_date == default_expiry
281 assert verification_attempt.passport_nationality == "US"
282 assert verification_attempt.passport_last_three_document_chars == "855"
284 update_badges(empty_pb2.Empty())
285 refresh_materialized_views_rapid(empty_pb2.Empty())
287 # the user should now have strong verification
288 with api_session(token) as api:
289 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
290 assert "strong_verification" in res.badges
291 assert res.has_strong_verification
292 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
293 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_VERIFIED
294 assert (
295 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
296 == res.has_strong_verification
297 )
299 # wrong dob = no badge
300 with session_scope() as session:
301 session.execute(update(User).where(User.id == user.id).values(birthdate=date(1988, 1, 2)))
303 update_badges(empty_pb2.Empty())
304 refresh_materialized_views_rapid(empty_pb2.Empty())
306 with api_session(token) as api:
307 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
308 assert "strong_verification" not in res.badges
309 assert not res.has_strong_verification
310 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_MISMATCH
311 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_VERIFIED
312 assert (
313 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
314 == res.has_strong_verification
315 )
317 # bad gender-sex correspondence = no badge
318 with session_scope() as session:
319 session.execute(update(User).where(User.id == user.id).values(birthdate=date(1988, 1, 1), gender="Woman"))
321 update_badges(empty_pb2.Empty())
322 refresh_materialized_views_rapid(empty_pb2.Empty())
324 with api_session(token) as api:
325 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
326 assert "strong_verification" not in res.badges
327 assert not res.has_strong_verification
328 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
329 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_MISMATCH
330 assert (
331 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
332 == res.has_strong_verification
333 )
335 with account_session(token) as account:
336 res = account.GetAccountInfo(empty_pb2.Empty())
337 assert not res.has_strong_verification
338 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
339 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_MISMATCH
341 # back to should have a badge
342 with session_scope() as session:
343 session.execute(update(User).where(User.id == user.id).values(gender="Man"))
345 update_badges(empty_pb2.Empty())
346 refresh_materialized_views_rapid(empty_pb2.Empty())
348 with api_session(token) as api:
349 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
350 assert "strong_verification" in res.badges
351 assert res.has_strong_verification
352 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
353 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_VERIFIED
354 assert (
355 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
356 == res.has_strong_verification
357 )
359 # check has_passport_sex_gender_exception
360 with real_admin_session(superuser_token) as admin:
361 res = admin.GetUserDetails(admin_pb2.GetUserDetailsReq(user=user.username))
362 assert "strong_verification" in res.badges
363 assert res.has_strong_verification
364 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
365 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_VERIFIED
367 admin.SetPassportSexGenderException(
368 admin_pb2.SetPassportSexGenderExceptionReq(user=user.username, passport_sex_gender_exception=True)
369 )
370 admin.ChangeUserGender(admin_pb2.ChangeUserGenderReq(user=user.username, gender="Woman"))
372 update_badges(empty_pb2.Empty())
373 refresh_materialized_views_rapid(empty_pb2.Empty())
375 with api_session(token) as api:
376 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
377 assert "strong_verification" in res.badges
378 assert res.has_strong_verification
379 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
380 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_VERIFIED
381 assert (
382 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
383 == res.has_strong_verification
384 )
386 with real_admin_session(superuser_token) as admin:
387 res = admin.GetUserDetails(admin_pb2.GetUserDetailsReq(user=user.username))
388 assert "strong_verification" in res.badges
389 assert res.has_strong_verification
390 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
391 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_VERIFIED
393 # now turn exception off
394 admin.SetPassportSexGenderException(
395 admin_pb2.SetPassportSexGenderExceptionReq(user=user.username, passport_sex_gender_exception=False)
396 )
398 update_badges(empty_pb2.Empty())
399 refresh_materialized_views_rapid(empty_pb2.Empty())
401 with api_session(token) as api:
402 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
403 assert "strong_verification" not in res.badges
404 assert not res.has_strong_verification
405 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
406 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_MISMATCH
407 assert (
408 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
409 == res.has_strong_verification
410 )
412 with real_admin_session(superuser_token) as admin:
413 res = admin.GetUserDetails(admin_pb2.GetUserDetailsReq(user=user.username))
414 assert "strong_verification" not in res.badges
415 assert not res.has_strong_verification
416 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
417 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_MISMATCH
420def test_strong_verification_delete_data(db, sv_config):
421 user, token = generate_user(birthdate=date(1988, 1, 1), gender="Man")
422 _, superuser_token = generate_user(is_superuser=True)
424 refresh_materialized_views_rapid(empty_pb2.Empty())
426 with api_session(token) as api:
427 assert not api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
428 assert (
429 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
430 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
431 )
433 # can remove SV data even if there is none, should do nothing
434 with account_session(token) as account:
435 account.DeleteStrongVerificationData(empty_pb2.Empty())
437 do_and_check_sv(
438 user,
439 token,
440 verification_id=5731012934821983,
441 sex="MALE",
442 dob="1988-01-01",
443 document_type="PASSPORT",
444 document_number="31195855",
445 document_expiry=default_expiry,
446 nationality="US",
447 )
449 refresh_materialized_views_rapid(empty_pb2.Empty())
451 # the user should now have strong verification
452 with api_session(token) as api:
453 assert api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
454 assert (
455 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
456 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
457 )
459 # check removing SV data
460 with account_session(token) as account:
461 account.DeleteStrongVerificationData(empty_pb2.Empty())
463 refresh_materialized_views_rapid(empty_pb2.Empty())
465 with api_session(token) as api:
466 assert not api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
467 assert (
468 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
469 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
470 )
472 with session_scope() as session:
473 assert (
474 len(
475 session.execute(
476 select(StrongVerificationAttempt).where(
477 or_(
478 StrongVerificationAttempt.passport_encrypted_data != None,
479 StrongVerificationAttempt.passport_date_of_birth != None,
480 StrongVerificationAttempt.passport_sex != None,
481 )
482 )
483 )
484 .scalars()
485 .all()
486 )
487 == 0
488 )
491def test_strong_verification_expiry(db, sv_config):
492 user, token = generate_user(birthdate=date(1988, 1, 1), gender="Man")
493 _, superuser_token = generate_user(is_superuser=True)
495 refresh_materialized_views_rapid(empty_pb2.Empty())
497 with api_session(token) as api:
498 assert not api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
499 assert (
500 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
501 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
502 )
504 expiry = date.today() + timedelta(days=10)
506 do_and_check_sv(
507 user,
508 token,
509 verification_id=5731012934821983,
510 sex="MALE",
511 dob="1988-01-01",
512 document_type="PASSPORT",
513 document_number="31195855",
514 document_expiry=expiry,
515 nationality="US",
516 )
518 # the user should now have strong verification
519 with api_session(token) as api:
520 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
521 assert res.has_strong_verification
522 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_VERIFIED
523 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_VERIFIED
525 with session_scope() as session:
526 attempt = session.execute(select(StrongVerificationAttempt)).scalars().one()
527 attempt.passport_expiry_date = date.today() - timedelta(days=2)
529 with api_session(token) as api:
530 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
531 assert not res.has_strong_verification
532 assert res.birthdate_verification_status == api_pb2.BIRTHDATE_VERIFICATION_STATUS_UNVERIFIED
533 assert res.gender_verification_status == api_pb2.GENDER_VERIFICATION_STATUS_UNVERIFIED
535 res = api.GetUser(api_pb2.GetUserReq(user=user.username))
536 assert not res.has_strong_verification
537 assert not res.has_strong_verification
539 do_and_check_sv(
540 user,
541 token,
542 verification_id=5731012934821985,
543 sex="MALE",
544 dob="1988-01-01",
545 document_type="PASSPORT",
546 document_number="PA41323412",
547 document_expiry=date.today() + timedelta(days=365),
548 nationality="AU",
549 )
551 refresh_materialized_views_rapid(empty_pb2.Empty())
553 with api_session(token) as api:
554 assert api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
555 assert (
556 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
557 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
558 )
561def test_strong_verification_regression(db, sv_config):
562 user, token = generate_user(birthdate=date(1988, 1, 1), gender="Man")
564 do_and_check_sv(
565 user,
566 token,
567 verification_id=5731012934821983,
568 sex="MALE",
569 dob="1988-01-01",
570 document_type="PASSPORT",
571 document_number="31195855",
572 document_expiry=default_expiry,
573 nationality="US",
574 return_after="INITIATED",
575 )
577 with api_session(token) as api:
578 api.Ping(api_pb2.PingReq())
581def test_strong_verification_regression2(db, sv_config):
582 user, token = generate_user(birthdate=date(1988, 1, 1), gender="Man")
584 do_and_check_sv(
585 user,
586 token,
587 verification_id=5731012934821983,
588 sex="MALE",
589 dob="1988-01-01",
590 document_type="PASSPORT",
591 document_number="31195855",
592 document_expiry=default_expiry,
593 nationality="US",
594 return_after="INITIATED",
595 )
597 do_and_check_sv(
598 user,
599 token,
600 verification_id=5731012934821985,
601 sex="MALE",
602 dob="1988-01-01",
603 document_type="PASSPORT",
604 document_number="PA41323412",
605 document_expiry=default_expiry,
606 nationality="AU",
607 )
609 refresh_materialized_views_rapid(empty_pb2.Empty())
611 with api_session(token) as api:
612 assert api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
613 assert (
614 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
615 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
616 )
619def test_strong_verification_disabled(db, feature_flags: FeatureFlags):
620 feature_flags.set("strong_verification_enabled", False)
621 user, token = generate_user()
623 with account_session(token) as account:
624 with pytest.raises(grpc.RpcError) as e:
625 account.InitiateStrongVerification(empty_pb2.Empty())
626 assert e.value.code() == grpc.StatusCode.UNAVAILABLE
627 assert e.value.details() == "Strong verification is currently disabled."
630def test_strong_verification_delete_data_cant_reverify(db, sv_config, push_collector: PushCollector):
631 user, token = generate_user(birthdate=date(1988, 1, 1), gender="Man")
632 _, superuser_token = generate_user(is_superuser=True)
634 refresh_materialized_views_rapid(empty_pb2.Empty())
636 with api_session(token) as api:
637 assert not api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
638 assert (
639 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
640 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
641 )
643 do_and_check_sv(
644 user,
645 token,
646 verification_id=5731012934821983,
647 sex="MALE",
648 dob="1988-01-01",
649 document_type="PASSPORT",
650 document_number="31195855",
651 document_expiry=default_expiry,
652 nationality="US",
653 )
655 refresh_materialized_views_rapid(empty_pb2.Empty())
657 # the user should now have strong verification
658 with api_session(token) as api:
659 assert api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
660 assert (
661 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
662 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
663 )
665 # There should be a notification confirming it
666 push_collector.pop_for_user(user.id, last=True)
668 # check removing SV data
669 with account_session(token) as account:
670 account.DeleteStrongVerificationData(empty_pb2.Empty())
672 refresh_materialized_views_rapid(empty_pb2.Empty())
674 with api_session(token) as api:
675 assert not api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
676 assert (
677 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
678 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
679 )
681 with session_scope() as session:
682 assert (
683 len(
684 session.execute(
685 select(StrongVerificationAttempt).where(
686 or_(
687 StrongVerificationAttempt.passport_encrypted_data != None,
688 StrongVerificationAttempt.passport_date_of_birth != None,
689 StrongVerificationAttempt.passport_sex != None,
690 )
691 )
692 )
693 .scalars()
694 .all()
695 )
696 == 0
697 )
699 reference_data = do_and_check_sv(
700 user,
701 token,
702 verification_id=5731012934821984,
703 sex="MALE",
704 dob="1988-01-01",
705 document_type="PASSPORT",
706 document_number="31195855",
707 document_expiry=default_expiry,
708 nationality="US",
709 return_after="APPROVED",
710 )
712 with patch("couchers.jobs.handlers.requests.post") as mock:
713 json_resp2 = {
714 "id": 5731012934821984,
715 "created": "2024-05-11T15:46:46Z",
716 "expires": "2024-05-11T16:17:26Z",
717 "state": "APPROVED",
718 "reference": reference_data,
719 "user_ip": "10.123.123.123",
720 "user_agent": "Iris%20ID/168357896 CFNetwork/1494.0.7 Darwin/23.4.0",
721 "given_names": "John Wayne",
722 "surname": "Doe",
723 "nationality": "US",
724 "sex": "MALE",
725 "date_of_birth": "1988-01-01",
726 "document_type": "PASSPORT",
727 "document_number": "31195855",
728 "expiry_date": default_expiry.isoformat(),
729 "issuing_country": "US",
730 "issuer": "Department of State, U.S. Government",
731 "portrait": "dGVzdHRlc3R0ZXN0...",
732 }
733 mock.return_value = type(
734 "__MockResponse",
735 (),
736 {
737 "status_code": 200,
738 "text": json.dumps(json_resp2),
739 "json": lambda: json_resp2,
740 },
741 )
742 while process_job():
743 pass
745 mock.assert_called_once_with(
746 "https://passportreader.app/api/v1/session.get",
747 auth=("dummy_pubkey", "dummy_secret"),
748 json={"id": 5731012934821984},
749 timeout=10,
750 verify="/etc/ssl/certs/ca-certificates.crt",
751 )
753 with session_scope() as session:
754 verification_attempt = session.execute(
755 select(StrongVerificationAttempt).where(StrongVerificationAttempt.iris_session_id == 5731012934821984)
756 ).scalar_one()
757 assert verification_attempt.user_id == user.id
758 assert verification_attempt.status == StrongVerificationAttemptStatus.duplicate
760 push = push_collector.pop_for_user(user.id, last=True)
761 assert push.content.title == "Strong Verification failed"
762 assert (
763 push.content.body
764 == "You used a passport that has already been used for verification. Please use another passport."
765 )
767 refresh_materialized_views_rapid(empty_pb2.Empty())
769 with api_session(token) as api:
770 assert not api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
771 assert (
772 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
773 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
774 )
777def test_strong_verification_duplicate_other_user(db, sv_config, push_collector: PushCollector):
778 user, token = generate_user(birthdate=date(1988, 1, 1), gender="Man")
779 user2, token2 = generate_user(birthdate=date(1988, 1, 1), gender="Man")
780 _, superuser_token = generate_user(is_superuser=True)
782 refresh_materialized_views_rapid(empty_pb2.Empty())
784 with api_session(token) as api:
785 assert not api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
786 assert (
787 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
788 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
789 )
791 # can remove SV data even if there is none, should do nothing
792 with account_session(token) as account:
793 account.DeleteStrongVerificationData(empty_pb2.Empty())
795 do_and_check_sv(
796 user,
797 token,
798 verification_id=5731012934821983,
799 sex="MALE",
800 dob="1988-01-01",
801 document_type="PASSPORT",
802 document_number="31195855",
803 document_expiry=default_expiry,
804 nationality="US",
805 )
807 refresh_materialized_views_rapid(empty_pb2.Empty())
809 # the user should now have strong verification
810 with api_session(token) as api:
811 assert api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
812 assert (
813 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
814 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
815 )
817 # check removing SV data
818 with account_session(token) as account:
819 account.DeleteStrongVerificationData(empty_pb2.Empty())
821 refresh_materialized_views_rapid(empty_pb2.Empty())
823 with api_session(token) as api:
824 assert not api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
825 assert (
826 api.GetLiteUser(api_pb2.GetLiteUserReq(user=user.username)).has_strong_verification
827 == api.GetUser(api_pb2.GetUserReq(user=user.username)).has_strong_verification
828 )
830 with session_scope() as session:
831 assert (
832 len(
833 session.execute(
834 select(StrongVerificationAttempt).where(
835 or_(
836 StrongVerificationAttempt.passport_encrypted_data != None,
837 StrongVerificationAttempt.passport_date_of_birth != None,
838 StrongVerificationAttempt.passport_sex != None,
839 )
840 )
841 )
842 .scalars()
843 .all()
844 )
845 == 0
846 )
848 reference_data = do_and_check_sv(
849 user2,
850 token2,
851 verification_id=5731012934821984,
852 sex="MALE",
853 dob="1988-01-01",
854 document_type="PASSPORT",
855 document_number="31195855",
856 document_expiry=default_expiry,
857 nationality="US",
858 return_after="APPROVED",
859 )
861 with patch("couchers.jobs.handlers.requests.post") as mock:
862 json_resp2 = {
863 "id": 5731012934821984,
864 "created": "2024-05-11T15:46:46Z",
865 "expires": "2024-05-11T16:17:26Z",
866 "state": "APPROVED",
867 "reference": reference_data,
868 "user_ip": "10.123.123.123",
869 "user_agent": "Iris%20ID/168357896 CFNetwork/1494.0.7 Darwin/23.4.0",
870 "given_names": "John Wayne",
871 "surname": "Doe",
872 "nationality": "US",
873 "sex": "MALE",
874 "date_of_birth": "1988-01-01",
875 "document_type": "PASSPORT",
876 "document_number": "31195855",
877 "expiry_date": default_expiry.isoformat(),
878 "issuing_country": "US",
879 "issuer": "Department of State, U.S. Government",
880 "portrait": "dGVzdHRlc3R0ZXN0...",
881 }
882 mock.return_value = type(
883 "__MockResponse",
884 (),
885 {
886 "status_code": 200,
887 "text": json.dumps(json_resp2),
888 "json": lambda: json_resp2,
889 },
890 )
891 while process_job():
892 pass
894 mock.assert_called_once_with(
895 "https://passportreader.app/api/v1/session.get",
896 auth=("dummy_pubkey", "dummy_secret"),
897 json={"id": 5731012934821984},
898 timeout=10,
899 verify="/etc/ssl/certs/ca-certificates.crt",
900 )
902 with session_scope() as session:
903 verification_attempt = session.execute(
904 select(StrongVerificationAttempt).where(StrongVerificationAttempt.iris_session_id == 5731012934821984)
905 ).scalar_one()
906 assert verification_attempt.user_id == user2.id
907 assert verification_attempt.status == StrongVerificationAttemptStatus.duplicate
909 push = push_collector.pop_for_user(user2.id, last=True)
910 assert push.content.title == "Strong Verification failed"
911 assert (
912 push.content.body
913 == "You used a passport that has already been used for verification. Please use another passport."
914 )
917def test_strong_verification_non_passport(db, sv_config, push_collector: PushCollector):
918 user, token = generate_user(birthdate=date(1988, 1, 1), gender="Man")
919 _, superuser_token = generate_user(is_superuser=True)
921 reference_data = do_and_check_sv(
922 user,
923 token,
924 verification_id=5731012934821984,
925 sex="MALE",
926 dob="1988-01-01",
927 document_type="IDENTITY_CARD",
928 document_number="31195855",
929 document_expiry=default_expiry,
930 nationality="US",
931 return_after="APPROVED",
932 )
934 with patch("couchers.jobs.handlers.requests.post") as mock:
935 json_resp2 = {
936 "id": 5731012934821984,
937 "created": "2024-05-11T15:46:46Z",
938 "expires": "2024-05-11T16:17:26Z",
939 "state": "APPROVED",
940 "reference": reference_data,
941 "user_ip": "10.123.123.123",
942 "user_agent": "Iris%20ID/168357896 CFNetwork/1494.0.7 Darwin/23.4.0",
943 "given_names": "John Wayne",
944 "surname": "Doe",
945 "nationality": "US",
946 "sex": "MALE",
947 "date_of_birth": "1988-01-01",
948 "document_type": "IDENTITY_CARD",
949 "document_number": "31195855",
950 "expiry_date": default_expiry.isoformat(),
951 "issuing_country": "US",
952 "issuer": "Department of State, U.S. Government",
953 "portrait": "dGVzdHRlc3R0ZXN0...",
954 }
955 mock.return_value = type(
956 "__MockResponse",
957 (),
958 {
959 "status_code": 200,
960 "text": json.dumps(json_resp2),
961 "json": lambda: json_resp2,
962 },
963 )
964 while process_job():
965 pass
967 mock.assert_called_once_with(
968 "https://passportreader.app/api/v1/session.get",
969 auth=("dummy_pubkey", "dummy_secret"),
970 json={"id": 5731012934821984},
971 timeout=10,
972 verify="/etc/ssl/certs/ca-certificates.crt",
973 )
975 with session_scope() as session:
976 verification_attempt = session.execute(
977 select(StrongVerificationAttempt).where(StrongVerificationAttempt.iris_session_id == 5731012934821984)
978 ).scalar_one()
979 assert verification_attempt.user_id == user.id
980 assert verification_attempt.status == StrongVerificationAttemptStatus.failed
982 push = push_collector.pop_for_user(user.id, last=True)
983 assert push.content.title == "Strong Verification failed"
984 assert (
985 push.content.body
986 == "You used a document other than a passport. You can only use a passport for Strong Verification."
987 )
990def test_lite_user_strong_verification_not_shared_between_users(db):
991 # regression: the lite_users view joined attempts to users only on birthdate/gender, so anyone born on the same
992 # day as a strongly verified user (with a compatible gender) got the badge too
993 verified_user, _ = generate_user(birthdate=date(1988, 1, 1), gender="Man", strong_verification=True)
994 other_user, other_token = generate_user(birthdate=date(1988, 1, 1), gender="Man")
996 refresh_materialized_views_rapid(empty_pb2.Empty())
998 with api_session(other_token) as api:
999 assert api.GetLiteUser(api_pb2.GetLiteUserReq(user=verified_user.username)).has_strong_verification
1000 assert not api.GetLiteUser(api_pb2.GetLiteUserReq(user=other_user.username)).has_strong_verification
1003def test_attempt_only_verifies_its_own_user(db):
1004 verified_user, _ = generate_user(birthdate=date(1988, 1, 1), gender="Man", strong_verification=True)
1005 other_user, _ = generate_user(birthdate=date(1988, 1, 1), gender="Man")
1007 with session_scope() as session:
1008 attempt = session.execute(
1009 select(StrongVerificationAttempt).where(StrongVerificationAttempt.user_id == verified_user.id)
1010 ).scalar_one()
1011 assert attempt.has_strong_verification(verified_user)
1012 assert not attempt.has_strong_verification(other_user)
1014 # as a SQL expression, without the join the two tables are still linked by the birthdate and gender predicates
1015 assert session.execute(
1016 select(User.id)
1017 .select_from(StrongVerificationAttempt)
1018 .where(StrongVerificationAttempt.has_strong_verification(User))
1019 ).scalars().all() == [verified_user.id]
1022@pytest.mark.parametrize("predicate", ["matches_birthdate", "matches_gender"])
1023def test_attempt_predicates_bind_their_own_user(db, predicate):
1024 verified_user, _ = generate_user(birthdate=date(1988, 1, 1), gender="Man", strong_verification=True)
1025 other_user, _ = generate_user(birthdate=date(1988, 1, 1), gender="Man")
1027 with session_scope() as session:
1028 attempt = session.execute(
1029 select(StrongVerificationAttempt).where(StrongVerificationAttempt.user_id == verified_user.id)
1030 ).scalar_one()
1031 assert getattr(attempt, predicate)(verified_user)
1032 assert not getattr(attempt, predicate)(other_user)
1034 assert session.execute(
1035 select(User.id)
1036 .select_from(StrongVerificationAttempt)
1037 .where(getattr(StrongVerificationAttempt, predicate)(User))
1038 ).scalars().all() == [verified_user.id]