-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_tests.py
More file actions
107 lines (92 loc) · 3.98 KB
/
unit_tests.py
File metadata and controls
107 lines (92 loc) · 3.98 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
import os
import app
import unittest
import tempfile
class FlaskrTestCase(unittest.TestCase):
def setUp(self):
self.db_fd, app.app.config['DATABASE'] = tempfile.mkstemp()
app.app.testing = True
self.app = app.app.test_client()
with app.app.app_context():
app.init_db()
def tearDown(self):
os.close(self.db_fd)
os.unlink(app.app.config['DATABASE'])
def test_login_page_loads(self):
"""Test that the login page loads successfully with correct title."""
rv = self.app.get('/')
assert b'Jibi - The Comic Book Review Platform' in rv.data
assert b'Login' in rv.data
def test_register_user_page_loads(self):
"""Test that the registration page loads successfully with form fields."""
rv = self.app.get('/register_user')
assert b'Create Account' in rv.data
assert b'First name' in rv.data
assert b'Last name' in rv.data
assert b'Email' in rv.data
assert b'Username' in rv.data
assert b'Password' in rv.data
assert b'Confirm Password' in rv.data
def test_user_auth_success(self):
"""Test successful user authentication."""
self.test_add_user_success()
rv = self.app.post('/user_auth', data=dict(
username='johndoe',
password='password123' # Pass plain text password, hash it in the function
), follow_redirects=True)
assert b'You look good today' in rv.data # Assuming the feed page greets the user with "Welcome"
assert b'John' in rv.data # Assuming the feed page greets the user with "Welcome"
def test_user_auth_fail_mismatch_password(self):
"""Test failed user authentication with wrong password."""
rv = self.app.post('/user_auth', data=dict(
username='johndoe',
password='invalid_password'
))
assert b'Credentials do not match.' in rv.data
def test_user_auth_fail_mismatch_username(self):
"""Test failed user authentication with wrong username."""
rv = self.app.post('/user_auth', data=dict(
username='invalid_username',
password='password123'
))
assert b'Credentials do not match.' in rv.data
def test_add_user_success(self):
"""Test successful user registration."""
rv = self.app.post('/add_user', data=dict(
first_name='John',
last_name='Doe',
email='john.doe@example.com',
username='johndoe',
password='password123',
confirm_password='password123'
), follow_redirects=True)
assert b'Your new account has been created.' in rv.data
def test_add_user_password_mismatch(self):
"""Test user registration fails if passwords don't match."""
self.test_add_user_success() # adds a user with a given username
# add another user with the same username as the one in the function above
rv = self.app.post('/add_user', data=dict(
first_name='John',
last_name='Doe',
email='john.doe@example.com',
username='johndoe',
password='password123',
confirm_password='wrongpassword'
), follow_redirects=True)
assert b'Passwords must match.' in rv.data
def test_add_user_duplicate_username(self):
"""Test user registration fails if username already exists."""
# Flash message: Username already exists. Please try another one.
"""Test user registration fails if username already exists."""
self.test_add_user_success()
rv = self.app.post('/add_user', data=dict(
first_name='John',
last_name='Doe',
email='john.doe@example.com',
username='johndoe',
password='password123',
confirm_password='password123'
), follow_redirects=True)
assert b'Username already exists. Please try another one.' in rv.data
if __name__ == '__main__':
unittest.main()