move ConfigProvider ctxmgr back to su tests

This commit is contained in:
Jimmy Zelinskie 2017-02-14 14:30:53 -05:00
parent 9df8e924a5
commit 8a1b48dd8c
2 changed files with 28 additions and 28 deletions

View file

@ -7,10 +7,10 @@ import time
import re import re
import json as py_json import json as py_json
from contextlib import contextmanager
from calendar import timegm
from httmock import urlmatch, HTTMock, all_requests
from StringIO import StringIO from StringIO import StringIO
from calendar import timegm
from contextlib import contextmanager
from httmock import urlmatch, HTTMock, all_requests
from urllib import urlencode from urllib import urlencode
from urlparse import urlparse, urlunparse, parse_qs from urlparse import urlparse, urlunparse, parse_qs
@ -119,15 +119,6 @@ CSRF_TOKEN_KEY = '_csrf_token'
CSRF_TOKEN = '123csrfforme' CSRF_TOKEN = '123csrfforme'
class ConfigForTesting(object):
def __enter__(self):
config_provider.reset_for_test()
return config_provider
def __exit__(self, type, value, traceback):
config_provider.reset_for_test()
class ApiTestCase(unittest.TestCase): class ApiTestCase(unittest.TestCase):
maxDiff = None maxDiff = None

View file

@ -1,4 +1,4 @@
from test.test_api_usage import ApiTestCase, READ_ACCESS_USER, ADMIN_ACCESS_USER, ConfigForTesting from test.test_api_usage import ApiTestCase, READ_ACCESS_USER, ADMIN_ACCESS_USER
from endpoints.api.suconfig import (SuperUserRegistryStatus, SuperUserConfig, SuperUserConfigFile, from endpoints.api.suconfig import (SuperUserRegistryStatus, SuperUserConfig, SuperUserConfigFile,
SuperUserCreateInitialSuperUser, SuperUserConfigValidate) SuperUserCreateInitialSuperUser, SuperUserConfigValidate)
from app import config_provider, all_queues from app import config_provider, all_queues
@ -8,16 +8,25 @@ from data import model
import unittest import unittest
class FreshConfigProvider(object):
def __enter__(self):
config_provider.reset_for_test()
return config_provider
def __exit__(self, type, value, traceback):
config_provider.reset_for_test()
class TestSuperUserRegistryStatus(ApiTestCase): class TestSuperUserRegistryStatus(ApiTestCase):
def test_registry_status(self): def test_registry_status(self):
with ConfigForTesting(): with FreshConfigProvider():
json = self.getJsonResponse(SuperUserRegistryStatus) json = self.getJsonResponse(SuperUserRegistryStatus)
self.assertEquals('upload-license', json['status']) self.assertEquals('upload-license', json['status'])
class TestSuperUserConfigFile(ApiTestCase): class TestSuperUserConfigFile(ApiTestCase):
def test_get_non_superuser(self): def test_get_non_superuser(self):
with ConfigForTesting(): with FreshConfigProvider():
# No user. # No user.
self.getResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=403) self.getResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=403)
@ -26,18 +35,18 @@ class TestSuperUserConfigFile(ApiTestCase):
self.getResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=403) self.getResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=403)
def test_get_superuser_invalid_filename(self): def test_get_superuser_invalid_filename(self):
with ConfigForTesting(): with FreshConfigProvider():
self.login(ADMIN_ACCESS_USER) self.login(ADMIN_ACCESS_USER)
self.getResponse(SuperUserConfigFile, params=dict(filename='somefile'), expected_code=404) self.getResponse(SuperUserConfigFile, params=dict(filename='somefile'), expected_code=404)
def test_get_superuser(self): def test_get_superuser(self):
with ConfigForTesting(): with FreshConfigProvider():
self.login(ADMIN_ACCESS_USER) self.login(ADMIN_ACCESS_USER)
result = self.getJsonResponse(SuperUserConfigFile, params=dict(filename='ssl.cert')) result = self.getJsonResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'))
self.assertFalse(result['exists']) self.assertFalse(result['exists'])
def test_post_non_superuser(self): def test_post_non_superuser(self):
with ConfigForTesting(): with FreshConfigProvider():
# No user, before config.yaml exists. # No user, before config.yaml exists.
self.postResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=400) self.postResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=400)
@ -52,25 +61,25 @@ class TestSuperUserConfigFile(ApiTestCase):
self.postResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=403) self.postResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=403)
def test_post_superuser_invalid_filename(self): def test_post_superuser_invalid_filename(self):
with ConfigForTesting(): with FreshConfigProvider():
self.login(ADMIN_ACCESS_USER) self.login(ADMIN_ACCESS_USER)
self.postResponse(SuperUserConfigFile, params=dict(filename='somefile'), expected_code=404) self.postResponse(SuperUserConfigFile, params=dict(filename='somefile'), expected_code=404)
def test_post_superuser(self): def test_post_superuser(self):
with ConfigForTesting(): with FreshConfigProvider():
self.login(ADMIN_ACCESS_USER) self.login(ADMIN_ACCESS_USER)
self.postResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=400) self.postResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=400)
class TestSuperUserCreateInitialSuperUser(ApiTestCase): class TestSuperUserCreateInitialSuperUser(ApiTestCase):
def test_no_config_file(self): def test_no_config_file(self):
with ConfigForTesting(): with FreshConfigProvider():
# If there is no config.yaml, then this method should security fail. # If there is no config.yaml, then this method should security fail.
data = dict(username='cooluser', password='password', email='fake@example.com') data = dict(username='cooluser', password='password', email='fake@example.com')
self.postResponse(SuperUserCreateInitialSuperUser, data=data, expected_code=403) self.postResponse(SuperUserCreateInitialSuperUser, data=data, expected_code=403)
def test_config_file_with_db_users(self): def test_config_file_with_db_users(self):
with ConfigForTesting(): with FreshConfigProvider():
# Write some config. # Write some config.
self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar')) self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar'))
@ -80,7 +89,7 @@ class TestSuperUserCreateInitialSuperUser(ApiTestCase):
self.postResponse(SuperUserCreateInitialSuperUser, data=data, expected_code=403) self.postResponse(SuperUserCreateInitialSuperUser, data=data, expected_code=403)
def test_config_file_with_no_db_users(self): def test_config_file_with_no_db_users(self):
with ConfigForTesting(): with FreshConfigProvider():
# Write some config. # Write some config.
self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar')) self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar'))
@ -103,7 +112,7 @@ class TestSuperUserCreateInitialSuperUser(ApiTestCase):
class TestSuperUserConfigValidate(ApiTestCase): class TestSuperUserConfigValidate(ApiTestCase):
def test_nonsuperuser_noconfig(self): def test_nonsuperuser_noconfig(self):
with ConfigForTesting(): with FreshConfigProvider():
self.login(ADMIN_ACCESS_USER) self.login(ADMIN_ACCESS_USER)
result = self.postJsonResponse(SuperUserConfigValidate, params=dict(service='someservice'), result = self.postJsonResponse(SuperUserConfigValidate, params=dict(service='someservice'),
data=dict(config={})) data=dict(config={}))
@ -112,7 +121,7 @@ class TestSuperUserConfigValidate(ApiTestCase):
def test_nonsuperuser_config(self): def test_nonsuperuser_config(self):
with ConfigForTesting(): with FreshConfigProvider():
# The validate config call works if there is no config.yaml OR the user is a superuser. # 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. # Add a config, and verify it breaks when unauthenticated.
json = self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar')) json = self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar'))
@ -132,7 +141,7 @@ class TestSuperUserConfigValidate(ApiTestCase):
class TestSuperUserConfig(ApiTestCase): class TestSuperUserConfig(ApiTestCase):
def test_get_non_superuser(self): def test_get_non_superuser(self):
with ConfigForTesting(): with FreshConfigProvider():
# No user. # No user.
self.getResponse(SuperUserConfig, expected_code=401) self.getResponse(SuperUserConfig, expected_code=401)
@ -141,7 +150,7 @@ class TestSuperUserConfig(ApiTestCase):
self.getResponse(SuperUserConfig, expected_code=403) self.getResponse(SuperUserConfig, expected_code=403)
def test_get_superuser(self): def test_get_superuser(self):
with ConfigForTesting(): with FreshConfigProvider():
self.login(ADMIN_ACCESS_USER) self.login(ADMIN_ACCESS_USER)
json = self.getJsonResponse(SuperUserConfig) json = self.getJsonResponse(SuperUserConfig)
@ -150,7 +159,7 @@ class TestSuperUserConfig(ApiTestCase):
self.assertIsNone(json['config']) self.assertIsNone(json['config'])
def test_put(self): def test_put(self):
with ConfigForTesting() as config: with FreshConfigProvider() as config:
# The update config call works if there is no config.yaml OR the user is a superuser. First # 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. # try writing it without a superuser present.
json = self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar')) json = self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar'))