Skip to content

Commit 351b585

Browse files
committed
test: Add tests for thirdparty parsing
1 parent 550c126 commit 351b585

File tree

4 files changed

+150
-3
lines changed

4 files changed

+150
-3
lines changed

tests/Django/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@
4444
}
4545

4646
MIDDLEWARE = [
47-
"supertokens-Fastapi.core.framework.django.django_middleware.middleware",
47+
"supertokens_python.framework.django.django_middleware.middleware",
4848
]

tests/Django/test_django.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@
1313
# under the License.
1414

1515
import json
16+
from urllib.parse import urlencode
1617
from datetime import datetime
1718
from inspect import isawaitable
1819
from typing import Any, Dict, Union
1920

2021
from django.http import HttpRequest, HttpResponse, JsonResponse
2122
from django.test import RequestFactory, TestCase
23+
2224
from supertokens_python import InputAppInfo, SupertokensConfig, init
2325
from supertokens_python.framework.django import middleware
2426
from supertokens_python.framework.django.django_response import (
2527
DjangoResponse as SuperTokensDjangoWrapper,
2628
)
27-
from supertokens_python.recipe import emailpassword, session
29+
from supertokens_python.recipe import emailpassword, session, thirdparty
2830
from supertokens_python.recipe.emailpassword.interfaces import APIInterface, APIOptions
2931
from supertokens_python.recipe.session import SessionContainer
3032
from supertokens_python.recipe.session.asyncio import (
@@ -393,6 +395,56 @@ async def test_optional_session(self):
393395
dict_response = json.loads(response.content)
394396
assert dict_response["s"] == "empty session"
395397

398+
async def test_thirdparty_parsing_works(self):
399+
init(
400+
supertokens_config=SupertokensConfig("http://localhost:3567"),
401+
app_info=InputAppInfo(
402+
app_name="SuperTokens Demo",
403+
api_domain="http://api.supertokens.io",
404+
website_domain="http://supertokens.io",
405+
api_base_path="/auth",
406+
),
407+
framework="django",
408+
mode="asgi",
409+
recipe_list=[
410+
thirdparty.init(
411+
sign_in_and_up_feature=thirdparty.SignInAndUpFeature(
412+
providers=[
413+
thirdparty.Apple(
414+
client_id="4398792-io.supertokens.example.service",
415+
client_key_id="7M48Y4RYDL",
416+
client_team_id="YWQCXGJRJL",
417+
client_private_key="-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgu8gXs+XYkqXD6Ala9Sf/iJXzhbwcoG5dMh1OonpdJUmgCgYIKoZIzj0DAQehRANCAASfrvlFbFCYqn3I2zeknYXLwtH30JuOKestDbSfZYxZNMqhF/OzdZFTV0zc5u5s3eN+oCWbnvl0hM+9IW0UlkdA\n-----END PRIVATE KEY-----",
418+
)
419+
]
420+
)
421+
),
422+
],
423+
)
424+
425+
start_st()
426+
427+
data = {
428+
"state": "afc596274293e1587315c",
429+
"code": "c7685e261f98e4b3b94e34b3a69ff9cf4.0.rvxt.eE8rO__6hGoqaX1B7ODPmA",
430+
}
431+
432+
request = self.factory.post(
433+
"/auth/callback/apple",
434+
urlencode(data).encode(),
435+
content_type="application/x-www-form-urlencoded",
436+
)
437+
temp = middleware(custom_response_view)(request)
438+
if not isawaitable(temp):
439+
raise Exception("Should never come here")
440+
response = await temp
441+
442+
self.assertEqual(response.status_code, 200)
443+
self.assertEqual(
444+
response.content,
445+
b'<html><head><script>window.location.replace("http://supertokens.io/auth/callback/apple?state=afc596274293e1587315c&code=c7685e261f98e4b3b94e34b3a69ff9cf4.0.rvxt.eE8rO__6hGoqaX1B7ODPmA");</script></head></html>',
446+
)
447+
396448

397449
def test_remove_header_works():
398450
response = HttpResponse()

tests/Flask/test_flask.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from flask import Flask, g, jsonify, make_response, request
2020
from supertokens_python import InputAppInfo, SupertokensConfig, init
2121
from supertokens_python.framework.flask import Middleware
22-
from supertokens_python.recipe import emailpassword, session
22+
from supertokens_python.recipe import emailpassword, session, thirdparty
2323
from supertokens_python.recipe.emailpassword.interfaces import APIInterface, APIOptions
2424
from supertokens_python.recipe.session import SessionContainer
2525
from supertokens_python.recipe.session.framework.flask import verify_session
@@ -108,6 +108,18 @@ async def email_exists_get(
108108
apis=override_email_password_apis
109109
)
110110
),
111+
thirdparty.init(
112+
sign_in_and_up_feature=thirdparty.SignInAndUpFeature(
113+
providers=[
114+
thirdparty.Apple(
115+
client_id="4398792-io.supertokens.example.service",
116+
client_key_id="7M48Y4RYDL",
117+
client_team_id="YWQCXGJRJL",
118+
client_private_key="-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgu8gXs+XYkqXD6Ala9Sf/iJXzhbwcoG5dMh1OonpdJUmgCgYIKoZIzj0DAQehRANCAASfrvlFbFCYqn3I2zeknYXLwtH30JuOKestDbSfZYxZNMqhF/OzdZFTV0zc5u5s3eN+oCWbnvl0hM+9IW0UlkdA\n-----END PRIVATE KEY-----",
119+
)
120+
]
121+
)
122+
),
111123
],
112124
)
113125

@@ -400,6 +412,23 @@ def test_optional_session(driver_config_app: Any):
400412
assert dict_response["s"] == "empty session"
401413

402414

415+
def test_thirdparty_parsing_works(driver_config_app: Any):
416+
start_st()
417+
418+
test_client = driver_config_app.test_client()
419+
data = {
420+
"state": "afc596274293e1587315c",
421+
"code": "c7685e261f98e4b3b94e34b3a69ff9cf4.0.rvxt.eE8rO__6hGoqaX1B7ODPmA",
422+
}
423+
response = test_client.post("/auth/callback/apple", data=data)
424+
425+
assert response.status_code == 200
426+
assert (
427+
response.data
428+
== b'<html><head><script>window.location.replace("http://supertokens.io/auth/callback/apple?state=afc596274293e1587315c&code=c7685e261f98e4b3b94e34b3a69ff9cf4.0.rvxt.eE8rO__6hGoqaX1B7ODPmA");</script></head></html>'
429+
)
430+
431+
403432
from flask.wrappers import Response
404433
from supertokens_python.framework.flask.flask_response import (
405434
FlaskResponse as SupertokensFlaskWrapper,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from pytest import fixture, mark
2+
from fastapi import FastAPI
3+
from supertokens_python.framework.fastapi import get_middleware
4+
from starlette.testclient import TestClient
5+
6+
from supertokens_python.recipe import session, thirdparty
7+
from supertokens_python import init
8+
9+
from tests.utils import (
10+
setup_function,
11+
teardown_function,
12+
start_st,
13+
st_init_common_args,
14+
)
15+
16+
17+
_ = setup_function # type:ignore
18+
_ = teardown_function # type:ignore
19+
_ = start_st # type:ignore
20+
21+
22+
pytestmark = mark.asyncio
23+
24+
25+
@fixture(scope="function")
26+
async def fastapi_client():
27+
app = FastAPI()
28+
app.add_middleware(get_middleware())
29+
30+
return TestClient(app)
31+
32+
33+
async def test_thirdpary_parsing_works(fastapi_client: TestClient):
34+
st_init_args = {
35+
**st_init_common_args,
36+
"recipe_list": [
37+
session.init(),
38+
thirdparty.init(
39+
sign_in_and_up_feature=thirdparty.SignInAndUpFeature(
40+
providers=[
41+
thirdparty.Apple(
42+
client_id="4398792-io.supertokens.example.service",
43+
client_key_id="7M48Y4RYDL",
44+
client_team_id="YWQCXGJRJL",
45+
client_private_key="-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgu8gXs+XYkqXD6Ala9Sf/iJXzhbwcoG5dMh1OonpdJUmgCgYIKoZIzj0DAQehRANCAASfrvlFbFCYqn3I2zeknYXLwtH30JuOKestDbSfZYxZNMqhF/OzdZFTV0zc5u5s3eN+oCWbnvl0hM+9IW0UlkdA\n-----END PRIVATE KEY-----",
46+
)
47+
]
48+
)
49+
),
50+
],
51+
}
52+
init(**st_init_args) # type: ignore
53+
start_st()
54+
55+
data = {
56+
"state": "afc596274293e1587315c",
57+
"code": "c7685e261f98e4b3b94e34b3a69ff9cf4.0.rvxt.eE8rO__6hGoqaX1B7ODPmA",
58+
}
59+
60+
res = fastapi_client.post("/auth/callback/apple", data=data)
61+
62+
assert res.status_code == 200
63+
assert (
64+
res.content
65+
== b'<html><head><script>window.location.replace("http://supertokens.io/auth/callback/apple?state=afc596274293e1587315c&code=c7685e261f98e4b3b94e34b3a69ff9cf4.0.rvxt.eE8rO__6hGoqaX1B7ODPmA");</script></head></html>'
66+
)

0 commit comments

Comments
 (0)