Skip to content

Commit 0467abd

Browse files
committed
response structure fix, name->username bug fix
1 parent cc54f15 commit 0467abd

File tree

3 files changed

+39
-31
lines changed

3 files changed

+39
-31
lines changed

api/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
class Users(db.Model):
1515
id = db.Column(db.Integer(), primary_key=True)
16-
name = db.Column(db.String(32), nullable=False)
16+
username = db.Column(db.String(32), nullable=False)
1717
email = db.Column(db.String(64), nullable=False)
1818
password = db.Column(db.Text())
1919
date_joined = db.Column(db.DateTime(), default=datetime.utcnow)
2020

2121
def __repr__(self):
22-
return f"User {self.name}"
22+
return f"User {self.username}"
2323

2424
def save(self):
2525
db.session.add(self)
@@ -31,8 +31,8 @@ def set_password(self, password):
3131
def check_password(self, password):
3232
return check_password_hash(self.password, password)
3333

34-
def update_name(self, new_name):
35-
self.name = new_name
34+
def update_username(self, new_username):
35+
self.username = new_username
3636

3737
def update_email(self, new_email):
3838
self.email = new_email

api/routes.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def check_if_token_revoked(jwt_header, jwt_payload):
3030
Flask-Restx models for api request and response data
3131
"""
3232

33-
signup_model = rest_api.model('SignUpModel', {"name": fields.String(required=True, min_length=2, max_length=32),
33+
signup_model = rest_api.model('SignUpModel', {"username": fields.String(required=True, min_length=2, max_length=32),
3434
"email": fields.String(required=True, min_length=4, max_length=64),
3535
"password": fields.String(required=True, min_length=4, max_length=16)
3636
})
@@ -39,7 +39,7 @@ def check_if_token_revoked(jwt_header, jwt_payload):
3939
"password": fields.String(required=True, min_length=4, max_length=16)
4040
})
4141

42-
user_edit_model = rest_api.model('UserEditModel', {"name": fields.String(required=True, min_length=2, max_length=32),
42+
user_edit_model = rest_api.model('UserEditModel', {"username": fields.String(required=True, min_length=2, max_length=32),
4343
"password": fields.String(required=True, min_length=4, max_length=16)
4444
})
4545

@@ -59,20 +59,22 @@ def post(self):
5959

6060
req_data = request.get_json()
6161

62-
_name = req_data.get("name")
62+
_username = req_data.get("username")
6363
_email = req_data.get("email")
6464
_password = req_data.get("password")
6565

6666
user_exists = Users.get_by_email(_email)
6767
if user_exists:
68-
abort(400, "Sorry. This email already exists.")
68+
return {"success": False,
69+
"msg": "Sorry. This email already exists."}, 400
6970

70-
new_user = Users(name=_name, email=_email)
71+
new_user = Users(username=_username, email=_email)
7172

7273
new_user.set_password(_password)
7374
new_user.save()
7475

75-
return {'message': 'User with (%s, %s) created successfully!' % (_name, _email)}, 201
76+
return {"success": True,
77+
"msg": "User with (%s, %s) created successfully!" % (_username, _email)}, 201
7678

7779

7880
@rest_api.route('/api/users/login')
@@ -92,10 +94,12 @@ def post(self):
9294
user_exists = Users.get_by_email(_email)
9395

9496
if not user_exists:
95-
abort(400, "Sorry. This email does not exist.")
97+
return {"success": False,
98+
"msg": "Sorry. This email does not exist."}, 400
9699

97100
if not user_exists.check_password(_password):
98-
abort(400, "Sorry. Wrong credentials.")
101+
return {"success": False,
102+
"msg": "Sorry. Wrong credentials."}, 400
99103

100104
# create access token uwing JWT
101105
access_token = create_access_token(identity=_email)
@@ -106,7 +110,7 @@ def post(self):
106110
@rest_api.route('/api/users/edit')
107111
class EditUser(Resource):
108112
"""
109-
Edits User's name or password or both using 'user_edit_model' input
113+
Edits User's username or password or both using 'user_edit_model' input
110114
"""
111115

112116
@rest_api.expect(user_edit_model)
@@ -117,28 +121,30 @@ def post(self):
117121
current_user = Users.get_by_email(user_email)
118122

119123
if not current_user:
120-
abort(400, "Sorry. Wrong auth token. This user does not exist.")
124+
return {"success": False,
125+
"msg": "Sorry. Wrong auth token. This user does not exist."}, 400
121126

122127
req_data = request.get_json()
123128

124-
_new_name = req_data.get("name")
129+
_new_username = req_data.get("username")
125130
_new_password = req_data.get("password")
126131

127-
if _new_name:
128-
current_user.update_name(_new_name)
132+
if _new_username:
133+
current_user.update_username(_new_username)
129134

130135
if _new_password:
131136
current_user.set_password(_new_password)
132137

133138
current_user.save()
134139

135-
return {"message": 'User details updated successfully!'}, 200
140+
return {"success": True,
141+
"msg": "User details updated successfully!"}, 200
136142

137143

138144
@rest_api.route('/api/users/logout')
139145
class LogoutUser(Resource):
140146
"""
141-
Edits User's name or password or both using 'user_edit_model' input
147+
Edits User's username or password or both using 'user_edit_model' input
142148
"""
143149

144150
@rest_api.expect(user_edit_model)
@@ -149,9 +155,11 @@ def delete(self):
149155
current_user = Users.get_by_email(user_email)
150156

151157
if not current_user:
152-
abort(400, "Sorry. Wrong auth token")
158+
return {"success": False,
159+
"msg": "Sorry. Wrong auth token"}, 400
153160

154161
jwt_block = JWTTokenBlocklist(jti=get_jwt()["jti"], created_at=datetime.now(timezone.utc))
155162
jwt_block.save()
156163

157-
return {"message": "JWT Token revoked successfully!"}, 200
164+
return {"success": True,
165+
"msg": "JWT Token revoked successfully!"}, 200

tests.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Sample test data
1515
"""
1616

17-
DUMMY_NAME = "apple"
17+
DUMMY_USERNAME = "apple"
1818
DUMMY_EMAIL = "[email protected]"
1919
DUMMY_PASS = "newpassword"
2020

@@ -30,10 +30,10 @@ def test_user_signup(client):
3030
Tests /users/signup API
3131
"""
3232
response = client.post(
33-
"/users/signup",
33+
"api/users/signup",
3434
data=json.dumps(
3535
{
36-
"name": DUMMY_NAME,
36+
"username": DUMMY_USERNAME,
3737
"email": DUMMY_EMAIL,
3838
"password": DUMMY_PASS
3939
}
@@ -42,18 +42,18 @@ def test_user_signup(client):
4242

4343
data = json.loads(response.data.decode())
4444
assert response.status_code == 201
45-
assert "User with (%s, %s) created successfully!" % (DUMMY_NAME, DUMMY_EMAIL) in data["message"]
45+
assert "User with (%s, %s) created successfully!" % (DUMMY_USERNAME, DUMMY_EMAIL) in data["msg"]
4646

4747

4848
def test_user_signup_invalid_data(client):
4949
"""
5050
Tests /users/signup API: invalid data like email field empty
5151
"""
5252
response = client.post(
53-
"/users/signup",
53+
"api/users/signup",
5454
data=json.dumps(
5555
{
56-
"name": DUMMY_NAME,
56+
"username": DUMMY_USERNAME,
5757
"email": "",
5858
"password": DUMMY_PASS
5959
}
@@ -62,15 +62,15 @@ def test_user_signup_invalid_data(client):
6262

6363
data = json.loads(response.data.decode())
6464
assert response.status_code == 400
65-
assert "Input payload validation failed" in data["message"]
65+
assert "'' is too short" in data["msg"]
6666

6767

6868
def test_user_login_correct(client):
6969
"""
7070
Tests /users/signup API: Correct credentials
7171
"""
7272
response = client.post(
73-
"/users/login",
73+
"api/users/login",
7474
data=json.dumps(
7575
{
7676
"email": DUMMY_EMAIL,
@@ -89,7 +89,7 @@ def test_user_login_error(client):
8989
Tests /users/signup API: Wrong credentials
9090
"""
9191
response = client.post(
92-
"/users/login",
92+
"api/users/login",
9393
data=json.dumps(
9494
{
9595
"email": DUMMY_EMAIL,
@@ -100,4 +100,4 @@ def test_user_login_error(client):
100100

101101
data = json.loads(response.data.decode())
102102
assert response.status_code == 400
103-
assert "Sorry. Wrong credentials." in data["message"]
103+
assert "Sorry. Wrong credentials." in data["msg"]

0 commit comments

Comments
 (0)