|
| 1 | +from unittest.mock import sentinel |
| 2 | + |
| 3 | +import pytest |
| 4 | +from sqlalchemy import select |
| 5 | + |
| 6 | +from h.models import UserIdentity |
| 7 | +from h.models.user_identity import IdentityProvider |
| 8 | +from h.services.orcid_client import ORCIDClientService, factory |
| 9 | + |
| 10 | + |
| 11 | +class TestORCIDClientService: |
| 12 | + def test_get_orcid(self, service, openid_client_service, JWTService): |
| 13 | + openid_client_service.get_id_token.return_value = sentinel.id_token |
| 14 | + JWTService.decode_token.return_value = {"sub": sentinel.orcid} |
| 15 | + |
| 16 | + orcid = service.get_orcid(sentinel.authorization_code) |
| 17 | + |
| 18 | + assert orcid == sentinel.orcid |
| 19 | + openid_client_service.get_id_token.assert_called_once_with( |
| 20 | + token_url=service.token_url, |
| 21 | + redirect_uri=sentinel.redirect_uri, |
| 22 | + auth=(sentinel.client_id, sentinel.client_secret), |
| 23 | + authorization_code=sentinel.authorization_code, |
| 24 | + ) |
| 25 | + JWTService.decode_token.assert_called_once_with( |
| 26 | + sentinel.id_token, service.key_set_url |
| 27 | + ) |
| 28 | + |
| 29 | + def test_get_orcid_returns_none_if_sub_missing( |
| 30 | + self, service, openid_client_service, JWTService |
| 31 | + ): |
| 32 | + openid_client_service.get_id_token.return_value = sentinel.id_token |
| 33 | + JWTService.decode_token.return_value = {} |
| 34 | + |
| 35 | + assert service.get_orcid(sentinel.authorization_code) is None |
| 36 | + |
| 37 | + def test_add_identity(self, service, db_session, user): |
| 38 | + orcid = "1111-1111-1111-1111" |
| 39 | + |
| 40 | + service.add_identity(user, orcid) |
| 41 | + |
| 42 | + stmt = select(UserIdentity).where( |
| 43 | + UserIdentity.user == user, |
| 44 | + UserIdentity.provider == IdentityProvider.ORCID, |
| 45 | + UserIdentity.provider_unique_id == orcid, |
| 46 | + ) |
| 47 | + assert db_session.execute(stmt).scalar() is not None |
| 48 | + |
| 49 | + def test_get_identity(self, service, user, user_identity): |
| 50 | + assert service.get_identity(user) == user_identity |
| 51 | + |
| 52 | + def test_get_identity_without_identities(self, service, user): |
| 53 | + user.identities = [] |
| 54 | + |
| 55 | + assert service.get_identity(user) is None |
| 56 | + |
| 57 | + def test_orcid_url_with_empty_orcid(self, service): |
| 58 | + assert service.orcid_url("") is None |
| 59 | + |
| 60 | + @pytest.fixture |
| 61 | + def user(self, factories): |
| 62 | + return factories.User() |
| 63 | + |
| 64 | + @pytest.fixture |
| 65 | + def user_identity(self, user, db_session): |
| 66 | + identity = UserIdentity( |
| 67 | + user=user, |
| 68 | + provider=IdentityProvider.ORCID, |
| 69 | + provider_unique_id="0000-0000-0000-0000", |
| 70 | + ) |
| 71 | + db_session.add(identity) |
| 72 | + db_session.flush() |
| 73 | + return identity |
| 74 | + |
| 75 | + @pytest.fixture |
| 76 | + def service(self, db_session, openid_client_service, user_service): |
| 77 | + return ORCIDClientService( |
| 78 | + db=db_session, |
| 79 | + host=IdentityProvider.ORCID, |
| 80 | + client_id=sentinel.client_id, |
| 81 | + client_secret=sentinel.client_secret, |
| 82 | + redirect_uri=sentinel.redirect_uri, |
| 83 | + openid_client_service=openid_client_service, |
| 84 | + user_service=user_service, |
| 85 | + ) |
| 86 | + |
| 87 | + @pytest.fixture(autouse=True) |
| 88 | + def JWTService(self, patch): |
| 89 | + return patch("h.services.orcid_client.JWTService") |
| 90 | + |
| 91 | + |
| 92 | +class TestFactory: |
| 93 | + def test_it( |
| 94 | + self, pyramid_request, ORCIDClientService, openid_client_service, user_service |
| 95 | + ): |
| 96 | + service = factory(sentinel.context, pyramid_request) |
| 97 | + |
| 98 | + ORCIDClientService.assert_called_once_with( |
| 99 | + db=pyramid_request.db, |
| 100 | + host=IdentityProvider.ORCID, |
| 101 | + client_id=sentinel.client_id, |
| 102 | + client_secret=sentinel.client_secret, |
| 103 | + redirect_uri=sentinel.redirect_uri, |
| 104 | + openid_client_service=openid_client_service, |
| 105 | + user_service=user_service, |
| 106 | + ) |
| 107 | + assert service == ORCIDClientService.return_value |
| 108 | + |
| 109 | + @pytest.fixture(autouse=True) |
| 110 | + def ORCIDClientService(self, patch): |
| 111 | + return patch("h.services.orcid_client.ORCIDClientService") |
| 112 | + |
| 113 | + @pytest.fixture |
| 114 | + def pyramid_request(self, pyramid_request, mocker): |
| 115 | + pyramid_request.registry.settings.update( |
| 116 | + { |
| 117 | + "orcid_host": IdentityProvider.ORCID, |
| 118 | + "orcid_client_id": sentinel.client_id, |
| 119 | + "orcid_client_secret": sentinel.client_secret, |
| 120 | + } |
| 121 | + ) |
| 122 | + pyramid_request.route_url = mocker.Mock(return_value=sentinel.redirect_uri) |
| 123 | + return pyramid_request |
0 commit comments