def test_main_arguments(): """Test mocking the arguments and running them through main()""" args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD, tenantcode=RANDOM_TENANTCODE, directory=CONFIG_DIR) result = Config(config_dir=CONFIG_DIR, output="wso_args.json").main(args) assert result is True
def test_simple_get(): """Test simple_get fucntion""" filename = 'postman-config.json' # Create mock args = argparse.Namespace(url="postman-echo.com", username=RANDOM_USERNAME, password=RANDOM_PASSWORD, tenantcode=RANDOM_TENANTCODE, proxyserver=None, proxyport=None, directory=CONFIG_DIR) Config(config_dir=CONFIG_DIR, output=filename).main(args) wso = WSO(config_dir=CONFIG_DIR, config_file=filename) # Check v2 default response = wso.simple_get("/headers/") assert response["headers"]["accept"] == "application/json;version=2" assert response["headers"]["authorization"] == AUTH.encode( RANDOM_USERNAME, RANDOM_PASSWORD) assert response["headers"]["aw-tenant-code"] == RANDOM_TENANTCODE # Check v1 response = wso.simple_get("/headers/", version=1) assert response["headers"]["accept"] == "application/json;version=1" assert response["headers"]["authorization"] == AUTH.encode( RANDOM_USERNAME, RANDOM_PASSWORD) assert response["headers"]["aw-tenant-code"] == RANDOM_TENANTCODE # Test querystring querystring = {} querystring[RANDOM_USERNAME] = RANDOM_TENANTCODE response = wso.simple_get("/get/", querystring=querystring, version=1) assert response["args"][RANDOM_USERNAME] == RANDOM_TENANTCODE # Test 204 returns None response = wso.simple_get("/status/204", version=1) assert response is None # Test over 9k size querystring["9k"] = "z" * 9000 response = wso.simple_get("/status/204", querystring=querystring, version=1)
def test_main_interactive(monkeypatch): """Test mocking the std in and writing the config""" url = StringIO('%s\n%s\n%s\n%s\n\n%s\n' % (RANDOM_URL, RANDOM_USERNAME, RANDOM_PASSWORD, RANDOM_TENANTCODE, CONFIG_DIR)) # Send std in and run the main function monkeypatch.setattr('sys.stdin', url) # Stage the empty args args = argparse.Namespace(url=None, username=None, password=None, tenantcode=None) result = Config(config_dir=CONFIG_DIR, output="wso_interactive.json").main(args) assert result is True
def test_import_proxy(): """Test mocking the arguments and running them through main()""" filename = 'wso_args_proxy.json' # Create mock args = argparse.Namespace(url=RANDOM_URL, username=RANDOM_USERNAME, password=RANDOM_PASSWORD, tenantcode=RANDOM_TENANTCODE, proxyserver=RANDOM_PROXYSERVER, proxyport=RANDOM_PROXYPORT, directory=CONFIG_DIR) result = Config(config_dir=CONFIG_DIR, output=filename).main(args) assert result is True assert AUTH.check_file_exists(filename) is True assert AUTH.verify_config(filename, 'authorization', AUTH.encode(RANDOM_USERNAME, RANDOM_PASSWORD)) is True assert AUTH.verify_config(filename, 'url', RANDOM_URL) is True assert AUTH.verify_config(filename, 'aw-tenant-code', RANDOM_TENANTCODE) is True assert AUTH.verify_config(filename, 'proxyserver', RANDOM_PROXYSERVER) is True assert AUTH.verify_config(filename, 'proxyport', RANDOM_PROXYPORT) is True proxy = WSO(config_dir=CONFIG_DIR, config_file="wso_args_proxy.json").import_proxy() expected_proxy = {} expected_proxy["http"] = "%s:%s" % (RANDOM_PROXYSERVER, RANDOM_PROXYPORT) expected_proxy["https"] = "%s:%s" % (RANDOM_PROXYSERVER, RANDOM_PROXYPORT) assert proxy == expected_proxy
"""Automated testing for configuring WSO""" import os import copy import string import random import argparse from io import StringIO from basic_auth import Auth from wso.configure import Config CONFIG_DIR = "config-tests" # Init package AUTH = Auth(config_dir=CONFIG_DIR) WSO_CONFIG = Config(config_dir=CONFIG_DIR) # Random int / str generators def random_string(string_length=15): """Generate a random string of fixed length """ letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(string_length)) def random_number(): """Generate a random string of fixed length """ return random.randint(0, 9999) # Create some random values to use for testing
def configure(self): """Interactive setup of config""" # Write config if none present Config().main(Config().get_args())