From ba613079f41c4435b5ac7114fe94823cc07b77ac Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 1 May 2018 17:12:51 +0200 Subject: [PATCH] API: test refactor --- mpwo_api/mpwo_api/tests/test_activities.py | 249 +------------------- mpwo_api/mpwo_api/tests/test_auth.py | 55 +++++ mpwo_api/mpwo_api/tests/test_sports.py | 252 +++++++++++++++++++++ 3 files changed, 308 insertions(+), 248 deletions(-) create mode 100644 mpwo_api/mpwo_api/tests/test_sports.py diff --git a/mpwo_api/mpwo_api/tests/test_activities.py b/mpwo_api/mpwo_api/tests/test_activities.py index b97cc5ca..58bc1c63 100644 --- a/mpwo_api/mpwo_api/tests/test_activities.py +++ b/mpwo_api/mpwo_api/tests/test_activities.py @@ -1,39 +1,7 @@ import datetime import json -from mpwo_api.tests.utils import add_activity, add_admin, add_sport, add_user - - -def test_get_all_sports(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - add_sport('running') - - 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/sports', - headers=dict( - Authorization='Bearer ' + json.loads( - resp_login.data.decode() - )['auth_token'] - ) - ) - data = json.loads(response.data.decode()) - - assert response.status_code == 200 - assert 'success' in data['status'] - - assert len(data['data']['sports']) == 2 - assert 'cycling' in data['data']['sports'][0]['label'] - assert 'running' in data['data']['sports'][1]['label'] +from mpwo_api.tests.utils import add_activity, add_sport, add_user def test_get_all_activities(app): @@ -86,218 +54,3 @@ def test_get_all_activities(app): assert 3600 == data['data']['activities'][0]['duration'] assert 1024 == data['data']['activities'][1]['duration'] - -def test_get_a_sport(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - - 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/sports/1', - headers=dict( - Authorization='Bearer ' + json.loads( - resp_login.data.decode() - )['auth_token'] - ) - ) - data = json.loads(response.data.decode()) - - assert response.status_code == 200 - assert 'success' in data['status'] - - assert len(data['data']['sports']) == 1 - assert 'cycling' in data['data']['sports'][0]['label'] - - -def test_add_a_sport(app): - add_admin() - - client = app.test_client() - resp_login = client.post( - '/api/auth/login', - data=json.dumps(dict( - email='admin@example.com', - password='12345678' - )), - content_type='application/json' - ) - response = client.post( - '/api/sports', - content_type='application/json', - data=json.dumps(dict( - label='surfing' - )), - headers=dict( - Authorization='Bearer ' + json.loads( - resp_login.data.decode() - )['auth_token'] - ) - ) - data = json.loads(response.data.decode()) - - assert response.status_code == 201 - assert 'created' in data['status'] - - assert len(data['data']['sports']) == 1 - assert 'surfing' in data['data']['sports'][0]['label'] - - -def test_add_a_sport_not_admin(app): - add_user('test', 'test@test.com', '12345678') - - 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.post( - '/api/sports', - content_type='application/json', - data=json.dumps(dict( - label='surfing' - )), - headers=dict( - Authorization='Bearer ' + json.loads( - resp_login.data.decode() - )['auth_token'] - ) - ) - data = json.loads(response.data.decode()) - - assert response.status_code == 401 - assert 'created' not in data['status'] - assert 'error' in data['status'] - assert 'You do not have permissions.' in data['message'] - - -def test_update_a_sport(app): - add_admin() - add_sport('cycling') - - client = app.test_client() - resp_login = client.post( - '/api/auth/login', - data=json.dumps(dict( - email='admin@example.com', - password='12345678' - )), - content_type='application/json' - ) - response = client.patch( - '/api/sports/1', - content_type='application/json', - data=json.dumps(dict( - label='cycling updated' - )), - headers=dict( - Authorization='Bearer ' + json.loads( - resp_login.data.decode() - )['auth_token'] - ) - ) - data = json.loads(response.data.decode()) - - assert response.status_code == 200 - assert 'success' in data['status'] - - assert len(data['data']['sports']) == 1 - assert 'cycling updated' in data['data']['sports'][0]['label'] - - -def test_update_a_sport_not_admin(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - - 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.patch( - '/api/sports/1', - content_type='application/json', - data=json.dumps(dict( - label='cycling updated' - )), - headers=dict( - Authorization='Bearer ' + json.loads( - resp_login.data.decode() - )['auth_token'] - ) - ) - data = json.loads(response.data.decode()) - - assert response.status_code == 401 - assert 'success' not in data['status'] - assert 'error' in data['status'] - assert 'You do not have permissions.' in data['message'] - - -def test_delete_a_sport(app): - add_admin() - add_sport('cycling') - - client = app.test_client() - resp_login = client.post( - '/api/auth/login', - data=json.dumps(dict( - email='admin@example.com', - password='12345678' - )), - content_type='application/json' - ) - response = client.delete( - '/api/sports/1', - content_type='application/json', - headers=dict( - Authorization='Bearer ' + json.loads( - resp_login.data.decode() - )['auth_token'] - ) - ) - assert response.status_code == 204 - - -def test_delete_a_sport_not_admin(app): - add_user('test', 'test@test.com', '12345678') - add_sport('cycling') - - 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.delete( - '/api/sports/1', - 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 == 401 - assert 'error' in data['status'] - assert 'You do not have permissions.' in data['message'] diff --git a/mpwo_api/mpwo_api/tests/test_auth.py b/mpwo_api/mpwo_api/tests/test_auth.py index 79f11e60..e2c16cff 100644 --- a/mpwo_api/mpwo_api/tests/test_auth.py +++ b/mpwo_api/mpwo_api/tests/test_auth.py @@ -526,3 +526,58 @@ def test_update_user_picture(app): assert data['status'] == 'success' assert data['message'] == 'User picture updated.' assert response.status_code == 200 + + +def test_update_user_no_picture(app): + add_user('test', 'test@test.com', '12345678') + + 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.post( + '/api/auth/picture', + headers=dict( + content_type='multipart/form-data', + authorization='Bearer ' + + json.loads(resp_login.data.decode())['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + assert data['status'] == 'fail' + assert data['message'] == 'No file part.' + assert response.status_code == 400 + + +def test_update_user_invalid_picture(app): + add_user('test', 'test@test.com', '12345678') + + 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.post( + '/api/auth/picture', + data=dict( + file=(BytesIO(b'avatar'), 'avatar.bmp') + ), + headers=dict( + content_type='multipart/form-data', + authorization='Bearer ' + + json.loads(resp_login.data.decode())['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + assert data['status'] == 'fail' + assert data['message'] == 'File extension not allowed.' + assert response.status_code == 400 diff --git a/mpwo_api/mpwo_api/tests/test_sports.py b/mpwo_api/mpwo_api/tests/test_sports.py new file mode 100644 index 00000000..4ae0ecfb --- /dev/null +++ b/mpwo_api/mpwo_api/tests/test_sports.py @@ -0,0 +1,252 @@ +import datetime +import json + +from mpwo_api.tests.utils import add_admin, add_sport, add_user + + +def test_get_all_sports(app): + add_user('test', 'test@test.com', '12345678') + add_sport('cycling') + add_sport('running') + + 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/sports', + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 200 + assert 'success' in data['status'] + + assert len(data['data']['sports']) == 2 + assert 'cycling' in data['data']['sports'][0]['label'] + assert 'running' in data['data']['sports'][1]['label'] + + +def test_get_a_sport(app): + add_user('test', 'test@test.com', '12345678') + add_sport('cycling') + + 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/sports/1', + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 200 + assert 'success' in data['status'] + + assert len(data['data']['sports']) == 1 + assert 'cycling' in data['data']['sports'][0]['label'] + + +def test_add_a_sport(app): + add_admin() + + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='admin@example.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.post( + '/api/sports', + content_type='application/json', + data=json.dumps(dict( + label='surfing' + )), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 201 + assert 'created' in data['status'] + + assert len(data['data']['sports']) == 1 + assert 'surfing' in data['data']['sports'][0]['label'] + + +def test_add_a_sport_not_admin(app): + add_user('test', 'test@test.com', '12345678') + + 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.post( + '/api/sports', + content_type='application/json', + data=json.dumps(dict( + label='surfing' + )), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 401 + assert 'created' not in data['status'] + assert 'error' in data['status'] + assert 'You do not have permissions.' in data['message'] + + +def test_update_a_sport(app): + add_admin() + add_sport('cycling') + + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='admin@example.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.patch( + '/api/sports/1', + content_type='application/json', + data=json.dumps(dict( + label='cycling updated' + )), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 200 + assert 'success' in data['status'] + + assert len(data['data']['sports']) == 1 + assert 'cycling updated' in data['data']['sports'][0]['label'] + + +def test_update_a_sport_not_admin(app): + add_user('test', 'test@test.com', '12345678') + add_sport('cycling') + + 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.patch( + '/api/sports/1', + content_type='application/json', + data=json.dumps(dict( + label='cycling updated' + )), + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + data = json.loads(response.data.decode()) + + assert response.status_code == 401 + assert 'success' not in data['status'] + assert 'error' in data['status'] + assert 'You do not have permissions.' in data['message'] + + +def test_delete_a_sport(app): + add_admin() + add_sport('cycling') + + client = app.test_client() + resp_login = client.post( + '/api/auth/login', + data=json.dumps(dict( + email='admin@example.com', + password='12345678' + )), + content_type='application/json' + ) + response = client.delete( + '/api/sports/1', + content_type='application/json', + headers=dict( + Authorization='Bearer ' + json.loads( + resp_login.data.decode() + )['auth_token'] + ) + ) + assert response.status_code == 204 + + +def test_delete_a_sport_not_admin(app): + add_user('test', 'test@test.com', '12345678') + add_sport('cycling') + + 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.delete( + '/api/sports/1', + 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 == 401 + assert 'error' in data['status'] + assert 'You do not have permissions.' in data['message']