Make it possible to set the instance admin

This commit is contained in:
Gergely Polonkai 2018-07-25 20:50:12 +02:00
parent 4b1fff6544
commit 029d29ffb1
2 changed files with 80 additions and 4 deletions

View File

@ -19,8 +19,10 @@
from datetime import datetime from datetime import datetime
import os import os
from warnings import warn
from flask import Flask, abort, current_app, redirect, render_template, request, url_for from flask import Flask, abort, current_app, has_app_context, redirect, render_template, request, \
url_for
from flask_babelex import Babel, get_locale as babel_get_locale from flask_babelex import Babel, get_locale as babel_get_locale
from flask_security import SQLAlchemyUserDatastore, current_user, login_required from flask_security import SQLAlchemyUserDatastore, current_user, login_required
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
@ -127,9 +129,6 @@ class CalendarSocialApp(Flask, RoutedMixin):
"""The default time zone of the app """The default time zone of the app
""" """
from warnings import warn
from flask import has_app_context
from pytz import timezone, utc from pytz import timezone, utc
from pytz.exceptions import UnknownTimeZoneError from pytz.exceptions import UnknownTimeZoneError
@ -148,6 +147,32 @@ class CalendarSocialApp(Flask, RoutedMixin):
return self._timezone return self._timezone
@property
def instance_admin(self):
"""The admin user of this instance
"""
from calsocial.models import AppState, User
if not has_app_context():
return None
admin_id = AppState['instance_admin']
try:
admin_id = int(admin_id)
except (TypeError, ValueError):
warn(f'Instance admin is not set correctly (value is {admin_id})')
return None
try:
return User.query.filter(User.id == admin_id).one()
except NoResultFound:
warn(f'Instance admin is not set correctly (value is {admin_id})')
return None
@staticmethod @staticmethod
def _current_calendar(): def _current_calendar():
from .calendar_system.gregorian import GregorianCalendar from .calendar_system.gregorian import GregorianCalendar

View File

@ -17,9 +17,60 @@
"""General tests for Calendar.social """General tests for Calendar.social
""" """
from flask import current_app
import pytest
def test_index_no_login(client): def test_index_no_login(client):
"""Test the main page without logging in """Test the main page without logging in
""" """
page = client.get('/') page = client.get('/')
assert b'Peek inside' in page.data assert b'Peek inside' in page.data
def test_instance_adin_unset(database):
"""Test the instance admin feature if the admin is not set
"""
with pytest.warns(UserWarning, match=r'Instance admin is not set correctly \(value is None\)'):
assert current_app.instance_admin is None
def test_instance_admin_bad_value(database):
"""Test the instance admin feature if the value is invalid
"""
from calsocial.models import AppState
AppState['instance_admin'] = 'value'
with pytest.warns(UserWarning, match=r'Instance admin is not set correctly \(value is value\)'):
assert current_app.instance_admin is None
def test_instance_admin_doesnot_exist(database):
"""Test the instance admin feature if the admin user does not exist
"""
from calsocial.models import AppState
AppState['instance_admin'] = '0'
with pytest.warns(UserWarning, match=r'Instance admin is not set correctly \(value is 0\)'):
assert current_app.instance_admin is None
def test_instance_admin(database):
"""Test the instance admin feature if the admin user does not exist
"""
from calsocial.models import db, AppState, User
user = User(username='admin')
db.session.add(user)
db.session.commit()
AppState['instance_admin'] = user.id
assert current_app.instance_admin == user