From 8a1b48dd8c6262d2915ee0263fd25e9a28d020fa Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Tue, 14 Feb 2017 14:30:53 -0500 Subject: [PATCH] move ConfigProvider ctxmgr back to su tests --- test/test_api_usage.py | 15 +++----------- test/test_suconfig_api.py | 41 ++++++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/test/test_api_usage.py b/test/test_api_usage.py index d5c738e59..0da570007 100644 --- a/test/test_api_usage.py +++ b/test/test_api_usage.py @@ -7,10 +7,10 @@ import time import re 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 calendar import timegm +from contextlib import contextmanager +from httmock import urlmatch, HTTMock, all_requests from urllib import urlencode from urlparse import urlparse, urlunparse, parse_qs @@ -119,15 +119,6 @@ CSRF_TOKEN_KEY = '_csrf_token' 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): maxDiff = None diff --git a/test/test_suconfig_api.py b/test/test_suconfig_api.py index ea3bef651..f24b28f19 100644 --- a/test/test_suconfig_api.py +++ b/test/test_suconfig_api.py @@ -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, SuperUserCreateInitialSuperUser, SuperUserConfigValidate) from app import config_provider, all_queues @@ -8,16 +8,25 @@ from data import model 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): def test_registry_status(self): - with ConfigForTesting(): + with FreshConfigProvider(): json = self.getJsonResponse(SuperUserRegistryStatus) self.assertEquals('upload-license', json['status']) class TestSuperUserConfigFile(ApiTestCase): def test_get_non_superuser(self): - with ConfigForTesting(): + with FreshConfigProvider(): # No user. 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) def test_get_superuser_invalid_filename(self): - with ConfigForTesting(): + with FreshConfigProvider(): self.login(ADMIN_ACCESS_USER) self.getResponse(SuperUserConfigFile, params=dict(filename='somefile'), expected_code=404) def test_get_superuser(self): - with ConfigForTesting(): + with FreshConfigProvider(): self.login(ADMIN_ACCESS_USER) result = self.getJsonResponse(SuperUserConfigFile, params=dict(filename='ssl.cert')) self.assertFalse(result['exists']) def test_post_non_superuser(self): - with ConfigForTesting(): + with FreshConfigProvider(): # No user, before config.yaml exists. 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) def test_post_superuser_invalid_filename(self): - with ConfigForTesting(): + with FreshConfigProvider(): self.login(ADMIN_ACCESS_USER) self.postResponse(SuperUserConfigFile, params=dict(filename='somefile'), expected_code=404) def test_post_superuser(self): - with ConfigForTesting(): + with FreshConfigProvider(): self.login(ADMIN_ACCESS_USER) self.postResponse(SuperUserConfigFile, params=dict(filename='ssl.cert'), expected_code=400) class TestSuperUserCreateInitialSuperUser(ApiTestCase): def test_no_config_file(self): - with ConfigForTesting(): + with FreshConfigProvider(): # If there is no config.yaml, then this method should security fail. data = dict(username='cooluser', password='password', email='fake@example.com') self.postResponse(SuperUserCreateInitialSuperUser, data=data, expected_code=403) def test_config_file_with_db_users(self): - with ConfigForTesting(): + with FreshConfigProvider(): # Write some config. self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar')) @@ -80,7 +89,7 @@ class TestSuperUserCreateInitialSuperUser(ApiTestCase): self.postResponse(SuperUserCreateInitialSuperUser, data=data, expected_code=403) def test_config_file_with_no_db_users(self): - with ConfigForTesting(): + with FreshConfigProvider(): # Write some config. self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar')) @@ -103,7 +112,7 @@ class TestSuperUserCreateInitialSuperUser(ApiTestCase): class TestSuperUserConfigValidate(ApiTestCase): def test_nonsuperuser_noconfig(self): - with ConfigForTesting(): + with FreshConfigProvider(): self.login(ADMIN_ACCESS_USER) result = self.postJsonResponse(SuperUserConfigValidate, params=dict(service='someservice'), data=dict(config={})) @@ -112,7 +121,7 @@ class TestSuperUserConfigValidate(ApiTestCase): 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. # Add a config, and verify it breaks when unauthenticated. json = self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar')) @@ -132,7 +141,7 @@ class TestSuperUserConfigValidate(ApiTestCase): class TestSuperUserConfig(ApiTestCase): def test_get_non_superuser(self): - with ConfigForTesting(): + with FreshConfigProvider(): # No user. self.getResponse(SuperUserConfig, expected_code=401) @@ -141,7 +150,7 @@ class TestSuperUserConfig(ApiTestCase): self.getResponse(SuperUserConfig, expected_code=403) def test_get_superuser(self): - with ConfigForTesting(): + with FreshConfigProvider(): self.login(ADMIN_ACCESS_USER) json = self.getJsonResponse(SuperUserConfig) @@ -150,7 +159,7 @@ class TestSuperUserConfig(ApiTestCase): self.assertIsNone(json['config']) 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 # try writing it without a superuser present. json = self.putJsonResponse(SuperUserConfig, data=dict(config={}, hostname='foobar'))