-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user_views.py
67 lines (58 loc) · 2.15 KB
/
test_user_views.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
import unittest
from flask import json
from flask_jwt_extended import create_access_token
from api.v1.app import create_app
from models.user import User
from mongoengine import connect, disconnect
class UserViewTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Disconnect any existing connections
disconnect()
# Create a Flask app instance using the factory
cls.app = create_app(config_name='test')
# Create a test client
cls.client = cls.app.test_client()
@classmethod
def tearDownClass(cls):
# Drop the test database
disconnect()
def setUp(self):
# Clear the database before each test
User.drop_collection()
def test_register_user(self):
response = self.client.post('/api/v1/users', json={
'first_name': 'John',
'email': 'john@example.com',
'phone': '1234567890',
'password': 'password123'
})
self.assertEqual(response.status_code, 201)
data = json.loads(response.data)
self.assertIn('first_name', data)
self.assertIn('email', data)
def test_register_user_missing_field(self):
response = self.client.post('/api/v1/users', json={
'first_name': 'John',
'email': 'john@example.com',
'phone': '1234567890'
})
self.assertEqual(response.status_code, 400)
data = json.loads(response.data)
self.assertEqual(data['error'], 'Missing password')
def test_get_user(self):
response = self.client.post('/api/v1/users', json={
'first_name': 'John',
'email': 'john@example.com',
'phone': '1234567890',
'password': 'password123'
})
user = User.objects(email='john@example.com').first()
self.assertEqual(response.status_code, 201)
response = self.client.get(f'/api/v1/users/{user.id}')
self.assertEqual(response.status_code, 200)
data = json.loads(response.data)
self.assertIn('first_name', data)
self.assertIn('email', data)
if __name__ == '__main__':
unittest.main()