Skip to content

Commit

Permalink
Fix tests and bugs after refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeneOskin committed Jan 28, 2016
1 parent 3fafbb7 commit 1ad2fab
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 45 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions .prospector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -20,4 +21,5 @@ pep257:
pylint:
options:
max-parents: 12

disable:
- super-on-old-class
2 changes: 1 addition & 1 deletion pavement.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@ def lint():
@task
def bats():
"""Run tests on CLI usage."""
sh('bats tests/integration')
sh('bats --tap tests/integration')
2 changes: 1 addition & 1 deletion serverauditor_sshconfig/cloud/commands/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion serverauditor_sshconfig/cloud/commands/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions serverauditor_sshconfig/cloud/cryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 0 additions & 10 deletions serverauditor_sshconfig/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
4 changes: 2 additions & 2 deletions serverauditor_sshconfig/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion serverauditor_sshconfig/core/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 42 additions & 0 deletions serverauditor_sshconfig/core/utils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
1 change: 1 addition & 0 deletions tests/cloud/test_models.py → tests/cloud/models_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import six
from collections import OrderedDict
from mock import patch, Mock
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from mock import patch, Mock
from unittest import TestCase
from serverauditor_sshconfig.core.storage.query import Query
Expand Down
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -193,23 +195,23 @@ 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)

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
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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(
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ deps =
nose
coverage
prospector[with_pyroma]
paver
commands =
pip install -U .
paver nosetests
Expand Down

0 comments on commit 1ad2fab

Please sign in to comment.