API & Client - update registration activation on user register/delete

+ minor refactor on administration (fix #15)
This commit is contained in:
Sam
2020-05-02 22:00:11 +02:00
parent e28d609013
commit 5d7b63f7aa
10 changed files with 193 additions and 10 deletions

View File

@ -1,4 +1,6 @@
from fittrackee_api import db
from flask import current_app
from sqlalchemy.event import listens_for
from ..users.models import User
@ -26,3 +28,25 @@ class AppConfig(db.Model):
"max_zip_file_size": self.max_zip_file_size,
"max_users": self.max_users,
}
def update_app_config():
config = AppConfig.query.first()
if config:
current_app.config[
'is_registration_enabled'
] = config.is_registration_enabled
@listens_for(User, 'after_insert')
def on_user_insert(mapper, connection, user):
@listens_for(db.Session, 'after_flush', once=True)
def receive_after_flush(session, context):
update_app_config()
@listens_for(User, 'after_delete')
def on_user_delete(mapper, connection, old_user):
@listens_for(db.Session, 'after_flush', once=True)
def receive_after_flush(session, context):
update_app_config()

View File

@ -62,7 +62,6 @@ def app_config():
config.max_single_file_size = 1048576
config.max_zip_file_size = 10485760
config.max_users = 0
config.registration = False
db.session.add(config)
db.session.commit()
return config

View File

@ -836,3 +836,70 @@ def test_update_user_invalid_picture(app, user_1):
assert data['status'] == 'fail'
assert data['message'] == 'File extension not allowed.'
assert response.status_code == 400
def test_it_disables_registration_on_user_registration(
app_no_config, app_config, user_1_admin, user_2
):
app_config.max_users = 3
client = app_no_config.test_client()
client.post(
'/api/auth/register',
data=json.dumps(
dict(
username='sam',
email='sam@test.com',
password='12345678',
password_conf='12345678',
)
),
content_type='application/json',
)
response = client.post(
'/api/auth/register',
data=json.dumps(
dict(
username='new',
email='new@test.com',
password='12345678',
password_conf='12345678',
)
),
content_type='application/json',
)
assert response.status_code == 403
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'Error. Registration is disabled.'
def test_it_does_not_disable_registration_on_user_registration(
app_no_config, app_config, user_1_admin, user_2,
):
app_config.max_users = 4
client = app_no_config.test_client()
client.post(
'/api/auth/register',
data=json.dumps(
dict(
username='sam',
email='sam@test.com',
password='12345678',
password_conf='12345678',
)
),
content_type='application/json',
)
response = client.post(
'/api/auth/register',
data=json.dumps(
dict(
username='new',
email='new@test.com',
password='12345678',
password_conf='12345678',
)
),
content_type='application/json',
)
assert response.status_code == 201

View File

@ -1252,3 +1252,70 @@ def test_admin_can_not_delete_its_own_account_if_no_other_admin(
'You can not delete your account, no other user has admin rights.'
in data['message']
)
def test_it_enables_registration_on_user_delete(
app_no_config, app_config, user_1_admin, user_2, user_3
):
app_config.max_users = 3
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',
)
client.delete(
'/api/users/toto',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
response = client.post(
'/api/auth/register',
data=json.dumps(
dict(
username='justatest',
email='test@test.com',
password='12345678',
password_conf='12345678',
)
),
content_type='application/json',
)
assert response.status_code == 201
def test_it_does_not_enable_registration_on_user_delete(
app_no_config, app_config, user_1_admin, user_2, user_3
):
app_config.max_users = 2
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',
)
client.delete(
'/api/users/toto',
headers=dict(
Authorization='Bearer '
+ json.loads(resp_login.data.decode())['auth_token']
),
)
response = client.post(
'/api/auth/register',
data=json.dumps(
dict(
username='justatest',
email='test@test.com',
password='12345678',
password_conf='12345678',
)
),
content_type='application/json',
)
assert response.status_code == 403
data = json.loads(response.data.decode())
assert data['status'] == 'error'
assert data['message'] == 'Error. Registration is disabled.'