2018-05-01 17:12:51 +02:00
|
|
|
import json
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
from fittrackee.activities.models import Activity, Sport
|
|
|
|
from fittrackee.users.models import User
|
|
|
|
from flask import Flask
|
|
|
|
|
2018-05-13 18:36:31 +02:00
|
|
|
expected_sport_1_cycling_result = {
|
|
|
|
'id': 1,
|
|
|
|
'label': 'Cycling',
|
2018-05-16 23:52:55 +02:00
|
|
|
'img': None,
|
2019-09-22 23:03:56 +02:00
|
|
|
'is_active': True,
|
2018-05-13 18:36:31 +02:00
|
|
|
}
|
2019-09-23 14:09:26 +02:00
|
|
|
expected_sport_1_cycling_admin_result = expected_sport_1_cycling_result.copy()
|
|
|
|
expected_sport_1_cycling_admin_result['has_activities'] = False
|
2018-05-01 17:12:51 +02:00
|
|
|
|
2018-05-13 18:36:31 +02:00
|
|
|
expected_sport_2_running_result = {
|
|
|
|
'id': 2,
|
|
|
|
'label': 'Running',
|
2018-05-16 23:52:55 +02:00
|
|
|
'img': None,
|
2019-09-22 23:03:56 +02:00
|
|
|
'is_active': True,
|
2018-05-13 18:36:31 +02:00
|
|
|
}
|
2019-09-23 14:09:26 +02:00
|
|
|
expected_sport_2_running_admin_result = expected_sport_2_running_result.copy()
|
|
|
|
expected_sport_2_running_admin_result['has_activities'] = False
|
|
|
|
|
|
|
|
expected_sport_1_cycling_inactive_result = {
|
|
|
|
'id': 1,
|
|
|
|
'label': 'Cycling',
|
|
|
|
'img': None,
|
|
|
|
'is_active': False,
|
|
|
|
}
|
|
|
|
expected_sport_1_cycling_inactive_admin_result = (
|
|
|
|
expected_sport_1_cycling_inactive_result.copy()
|
|
|
|
)
|
|
|
|
expected_sport_1_cycling_inactive_admin_result['has_activities'] = False
|
2018-05-01 17:12:51 +02:00
|
|
|
|
|
|
|
|
2020-05-10 15:55:56 +02:00
|
|
|
class TestGetSports:
|
|
|
|
def test_it_gets_all_sports(
|
2021-01-02 19:28:03 +01:00
|
|
|
self,
|
|
|
|
app: Flask,
|
|
|
|
user_1: User,
|
|
|
|
sport_1_cycling: Sport,
|
|
|
|
sport_2_running: Sport,
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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 data['data']['sports'][0] == expected_sport_1_cycling_result
|
|
|
|
assert data['data']['sports'][1] == expected_sport_2_running_result
|
|
|
|
|
|
|
|
def test_it_gets_all_sports_with_inactive_one(
|
2021-01-02 19:28:03 +01:00
|
|
|
self,
|
|
|
|
app: Flask,
|
|
|
|
user_1: User,
|
|
|
|
sport_1_cycling_inactive: Sport,
|
|
|
|
sport_2_running: Sport,
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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 (
|
|
|
|
data['data']['sports'][0]
|
|
|
|
== expected_sport_1_cycling_inactive_result
|
|
|
|
)
|
|
|
|
assert data['data']['sports'][1] == expected_sport_2_running_result
|
|
|
|
|
|
|
|
def test_it_gets_all_sports_with_admin_rights(
|
2021-01-02 19:28:03 +01:00
|
|
|
self,
|
|
|
|
app: Flask,
|
|
|
|
user_1_admin: User,
|
|
|
|
sport_1_cycling_inactive: Sport,
|
|
|
|
sport_2_running: Sport,
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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.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 (
|
|
|
|
data['data']['sports'][0]
|
|
|
|
== expected_sport_1_cycling_inactive_admin_result
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
data['data']['sports'][1] == expected_sport_2_running_admin_result
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestGetSport:
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_gets_a_sport(
|
|
|
|
self, app: Flask, user_1: User, sport_1_cycling: Sport
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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 data['data']['sports'][0] == expected_sport_1_cycling_result
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_returns_404_if_sport_does_not_exist(
|
|
|
|
self, app: Flask, user_1: User
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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 == 404
|
|
|
|
assert 'not found' in data['status']
|
|
|
|
assert len(data['data']['sports']) == 0
|
|
|
|
|
|
|
|
def test_it_gets_a_inactive_sport(
|
2021-01-02 19:28:03 +01:00
|
|
|
self, app: Flask, user_1: User, sport_1_cycling_inactive: Sport
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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 (
|
|
|
|
data['data']['sports'][0]
|
|
|
|
== expected_sport_1_cycling_inactive_result
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_it_get_an_inactive_sport_with_admin_rights(
|
2021-01-02 19:28:03 +01:00
|
|
|
self, app: Flask, user_1_admin: User, sport_1_cycling_inactive: Sport
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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.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 (
|
|
|
|
data['data']['sports'][0]
|
|
|
|
== expected_sport_1_cycling_inactive_admin_result
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestUpdateSport:
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_disables_a_sport(
|
|
|
|
self, app: Flask, user_1_admin: User, sport_1_cycling: Sport
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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(is_active=False)),
|
|
|
|
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 data['data']['sports'][0]['is_active'] is False
|
|
|
|
assert data['data']['sports'][0]['has_activities'] is False
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_enables_a_sport(
|
|
|
|
self, app: Flask, user_1_admin: User, sport_1_cycling: Sport
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
sport_1_cycling.is_active = False
|
|
|
|
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(is_active=True)),
|
|
|
|
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 data['data']['sports'][0]['is_active'] is True
|
|
|
|
assert data['data']['sports'][0]['has_activities'] is False
|
|
|
|
|
|
|
|
def test_it_disables_a_sport_with_activities(
|
2021-01-02 19:28:03 +01:00
|
|
|
self,
|
|
|
|
app: Flask,
|
|
|
|
user_1_admin: User,
|
|
|
|
sport_1_cycling: Sport,
|
|
|
|
activity_cycling_user_1: Activity,
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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(is_active=False)),
|
|
|
|
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 data['data']['sports'][0]['is_active'] is False
|
|
|
|
assert data['data']['sports'][0]['has_activities'] is True
|
|
|
|
|
|
|
|
def test_it_enables_a_sport_with_activities(
|
2021-01-02 19:28:03 +01:00
|
|
|
self,
|
|
|
|
app: Flask,
|
|
|
|
user_1_admin: User,
|
|
|
|
sport_1_cycling: Sport,
|
|
|
|
activity_cycling_user_1: Activity,
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
sport_1_cycling.is_active = False
|
|
|
|
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(is_active=True)),
|
|
|
|
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 data['data']['sports'][0]['is_active'] is True
|
|
|
|
assert data['data']['sports'][0]['has_activities'] is True
|
|
|
|
|
|
|
|
def test_returns_error_if_user_has_no_admin_rights(
|
2021-01-02 19:28:03 +01:00
|
|
|
self, app: Flask, user_1: User, sport_1_cycling: Sport
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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(is_active=False)),
|
|
|
|
headers=dict(
|
|
|
|
Authorization='Bearer '
|
|
|
|
+ json.loads(resp_login.data.decode())['auth_token']
|
|
|
|
),
|
|
|
|
)
|
|
|
|
data = json.loads(response.data.decode())
|
|
|
|
|
|
|
|
assert response.status_code == 403
|
|
|
|
assert 'success' not in data['status']
|
|
|
|
assert 'error' in data['status']
|
|
|
|
assert 'You do not have permissions.' in data['message']
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_returns_error_if_payload_is_invalid(
|
|
|
|
self, app: Flask, user_1_admin: User
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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()),
|
|
|
|
headers=dict(
|
|
|
|
Authorization='Bearer '
|
|
|
|
+ json.loads(resp_login.data.decode())['auth_token']
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
data = json.loads(response.data.decode())
|
|
|
|
assert response.status_code == 400
|
|
|
|
assert 'error' in data['status']
|
|
|
|
assert 'Invalid payload.' in data['message']
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_returns_error_if_sport_does_not_exist(
|
|
|
|
self, app: Flask, user_1_admin: User
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
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(is_active=False)),
|
|
|
|
headers=dict(
|
|
|
|
Authorization='Bearer '
|
|
|
|
+ json.loads(resp_login.data.decode())['auth_token']
|
|
|
|
),
|
|
|
|
)
|
|
|
|
data = json.loads(response.data.decode())
|
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
assert 'not found' in data['status']
|
|
|
|
assert len(data['data']['sports']) == 0
|