-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_example_basic.py
93 lines (75 loc) · 2.43 KB
/
test_example_basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import,
division,
print_function,
unicode_literals
)
import io
import json
import os
import pytest
import requests
from flask import url_for
from example_basic import app
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
@pytest.yield_fixture(scope="session")
def test_app():
"""
Create test context for app and yield test client.
"""
app.config.update({
"SERVER_NAME": "localhost:8000",
"TESTING": True,
"DEBUG": False
})
with app.test_request_context():
yield app.test_client()
@pytest.fixture
def mocked_token_endpoint(monkeypatch):
"""
Mocked Token endpoint.
"""
def fetch_token(self, token_url, code=None, authorization_response=None,
body='', auth=None, username=None, password=None,
method='POST', timeout=None, headers=None, verify=True,
**kwargs):
app.logger.info("Token endpoint called at %s", token_url)
return {"Foo": "Bar"}
monkeypatch.setattr(
"requests_oauthlib.OAuth2Session.fetch_token", fetch_token)
@pytest.fixture
def mocked_userinfo_endpoint(monkeypatch):
"""
Mocked UserInfo endpoint.
"""
def mock_request(self, method, url, *args, **kwargs):
app.logger.info("UserInfo endpoint called at %s", url)
userinfo = {"name": "Demo User"}
response = requests.Response()
response.status_code = 200
response.raw = io.BytesIO(json.dumps(userinfo).encode("utf-8"))
return response
monkeypatch.setattr("requests.sessions.Session.request", mock_request)
def test_index(test_app):
"""
Test if index redirects to provider correctly.
"""
# check we are redirected to provider
res = test_app.get(url_for("index"))
assert res.status_code == 302
# load provider authorization page without an error
url = res.headers.get("Location")
res = requests.get(url, allow_redirects=1)
assert res.status_code == 200
assert res.headers["Aq-Session-Uri"]
def test_authorized(test_app, mocked_token_endpoint, mocked_userinfo_endpoint):
"""
Test if app displays the user information after authorization.
"""
url = url_for("authorized", code="1234")
res = test_app.get(url)
assert res.status_code == 200
data = json.loads(res.data.decode("utf-8"))
assert data["name"] == "Demo User"