API: test refactor

This commit is contained in:
Sam 2018-05-01 17:12:51 +02:00
parent 7c27086f86
commit ba613079f4
3 changed files with 308 additions and 248 deletions

View File

@ -1,39 +1,7 @@
import datetime import datetime
import json import json
from mpwo_api.tests.utils import add_activity, add_admin, add_sport, add_user from mpwo_api.tests.utils import add_activity, 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_all_activities(app): def test_get_all_activities(app):
@ -86,218 +54,3 @@ def test_get_all_activities(app):
assert 3600 == data['data']['activities'][0]['duration'] assert 3600 == data['data']['activities'][0]['duration']
assert 1024 == data['data']['activities'][1]['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']

View File

@ -526,3 +526,58 @@ def test_update_user_picture(app):
assert data['status'] == 'success' assert data['status'] == 'success'
assert data['message'] == 'User picture updated.' assert data['message'] == 'User picture updated.'
assert response.status_code == 200 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

View File

@ -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']