|
20 | 20 | from cads_processing_api_service import auth, exceptions, models
|
21 | 21 |
|
22 | 22 |
|
| 23 | +@pytest.mark.parametrize( |
| 24 | + "pat, jwt, expected", |
| 25 | + [ |
| 26 | + ("test_pat", None, ("PRIVATE-TOKEN", "test_pat")), |
| 27 | + (None, "test_jwt", ("Authorization", "test_jwt")), |
| 28 | + (None, None, (None, None)), |
| 29 | + ], |
| 30 | + ids=["pat", "jwt", "neither"], |
| 31 | +) |
| 32 | +def test_get_auth_header(pat, jwt, expected) -> None: |
| 33 | + auth_header = auth.get_auth_header(pat, jwt) |
| 34 | + assert auth_header == expected |
| 35 | + |
| 36 | + |
| 37 | +def test_get_user_info_authenticated(mocker) -> None: |
| 38 | + auth_header = ("PRIVATE-TOKEN", "test_pat") |
| 39 | + portal_header = "test_portal" |
| 40 | + mocker.patch( |
| 41 | + "cads_processing_api_service.auth.authenticate_user", |
| 42 | + return_value={ |
| 43 | + "sub": "test_user", |
| 44 | + "role": "user", |
| 45 | + |
| 46 | + }, |
| 47 | + ) |
| 48 | + expected = ( "test_user", "user", "[email protected]") |
| 49 | + user_info = auth.get_user_info(auth_header, portal_header) |
| 50 | + assert user_info == expected |
| 51 | + |
| 52 | + |
| 53 | +def test_get_user_info_unauthenticated() -> None: |
| 54 | + auth_header = (None, None) |
| 55 | + portal_header = "test_portal" |
| 56 | + user_info = auth.get_user_info(auth_header, portal_header) |
| 57 | + assert user_info == ("unauthenticated", None, None) |
| 58 | + |
| 59 | + |
| 60 | +def test_get_auth_info_not_allowed_unauthenticated() -> None: |
| 61 | + pat = None |
| 62 | + jwt = None |
| 63 | + portal_header = "test_portal" |
| 64 | + allow_unauthenticated = False |
| 65 | + with pytest.raises(exceptions.PermissionDenied): |
| 66 | + auth.get_auth_info(pat, jwt, portal_header, allow_unauthenticated) |
| 67 | + |
| 68 | + |
| 69 | +def test_get_auth_info_allowed_unauthenticated() -> None: |
| 70 | + pat = None |
| 71 | + jwt = None |
| 72 | + portal_header = "test_portal" |
| 73 | + allow_unauthenticated = True |
| 74 | + expected = models.AuthInfo( |
| 75 | + user_uid="unauthenticated", |
| 76 | + user_role=None, |
| 77 | + email=None, |
| 78 | + request_origin=None, |
| 79 | + auth_header=(None, None), |
| 80 | + portals=("test_portal",), |
| 81 | + ) |
| 82 | + auth_info = auth.get_auth_info(pat, jwt, portal_header, allow_unauthenticated) |
| 83 | + assert auth_info == expected |
| 84 | + |
| 85 | + |
23 | 86 | def test_format_missing_licences_message(mocker) -> None:
|
24 | 87 | request_url = "http://base_url/api/v1/processes/process_id/execution"
|
25 | 88 | process_id = "test_process_id"
|
|
0 commit comments