Skip to content

Commit

Permalink
Update Backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
salvatorelaiso committed Jul 27, 2023
1 parent 941a425 commit ded3cb8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
48 changes: 35 additions & 13 deletions pyeudiw/tests/satosa/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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!"
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ isort
autoflake
bandit
autopep8
beautifulsoup4
lxml

0 comments on commit ded3cb8

Please sign in to comment.