Repository navigation
Expand file tree
/
Copy pathprofile_integration_testing.py
More file actions
87 lines (72 loc) · 3.36 KB
/
Copy pathprofile_integration_testing.py
File metadata and controls
87 lines (72 loc) · 3.36 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
import unittest
from flask import session
from app import app, users_collection
class ProfileManagementTestCase(unittest.TestCase):
def setUp(self):
"""Set up test client and test database."""
self.app = app.test_client()
self.app.testing = True
# Clear previous entries in MongoDB collection
users_collection.delete_many({})
# Insert an existing user in the MongoDB collection for testing
self.existing_user = 'johndoe@gmail.com'
self.existing_user_data = {
'firstName': 'John',
'lastName': 'Doe',
'email': self.existing_user,
'password': 'password123'
}
users_collection.insert_one(self.existing_user_data)
# Test case for successful profile update
def test_successful_profile_update(self):
response = self.app.post(f'/profile/{self.existing_user}', data={
'firstName': 'Johnny',
'lastName': 'Doe',
'email': 'johnnydoe@gmail.com',
'password': 'newpassword123',
'confirmPass': 'newpassword123',
'action': 'Save Changes'
}, follow_redirects=True)
# Check response status
self.assertEqual(response.status_code, 200) # Expect 200 OK after redirection
# Query MongoDB for updated user information
updated_user = users_collection.find_one({"email": "johnnydoe@gmail.com"})
self.assertIsNotNone(updated_user) # Check if new email exists
self.assertEqual(updated_user['firstName'], 'Johnny')
self.assertEqual(updated_user['email'], 'johnnydoe@gmail.com')
self.assertEqual(updated_user['password'], 'newpassword123')
# Ensure the old email no longer exists in MongoDB
old_user = users_collection.find_one({"email": self.existing_user})
self.assertIsNone(old_user)
# Test case for when passwords do not match
def test_password_mismatch(self):
response = self.app.post(f'/profile/{self.existing_user}', data={
'firstName': 'Johnny',
'lastName': 'Doe',
'email': 'johnnydoe@gmail.com',
'password': 'newpassword123',
'confirmPass': 'wrongpassword123',
'action': 'Save Changes'
}, follow_redirects=True)
self.assertEqual(response.status_code, 200) # Expect 200 OK
self.assertIn(b"Passwords do not match", response.data)
# Ensure MongoDB data hasn't been altered
user = users_collection.find_one({"email": self.existing_user})
self.assertEqual(user['firstName'], 'John')
# Test case for missing fields (empty email)
def test_missing_field(self):
response = self.app.post(f'/profile/{self.existing_user}', data={
'firstName': 'Johnny',
'lastName': 'Doe',
'email': '',
'password': 'newpassword123',
'confirmPass': 'newpassword123',
'action': 'Save Changes'
}, follow_redirects=True)
self.assertEqual(response.status_code, 200) # Expect 200 OK
self.assertIn(b"All fields must be filled", response.data)
# Ensure MongoDB data hasn't been altered
user = users_collection.find_one({"email": self.existing_user})
self.assertEqual(user['firstName'], 'John')
if __name__ == '__main__':
unittest.main()