-
Notifications
You must be signed in to change notification settings - Fork 54
/
unit_test.py
61 lines (47 loc) · 2.7 KB
/
unit_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
import unittest
from flask import session
from main import app
class FlaskAppTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
self.app.testing = True
def test_login(self):
with self.app as client:
# Perform a POST request to the login endpoint with valid credentials
response = client.post('/pythonlogin/', data=dict(username='testuser', password='testpassword'))
self.assertEqual(response.status_code, 302) # expect a redirect response
# Ensure session variables are set correctly
with client.session_transaction() as sess:
self.assertTrue(sess['loggedin'])
self.assertEqual(sess['username'], 'testuser')
def test_register(self):
with self.app as client:
# Perform a POST request to the registration endpoint with valid form data
response = client.post('/pythonlogin/register', data=dict(username='newuser', password='newpassword', email='test@example.com'))
self.assertEqual(response.status_code, 302) # expect a redirect response
# Ensure account was successfully registered
# You can check the database or perform a login request to verify the registration
def test_home(self):
with self.app as client:
# Perform a GET request to the home endpoint without logging in
response = client.get('/')
self.assertEqual(response.status_code, 302) # expect a redirect response
# Log in the user
client.post('/pythonlogin/', data=dict(username='testuser', password='testpassword'))
# Perform a GET request to the home endpoint after logging in
response = client.get('/')
self.assertEqual(response.status_code, 200) # expect a successful response
self.assertIn(b'Home Page', response.data) # expect 'Home Page' to be in the response content
def test_profile(self):
with self.app as client:
# Perform a GET request to the profile endpoint without logging in
response = client.get('/profile')
self.assertEqual(response.status_code, 302) # expect a redirect response
# Log in the user
client.post('/pythonlogin/', data=dict(username='testuser', password='testpassword'))
# Perform a GET request to the profile endpoint after logging in
response = client.get('/profile')
self.assertEqual(response.status_code, 200) # expect a successful response
self.assertIn(b'Profile Page', response.data) # expect 'Profile Page' to be in the response content
if __name__ == '__main__':
unittest.main()