From 1ad2fab3b545553a65a46d2cc676e5371f92195e Mon Sep 17 00:00:00 2001 From: EvgeneOskin Date: Thu, 28 Jan 2016 09:42:39 +0600 Subject: [PATCH] Fix tests and bugs after refactoring. --- .pre-commit-config.yaml | 6 +-- .prospector.yaml | 6 ++- pavement.py | 2 +- .../cloud/commands/group.py | 2 +- .../cloud/commands/host.py | 2 +- serverauditor_sshconfig/cloud/cryptor.py | 4 +- serverauditor_sshconfig/core/api.py | 10 ---- serverauditor_sshconfig/core/models.py | 4 +- .../core/storage/__init__.py | 2 +- serverauditor_sshconfig/core/utils.py | 42 +++++++++++++++++ .../cloud/{test_models.py => models_test.py} | 1 + .../storage/{test_query.py => query_test.py} | 1 + .../{test_storage.py => storage_test.py} | 46 ++++++++++--------- tests/{test_cryptor.py => cryptor_test.py} | 0 ...{test_ssh_config.py => ssh_config_test.py} | 0 tox.ini | 1 + 16 files changed, 84 insertions(+), 45 deletions(-) rename tests/cloud/{test_models.py => models_test.py} (98%) rename tests/core/storage/{test_query.py => query_test.py} (98%) rename tests/core/storage/{test_storage.py => storage_test.py} (90%) rename tests/{test_cryptor.py => cryptor_test.py} (100%) rename tests/{test_ssh_config.py => ssh_config_test.py} (100%) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 7767e9c..95f9657 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,7 +14,7 @@ - id: trailing-whitespace - id: name-tests-test -- repo: https://github.com/guykisel/prospector-mirror - sha: 00fbd80101566b1b9c873c71f2ab7b95b8bd0a7d +- repo: https://github.com/pre-commit/pre-commit + sha: cdf726bbedb15f33ca60fdc397fec379946755fc hooks: - - id: prospector + - id: validate_config diff --git a/.prospector.yaml b/.prospector.yaml index d9fba43..5d4f0ca 100644 --- a/.prospector.yaml +++ b/.prospector.yaml @@ -7,9 +7,10 @@ ignore-paths: - .git - setup.py - build + - tests - serverauditor_sshconfig/core/ssh_config.py # it's too complex - serverauditor_sshconfig/core/cryptor.py # it's too complex - + - serverauditor_sshconfig/core/utils.py # it's too complex # TODO Remove this file in production - serverauditor_sshconfig/sync/services/aws.py @@ -20,4 +21,5 @@ pep257: pylint: options: max-parents: 12 - + disable: + - super-on-old-class diff --git a/pavement.py b/pavement.py index 1cb67c9..3ac662d 100644 --- a/pavement.py +++ b/pavement.py @@ -97,4 +97,4 @@ def lint(): @task def bats(): """Run tests on CLI usage.""" - sh('bats tests/integration') + sh('bats --tap tests/integration') diff --git a/serverauditor_sshconfig/cloud/commands/group.py b/serverauditor_sshconfig/cloud/commands/group.py index ff60b89..38e00b3 100644 --- a/serverauditor_sshconfig/cloud/commands/group.py +++ b/serverauditor_sshconfig/cloud/commands/group.py @@ -14,7 +14,7 @@ class GroupCommand(DetailCommand): def __init__(self, *args, **kwargs): """Construct new group command.""" - super(GroupCommand, self).__init__(self, *args, **kwargs) + super(GroupCommand, self).__init__(*args, **kwargs) self.ssh_config_args = SshConfigArgs() def get_parser(self, prog_name): diff --git a/serverauditor_sshconfig/cloud/commands/host.py b/serverauditor_sshconfig/cloud/commands/host.py index ec68397..5a88bef 100644 --- a/serverauditor_sshconfig/cloud/commands/host.py +++ b/serverauditor_sshconfig/cloud/commands/host.py @@ -15,7 +15,7 @@ class HostCommand(DetailCommand): def __init__(self, *args, **kwargs): """Construct new host command.""" - super(HostCommand, self).__init__(self, *args, **kwargs) + super(HostCommand, self).__init__(*args, **kwargs) self.ssh_config_args = SshConfigArgs() def get_parser(self, prog_name): diff --git a/serverauditor_sshconfig/cloud/cryptor.py b/serverauditor_sshconfig/cloud/cryptor.py index ff9e5f2..9dde53a 100644 --- a/serverauditor_sshconfig/cloud/cryptor.py +++ b/serverauditor_sshconfig/cloud/cryptor.py @@ -91,7 +91,7 @@ class RNCryptor(CryptoSettings): def pre_decrypt_data(self, data): """Patch ciphertext.""" data = to_bytes(data) - return base64.decodestring(data) + return base64.b64decode(data) # pylint: disable=no-self-use def post_decrypt_data(self, data): @@ -142,7 +142,7 @@ def pre_encrypt_data(self, data): # pylint: disable=no-self-use def post_encrypt_data(self, data): """Patch ciphertext.""" - data = base64.encodestring(data) + data = base64.b64encode(data) return to_str(data) def encrypt(self, data): diff --git a/serverauditor_sshconfig/core/api.py b/serverauditor_sshconfig/core/api.py index 8f36e1a..313bd38 100644 --- a/serverauditor_sshconfig/core/api.py +++ b/serverauditor_sshconfig/core/api.py @@ -30,16 +30,6 @@ def __call__(self, request): request.headers[self.header_name] = self.auth_header return request - def __str__(self): - """Convert it to string.""" - return unicode(self).encode('utf-8') - - def __unicode__(self): - """Convert it to string.""" - return '{key}: {value}'.format( - key=self.header_name, value=self.auth_header - ) - def hash_password(password): """Generate hash from password.""" diff --git a/serverauditor_sshconfig/core/models.py b/serverauditor_sshconfig/core/models.py index 49b2790..b404159 100644 --- a/serverauditor_sshconfig/core/models.py +++ b/serverauditor_sshconfig/core/models.py @@ -66,7 +66,7 @@ class RemoteInstance(AbstractModel): """Class that represent model sync revision.""" fields = { - 'id': Field(long, False, None), + 'id': Field(int, False, None), # States could be one of 'created' / 'updated' / 'synced' 'state': Field(str, False, 'created'), 'updated_at': Field(str, False, None), @@ -77,7 +77,7 @@ class Model(AbstractModel): """Base model with relations.""" _mandatory_fields = { - 'id': Field(long, False, None), + 'id': Field(int, False, None), 'remote_instance': Field(RemoteInstance, False, None) } diff --git a/serverauditor_sshconfig/core/storage/__init__.py b/serverauditor_sshconfig/core/storage/__init__.py index 4abdd0f..532d187 100644 --- a/serverauditor_sshconfig/core/storage/__init__.py +++ b/serverauditor_sshconfig/core/storage/__init__.py @@ -78,7 +78,7 @@ def save(self, model): Will return model with id and saved mapped fields Model instances with ids. """ - model = self.strategy.saver.save(model) + model = self.strategies.saver.save(model) if getattr(model, model.id_name): saved_model = self.update(model) else: diff --git a/serverauditor_sshconfig/core/utils.py b/serverauditor_sshconfig/core/utils.py index 1d242ba..95f7348 100644 --- a/serverauditor_sshconfig/core/utils.py +++ b/serverauditor_sshconfig/core/utils.py @@ -1,6 +1,48 @@ # -*- coding: utf-8 -*- """Miscellaneous extra functions.""" import os +from six import PY2, PY3 + + +if PY2: + p_input = raw_input + p_map = map + + def to_bytes(s): + if isinstance(s, str): + return s + if isinstance(s, unicode): + return s.encode('utf-8') + + to_str = to_bytes + + def bchr(s): + return chr(s) + + def bord(s): + return ord(s) + +elif PY3: + p_input = input + p_map = lambda f, it: list(map(f, it)) + + def to_bytes(s): + if isinstance(s, bytes): + return s + if isinstance(s, str): + return s.encode('utf-8') + + def to_str(s): + if isinstance(s, bytes): + return s.decode('utf-8') + if isinstance(s, str): + return s + + def bchr(s): + return bytes([s]) + + def bord(s): + return s def expand_and_format_path(paths, **kwargs): diff --git a/tests/cloud/test_models.py b/tests/cloud/models_test.py similarity index 98% rename from tests/cloud/test_models.py rename to tests/cloud/models_test.py index 03066c1..3fd5657 100644 --- a/tests/cloud/test_models.py +++ b/tests/cloud/models_test.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import six from collections import OrderedDict from mock import patch, Mock diff --git a/tests/core/storage/test_query.py b/tests/core/storage/query_test.py similarity index 98% rename from tests/core/storage/test_query.py rename to tests/core/storage/query_test.py index aded687..606eaea 100644 --- a/tests/core/storage/test_query.py +++ b/tests/core/storage/query_test.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from mock import patch, Mock from unittest import TestCase from serverauditor_sshconfig.core.storage.query import Query diff --git a/tests/core/storage/test_storage.py b/tests/core/storage/storage_test.py similarity index 90% rename from tests/core/storage/test_storage.py rename to tests/core/storage/storage_test.py index 9736f16..79db7f4 100644 --- a/tests/core/storage/test_storage.py +++ b/tests/core/storage/storage_test.py @@ -1,4 +1,6 @@ +# -*- coding: utf-8 -*- import tempfile +from six import integer_types from mock import patch from unittest import TestCase from serverauditor_sshconfig.cloud.models import ( @@ -60,20 +62,20 @@ def test_save_strategy(self): saved_sshidentity = self.storage.save(self.sshidentity) self.assertIsNotNone(saved_sshidentity.id) - self.assertIsInstance(saved_sshidentity.ssh_key, long) + self.assertIsInstance(saved_sshidentity.ssh_key, integer_types) self.sshconfig.ssh_identity = saved_sshidentity.id saved_sshconfig = self.storage.save(self.sshconfig) self.assertIsNotNone(saved_sshconfig.id) - self.assertIsInstance(saved_sshconfig.ssh_identity, long) + self.assertIsInstance(saved_sshconfig.ssh_identity, integer_types) self.host.ssh_config = saved_sshconfig.id saved_group = self.storage.save(self.group) self.assertIsNotNone(saved_group.id) self.host.group = saved_group.id saved_host = self.storage.save(self.host) - self.assertIsInstance(saved_host.ssh_config, long) - self.assertIsInstance(saved_host.group, long) + self.assertIsInstance(saved_host.ssh_config, integer_types) + self.assertIsInstance(saved_host.group, integer_types) self.assertIsNotNone(saved_host.id) def test_get_strategy(self): @@ -193,8 +195,8 @@ def test_save_strategy(self): saved_host = self.storage.save(self.host) self.assertIsNotNone(saved_host.id) - self.assertIsInstance(saved_host.ssh_config, long) - self.assertIsInstance(saved_host.group, long) + self.assertIsInstance(saved_host.ssh_config, integer_types) + self.assertIsInstance(saved_host.group, integer_types) saved_group = self.storage.get(Group, id=saved_host.group) self.assertIsNotNone(saved_group.id) @@ -202,14 +204,14 @@ def test_save_strategy(self): saved_sshconfig = self.storage.get(SshConfig, id=saved_host.ssh_config) self.assertIsNotNone(saved_sshconfig.id) self.assertIsInstance(saved_sshconfig.ssh_identity, SshIdentity) - self.assertIsInstance(saved_sshconfig.ssh_identity.id, long) + self.assertIsInstance(saved_sshconfig.ssh_identity.id, integer_types) saved_sshidentity = self.storage.get( SshIdentity, id=saved_sshconfig.ssh_identity.id ) self.assertIsNotNone(saved_sshidentity.id) self.assertIsInstance(saved_sshidentity.ssh_key, SshKey) - self.assertIsInstance(saved_sshidentity.ssh_key.id, long) + self.assertIsInstance(saved_sshidentity.ssh_key.id, integer_types) saved_sshkey = self.storage.get( SshKey, id=saved_sshidentity.ssh_key.id @@ -234,21 +236,21 @@ def test_get_strategy(self): self.assertEqual(got_host.label, self.host.label) self.assertIsInstance(got_host.group, Group) - self.assertIsInstance(got_host.group.id, long) + self.assertIsInstance(got_host.group.id, integer_types) self.assertEqual(got_host.group.label, self.group.label) self.assertIsInstance(got_host.ssh_config, SshConfig) - self.assertIsInstance(got_host.ssh_config.id, long) + self.assertIsInstance(got_host.ssh_config.id, integer_types) self.assertEqual(got_host.ssh_config.port, self.sshconfig.port) self.assertIsInstance(got_host.ssh_config.ssh_identity, SshIdentity) - self.assertIsInstance(got_host.ssh_config.ssh_identity.id, long) + self.assertIsInstance(got_host.ssh_config.ssh_identity.id, integer_types) self.assertEqual(got_host.ssh_config.ssh_identity.label, self.sshidentity.label) self.assertIsInstance(got_host.ssh_config.ssh_identity.ssh_key, SshKey) self.assertIsInstance(got_host.ssh_config.ssh_identity.ssh_key.id, - long) + integer_types) self.assertEqual(got_host.ssh_config.ssh_identity.ssh_key.label, self.sshkey.label) @@ -265,12 +267,12 @@ def test_get_strategy(self): self.assertEqual(got_sshconfig.port, self.sshconfig.port) self.assertIsInstance(got_sshconfig.ssh_identity, SshIdentity) - self.assertIsInstance(got_sshconfig.ssh_identity.id, long) + self.assertIsInstance(got_sshconfig.ssh_identity.id, integer_types) self.assertEqual(got_sshconfig.ssh_identity.label, self.sshidentity.label) self.assertIsInstance(got_sshconfig.ssh_identity.ssh_key, SshKey) - self.assertIsInstance(got_sshconfig.ssh_identity.ssh_key.id, long) + self.assertIsInstance(got_sshconfig.ssh_identity.ssh_key.id, integer_types) self.assertEqual(got_sshconfig.ssh_identity.ssh_key.label, self.sshkey.label) @@ -282,7 +284,7 @@ def test_get_strategy(self): self.assertEqual(got_sshidentity.label, self.sshidentity.label) self.assertIsInstance(got_sshidentity.ssh_key, SshKey) - self.assertIsInstance(got_sshidentity.ssh_key.id, long) + self.assertIsInstance(got_sshidentity.ssh_key.id, integer_types) self.assertEqual(got_sshidentity.ssh_key.label, self.sshkey.label) got_sshkey = self.storage.get( @@ -312,21 +314,21 @@ def test_get_all_strategy(self): self.assertEqual(got_host.label, self.host.label) self.assertIsInstance(got_host.group, Group) - self.assertIsInstance(got_host.group.id, long) + self.assertIsInstance(got_host.group.id, integer_types) self.assertEqual(got_host.group.label, self.group.label) self.assertIsInstance(got_host.ssh_config, SshConfig) - self.assertIsInstance(got_host.ssh_config.id, long) + self.assertIsInstance(got_host.ssh_config.id, integer_types) self.assertEqual(got_host.ssh_config.port, self.sshconfig.port) self.assertIsInstance(got_host.ssh_config.ssh_identity, SshIdentity) - self.assertIsInstance(got_host.ssh_config.ssh_identity.id, long) + self.assertIsInstance(got_host.ssh_config.ssh_identity.id, integer_types) self.assertEqual(got_host.ssh_config.ssh_identity.label, self.sshidentity.label) self.assertIsInstance(got_host.ssh_config.ssh_identity.ssh_key, SshKey) self.assertIsInstance(got_host.ssh_config.ssh_identity.ssh_key.id, - long) + integer_types) self.assertEqual(got_host.ssh_config.ssh_identity.ssh_key.label, self.sshkey.label) @@ -345,12 +347,12 @@ def test_get_all_strategy(self): self.assertEqual(got_sshconfig.port, self.sshconfig.port) self.assertIsInstance(got_sshconfig.ssh_identity, SshIdentity) - self.assertIsInstance(got_sshconfig.ssh_identity.id, long) + self.assertIsInstance(got_sshconfig.ssh_identity.id, integer_types) self.assertEqual(got_sshconfig.ssh_identity.label, self.sshidentity.label) self.assertIsInstance(got_sshconfig.ssh_identity.ssh_key, SshKey) - self.assertIsInstance(got_sshconfig.ssh_identity.ssh_key.id, long) + self.assertIsInstance(got_sshconfig.ssh_identity.ssh_key.id, integer_types) self.assertEqual(got_sshconfig.ssh_identity.ssh_key.label, self.sshkey.label) @@ -362,7 +364,7 @@ def test_get_all_strategy(self): self.assertEqual(got_sshidentity.label, self.sshidentity.label) self.assertIsInstance(got_sshidentity.ssh_key, SshKey) - self.assertIsInstance(got_sshidentity.ssh_key.id, long) + self.assertIsInstance(got_sshidentity.ssh_key.id, integer_types) self.assertEqual(got_sshidentity.ssh_key.label, self.sshkey.label) got_sshkies = self.storage.get_all(SshKey) diff --git a/tests/test_cryptor.py b/tests/cryptor_test.py similarity index 100% rename from tests/test_cryptor.py rename to tests/cryptor_test.py diff --git a/tests/test_ssh_config.py b/tests/ssh_config_test.py similarity index 100% rename from tests/test_ssh_config.py rename to tests/ssh_config_test.py diff --git a/tox.ini b/tox.ini index 168a4b2..fc1e190 100644 --- a/tox.ini +++ b/tox.ini @@ -13,6 +13,7 @@ deps = nose coverage prospector[with_pyroma] + paver commands = pip install -U . paver nosetests