2019-11-13 18:40:01 +01:00
|
|
|
import json
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
from flask import Flask
|
|
|
|
|
2021-01-20 16:47:00 +01:00
|
|
|
from fittrackee.users.models import User
|
|
|
|
|
2019-11-13 18:40:01 +01:00
|
|
|
|
2020-05-10 15:55:56 +02:00
|
|
|
class TestGetConfig:
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_gets_application_config(
|
|
|
|
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/config',
|
|
|
|
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 data['data']['gpx_limit_import'] == 10
|
|
|
|
assert data['data']['is_registration_enabled'] is True
|
|
|
|
assert data['data']['max_single_file_size'] == 1048576
|
|
|
|
assert data['data']['max_zip_file_size'] == 10485760
|
|
|
|
assert data['data']['max_users'] == 100
|
2020-09-16 13:01:15 +02:00
|
|
|
assert data['data']['map_attribution'] == (
|
|
|
|
'© <a href="http://www.openstreetmap.org/copyright" '
|
|
|
|
'target="_blank" rel="noopener noreferrer">OpenStreetMap</a> '
|
|
|
|
'contributors'
|
|
|
|
)
|
2020-05-10 15:55:56 +02:00
|
|
|
|
|
|
|
def test_it_returns_error_if_application_has_no_config(
|
2021-01-02 19:28:03 +01:00
|
|
|
self, app_no_config: Flask, user_1_admin: User
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
client = app_no_config.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/config',
|
|
|
|
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 == 500
|
|
|
|
assert 'error' in data['status']
|
|
|
|
assert 'Error on getting configuration.' in data['message']
|
|
|
|
|
|
|
|
def test_it_returns_error_if_application_has_several_config(
|
2021-01-02 19:28:03 +01:00
|
|
|
self, app: Flask, app_config: 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.get(
|
|
|
|
'/api/config',
|
|
|
|
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 == 500
|
|
|
|
assert 'error' in data['status']
|
|
|
|
assert 'Error on getting configuration.' in data['message']
|
|
|
|
|
|
|
|
|
|
|
|
class TestUpdateConfig:
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_updates_config_when_user_is_admin(
|
|
|
|
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/config',
|
|
|
|
content_type='application/json',
|
|
|
|
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
|
|
|
|
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 data['data']['gpx_limit_import'] == 100
|
|
|
|
assert data['data']['is_registration_enabled'] is True
|
|
|
|
assert data['data']['max_single_file_size'] == 1048576
|
|
|
|
assert data['data']['max_zip_file_size'] == 10485760
|
|
|
|
assert data['data']['max_users'] == 10
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_updates_all_config(
|
|
|
|
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/config',
|
|
|
|
content_type='application/json',
|
|
|
|
data=json.dumps(
|
|
|
|
dict(
|
|
|
|
gpx_limit_import=20,
|
|
|
|
max_single_file_size=10000,
|
|
|
|
max_zip_file_size=25000,
|
|
|
|
max_users=50,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
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 data['data']['gpx_limit_import'] == 20
|
|
|
|
assert data['data']['is_registration_enabled'] is True
|
|
|
|
assert data['data']['max_single_file_size'] == 10000
|
|
|
|
assert data['data']['max_zip_file_size'] == 25000
|
|
|
|
assert data['data']['max_users'] == 50
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_returns_403_when_user_is_not_an_admin(
|
|
|
|
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.patch(
|
|
|
|
'/api/config',
|
|
|
|
content_type='application/json',
|
|
|
|
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
|
|
|
|
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_it_returns_400_if_invalid_is_payload(
|
|
|
|
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/config',
|
|
|
|
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']
|
|
|
|
|
|
|
|
def test_it_returns_error_on_update_if_application_has_no_config(
|
2021-01-02 19:28:03 +01:00
|
|
|
self, app_no_config: Flask, user_1_admin: User
|
|
|
|
) -> None:
|
2020-05-10 15:55:56 +02:00
|
|
|
client = app_no_config.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/config',
|
|
|
|
content_type='application/json',
|
|
|
|
data=json.dumps(dict(gpx_limit_import=100, max_users=10)),
|
|
|
|
headers=dict(
|
|
|
|
Authorization='Bearer '
|
|
|
|
+ json.loads(resp_login.data.decode())['auth_token']
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
data = json.loads(response.data.decode())
|
|
|
|
assert response.status_code == 500
|
|
|
|
assert 'error' in data['status']
|
|
|
|
assert 'Error on updating configuration.' in data['message']
|