forked from xiaomi388/comsw-4156-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user.py
137 lines (124 loc) · 4.45 KB
/
test_user.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
from flask import Flask
import db
from werkzeug.datastructures import ImmutableMultiDict
import unittest
import user
class TestUser(unittest.TestCase):
def test_register_happy_path(self):
db.clear()
db.init_db()
mock_form = ImmutableMultiDict([
('email', '[email protected]'),
('password', '12345678'),
('name', 'testtestuser'),
('mobile_phone', '8148888477'),
('zipcode', 10026)])
ret, code = user.register(mock_form)
self.assertEqual(code, 201)
self.assertEqual(ret, json.dumps({"error": ""}))
def test_register_input_invalid(self):
db.clear()
db.init_db()
mock_form = ImmutableMultiDict([
('email', '123columbia.edu'),
('password', '12345'),
('name', 'te'),
('mobile_phone', '8148888477'),
('zipcode', 10026)])
error, code = user.register(mock_form)
self.assertEqual(code, 400)
def test_register_sql_error(self):
db.clear()
db.init_db()
mock_form0 = ImmutableMultiDict([
('email', '[email protected]'),
('password', '12345678'),
('name', 'testtestuser'),
('mobile_phone', '8148888477'),
('zipcode', 10026)])
user.register(mock_form0)
mock_form = ImmutableMultiDict([
('email', '[email protected]'),
('password', '12345678'),
('name', 'testtestuser'),
('mobile_phone', '8148888477'),
('zipcode', 10026)])
error, code = user.register(mock_form)
self.assertEqual(code, 500)
def test_user_login_happy_path(self):
db.clear()
db.init_db()
db.insert_mock_user()
app = Flask(__name__)
with app.app_context():
mock_form = ImmutableMultiDict([
('email', '[email protected]'),
('password', 'password')])
resp, code, saved_user = user.user_login(mock_form)
self.assertEqual(saved_user is not None, True)
self.assertEqual(resp.data.decode("utf-8"), "{\"error\": \"\"}")
self.assertEqual(code, 200)
def test_user_login_no_such_email(self):
db.clear()
db.init_db()
db.insert_mock_user()
email = '[email protected]'
password = 'password'
mock_form = ImmutableMultiDict([
('email', email),
('password', password)])
resp, code, saved_user = user.user_login(mock_form)
self.assertEqual(saved_user, None)
self.assertEqual(resp, json.dumps({"error": f"No such email {email}"}))
self.assertEqual(code, 400)
def test_user_login_wrong_password(self):
db.clear()
db.init_db()
db.insert_mock_user()
email = "[email protected]"
password = "12345678900"
mock_form = ImmutableMultiDict([
('email', email),
('password', password)])
resp, code, saved_user = user.user_login(mock_form)
self.assertEqual(saved_user, None)
self.assertEqual(resp,
json.dumps({"error": f"wrong password {email}"}))
self.assertEqual(code, 400)
def test_user_login_input_invalid(self):
db.clear()
db.init_db()
db.insert_mock_user()
email = "[email protected]"
password = None
mock_form = ImmutableMultiDict([
('email', email),
('password', password)])
resp, code, saved_user = user.user_login(mock_form)
self.assertEqual(saved_user, None)
supposed_resp =\
json.dumps({"error": "invalid input"})
self.assertEqual(resp, supposed_resp)
self.assertEqual(code, 400)
def test_get_user_happy_path(self):
db.clear()
db.init_db()
db.insert_mock_user()
email = "[email protected]"
saved_user = user.UserLoginObj.get(email)
self.assertEqual(saved_user is not None, True)
def test_get_user_empty_email(self):
db.clear()
db.init_db()
db.insert_mock_user()
email = ""
saved_user = user.UserLoginObj.get(email)
self.assertEqual(saved_user, None)
def test_get_user_no_such_email(self):
db.clear()
db.init_db()
db.insert_mock_user()
email = "[email protected]"
saved_user = user.UserLoginObj.get(email)
self.assertEqual(saved_user, None)