From 029d29ffb1eb43f44cb89b72807d8bcea9f9b8f5 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 25 Jul 2018 20:50:12 +0200 Subject: [PATCH] Make it possible to set the instance admin --- calsocial/__init__.py | 33 ++++++++++++++++++++++---- tests/test_calsocial.py | 51 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/calsocial/__init__.py b/calsocial/__init__.py index 96b989b..e56a9cc 100644 --- a/calsocial/__init__.py +++ b/calsocial/__init__.py @@ -19,8 +19,10 @@ from datetime import datetime 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_security import SQLAlchemyUserDatastore, current_user, login_required from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound @@ -127,9 +129,6 @@ class CalendarSocialApp(Flask, RoutedMixin): """The default time zone of the app """ - from warnings import warn - - from flask import has_app_context from pytz import timezone, utc from pytz.exceptions import UnknownTimeZoneError @@ -148,6 +147,32 @@ class CalendarSocialApp(Flask, RoutedMixin): 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 def _current_calendar(): from .calendar_system.gregorian import GregorianCalendar diff --git a/tests/test_calsocial.py b/tests/test_calsocial.py index f8c6cfc..d5dfee1 100644 --- a/tests/test_calsocial.py +++ b/tests/test_calsocial.py @@ -17,9 +17,60 @@ """General tests for Calendar.social """ +from flask import current_app + +import pytest + def test_index_no_login(client): """Test the main page without logging in """ page = client.get('/') 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