From 93eba03c9fc38a18e55fefa2e3ec30cd217f44dc Mon Sep 17 00:00:00 2001
From: "Antoine R. Dumont (@ardumont)" <antoine.romain.dumont@gmail.com>
Date: Wed, 3 Mar 2021 15:23:50 +0100
Subject: [PATCH] swh.auth: Add coverage to userinfo endpoint

Related to T3079
---
 swh/auth/tests/conftest.py    |  3 ++-
 swh/auth/tests/sample_data.py | 12 ++++++++++++
 swh/auth/tests/test_auth.py   |  9 ++++++++-
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/swh/auth/tests/conftest.py b/swh/auth/tests/conftest.py
index d34c344..a89e3eb 100644
--- a/swh/auth/tests/conftest.py
+++ b/swh/auth/tests/conftest.py
@@ -7,7 +7,7 @@ import pytest
 
 from swh.auth import KeycloakOpenIDConnect
 
-from .sample_data import OIDC_PROFILE, REALM, SERVER_URL, WELL_KNOWN
+from .sample_data import OIDC_PROFILE, REALM, SERVER_URL, USER_INFO, WELL_KNOWN
 
 
 @pytest.fixture
@@ -24,6 +24,7 @@ def mock_keycloak(requests_mock):
     """
     requests_mock.get(WELL_KNOWN["well-known"], json=WELL_KNOWN)
     requests_mock.post(WELL_KNOWN["token_endpoint"], json=OIDC_PROFILE)
+    requests_mock.get(WELL_KNOWN["userinfo_endpoint"], json=USER_INFO)
 
     return requests_mock
 
diff --git a/swh/auth/tests/sample_data.py b/swh/auth/tests/sample_data.py
index 0699384..77e4602 100644
--- a/swh/auth/tests/sample_data.py
+++ b/swh/auth/tests/sample_data.py
@@ -270,3 +270,15 @@ OIDC_PROFILE = {
     "session_state": "d82b90d1-0a94-4e74-ad66-dd95341c7b6d",
     "token_type": "bearer",
 }
+
+
+USER_INFO = {
+    "email": "john.doe@example.com",
+    "email_verified": False,
+    "family_name": "Doe",
+    "given_name": "John",
+    "groups": ["/staff"],
+    "name": "John Doe",
+    "preferred_username": "johndoe",
+    "sub": "feacd344-b468-4a65-a236-14f61e6b7200",
+}
diff --git a/swh/auth/tests/test_auth.py b/swh/auth/tests/test_auth.py
index 0222b15..673b515 100644
--- a/swh/auth/tests/test_auth.py
+++ b/swh/auth/tests/test_auth.py
@@ -8,7 +8,7 @@ from urllib.parse import parse_qs, urlparse
 from keycloak.exceptions import KeycloakAuthenticationError, KeycloakConnectionError
 import pytest
 
-from .sample_data import OIDC_PROFILE, WELL_KNOWN
+from .sample_data import OIDC_PROFILE, USER_INFO, WELL_KNOWN
 
 
 def test_auth_connection_failure(keycloak_open_id_connect):
@@ -68,3 +68,10 @@ def test_auth_refresh_token(mock_keycloak, keycloak_open_id_connect):
     assert actual_result is not None
 
     assert mock_keycloak.called
+
+
+def test_auth_userinfo(mock_keycloak, keycloak_open_id_connect):
+    actual_user_info = keycloak_open_id_connect.userinfo("refresh-token")
+    assert actual_user_info == USER_INFO
+
+    assert mock_keycloak.called
-- 
GitLab