-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_auth.py
78 lines (62 loc) · 2.82 KB
/
test_auth.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
import unittest
from dataclasses import dataclass
from typing import ByteString
from flask import Flask
from flask.testing import FlaskClient
from flask import g, session
from example.flaskr.db import get_db
from example.tests.conftest import AuthActions, TestBase
@dataclass
class AuthParameters:
username: str
password: str
message: ByteString
class TestAuth(TestBase):
def test_register(self, app: Flask, client: FlaskClient):
# test that viewing the page renders without template errors
self.assertStatus(client.get("/auth/register"), 200)
# test that successful registration redirects to the login page
response = client.post("/auth/register", data={"username": "a", "password": "a"})
self.assertLocationHeader(response, "http://localhost/auth/login")
# test that the user was inserted into the database
with app.app_context():
self.assertIsNotNone(get_db().execute("select * from user where username = 'a'").fetchone())
def test_register_validate_input(self, _, client: FlaskClient):
authparams = [
AuthParameters("", "", b"Username is required."),
AuthParameters("a", "", b"Password is required."),
AuthParameters("test", "test", b"already registered")
]
for authparam in authparams:
username, password, message = vars(authparam).values()
response = client.post("/auth/register", data={"username": username, "password": password})
self.assertInResponse(message, response)
def test_login(self, _, client: FlaskClient):
# test that viewing the page renders without template errors
self.assertStatus(client.get("/auth/login"), 200)
# test that successful login redirects to the index page
auth = AuthActions(client)
response = auth.login()
self.assertLocationHeader(response, "http://localhost/")
# login request set the user_id in the session
# check that the user is loaded from the session
client.get("/")
self.assertEqual(session["user_id"], 1)
self.assertEqual(g.user["username"], "test")
def test_login_validate_input(self, _, client: FlaskClient):
auth = AuthActions(client)
authparams = [
AuthParameters("a", "test", b"Incorrect username."),
AuthParameters("test", "a", b"Incorrect password.")
]
for authparam in authparams:
username, password, message = vars(authparam).values()
response = auth.login(username, password)
self.assertInResponse(message, response)
def test_logout(self, _, client: FlaskClient):
auth = AuthActions(client)
auth.login()
auth.logout()
self.assertNotIn("user_id", session)
if __name__ == '__main__':
unittest.main()