diff --git a/SAS/TMSS/backend/test/t_tmssapp_authorization_REST_API.py b/SAS/TMSS/backend/test/t_tmssapp_authorization_REST_API.py index 33272375a3916e81b358c6e53c06a253e7979ba1..f9e99566225932d34a5129cff112d657c9640146 100755 --- a/SAS/TMSS/backend/test/t_tmssapp_authorization_REST_API.py +++ b/SAS/TMSS/backend/test/t_tmssapp_authorization_REST_API.py @@ -85,18 +85,23 @@ class OIDCSession(unittest.TestCase): if "OIDC_RP_CLIENT_ID" not in os.environ.keys(): raise unittest.SkipTest("Cannot run OIDCSession-test when OIDC_RP_CLIENT_ID is not set in the environment to configure django settings") + # Fix the base url if required, since our test Keycloak expects localhost and not 127.0.0.1 + self.BASE_URL_fixed = BASE_URL.replace('127.0.0.1', 'localhost') + self.hostname = urllib.parse.urlparse(self.BASE_URL_fixed).hostname + self.port = urllib.parse.urlparse(self.BASE_URL_fixed).port + @integration_test def test_failure_without_authentication(self): with requests.Session() as session: session.verify = False - r = session.get(BASE_URL + '/task_draft/?format=api') + r = session.get(self.BASE_URL_fixed + '/task_draft/?format=api') self.assertEqual(r.status_code, 401) self.assertIn("Authentication credentials were not provided", r.content.decode('utf8')) @integration_test def test_failure_using_wrong_credentials(self): with self.assertRaises(ValueError) as err: - with TMSSsession('nouser', 'wrongpassword', urllib.parse.urlparse(BASE_URL).hostname, urllib.parse.urlparse(BASE_URL).port): + with TMSSsession('nouser', 'wrongpassword', self.hostname, self.port): pass self.assertIn('The username and/or password you specified are not correct', str(err.exception)) @@ -106,15 +111,11 @@ class OIDCSession(unittest.TestCase): ''' Note: This test expects the test env AUTH credentials to work against the OpenID provider in use. If that is not the case, set working credentials via OIDC_USERNAME and OIDC_PASSWORD env vars. - todo: fix this test for Keycloak. We can login and get redirected back to TMSS as expected, - but unexpectedly get a 401 then, i.e. the session is not authenticated even though it should be. - The callback looks the same as when the credentials are posted with the browser, and there we end up with - an authenticated session. (Not sure if this is somehow cookie-related or so, but Keycloak seems happy...) ''' username = os.environ.get('OIDC_USERNAME', AUTH.username) password = os.environ.get('OIDC_PASSWORD', AUTH.password) - with TMSSsession(username, password, urllib.parse.urlparse(BASE_URL).hostname, urllib.parse.urlparse(BASE_URL).port) as tmss_session: - r = tmss_session.session.get(BASE_URL + '/task_draft/') + with TMSSsession(username, password, self.hostname, self.port) as tmss_session: + r = tmss_session.session.get(self.BASE_URL_fixed + '/task_draft/') self.assertEqual(r.status_code, 200) self.assertIn('results', r.json())