summaryrefslogtreecommitdiff
path: root/src/leap/base/tests/test_config.py
diff options
context:
space:
mode:
authorkali <kali@leap.se>2012-08-27 05:13:29 +0900
committerkali <kali@leap.se>2012-08-27 05:13:29 +0900
commitc2786de7d2d09d600fa516f93cc1a1f851f0d705 (patch)
tree8b14f3b74b7f9e506fb77eb9826a0c4843507667 /src/leap/base/tests/test_config.py
parentc2955d9655e3e7ccb3c5fc9e7d00a0d493d48a40 (diff)
make provider-fetch tests pass
test provider-definition dump functions. refactored the set-temp-to-home methods to setUpClass separate provider-fetch tests on different testcases (so they actually call to requests).
Diffstat (limited to 'src/leap/base/tests/test_config.py')
-rw-r--r--src/leap/base/tests/test_config.py100
1 files changed, 75 insertions, 25 deletions
diff --git a/src/leap/base/tests/test_config.py b/src/leap/base/tests/test_config.py
index c5231de2..73b0f32c 100644
--- a/src/leap/base/tests/test_config.py
+++ b/src/leap/base/tests/test_config.py
@@ -1,12 +1,15 @@
+import json
import os
import platform
import socket
-import tempfile
+#import tempfile
import mock
import requests
from leap.base import config
+from leap.base import exceptions
+from leap.util.fileutil import mkdir_p
from leap.testing.basetest import BaseLeapTest
@@ -18,29 +21,60 @@ except ImportError:
_system = platform.system()
-class DefinitionTestCase(BaseLeapTest):
+class ProviderTest(BaseLeapTest):
+ # override per test fixtures
+ def setUp(self):
+ pass
+
+ def tearDown(self):
+ pass
+
+
+class BareHomeTestCase(ProviderTest):
+
+ __name__ = "provider_config_tests"
+
+ def test_should_raise_if_missing_eip_json(self):
+ with self.assertRaises(exceptions.MissingConfigFileError):
+ config.get_config_json(os.path.join(self.home, 'eip.json'))
+
+
+class ProviderDefinitionTestCase(ProviderTest):
# XXX See how to merge with test_providers
# -- kali 2012-08-24 00:38
__name__ = "provider_config_tests"
def setUp(self):
- self.old_home = os.environ['HOME']
- self.home = tempfile.mkdtemp()
- os.environ['HOME'] = self.home
- pass
-
- #Not correct removing the test directories but will be refactor out
- #with kali's new test classes
- def tearDown(self):
- os.environ['HOME'] = self.old_home
- pass
+ # dump a sample eip file
+ # XXX Move to Use EIP Spec Instead!!!
+ EIP_JSON = {
+ "provider": "testprovider.org",
+ "transport": "openvpn",
+ "openvpn_protocol": "tcp",
+ "openvpn_port": "80",
+ "openvpn_ca_certificate": "~/.config/leap/testprovider.org/"
+ "keys/ca/testprovider-ca-cert-"
+ "2013-01-01.pem",
+ "openvpn_client_certificate": "~/.config/leap/testprovider.org/"
+ "keys/client/openvpn-2012-09-31.pem",
+ "connect_on_login": True,
+ "block_cleartext_traffic": True,
+ "primary_gateway": "usa_west",
+ "secondary_gateway": "france",
+ "management_password": "oph7Que1othahwiech6J"
+ }
+ path = os.path.join(self.home, '.config', 'leap')
+ mkdir_p(path)
+ with open(os.path.join(path, 'eip.json'), 'w') as fp:
+ json.dump(EIP_JSON, fp)
def test_complete_file(self):
with mock.patch.object(requests, "get") as mock_method:
mock_method.return_value.status_code = 200
mock_method.return_value.json = {
+ #XXX get from providers template
u'api_uri': u'https://api.testprovider.org/',
u'api_version': u'0.1.0',
u'ca_cert': u'8aab80ae4326fd30721689db813733783fe0bd7e',
@@ -56,23 +90,46 @@ class DefinitionTestCase(BaseLeapTest):
cf = config.Configuration("http://localhost/")
self.assertIn('default', cf.providers)
+#
+# provider fetch tests block
+#
+
+# these tests below should move to wherever
+# we put the fetcher for provider files and related stuff.
+# TODO:
+# - We're instantiating a ProviderTest because we're doing the home wipeoff
+# on setUpClass instead of the setUp (for speedup of the general cases).
+
+# We really should be testing all of them in the same testCase, and
+# doing an extra wipe of the tempdir... but be careful!!!! do not mess with
+# os.environ home more than needed... that could potentially bite!
+
+
+class ProviderFetchConError(ProviderTest):
def test_connection_error(self):
with mock.patch.object(requests, "get") as mock_method:
mock_method.side_effect = requests.ConnectionError
cf = config.Configuration()
self.assertIsInstance(cf.error, str)
+
+class ProviderFetchHttpError(ProviderTest):
def test_file_not_found(self):
with mock.patch.object(requests, "get") as mock_method:
mock_method.side_effect = requests.HTTPError
cf = config.Configuration()
self.assertIsInstance(cf.error, str)
+
+class ProviderFetchInvalidUrl(ProviderTest):
def test_invalid_url(self):
cf = config.Configuration("ht")
self.assertTrue(cf.error)
+# end provider fetch tests
+
+
class ConfigHelperFunctions(BaseLeapTest):
__name__ = "config_helper_tests"
@@ -83,12 +140,7 @@ class ConfigHelperFunctions(BaseLeapTest):
def tearDown(self):
pass
- #
# tests
- #
-
- # XXX fixme! /home/user should
- # be replaced for proper home lookup.
@unittest.skipUnless(_system == "Linux", "linux only")
def test_lin_get_config_file(self):
@@ -98,8 +150,9 @@ class ConfigHelperFunctions(BaseLeapTest):
self.assertEqual(
config.get_config_file(
'test', folder="foo/bar"),
- '/home/%s/.config/leap/foo/bar/test' %
- config.get_username())
+ os.path.expanduser(
+ '~/.config/leap/foo/bar/test')
+ )
@unittest.skipUnless(_system == "Darwin", "mac only")
def test_mac_get_config_file(self):
@@ -130,10 +183,7 @@ class ConfigHelperFunctions(BaseLeapTest):
"""
self.assertEqual(
config.get_config_dir(),
- #XXX not correct!!!
- #hardcoded home
- '/home/%s/.config/leap' %
- self.get_username())
+ os.path.expanduser('~/.config/leap'))
@unittest.skipUnless(_system == "Darwin", "mac only")
def test_mac_get_config_dir(self):
@@ -158,8 +208,8 @@ class ConfigHelperFunctions(BaseLeapTest):
"""
self.assertEqual(
config.get_default_provider_path(),
- '/home/%s/.config/leap/providers/default/' %
- config.get_username())
+ os.path.expanduser('~/.config/leap/providers/default/')
+ )
# validate ip