Repository navigation
Expand file tree
/
Copy pathtest_app.py
More file actions
137 lines (105 loc) · 4.62 KB
/
Copy pathtest_app.py
File metadata and controls
137 lines (105 loc) · 4.62 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import pytest
from app import app, build_auth_url
@pytest.fixture
def client():
"""Fixture que cria um cliente de teste Flask."""
app.config["TESTING"] = True
with app.test_client() as client:
yield client
class TestIndexRoute:
"""Testes para a rota GET / (formulario de client_id)."""
def test_index_returns_200(self, client):
response = client.get("/")
assert response.status_code == 200
def test_index_contains_form(self, client):
response = client.get("/")
html = response.data.decode("utf-8")
assert 'action="/authorize"' in html
assert 'name="client_id"' in html
class TestAuthorizeRoute:
"""Testes para a rota POST /authorize."""
def test_missing_client_id_returns_400(self, client):
response = client.post("/authorize", data={})
assert response.status_code == 400
assert b"obrigatorio" in response.data
def test_empty_client_id_returns_400(self, client):
response = client.post("/authorize", data={"client_id": " "})
assert response.status_code == 400
def test_valid_client_id_redirects(self, client):
response = client.post("/authorize", data={"client_id": "meu-client-123"})
assert response.status_code == 302
location = response.headers["Location"]
assert "accounts.tiny.com.br" in location
assert "client_id=meu-client-123" in location
assert "response_type=code" in location
assert "scope=openid" in location
assert "redirect_uri=" in location
assert "state=" in location
class TestCallbackRoute:
"""Testes para a rota GET /callback."""
def test_missing_code_returns_400(self, client):
response = client.get("/callback")
assert response.status_code == 400
assert b"codigo de autorizacao" in response.data.lower()
def test_error_param_returns_400(self, client):
response = client.get("/callback?error=access_denied")
assert response.status_code == 400
assert b"access_denied" in response.data
def test_error_with_description(self, client):
response = client.get(
"/callback?error=invalid_request&error_description=missing+client_id"
)
assert response.status_code == 400
assert b"invalid_request" in response.data
def test_valid_code_shows_code(self, client):
response = client.get("/callback?code=abc123xyz")
assert response.status_code == 200
assert b"abc123xyz" in response.data
def test_callback_error_has_text_plain_header(self, client):
response = client.get("/callback?error=access_denied")
assert response.content_type == "text/plain; charset=utf-8"
class TestBuildAuthUrl:
"""Testes para a funcao auxiliar build_auth_url."""
def test_returns_valid_url(self):
url = build_auth_url(
client_id="test-client",
redirect_uri="http://localhost/callback",
state="random-state",
)
assert url.startswith("https://accounts.tiny.com.br/")
assert "client_id=test-client" in url
assert "redirect_uri=http%3A%2F%2Flocalhost%2Fcallback" in url
assert "state=random-state" in url
assert "scope=openid" in url
assert "response_type=code" in url
def test_all_params_are_encoded(self):
url = build_auth_url(
client_id="id with spaces",
redirect_uri="http://host/path?x=1",
state="st/+=?",
)
assert "client_id=id+with+spaces" in url
assert "state=st%2F%2B%3D%3F" in url
class TestLlmsTxtRoute:
"""Testes para a rota GET /llms.txt."""
def test_llms_txt_returns_200(self, client):
response = client.get("/llms.txt")
assert response.status_code == 200
def test_llms_txt_content_type(self, client):
response = client.get("/llms.txt")
assert response.content_type == "text/plain; charset=utf-8"
def test_llms_txt_contains_project_name(self, client):
response = client.get("/llms.txt")
assert b"olist-tiny-code-auth" in response.data
class TestRobotsTxtRoute:
"""Testes para a rota GET /robots.txt."""
def test_robots_txt_returns_200(self, client):
response = client.get("/robots.txt")
assert response.status_code == 200
def test_robots_txt_content_type(self, client):
response = client.get("/robots.txt")
assert response.content_type == "text/plain; charset=utf-8"
def test_robots_txt_allows_all(self, client):
response = client.get("/robots.txt")
assert b"User-agent: *" in response.data
assert b"Allow: /" in response.data