From ded3cb888f463b8c9a477d731ebe544f1770acba Mon Sep 17 00:00:00 2001 From: Salvatore Laiso Date: Thu, 27 Jul 2023 17:51:58 +0200 Subject: [PATCH] Update Backend tests --- pyeudiw/tests/satosa/test_backend.py | 48 ++++++++++++++++++++-------- requirements-dev.txt | 2 ++ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/pyeudiw/tests/satosa/test_backend.py b/pyeudiw/tests/satosa/test_backend.py index 68b0e8a8..dac7cc55 100644 --- a/pyeudiw/tests/satosa/test_backend.py +++ b/pyeudiw/tests/satosa/test_backend.py @@ -4,9 +4,12 @@ import pytest import urllib.parse +from bs4 import BeautifulSoup + +from pyeudiw.jwt.utils import unpad_jwt_payload from pyeudiw.oauth2.dpop import DPoPIssuer from pyeudiw.satosa.backend import OpenID4VPBackend -from pyeudiw.jwt import JWSHelper +from pyeudiw.jwt import JWSHelper, unpad_jwt_header from pyeudiw.jwk import JWK from pyeudiw.tools.utils import iat_now @@ -359,26 +362,39 @@ def test_pre_request_endpoint(self, context): assert pre_request_endpoint.status == "200" assert pre_request_endpoint.message - # TODO: assert that's a qrcode - # assert "svg xmlns:svg="http://www.w3.org/2000/" in pre_request_endpoint.message - - # TODO test same-device - - # TODO - def _test_pre_request_endpoint_mobile(self, context): + assert "src='data:image/svg+xml;base64," in pre_request_endpoint.message + + soup = BeautifulSoup(pre_request_endpoint.message, 'html.parser') + # get the img tag with src attribute starting with data:image/svg+xml;base64, + img_tag = soup.find( + lambda tag: tag.name == 'img' and tag.get('src', '').startswith('data:image/svg+xml;base64,')) + assert img_tag + # get the src attribute + src = img_tag['src'] + # remove the data:image/svg+xml;base64, part + data = src.replace('data:image/svg+xml;base64,', '') + # decode the base64 data + decoded = base64.b64decode(data).decode("utf-8") + + svg = BeautifulSoup(decoded, features="xml") + assert svg + assert svg.find("svg") + assert svg.find_all("svg:rect") + + def test_pre_request_endpoint_mobile(self, context): internal_data = InternalData() context.http_headers = dict( - HTTP_USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36" + HTTP_USER_AGENT="Mozilla/5.0 (Linux; Android 10; SM-G960F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.92 Mobile Safari/537.36" ) pre_request_endpoint = self.backend.pre_request_endpoint( context, internal_data) + assert pre_request_endpoint + assert "302" in pre_request_endpoint.status - decoded = base64.b64decode( - pre_request_endpoint.message).decode("utf-8") - assert decoded.startswith("eudiw://authorize?") + assert f"{CONFIG['authorization']['url_scheme']}://authorize" in pre_request_endpoint.message unquoted = urllib.parse.unquote( - decoded, encoding='utf-8', errors='replace') + pre_request_endpoint.message, encoding='utf-8', errors='replace') parsed = urllib.parse.urlparse(unquoted) assert parsed.scheme == "eudiw" @@ -399,6 +415,7 @@ def test_redirect_endpoint(self, context): assert redirect_endpoint # TODO any additional checks after the backend returned the user attributes to satosa core + def test_request_endpoint(self, context): jwshelper = JWSHelper(PRIVATE_JWK) @@ -430,6 +447,11 @@ def test_request_endpoint(self, context): # TODO assertion su JWS decodificato # ... + header = unpad_jwt_header(msg["response"]) + payload = unpad_jwt_payload(msg["response"]) + print() + print("header", header) + print("payload", payload) def test_handle_error(self, context): error_message = "Error message!" diff --git a/requirements-dev.txt b/requirements-dev.txt index 951c2d28..44c2b593 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -6,3 +6,5 @@ isort autoflake bandit autopep8 +beautifulsoup4 +lxml