-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
53 lines (46 loc) · 2.16 KB
/
tests.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
import unittest
import json
from auth_app import app
from test_users_repo import TestUsersRepo
class AuthTest(unittest.TestCase):
def setUp(self):
self.repo = TestUsersRepo()
self.client = app.test_client(self)
def test_user_creation(self):
email = '[email protected]'
response = self.client.post('/register',
data=json.dumps({'email': email, 'password': '123'}),
content_type='application/json')
assert response.status_code == 201
from_db = self.repo.find(email)
assert from_db is not None
def test_user_creation_with_invalid_email(self):
email = 'mail'
response = self.client.post('/register',
data=json.dumps({'email': email, 'password': '123'}),
content_type='application/json')
assert response.status_code == 400
from_db = self.repo.find(email)
assert from_db is None
def test_login_with_correct_data_returns_token(self):
email = '[email protected]'
self.client.post('/register',
data=json.dumps({'email': email, 'password': '123'}),
content_type='application/json')
response = self.client.post('/login',
data=json.dumps({'email': email, 'password': '123'}),
content_type='application/json')
assert response.status_code == 200
assert 'token' in str(response.data)
def test_login_with_incorrect_data_returns_unauthorized(self):
email = '[email protected]'
self.client.post('/register',
data=json.dumps({'email': email, 'password': '123'}),
content_type='application/json')
response = self.client.post('/login',
data=json.dumps({'email': email, 'password': 'wrong_password'}),
content_type='application/json')
assert response.status_code == 401
assert len(response.data) == 0
def tearDown(self):
self.repo.delete_all()