API - add command to set an admin after fresh install

This commit is contained in:
Sam 2022-02-12 14:30:59 +01:00
parent b1cafc585d
commit 866a4480c1
7 changed files with 73 additions and 1 deletions

View File

@ -161,6 +161,9 @@ serve-python-dev:
echo 'Running on https://$(HOST):$(PORT)' echo 'Running on https://$(HOST):$(PORT)'
$(FLASK) run --with-threads -h $(HOST) -p $(PORT) --cert=adhoc $(FLASK) run --with-threads -h $(HOST) -p $(PORT) --cert=adhoc
set-admin:
$(FLASK) set-admin $(USERNAME)
test-e2e: init-db test-e2e: init-db
$(PYTEST) e2e --driver firefox $(PYTEST_ARGS) $(PYTEST) e2e --driver firefox $(PYTEST_ARGS)

View File

@ -4,12 +4,15 @@ import os
import shutil import shutil
from typing import Dict, Optional from typing import Dict, Optional
import click
import gunicorn.app.base import gunicorn.app.base
from flask import Flask from flask import Flask
from flask_dramatiq import worker from flask_dramatiq import worker
from flask_migrate import upgrade from flask_migrate import upgrade
from fittrackee import create_app, db from fittrackee import create_app, db
from fittrackee.users.exceptions import UserNotFoundException
from fittrackee.users.utils import set_admin_rights
HOST = os.getenv('HOST', '0.0.0.0') HOST = os.getenv('HOST', '0.0.0.0')
PORT = os.getenv('PORT', '5000') PORT = os.getenv('PORT', '5000')
@ -56,6 +59,17 @@ def drop_db() -> None:
print('Uploaded files deleted.') print('Uploaded files deleted.')
@app.cli.command('set-admin')
@click.argument('username')
def set_admin(username: str) -> None:
"""Set admin rights for given user"""
try:
set_admin_rights(username)
print(f"User '{username}' updated.")
except UserNotFoundException:
print(f"User '{username}' not found.")
def main() -> None: def main() -> None:
options = {'bind': f'{HOST}:{PORT}', 'workers': WORKERS} options = {'bind': f'{HOST}:{PORT}', 'workers': WORKERS}
StandaloneApplication(app, options).run() StandaloneApplication(app, options).run()

View File

@ -0,0 +1,30 @@
import pytest
from flask import Flask
from fittrackee.users.exceptions import UserNotFoundException
from fittrackee.users.models import User
from fittrackee.users.utils import set_admin_rights
from ..utils import random_string
class TestSetAdminRights:
def test_it_raises_exception_if_user_does_not_exist(
self, app: Flask
) -> None:
with pytest.raises(UserNotFoundException):
set_admin_rights(random_string())
def test_it_sets_admin_right_for_a_given_user(
self, app: Flask, user_1: User
) -> None:
set_admin_rights(user_1.username)
assert user_1.admin is True
def test_it_does_not_raise_exception_when_user_has_already_admin_right(
self, app: Flask, user_1_admin: User
) -> None:
set_admin_rights(user_1_admin.username)
assert user_1_admin.admin is True

12
fittrackee/tests/utils.py Normal file
View File

@ -0,0 +1,12 @@
import random
import string
from typing import Optional
def random_string(length: Optional[int] = None) -> str:
if length is None:
length = 10
return ''.join(
random.choice(string.ascii_letters + string.digits)
for _ in range(length)
)

View File

@ -0,0 +1,2 @@
class UserNotFoundException(Exception):
...

View File

@ -3,12 +3,14 @@ from typing import Optional, Tuple
from flask import Request from flask import Request
from fittrackee import db
from fittrackee.responses import ( from fittrackee.responses import (
ForbiddenErrorResponse, ForbiddenErrorResponse,
HttpResponse, HttpResponse,
UnauthorizedErrorResponse, UnauthorizedErrorResponse,
) )
from .exceptions import UserNotFoundException
from .models import User from .models import User
@ -84,3 +86,11 @@ def can_view_workout(
if auth_user_id != workout_user_id: if auth_user_id != workout_user_id:
return ForbiddenErrorResponse() return ForbiddenErrorResponse()
return None return None
def set_admin_rights(username: str) -> None:
user = User.query.filter_by(username=username).first()
if not user:
raise UserNotFoundException()
user.admin = True
db.session.commit()

View File

@ -62,6 +62,7 @@ Sphinx = "^4.4.0"
[tool.poetry.scripts] [tool.poetry.scripts]
fittrackee = 'fittrackee.__main__:main' fittrackee = 'fittrackee.__main__:main'
fittrackee_set_admin = 'fittrackee.__main__:set_admin'
fittrackee_upgrade_db = 'fittrackee.__main__:upgrade_db' fittrackee_upgrade_db = 'fittrackee.__main__:upgrade_db'
fittrackee_worker = 'fittrackee.__main__:dramatiq_worker' fittrackee_worker = 'fittrackee.__main__:dramatiq_worker'