API: display user statistics in users routes

This commit is contained in:
Sam 2018-06-05 18:41:13 +02:00
parent e7b05feb45
commit a079a4a741
2 changed files with 52 additions and 19 deletions

View File

@ -21,11 +21,49 @@ def test_single_user(app, user_1):
data = json.loads(response.data.decode()) data = json.loads(response.data.decode())
assert response.status_code == 200 assert response.status_code == 200
assert 'success' in data['status'] assert data['status'] == 'success'
assert data['data'] is not None
assert data['data']['username'] == 'test'
assert data['data']['email'] == 'test@test.com'
assert data['data']['created_at']
assert not data['data']['admin']
assert data['data']['first_name'] is None
assert data['data']['last_name'] is None
assert data['data']['birth_date'] is None
assert data['data']['bio'] is None
assert data['data']['location'] is None
assert data['data']['nb_activities'] == 0
assert data['data']['nb_sports'] == 0
assert data['data']['total_distance'] == 0
assert data['data']['total_duration'] == '0:00:00'
assert 'created_at' in data['data']
assert 'test' in data['data']['username'] def test_single_user_with_activities(
assert 'test@test.com' in data['data']['email'] app, user_1, sport_1_cycling, sport_2_running,
activity_cycling_user_1, activity_running_user_1
):
"""=> Get single user details"""
client = app.test_client()
response = client.get(f'/api/users/{user_1.id}')
data = json.loads(response.data.decode())
assert response.status_code == 200
assert data['status'] == 'success'
assert data['data'] is not None
assert data['data']['username'] == 'test'
assert data['data']['email'] == 'test@test.com'
assert data['data']['created_at']
assert not data['data']['admin']
assert data['data']['first_name'] is None
assert data['data']['last_name'] is None
assert data['data']['birth_date'] is None
assert data['data']['bio'] is None
assert data['data']['location'] is None
assert data['data']['nb_activities'] == 2
assert data['data']['nb_sports'] == 2
assert data['data']['total_distance'] == 22
assert data['data']['total_duration'] == '1:57:04'
def test_single_user_no_id(app): def test_single_user_no_id(app):
@ -67,6 +105,14 @@ def test_users_list(app, user_1, user_2):
assert 'toto' in data['data']['users'][1]['username'] assert 'toto' in data['data']['users'][1]['username']
assert 'test@test.com' in data['data']['users'][0]['email'] assert 'test@test.com' in data['data']['users'][0]['email']
assert 'toto@toto.com' in data['data']['users'][1]['email'] assert 'toto@toto.com' in data['data']['users'][1]['email']
assert data['data']['users'][0]['nb_activities'] == 0
assert data['data']['users'][0]['nb_sports'] == 0
assert data['data']['users'][0]['total_distance'] == 0
assert data['data']['users'][0]['total_duration'] == '0:00:00'
assert data['data']['users'][1]['nb_activities'] == 0
assert data['data']['users'][1]['nb_sports'] == 0
assert data['data']['users'][1]['total_distance'] == 0
assert data['data']['users'][1]['total_duration'] == '0:00:00'
def test_encode_auth_token(app, user_1): def test_encode_auth_token(app, user_1):

View File

@ -9,19 +9,10 @@ users_blueprint = Blueprint('users', __name__)
def get_users(): def get_users():
"""Get all users""" """Get all users"""
users = User.query.all() users = User.query.all()
users_list = []
for user in users:
user_object = {
'id': user.id,
'username': user.username,
'email': user.email,
'created_at': user.created_at
}
users_list.append(user_object)
response_object = { response_object = {
'status': 'success', 'status': 'success',
'data': { 'data': {
'users': users_list 'users': [user.serialize() for user in users]
} }
} }
return jsonify(response_object), 200 return jsonify(response_object), 200
@ -41,11 +32,7 @@ def get_single_user(user_id):
else: else:
response_object = { response_object = {
'status': 'success', 'status': 'success',
'data': { 'data': user.serialize()
'username': user.username,
'email': user.email,
'created_at': user.created_at
}
} }
return jsonify(response_object), 200 return jsonify(response_object), 200
except ValueError: except ValueError: