Skip to content

Commit

Permalink
bug fix for __dir__ methods + basic token is now sent instead of (use…
Browse files Browse the repository at this point in the history
…rname, password)
  • Loading branch information
jpoullet2000 committed Mar 7, 2018
1 parent 9b876e9 commit 16e2041
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 23 deletions.
6 changes: 5 additions & 1 deletion atlasclient/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,11 @@ def __init__(self, parent, data=None):
self._relationship_cache = {}

def __dir__(self):
return self.__dict__.keys() + list(self.fields) + self.relationships.keys()
fields_dict = dict()
for field in self.fields:
fields_dict[field] = field
d1 = dict(self.__dict__, **fields_dict, **self.relationships)
return d1.keys()

@property
def identifier(self):
Expand Down
10 changes: 6 additions & 4 deletions atlasclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def __init__(self, host, port=None, username=None, password=None,
self._version = None

def __dir__(self):
return self.__dict__.keys() + ENTRY_POINTS.keys()
d1 = dict(self.__dict__, **ENTRY_POINTS)
return d1.keys()

def check_version(self):
if self.version < base.OLDEST_SUPPORTED_VERSION:
Expand Down Expand Up @@ -112,10 +113,11 @@ class HttpClient(object):
"""
def __init__(self, host, username, password, identifier, validate_ssl=True,
timeout=10, max_retries=5):

basic_token = utils.generate_http_basic_token(username=username, password=password)
self.request_params = {
'headers': {'X-Requested-By': identifier},
'auth': (username, password),
'headers': {'X-Requested-By': identifier,
'Authorization': 'Basic {}'.format(basic_token)},
#'auth': (username, password),
'verify': validate_ssl,
'timeout': timeout,
}
Expand Down
9 changes: 9 additions & 0 deletions atlasclient/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# under the License.

import re
import base64

try:
from logging import NullHandler # pylint: disable=unused-import
Expand Down Expand Up @@ -77,6 +78,14 @@ def version_str(version):
else:
raise ValueError("Invalid version: %s" % version)

def generate_http_basic_token(username, password):
"""
Generates a HTTP basic token from username and password
Returns a token string (not a byte)
"""
token = base64.b64encode('{}:{}'.format(username, password).encode('utf-8')).decode('utf-8')
return token

def generate_base_url(host, protocol=None, port=None):
matches = re.match(r'^(([^:]+)://)?([^/:]+)(:([^/]+))?', host)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
six >=1.11.0
Click >=6.0
requests >=2.18.4
20 changes: 20 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
try:
from mock import MagicMock
except ImportError:
from unittest.mock import MagicMock

from atlasclient.base import Model, QueryableModel
from atlasclient.client import HttpClient

class TestBase():

def test_model(self, monkeypatch):
monkeypatch.setattr('atlasclient.base.QueryableModel', MagicMock(QueryableModel))
monkeypatch.setattr('atlasclient.client.HttpClient', MagicMock(HttpClient))
queryablemodel = QueryableModel
httpclient = HttpClient
queryablemodel.client = httpclient
data = {'entity': {'createdBy': 'owner'}}
model = Model(parent=queryablemodel, data=data)
assert 'parent' in dir(model)
assert model.identifier is None
5 changes: 3 additions & 2 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class TestClient():
def test_atlas_client(self):
client = Atlas('localhost', port=21000, username='admin', password='admin')
assert client.base_url == 'http://localhost:21000'
assert 'auth' in client.client.request_params.keys()
assert client.client.request_params['headers'] == {'X-Requested-By': 'python-atlasclient'}
assert 'headers' in client.client.request_params.keys()
assert 'X-Requested-By' in client.client.request_params['headers']
assert 'entity_post' in dir(client)

def test_http_client(self):
pass
16 changes: 0 additions & 16 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,6 @@ def test_entity_post(self, mocker, atlas_client, entity_guid_response, entity_po
atlas_client.entity_post.create(data=entity_guid_response)
atlas_client.entity_post.client.post.assert_called_with(atlas_client.entity_post.url, data=entity_guid_response)

# for e in entity_collection:
# assert e._data == entity_guid_response
# mocker.patch.object(e.client, 'post')
# e.client.post.return_value = entity_guid_response
# e.create()
# e.client.post.assert_called_with(e._href, data=e._data)
# with pytest.raises(exceptions.MethodNotImplemented) as err:
# e.update()
# with pytest.raises(exceptions.MethodNotImplemented) as err:
# e.delete()

def test_entity_bulk_get(self, mocker, atlas_client, entity_bulk_response):
mocker.patch.object(atlas_client.client, 'get')
Expand Down Expand Up @@ -206,12 +196,6 @@ def test_update_entity_by_guid_classifications(self, mocker, entity_guid_classif
data=[{'typeName': 'Confidential'}, {'typeName': None}])


# def test_delete_entity_guid_classification(self, mocker, entity_guid_response, entity_guid):
# mocker.patch.object(entity_guid.classifications('classtype1').client, 'delete')
# entity_guid.classifications('classtype1').client.delete.return_value = {}
# entity_guid.classifications('classtype1').delete()
# entity_guid.client.delete.assert_called_with(entity_guid.classifications('classtype1')._href)

class TestTypeDefs():
def test_typedefs_get(self, mocker, atlas_client, typedefs_response):
mocker.patch.object(atlas_client.client, 'request')
Expand Down

0 comments on commit 16e2041

Please sign in to comment.