-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth_test.py
93 lines (74 loc) · 2.79 KB
/
auth_test.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
import sqlite3
import unittest
from unittest import mock
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
import realtimechess
class TestAuthDebug(AioHTTPTestCase):
async def get_application(self):
return realtimechess.make_app(True)
@unittest_run_loop
async def test_authenticated_checks_logged_in(self):
response = await self.client.post("/newgame", data={})
assert response.status == 403
@unittest_run_loop
async def test_login_needs_name(self):
response = await self.client.post("/anonymous_login", data={})
assert response.status == 400
assert "Need name" in await response.text()
@unittest_run_loop
async def test_login_invalid_name(self):
response = await self.client.post(
"/anonymous_login", data={"name": "as-d%!¤"})
assert response.status == 400
assert "Invalid name" in await response.text()
@unittest_run_loop
async def test_valid_password_but_user_not_present(self):
cookies = {}
cookies["name"] = "Petter"
cookies["password"] = self.app["user_manager"]._password("Petter")
self.client.session.cookie_jar.update_cookies(cookies)
response = await self.client.get("/")
assert response.status == 200
assert "/?g=" in str(response.url)
@unittest_run_loop
async def test_invalid_password(self):
cookies = {}
cookies["name"] = "Petter2"
cookies["password"] = "xyz"
self.client.session.cookie_jar.update_cookies(cookies)
response = await self.client.get("/")
assert response.status == 200
# Redirected to login page.
assert "/loginpage" in str(response.url)
@unittest_run_loop
async def test_user_already_exists(self):
data = {"name": "Petter3"}
response = await self.client.post("/anonymous_login", data=data)
assert response.status == 200
self.client.session.cookie_jar.clear()
response = await self.client.post("/anonymous_login", data=data)
assert response.status == 200
class TestAuthNoDebug(AioHTTPTestCase):
async def get_application(self):
real_sqlite3_connect = sqlite3.connect
with mock.patch("sqlite3.connect", autospec=True) as mock_connect:
self.mock_connect = mock_connect
self.mock_connect.return_value = real_sqlite3_connect(":memory:")
return realtimechess.make_app(False)
@unittest_run_loop
async def test_real_db(self):
self.mock_connect.assert_called_with("auth.db")
@unittest_run_loop
async def test_setdebug_not_present(self):
response = await self.client.post("/setdebug", data={})
assert response.status == 404
@unittest_run_loop
async def test_user_already_exists(self):
data = {"name": "Petter3"}
response = await self.client.post("/anonymous_login", data=data)
assert response.status == 200
self.client.session.cookie_jar.clear()
response = await self.client.post("/anonymous_login", data=data)
assert response.status == 401
if __name__ == '__main__':
unittest.main()