-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtests.py
99 lines (83 loc) · 2.26 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
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
# -*- encoding: utf-8 -*-
"""
Copyright (c) 2019 - present AppSeed.us
"""
import pytest
import json
from api import app
"""
Sample test data
"""
DUMMY_USERNAME = "apple"
DUMMY_EMAIL = "apple@apple.com"
DUMMY_PASS = "newpassword"
@pytest.fixture
def client():
with app.test_client() as client:
yield client
def test_user_signup(client):
"""
Tests /users/register API
"""
response = client.post(
"api/users/register",
data=json.dumps(
{
"username": DUMMY_USERNAME,
"email": DUMMY_EMAIL,
"password": DUMMY_PASS
}
),
content_type="application/json")
data = json.loads(response.data.decode())
assert response.status_code == 200
assert "The user was successfully registered" in data["msg"]
def test_user_signup_invalid_data(client):
"""
Tests /users/register API: invalid data like email field empty
"""
response = client.post(
"api/users/register",
data=json.dumps(
{
"username": DUMMY_USERNAME,
"email": "",
"password": DUMMY_PASS
}
),
content_type="application/json")
data = json.loads(response.data.decode())
assert response.status_code == 400
assert "'' is too short" in data["msg"]
def test_user_login_correct(client):
"""
Tests /users/signup API: Correct credentials
"""
response = client.post(
"api/users/login",
data=json.dumps(
{
"email": DUMMY_EMAIL,
"password": DUMMY_PASS
}
),
content_type="application/json")
data = json.loads(response.data.decode())
assert response.status_code == 200
assert data["token"] != ""
def test_user_login_error(client):
"""
Tests /users/signup API: Wrong credentials
"""
response = client.post(
"api/users/login",
data=json.dumps(
{
"email": DUMMY_EMAIL,
"password": DUMMY_EMAIL
}
),
content_type="application/json")
data = json.loads(response.data.decode())
assert response.status_code == 400
assert "Wrong credentials." in data["msg"]