Skip to content

Commit

Permalink
WC-3136: Re-format through black code formatter
Browse files Browse the repository at this point in the history
Result of command: `black -t py27 .`
Package used: `dev-python/black-19.10_beta0::gentoo`
  • Loading branch information
sjlongland committed Sep 3, 2020
1 parent 34d98b0 commit c23ddf1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 67 deletions.
1 change: 1 addition & 0 deletions pyhaystack/client/mixins/vendor/widesky/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
VRT Widesky low-level Password mix-in.
"""


class PasswordOpsMixin(object):
"""
The Password operations mix-in implements low-level support for
Expand Down
41 changes: 20 additions & 21 deletions pyhaystack/client/ops/vendor/widesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,30 +249,31 @@ class WideSkyPasswordChangeOperation(BaseAuthOperation):
The Password Change operation implements the logic required to change a
user's password.
"""

def __init__(self, session, new_password, **kwargs):
super(WideSkyPasswordChangeOperation, self).__init__(
session=session, uri='user/updatePassword', **kwargs
session=session, uri="user/updatePassword", **kwargs
)
self._new_password = new_password
self._state_machine = fysom.Fysom(
initial='init', final='done',
events=[
# Event Current State New State
('send_update', 'init', 'update'),
('update_done', 'update', 'done'),
('exception', '*', 'done'),
], callbacks={
'onsend_update': self._do_submit,
'onenterdone': self._do_done,
})
initial="init",
final="done",
events=[
# Event Current State New State
("send_update", "init", "update"),
("update_done", "update", "done"),
("exception", "*", "done"),
],
callbacks={"onsend_update": self._do_submit, "onenterdone": self._do_done,},
)

def go(self):
"""
Start the request.
"""
try:
self._state_machine.send_update()
except: # Catch all exceptions to pass to caller.
except: # Catch all exceptions to pass to caller.
self._state_machine.exception(result=AsynchronousException())

def _do_submit(self, event):
Expand All @@ -281,15 +282,13 @@ def _do_submit(self, event):
"""
try:
self._session._post(
uri=self._uri,
callback=self._update_done,
body=json.dumps({ "newPassword": self._new_password }),
headers={
"Content-Type": "application/json"
},
api=False
uri=self._uri,
callback=self._update_done,
body=json.dumps({"newPassword": self._new_password}),
headers={"Content-Type": "application/json"},
api=False,
)
except: # Catch all exceptions to pass to caller.
except: # Catch all exceptions to pass to caller.
self._state_machine.exception(result=AsynchronousException())

def _update_done(self, response):
Expand All @@ -298,7 +297,7 @@ def _update_done(self, response):
response.reraise()

self._state_machine.update_done(result=None)
except: # Catch all exceptions to pass to caller.
except: # Catch all exceptions to pass to caller.
self._state_machine.exception(result=AsynchronousException())

def _do_done(self, event):
Expand Down
8 changes: 5 additions & 3 deletions pyhaystack/client/widesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ def _decode_str(s, enc="utf-8"):


class WideskyHaystackSession(
crud.CRUDOpsMixin, multihis.MultiHisOpsMixin,
password.PasswordOpsMixin, HaystackSession
crud.CRUDOpsMixin,
multihis.MultiHisOpsMixin,
password.PasswordOpsMixin,
HaystackSession,
):
"""
The WideskyHaystackSession class implements some base support for
Expand Down Expand Up @@ -129,7 +131,7 @@ def _on_authenticate_done(self, operation, **kwargs):
}

if self._impersonate:
self._client.headers['X-IMPERSONATE'] = self._impersonate
self._client.headers["X-IMPERSONATE"] = self._impersonate
except:
self._auth_result = None
self._client.headers = {}
Expand Down
88 changes: 45 additions & 43 deletions tests/test_widesky.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import datetime
import pytz
import time

# JSON encoding/decoding
import json

Expand All @@ -39,11 +39,11 @@

class DummyWsOperation(object):
result = {
'token_type': 'Bearer',
'access_token': 'abc123',
'expires_in': (time.time() + 1.0) * 1000.0
"token_type": "Bearer",
"access_token": "abc123",
"expires_in": (time.time() + 1.0) * 1000.0,
}


@pytest.fixture
def server_session():
Expand All @@ -52,33 +52,35 @@ def server_session():
"""
server = dummy_http.DummyHttpServer()
session = widesky.WideskyHaystackSession(
uri=BASE_URI,
username='testuser',
password='testpassword',
client_id='testclient',
client_secret='testclientsecret',
http_client=dummy_http.DummyHttpClient,
http_args={'server': server, 'debug': True},
grid_format=hszinc.MODE_ZINC)
uri=BASE_URI,
username="testuser",
password="testpassword",
client_id="testclient",
client_secret="testclientsecret",
http_client=dummy_http.DummyHttpClient,
http_args={"server": server, "debug": True},
grid_format=hszinc.MODE_ZINC,
)

# Force an authentication.
op = session.authenticate()

# Pop the request off the stack. We'll assume it's fine for now.
rq = server.next_request()
assert server.requests() == 0, 'More requests waiting'
rq.respond(status=200,
headers={
b'Content-Type': 'application/json'
},
content='''{
assert server.requests() == 0, "More requests waiting"
rq.respond(
status=200,
headers={b"Content-Type": "application/json"},
content="""{
"token_type": "Bearer",
"access_token": "DummyAccessToken",
"refresh_token": "DummyRefreshToken",
"expires_in": %f
}''' % ((time.time() + 86400) * 1000.0))
assert op.state == 'done'
logging.debug('Result = %s', op.result)
}"""
% ((time.time() + 86400) * 1000.0),
)
assert op.state == "done"
logging.debug("Result = %s", op.result)
assert server.requests() == 0
assert session.is_logged_in
return (server, session)
Expand All @@ -88,56 +90,56 @@ class TestImpersonateParam(object):
"""
Test is_logged_in property
"""

def test_impersonate_is_set_in_client_header(self):
"""
is_logged_in == False if _auth_result is None.
"""
user_id = '12345ab'
user_id = "12345ab"
server = dummy_http.DummyHttpServer()
session = widesky.WideskyHaystackSession(
uri=BASE_URI,
username='testuser',
password='testpassword',
client_id='testclient',
client_secret='testclientsecret',
username="testuser",
password="testpassword",
client_id="testclient",
client_secret="testclientsecret",
impersonate=user_id,
http_client=dummy_http.DummyHttpClient,
http_args={'server': server, 'debug': True})
http_args={"server": server, "debug": True},
)

session._on_authenticate_done(DummyWsOperation())

assert session._client.headers['X-IMPERSONATE'] is user_id
assert session._client.headers["X-IMPERSONATE"] is user_id


class TestUpdatePassword(object):
"""
Test the update_password op
"""

def test_update_pwd_endpoint_is_called(self, server_session):
(server, session) = server_session

# Issue the request
op = session.update_password('hello123X')
op = session.update_password("hello123X")

# Pop the request off the stack and inspect it
rq = server.next_request()
assert server.requests() == 0, 'More requests waiting'
assert server.requests() == 0, "More requests waiting"

assert rq.headers.get('Authorization') == b'Bearer DummyAccessToken'
assert rq.headers.get("Authorization") == b"Bearer DummyAccessToken"
body = json.loads(rq.body)
assert body == {"newPassword": "hello123X"}

rq.respond(status=200,
headers={
b'Content-Type': 'application/json'
},
content='{}')
rq.respond(
status=200, headers={b"Content-Type": "application/json"}, content="{}"
)

assert op.state == 'done'
logging.debug('Result = %s', op.result)
assert op.state == "done"
logging.debug("Result = %s", op.result)
assert server.requests() == 0

op.wait()
assert op.result is None

Expand Down

0 comments on commit c23ddf1

Please sign in to comment.