2021-05-20 01:21:18 +03:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2021 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License")
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import testing_config # Must be imported before the module under test.
|
|
|
|
|
|
|
|
import flask
|
2022-01-11 19:20:40 +03:00
|
|
|
from unittest import mock
|
2021-05-20 01:21:18 +03:00
|
|
|
import werkzeug.exceptions # Flask HTTP stuff.
|
|
|
|
|
|
|
|
from api import accounts_api
|
2022-08-18 04:14:19 +03:00
|
|
|
from internals import user_models
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2021-09-23 23:01:43 +03:00
|
|
|
test_app = flask.Flask(__name__)
|
|
|
|
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2021-06-29 05:05:04 +03:00
|
|
|
class AccountsAPITest(testing_config.CustomTestCase):
|
2021-05-20 01:21:18 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
2022-08-18 04:14:19 +03:00
|
|
|
self.app_admin = user_models.AppUser(email='admin@example.com')
|
2021-06-09 20:35:55 +03:00
|
|
|
self.app_admin.is_admin = True
|
|
|
|
self.app_admin.put()
|
2022-07-14 00:31:27 +03:00
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
self.app_editor = user_models.AppUser(email='editor@example.com')
|
2022-07-14 00:31:27 +03:00
|
|
|
self.app_editor.is_site_editor = True
|
|
|
|
self.app_editor.put()
|
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
self.appuser_1 = user_models.AppUser(email='user@example.com')
|
2021-05-20 01:21:18 +03:00
|
|
|
self.appuser_1.put()
|
2021-06-17 23:47:02 +03:00
|
|
|
self.appuser_id = self.appuser_1.key.integer_id()
|
2021-05-20 01:21:18 +03:00
|
|
|
|
|
|
|
self.request_path = '/api/v0/accounts/%d' % self.appuser_id
|
|
|
|
self.handler = accounts_api.AccountsAPI()
|
|
|
|
|
|
|
|
def tearDown(self):
|
2021-06-17 23:47:02 +03:00
|
|
|
self.appuser_1.key.delete()
|
|
|
|
self.app_admin.key.delete()
|
2022-07-14 00:31:27 +03:00
|
|
|
self.app_editor.key.delete()
|
2021-05-20 01:21:18 +03:00
|
|
|
|
|
|
|
def test_create__normal_valid(self):
|
|
|
|
"""Admin wants to register a normal account."""
|
2021-06-09 20:35:55 +03:00
|
|
|
testing_config.sign_in('admin@example.com', 123567890)
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2022-07-14 00:31:27 +03:00
|
|
|
json_data = {
|
|
|
|
'email': 'new_user@example.com',
|
|
|
|
'isAdmin': False, 'isSiteEditor': False}
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(self.request_path, json=json_data):
|
2021-05-20 01:21:18 +03:00
|
|
|
actual_json = self.handler.do_post()
|
2022-07-14 00:31:27 +03:00
|
|
|
self.assertEqual('new_user@example.com', actual_json['email'])
|
|
|
|
self.assertFalse(actual_json['is_site_editor'])
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertFalse(actual_json['is_admin'])
|
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
new_appuser = (user_models.AppUser.query(
|
|
|
|
user_models.AppUser.email == 'new_user@example.com').get())
|
2022-09-15 23:43:22 +03:00
|
|
|
result_email = new_appuser.email
|
|
|
|
result_is_admin = new_appuser.is_admin
|
|
|
|
new_appuser.key.delete()
|
|
|
|
self.assertEqual('new_user@example.com', result_email)
|
|
|
|
self.assertFalse(result_is_admin)
|
2022-08-18 04:14:19 +03:00
|
|
|
|
2022-07-14 00:31:27 +03:00
|
|
|
def test_create__site_editor_valid(self):
|
|
|
|
"""Admin wants to register a new site editor account."""
|
|
|
|
testing_config.sign_in('admin@example.com', 123567890)
|
|
|
|
|
|
|
|
json_data = {
|
|
|
|
'email': 'new_site_editor@example.com',
|
|
|
|
'isAdmin': False, 'isSiteEditor': True}
|
|
|
|
with test_app.test_request_context(self.request_path, json=json_data):
|
|
|
|
actual_json = self.handler.do_post()
|
|
|
|
self.assertEqual('new_site_editor@example.com', actual_json['email'])
|
|
|
|
self.assertFalse(actual_json['is_admin'])
|
|
|
|
self.assertTrue(actual_json['is_site_editor'])
|
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
new_appuser = user_models.AppUser.query(
|
|
|
|
user_models.AppUser.email == 'new_site_editor@example.com').get()
|
2022-07-14 00:31:27 +03:00
|
|
|
self.assertEqual('new_site_editor@example.com', new_appuser.email)
|
|
|
|
self.assertTrue(new_appuser.is_site_editor)
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertFalse(new_appuser.is_admin)
|
|
|
|
|
2022-10-19 17:14:52 +03:00
|
|
|
# Clean up
|
|
|
|
new_appuser.key.delete()
|
|
|
|
|
2021-05-20 01:21:18 +03:00
|
|
|
def test_create__admin_valid(self):
|
|
|
|
"""Admin wants to register a new admin account."""
|
2021-06-09 20:35:55 +03:00
|
|
|
testing_config.sign_in('admin@example.com', 123567890)
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2022-07-14 00:31:27 +03:00
|
|
|
json_data = {
|
|
|
|
'email': 'new_admin@example.com',
|
|
|
|
'isAdmin': True, 'isSiteEditor': True}
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(self.request_path, json=json_data):
|
2021-05-20 01:21:18 +03:00
|
|
|
actual_json = self.handler.do_post()
|
|
|
|
self.assertEqual('new_admin@example.com', actual_json['email'])
|
2022-07-14 00:31:27 +03:00
|
|
|
self.assertTrue(actual_json['is_site_editor'])
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertTrue(actual_json['is_admin'])
|
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
new_appuser = user_models.AppUser.query(
|
|
|
|
user_models.AppUser.email == 'new_admin@example.com').get()
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertEqual('new_admin@example.com', new_appuser.email)
|
|
|
|
self.assertTrue(new_appuser.is_admin)
|
|
|
|
|
2022-10-19 17:14:52 +03:00
|
|
|
# Clean up
|
|
|
|
new_appuser.key.delete()
|
|
|
|
|
2021-05-20 01:21:18 +03:00
|
|
|
def test_create__forbidden(self):
|
|
|
|
"""Regular user cannot create an account."""
|
|
|
|
testing_config.sign_in('one@example.com', 123567890)
|
|
|
|
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(self.request_path):
|
2021-05-20 01:21:18 +03:00
|
|
|
with self.assertRaises(werkzeug.exceptions.Forbidden):
|
|
|
|
self.handler.do_post(self.appuser_id)
|
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
new_appuser = user_models.AppUser.query(
|
|
|
|
user_models.AppUser.email == 'new@example.com').get()
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertIsNone(new_appuser)
|
|
|
|
|
2022-07-14 00:31:27 +03:00
|
|
|
def test_create__site_editor_forbidden(self):
|
|
|
|
"""Site editors cannot create an account."""
|
|
|
|
testing_config.sign_in('editor@example.com', 123567890)
|
|
|
|
|
|
|
|
with test_app.test_request_context(self.request_path):
|
|
|
|
with self.assertRaises(werkzeug.exceptions.Forbidden):
|
|
|
|
self.handler.do_post()
|
2022-08-18 04:14:19 +03:00
|
|
|
|
|
|
|
new_appuser = user_models.AppUser.query(
|
|
|
|
user_models.AppUser.email == 'new@example.com').get()
|
2022-07-14 00:31:27 +03:00
|
|
|
self.assertIsNone(new_appuser)
|
|
|
|
|
2021-05-20 01:21:18 +03:00
|
|
|
def test_create__invalid(self):
|
|
|
|
"""We cannot create an account without an email address."""
|
2021-06-09 20:35:55 +03:00
|
|
|
testing_config.sign_in('admin@example.com', 123567890)
|
2021-05-20 01:21:18 +03:00
|
|
|
|
|
|
|
json_data = {'isAdmin': False} # No email
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(self.request_path, json=json_data):
|
2021-09-22 03:49:58 +03:00
|
|
|
with self.assertRaises(werkzeug.exceptions.BadRequest):
|
2021-05-20 01:21:18 +03:00
|
|
|
self.handler.do_post()
|
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
new_appuser = user_models.AppUser.query(
|
|
|
|
user_models.AppUser.email == 'new@example.com').get()
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertIsNone(new_appuser)
|
|
|
|
|
|
|
|
def test_create__duplicate(self):
|
|
|
|
"""We cannot create an account with a duplicate email."""
|
2021-06-09 20:35:55 +03:00
|
|
|
testing_config.sign_in('admin@example.com', 123567890)
|
2021-05-20 01:21:18 +03:00
|
|
|
|
|
|
|
json_data = {'email': 'user@example.com'}
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(self.request_path, json=json_data):
|
2021-05-20 01:21:18 +03:00
|
|
|
with self.assertRaises(werkzeug.exceptions.BadRequest):
|
|
|
|
self.handler.do_post()
|
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
unrevised_appuser = user_models.AppUser.get_by_id(self.appuser_id)
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertEqual('user@example.com', unrevised_appuser.email)
|
|
|
|
|
|
|
|
def test_delete__valid(self):
|
|
|
|
"""Admin wants to delete an account."""
|
2021-06-09 20:35:55 +03:00
|
|
|
testing_config.sign_in('admin@example.com', 123567890)
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(self.request_path):
|
2022-10-16 09:39:35 +03:00
|
|
|
actual_json = self.handler.do_delete(account_id=self.appuser_id)
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertEqual({'message': 'Done'}, actual_json)
|
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
revised_appuser = user_models.AppUser.get_by_id(self.appuser_id)
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertIsNone(revised_appuser)
|
|
|
|
|
|
|
|
def test_delete__forbidden(self):
|
|
|
|
"""Regular user cannot delete an account."""
|
|
|
|
testing_config.sign_in('one@example.com', 123567890)
|
|
|
|
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(self.request_path):
|
2021-05-20 01:21:18 +03:00
|
|
|
with self.assertRaises(werkzeug.exceptions.Forbidden):
|
2022-10-16 09:39:35 +03:00
|
|
|
self.handler.do_delete(account_id=self.appuser_id)
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
unrevised_appuser = user_models.AppUser.get_by_id(self.appuser_id)
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertEqual('user@example.com', unrevised_appuser.email)
|
|
|
|
|
|
|
|
def test_delete__invalid(self):
|
|
|
|
"""We cannot delete an account without an account_id."""
|
2021-06-09 20:35:55 +03:00
|
|
|
testing_config.sign_in('admin@example.com', 123567890)
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(self.request_path):
|
2021-05-20 01:21:18 +03:00
|
|
|
with self.assertRaises(werkzeug.exceptions.BadRequest):
|
2022-10-16 09:39:35 +03:00
|
|
|
self.handler.do_delete()
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
unrevised_appuser = user_models.AppUser.get_by_id(self.appuser_id)
|
2021-05-20 01:21:18 +03:00
|
|
|
self.assertEqual('user@example.com', unrevised_appuser.email)
|
|
|
|
|
|
|
|
|
|
|
|
def test_delete__not_found(self):
|
|
|
|
"""We cannot delete an account with the wrong account_id."""
|
2021-06-09 20:35:55 +03:00
|
|
|
testing_config.sign_in('admin@example.com', 123567890)
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2021-09-23 23:01:43 +03:00
|
|
|
with test_app.test_request_context(self.request_path):
|
2021-05-20 01:21:18 +03:00
|
|
|
with self.assertRaises(werkzeug.exceptions.NotFound):
|
2022-10-16 09:39:35 +03:00
|
|
|
self.handler.do_delete(account_id=self.appuser_id + 1)
|
2021-05-20 01:21:18 +03:00
|
|
|
|
2022-08-18 04:14:19 +03:00
|
|
|
unrevised_appuser = user_models.AppUser.get_by_id(self.appuser_id)
|
2021-09-22 03:49:58 +03:00
|
|
|
self.assertEqual('user@example.com', unrevised_appuser.email)
|