API & Client : replace id with username to fetch a user

This commit is contained in:
Sam
2020-02-08 13:09:01 +01:00
parent 33ed19a7e7
commit 2c3bc0f9bc
10 changed files with 28 additions and 49 deletions

View File

@ -13,7 +13,7 @@ def test_ping(app):
assert 'success' in data['status']
def test_single_user(app, user_1):
def test_single_user(app, user_1, user_2):
"""=> Get single user details"""
client = app.test_client()
resp_login = client.post(
@ -22,7 +22,7 @@ def test_single_user(app, user_1):
content_type='application/json',
)
response = client.get(
f'/api/users/{user_1.id}',
f'/api/users/{user_2.username}',
content_type='application/json',
headers=dict(
Authorization='Bearer '
@ -36,8 +36,8 @@ def test_single_user(app, user_1):
assert len(data['data']['users']) == 1
user = data['data']['users'][0]
assert user['username'] == 'test'
assert user['email'] == 'test@test.com'
assert user['username'] == 'toto'
assert user['email'] == 'toto@toto.com'
assert user['created_at']
assert not user['admin']
assert user['first_name'] is None
@ -71,7 +71,7 @@ def test_single_user_with_activities(
content_type='application/json',
)
response = client.get(
f'/api/users/{user_1.id}',
f'/api/users/{user_1.username}',
content_type='application/json',
headers=dict(
Authorization='Bearer '
@ -104,30 +104,7 @@ def test_single_user_with_activities(
assert user['total_duration'] == '1:57:04'
def test_single_user_no_id(app, user_1):
"""=> Ensure error is thrown if an id is not provided."""
client = app.test_client()
resp_login = client.post(
'/api/auth/login',
data=json.dumps(dict(email='test@test.com', password='12345678')),
content_type='application/json',
)
response = client.get(
'/api/users/blah',
content_type='application/json',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
data = json.loads(response.data.decode())
assert response.status_code == 404
assert 'fail' in data['status']
assert 'User does not exist.' in data['message']
def test_single_user_wrong_id(app, user_1):
def test_single_user_no_existing(app, user_1):
"""=> Ensure error is thrown if the id does not exist."""
client = app.test_client()
resp_login = client.post(
@ -136,7 +113,7 @@ def test_single_user_wrong_id(app, user_1):
content_type='application/json',
)
response = client.get(
'/api/users/99999999999',
'/api/users/not_existing',
content_type='application/json',
headers=dict(
Authorization='Bearer '

View File

@ -98,9 +98,9 @@ def get_users(auth_user_id):
return jsonify(response_object), 200
@users_blueprint.route('/users/<user_id>', methods=['GET'])
@users_blueprint.route('/users/<user_name>', methods=['GET'])
@authenticate
def get_single_user(auth_user_id, user_id):
def get_single_user(auth_user_id, user_name):
"""
Get single user details
@ -149,7 +149,7 @@ def get_single_user(auth_user_id, user_id):
}
:param integer auth_user_id: authenticate user id (from JSON Web Token)
:param integer user_id: user id
:param integer user_name: user name
:reqheader Authorization: OAuth 2.0 Bearer Token
@ -164,7 +164,7 @@ def get_single_user(auth_user_id, user_id):
response_object = {'status': 'fail', 'message': 'User does not exist.'}
try:
user = User.query.filter_by(id=int(user_id)).first()
user = User.query.filter_by(username=user_name).first()
if not user:
return jsonify(response_object), 404
else: