-
Notifications
You must be signed in to change notification settings - Fork 96
/
test_website.py
174 lines (137 loc) · 5.53 KB
/
test_website.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import logging
from collections import namedtuple
import pytest
import aiohttp.web
from sqlalchemy import select
from sqlalchemy.dialects.postgresql import insert as pg_insert
from pyslackersweb.website import tasks
from pyslackersweb.models import Source, domains
SlackInviteTestParam = namedtuple("SlackInviteTestParam", "response data expected")
async def test_endpoint_index(client):
r = await client.get("/")
assert r.history[0].url.path == "/"
assert r.history[0].status == 302
assert r.status == 200
assert r.url.path == "/web"
async def test_endpoint_slack(client):
r = await client.get("/web/slack")
assert r.status == 200
@pytest.mark.parametrize(
"slack_client,data,expected",
(
SlackInviteTestParam(
response={},
data={"email": "error@example.com", "agree_tos": True},
expected={"html": "successAlert", "domain": "example.com"},
),
SlackInviteTestParam(
response={},
data={"email": "error@EXAMPLE.COM", "agree_tos": True},
expected={"html": "successAlert", "domain": "example.com"},
),
SlackInviteTestParam(
response={},
data={"agree_tos": True},
expected={"html": "Missing data for required field"},
),
SlackInviteTestParam(
response={},
data={"email": "error@example.com", "agree_tos": False},
expected={"html": "There was an error processing your invite"},
),
SlackInviteTestParam(
response={},
data={"email": "foobar", "agree_tos": True},
expected={"html": "Not a valid email address"},
),
SlackInviteTestParam(
response={"body": {"ok": False, "error": "already_in_team"}},
data={"email": "error@example.com", "agree_tos": True},
expected={"html": "Reason: already_in_team", "domain": "example.com"},
),
SlackInviteTestParam(
response={"body": {"ok": False, "error": "already_invited"}},
data={"email": "error@example.com", "agree_tos": True},
expected={"html": "already_invited", "domain": "example.com"},
),
SlackInviteTestParam(
response={"body": {"ok": False, "error": "not_authed"}},
data={"email": "error@example.com", "agree_tos": True},
expected={"html": "Reason: not_authed", "domain": "example.com"},
),
SlackInviteTestParam(
response={"status": 500},
data={"email": "error@example.com", "agree_tos": True},
expected={"html": "Reason: Error contacting slack API", "domain": "example.com"},
),
),
indirect=["slack_client"],
)
async def test_endpoint_slack_invite(client, data, expected):
r = await client.post(path="/web/slack", data=data)
html = await r.text()
assert r.status == 200
assert expected["html"] in html
async with client.app["pg"].acquire() as conn:
rows = await conn.fetch(select([domains.c.blocked, domains.c.domain]))
if "domain" in expected:
assert len(rows) == 1
assert rows[0]["domain"] == expected["domain"]
else:
assert len(rows) == 0
@pytest.mark.parametrize(
"slack_client,data,expected",
(
SlackInviteTestParam(
response={}, data={"email": "foo@urhen.com", "agree_tos": True}, expected="successAlert"
),
SlackInviteTestParam(
response={}, data={"email": "foo@URHEN.com", "agree_tos": True}, expected="successAlert"
),
),
indirect=["slack_client"],
)
async def test_invite_banned_email_domain(client, data, expected):
async with client.app["pg"].acquire() as conn:
await conn.fetchrow(
pg_insert(domains)
.values(domain="urhen.com", blocked=True, source=Source.MANUAL)
.on_conflict_do_nothing(index_elements=[domains.c.domain])
)
r = await client.post(path="/web/slack", data=data)
html = await r.text()
assert r.status == 200
assert expected in html
client.app["subapps"]["website"][ # pylint: disable=protected-access
"slack_client"
]._request.assert_not_awaited()
@pytest.fixture
def disable_invites():
import pyslackersweb.website.settings
pyslackersweb.website.settings.DISABLE_INVITES = True
yield
pyslackersweb.website.settings.DISABLE_INVITES = False
async def test_disable_invites(client, disable_invites):
r = await client.get(path="/web/slack")
html = await r.text()
assert r.status == 200
assert "Invites are disabled at this time" in html
r = await client.post(path="/web/slack", data={"email": "foo@example.com", "agree_tos": True})
html = await r.text()
assert r.status == 200
assert "Invites are disabled at this time" in html
assert not client.app["slack_client"]._request.called
async def test_invite_use_legacy_client(client):
data = {"email": "error@example.com", "agree_tos": True}
del client.app["subapps"]["website"]["slack_client"]
r = await client.post(path="/web/slack", data=data)
assert r.status == 200
del client.app["subapps"]["website"]["slack_client_legacy"]
r = await client.post(path="/web/slack", data=data)
assert r.status == 500
async def test_task_sync_github_repositories(client, caplog):
async with aiohttp.ClientSession() as session:
result = await tasks.sync_github_repositories(session, client.app["redis"])
assert result
for record in caplog.records:
assert record.levelno <= logging.INFO