-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_auth.py
107 lines (91 loc) · 3.7 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
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 unittest
from flask import url_for
from app import create_app, db
from app.models import User
class AuthTestCase(unittest.TestCase):
def setUp(self):
self.app = create_app({
'TESTING': True,
'WTF_CSRF_ENABLED': False,
'SQLALCHEMY_DATABASE_URI': 'sqlite:///:memory:',
'SECRET_KEY': 'test_secret_key'
})
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
self.client = self.app.test_client()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def test_register_and_login(self):
# Test registration
response = self.client.post('/auth/register', data={
'username': 'testuser',
'email': '[email protected]',
'password': 'password'
}, follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(
b'Congratulations, you are now a registered user!', response.data)
# Test login
response = self.client.post('/auth/login', data={
'email': '[email protected]',
'password': 'password'
}, follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Logged in successfully.', response.data)
# Test accessing a protected page
response = self.client.get('/dashboard', follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Welcome to Your StockWatch Dashboard', response.data)
def test_logout(self):
# Register a user
self.client.post('/auth/register', data={
'username': 'testuser',
'email': '[email protected]',
'password': 'password'
}, follow_redirects=True)
# Login
self.client.post('/auth/login', data={
'email': '[email protected]',
'password': 'password'
}, follow_redirects=True)
# Test logout
response = self.client.get('/auth/logout', follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'You have been logged out.', response.data)
# Verify that accessing a protected page redirects to login
response = self.client.get('/dashboard', follow_redirects=True)
self.assertIn(b'Please log in to access this page', response.data)
def test_login_with_incorrect_password(self):
# Register a user
self.client.post('/auth/register', data={
'username': 'testuser',
'email': '[email protected]',
'password': 'password'
}, follow_redirects=True)
# Attempt to login with incorrect password
response = self.client.post('/auth/login', data={
'email': '[email protected]',
'password': 'wrongpassword'
}, follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Invalid username or password', response.data)
def test_register_existing_user(self):
# Register a user
self.client.post('/auth/register', data={
'username': 'testuser',
'email': '[email protected]',
'password': 'password'
}, follow_redirects=True)
# Attempt to register the same user again
response = self.client.post('/auth/register', data={
'username': 'testuser',
'email': '[email protected]',
'password': 'password'
}, follow_redirects=True)
self.assertEqual(response.status_code, 200)
self.assertIn(b'Email address already in use', response.data)
if __name__ == '__main__':
unittest.main()