Move config handling into a provider class to make testing much easier
This commit is contained in:
parent
c0c27648ea
commit
6d604a656a
8 changed files with 207 additions and 121 deletions
|
@ -1,19 +1,28 @@
|
|||
from test.test_api_usage import ApiTestCase, READ_ACCESS_USER, ADMIN_ACCESS_USER
|
||||
from endpoints.api.suconfig import (SuperUserRegistryStatus, SuperUserConfig, SuperUserConfigFile,
|
||||
SuperUserCreateInitialSuperUser, SuperUserConfigValidate)
|
||||
from app import OVERRIDE_CONFIG_YAML_FILENAME
|
||||
from app import CONFIG_PROVIDER
|
||||
from data.database import User
|
||||
|
||||
import unittest
|
||||
import os
|
||||
|
||||
|
||||
class ConfigForTesting(object):
|
||||
def __enter__(self):
|
||||
CONFIG_PROVIDER.reset_for_test()
|
||||
return CONFIG_PROVIDER
|
||||
|
||||
def __exit__(self, type, value, traceback):
|
||||
pass
|
||||
|
||||
class TestSuperUserRegistryStatus(ApiTestCase):
|
||||
def test_registry_status(self):
|
||||
json = self.getJsonResponse(SuperUserRegistryStatus)
|
||||
self.assertTrue(json['is_testing'])
|
||||
self.assertTrue(json['valid_db'])
|
||||
self.assertFalse(json['file_exists'])
|
||||
self.assertFalse(json['ready'])
|
||||
with ConfigForTesting():
|
||||
json = self.getJsonResponse(SuperUserRegistryStatus)
|
||||
self.assertTrue(json['is_testing'])
|
||||
self.assertTrue(json['valid_db'])
|
||||
self.assertFalse(json['file_exists'])
|
||||
self.assertFalse(json['ready'])
|
||||
|
||||
|
||||
class TestSuperUserConfigFile(ApiTestCase):
|
||||
|
@ -58,7 +67,7 @@ class TestSuperUserCreateInitialSuperUser(ApiTestCase):
|
|||
self.postResponse(SuperUserCreateInitialSuperUser, data=data, expected_code=403)
|
||||
|
||||
def test_config_file_with_db_users(self):
|
||||
try:
|
||||
with ConfigForTesting():
|
||||
# Write some config.
|
||||
self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar'))
|
||||
|
||||
|
@ -66,11 +75,9 @@ class TestSuperUserCreateInitialSuperUser(ApiTestCase):
|
|||
# fail.
|
||||
data = dict(username='cooluser', password='password', email='fake@example.com')
|
||||
self.postResponse(SuperUserCreateInitialSuperUser, data=data, expected_code=403)
|
||||
finally:
|
||||
os.remove(OVERRIDE_CONFIG_YAML_FILENAME)
|
||||
|
||||
def test_config_file_with_no_db_users(self):
|
||||
try:
|
||||
with ConfigForTesting():
|
||||
# Write some config.
|
||||
self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar'))
|
||||
|
||||
|
@ -90,9 +97,6 @@ class TestSuperUserCreateInitialSuperUser(ApiTestCase):
|
|||
result = self.getJsonResponse(SuperUserConfig)
|
||||
self.assertEquals(['cooluser'], result['config']['SUPER_USERS'])
|
||||
|
||||
finally:
|
||||
os.remove(OVERRIDE_CONFIG_YAML_FILENAME)
|
||||
|
||||
|
||||
class TestSuperUserConfigValidate(ApiTestCase):
|
||||
def test_nonsuperuser_noconfig(self):
|
||||
|
@ -104,7 +108,7 @@ class TestSuperUserConfigValidate(ApiTestCase):
|
|||
|
||||
|
||||
def test_nonsuperuser_config(self):
|
||||
try:
|
||||
with ConfigForTesting():
|
||||
# The validate config call works if there is no config.yaml OR the user is a superuser.
|
||||
# Add a config, and verify it breaks when unauthenticated.
|
||||
json = self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar'))
|
||||
|
@ -120,8 +124,6 @@ class TestSuperUserConfigValidate(ApiTestCase):
|
|||
data=dict(config={}))
|
||||
|
||||
self.assertFalse(result['status'])
|
||||
finally:
|
||||
os.remove(OVERRIDE_CONFIG_YAML_FILENAME)
|
||||
|
||||
|
||||
class TestSuperUserConfig(ApiTestCase):
|
||||
|
@ -142,14 +144,14 @@ class TestSuperUserConfig(ApiTestCase):
|
|||
self.assertIsNone(json['config'])
|
||||
|
||||
def test_put(self):
|
||||
try:
|
||||
with ConfigForTesting() as config:
|
||||
# The update config call works if there is no config.yaml OR the user is a superuser. First
|
||||
# try writing it without a superuser present.
|
||||
json = self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar'))
|
||||
self.assertTrue(json['exists'])
|
||||
|
||||
# Verify the config.yaml file exists.
|
||||
self.assertTrue(os.path.exists(OVERRIDE_CONFIG_YAML_FILENAME))
|
||||
# Verify the config file exists.
|
||||
self.assertTrue(config.yaml_exists())
|
||||
|
||||
# Try writing it again. This should now fail, since the config.yaml exists.
|
||||
self.putResponse(SuperUserConfig, data=dict(config={}, hostname='barbaz'), expected_code=403)
|
||||
|
@ -170,8 +172,6 @@ class TestSuperUserConfig(ApiTestCase):
|
|||
json = self.getJsonResponse(SuperUserConfig)
|
||||
self.assertIsNotNone(json['config'])
|
||||
|
||||
finally:
|
||||
os.remove(OVERRIDE_CONFIG_YAML_FILENAME)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Reference in a new issue