API - remove privacy policy and about text when empty string provided

This commit is contained in:
Sam 2023-02-26 09:02:18 +01:00
parent 4e3d2f98cf
commit f37cecec78
2 changed files with 61 additions and 13 deletions

View File

@ -161,10 +161,15 @@ def update_application_config(auth_user: User) -> Union[Dict, HttpResponse]:
if 'admin_contact' in config_data:
config.admin_contact = admin_contact if admin_contact else None
if 'about' in config_data:
config.about = config_data.get('about')
config.about = (
config_data.get('about') if config_data.get('about') else None
)
if 'privacy_policy' in config_data:
config.privacy_policy = config_data.get('privacy_policy')
config.privacy_policy_date = datetime.utcnow()
privacy_policy = config_data.get('privacy_policy')
config.privacy_policy = privacy_policy if privacy_policy else None
config.privacy_policy_date = (
datetime.utcnow() if privacy_policy else None
)
if config.max_zip_file_size < config.max_single_file_size:
return InvalidPayloadErrorResponse(

View File

@ -6,6 +6,7 @@ from unittest.mock import Mock, patch
import pytest
from flask import Flask
from fittrackee import db
from fittrackee.application.models import AppConfig
from fittrackee.users.models import User
@ -340,11 +341,7 @@ class TestUpdateConfig(ApiTestCaseMixin):
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(
dict(
about=about,
)
),
data=json.dumps(dict(about=about)),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
@ -353,6 +350,28 @@ class TestUpdateConfig(ApiTestCaseMixin):
assert 'success' in data['status']
assert data['data']['about'] == about
def test_it_empties_about_text_when_text_is_an_empty_string(
self, app: Flask, user_1_admin: User
) -> None:
app_config = AppConfig.query.first()
app_config.about = self.random_string()
db.session.commit()
client, auth_token = self.get_test_client_and_auth_token(
app, user_1_admin.email
)
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict(about='')),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 200
data = json.loads(response.data.decode())
assert 'success' in data['status']
assert data['data']['about'] is None
def test_it_updates_privacy_policy(
self,
app: Flask,
@ -371,11 +390,7 @@ class TestUpdateConfig(ApiTestCaseMixin):
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(
dict(
privacy_policy=privacy_policy,
)
),
data=json.dumps(dict(privacy_policy=privacy_policy)),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
@ -387,6 +402,34 @@ class TestUpdateConfig(ApiTestCaseMixin):
'privacy_policy_date'
] == privacy_policy_date.strftime('%a, %d %b %Y %H:%M:%S GMT')
@pytest.mark.parametrize('input_privacy_policy', ['', None])
def test_it_empties_privacy_policy_date_when_no_privacy_policy(
self,
app: Flask,
user_1_admin: User,
input_privacy_policy: Optional[str],
) -> None:
app_config = AppConfig.query.first()
app_config.privacy_policy = self.random_string()
app_config.privacy_policy_date = datetime.utcnow()
db.session.commit()
client, auth_token = self.get_test_client_and_auth_token(
app, user_1_admin.email
)
response = client.patch(
'/api/config',
content_type='application/json',
data=json.dumps(dict(privacy_policy=input_privacy_policy)),
headers=dict(Authorization=f'Bearer {auth_token}'),
)
assert response.status_code == 200
data = json.loads(response.data.decode())
assert 'success' in data['status']
assert data['data']['privacy_policy'] is None
assert data['data']['privacy_policy_date'] is None
@pytest.mark.parametrize(
'client_scope, can_access',
[