2022-11-30 11:32:55 -05:00
|
|
|
"""Test_authentication."""
|
2022-12-12 15:43:19 -05:00
|
|
|
import base64
|
|
|
|
|
|
|
|
import jwt
|
2022-11-30 11:32:55 -05:00
|
|
|
from flask import Flask
|
|
|
|
from flask.testing import FlaskClient
|
|
|
|
from tests.spiffworkflow_backend.helpers.base_test import BaseTest
|
|
|
|
|
|
|
|
|
2022-12-05 10:46:26 -05:00
|
|
|
class TestFlaskOpenId(BaseTest):
|
2022-12-01 16:22:50 -05:00
|
|
|
"""An integrated Open ID that responds to openID requests.
|
|
|
|
|
|
|
|
By referencing a build in YAML file. Useful for
|
|
|
|
local development, testing, demos etc...
|
|
|
|
"""
|
2022-11-30 11:32:55 -05:00
|
|
|
|
2022-12-01 14:12:25 -05:00
|
|
|
def test_discovery_of_endpoints(
|
|
|
|
self,
|
|
|
|
app: Flask,
|
|
|
|
client: FlaskClient,
|
|
|
|
with_db_and_bpmn_file_cleanup: None,
|
|
|
|
) -> None:
|
2022-12-01 16:22:50 -05:00
|
|
|
"""Test discovery endpoints."""
|
2022-12-01 11:42:36 -05:00
|
|
|
response = client.get("/openid/.well-known/openid-configuration")
|
2022-11-30 11:32:55 -05:00
|
|
|
discovered_urls = response.json
|
|
|
|
assert "http://localhost/openid" == discovered_urls["issuer"]
|
2022-12-01 14:12:25 -05:00
|
|
|
assert (
|
|
|
|
"http://localhost/openid/auth" == discovered_urls["authorization_endpoint"]
|
|
|
|
)
|
2022-11-30 11:32:55 -05:00
|
|
|
assert "http://localhost/openid/token" == discovered_urls["token_endpoint"]
|
|
|
|
|
2022-12-01 14:12:25 -05:00
|
|
|
def test_get_login_page(
|
|
|
|
self,
|
|
|
|
app: Flask,
|
|
|
|
client: FlaskClient,
|
|
|
|
with_db_and_bpmn_file_cleanup: None,
|
|
|
|
) -> None:
|
2022-12-01 16:22:50 -05:00
|
|
|
"""It should be possible to get to a login page."""
|
2022-12-01 14:12:25 -05:00
|
|
|
data = {"state": {"bubblegum": 1, "daydream": 2}}
|
2022-11-30 11:32:55 -05:00
|
|
|
response = client.get("/openid/auth", query_string=data)
|
2022-12-01 15:01:25 -05:00
|
|
|
assert b"<h2>Login</h2>" in response.data
|
2022-11-30 11:32:55 -05:00
|
|
|
assert b"bubblegum" in response.data
|
|
|
|
|
2022-12-01 14:12:25 -05:00
|
|
|
def test_get_token(
|
|
|
|
self,
|
2022-11-30 11:32:55 -05:00
|
|
|
app: Flask,
|
|
|
|
client: FlaskClient,
|
2022-12-01 14:12:25 -05:00
|
|
|
with_db_and_bpmn_file_cleanup: None,
|
|
|
|
) -> None:
|
2022-12-20 15:47:30 -05:00
|
|
|
"""Test_get_token."""
|
|
|
|
code = "testadmin1:1234123412341234"
|
2022-12-12 15:43:19 -05:00
|
|
|
|
2022-12-01 16:22:50 -05:00
|
|
|
"""It should be possible to get a token."""
|
2022-12-12 15:43:19 -05:00
|
|
|
backend_basic_auth_string = code
|
|
|
|
backend_basic_auth_bytes = bytes(backend_basic_auth_string, encoding="ascii")
|
|
|
|
backend_basic_auth = base64.b64encode(backend_basic_auth_bytes)
|
2022-11-30 11:32:55 -05:00
|
|
|
headers = {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
2022-12-12 15:43:19 -05:00
|
|
|
"Authorization": f"Basic {backend_basic_auth.decode('utf-8')}",
|
2022-11-30 11:32:55 -05:00
|
|
|
}
|
2022-12-01 14:12:25 -05:00
|
|
|
data = {
|
|
|
|
"grant_type": "authorization_code",
|
2022-11-30 11:32:55 -05:00
|
|
|
"code": code,
|
2022-12-01 14:12:25 -05:00
|
|
|
"redirect_url": "http://localhost:7000/v1.0/login_return",
|
2022-11-30 11:32:55 -05:00
|
|
|
}
|
|
|
|
response = client.post("/openid/token", data=data, headers=headers)
|
|
|
|
assert response
|
2022-12-12 15:43:19 -05:00
|
|
|
assert response.is_json
|
2022-12-20 15:47:30 -05:00
|
|
|
assert "access_token" in response.json
|
|
|
|
assert "id_token" in response.json
|
|
|
|
assert "refresh_token" in response.json
|
2022-12-13 08:14:44 -05:00
|
|
|
|
2022-12-20 15:47:30 -05:00
|
|
|
decoded_token = jwt.decode(
|
|
|
|
response.json["id_token"], options={"verify_signature": False}
|
|
|
|
)
|
|
|
|
assert "iss" in decoded_token
|
|
|
|
assert "email" in decoded_token
|