def server_session(): """ Initialise a HaystackSession and dummy HTTP server instance. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username='******', password='******', 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={ '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) assert server.requests() == 0 assert session.is_logged_in return (server, session)
def test_logout_if_response_401_via_exception(self): """ Function handles HTTPStatusError with code 401 like a 401 response. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username='******', password='******', client_id='testclient', client_secret='testclientsecret', http_client=dummy_http.DummyHttpClient, http_args={ 'server': server, 'debug': True }) # Seed this with parameters auth_result = { 'expires_in': (time.time() + 3600.0) * 1000.0, 'access_token': 'abcdefgh', 'refresh_token': '12345678', } session._auth_result = auth_result # Generate a HTTPStatusError, wrap it up in an AsynchronousException try: raise HTTPStatusError('Unauthorized', 401) except HTTPStatusError: res = AsynchronousException() # This should drop our session session._on_http_grid_response(res) assert session._auth_result is None
def test_logout_if_response_401(self): """ Function drops the session if the response is a 401 response. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username="******", password="******", client_id="testclient", client_secret="testclientsecret", http_client=dummy_http.DummyHttpClient, http_args={ "server": server, "debug": True }, ) # Seed this with parameters auth_result = { "expires_in": (time.time() + 3600.0) * 1000.0, "access_token": "abcdefgh", "refresh_token": "12345678", } session._auth_result = auth_result # A dummy response, we don't care much about the content. res = HTTPResponse(status_code=401, headers={}, body="") # This should drop our session session._on_http_grid_response(res) assert session._auth_result is None
def test_logout_if_response_401(self): """ Function drops the session if the response is a 401 response. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username='******', password='******', client_id='testclient', client_secret='testclientsecret', http_client=dummy_http.DummyHttpClient, http_args={ 'server': server, 'debug': True }) # Seed this with parameters auth_result = { 'expires_in': (time.time() + 3600.0) * 1000.0, 'access_token': 'abcdefgh', 'refresh_token': '12345678', } session._auth_result = auth_result # A dummy response, we don't care much about the content. res = HTTPResponse(status_code=401, headers={}, body='') # This should drop our session session._on_http_grid_response(res) assert session._auth_result is None
def test_no_op_if_exception_not_http_status_error(self): """ Function ignores exceptions other than HTTP status errors. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username="******", password="******", client_id="testclient", client_secret="testclientsecret", http_client=dummy_http.DummyHttpClient, http_args={ "server": server, "debug": True }, ) # Seed this with parameters auth_result = { "expires_in": (time.time() + 3600.0) * 1000.0, "access_token": "abcdefgh", "refresh_token": "12345678", } session._auth_result = auth_result # Generate a dummy exception, wrap it up in an AsynchronousException class DummyError(Exception): pass try: raise DummyError("Testing") except DummyError: res = AsynchronousException() # This should do nothing session._on_http_grid_response(res) # Same keys assert set(auth_result.keys()) == set( session._auth_result.keys()), "Keys mismatch" for key in auth_result.keys(): assert auth_result[key] == session._auth_result[key], ( "Mismatching key %s" % key)
def test_no_op_if_exception_not_http_status_error(self): """ Function ignores exceptions other than HTTP status errors. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username='******', password='******', client_id='testclient', client_secret='testclientsecret', http_client=dummy_http.DummyHttpClient, http_args={ 'server': server, 'debug': True }) # Seed this with parameters auth_result = { 'expires_in': (time.time() + 3600.0) * 1000.0, 'access_token': 'abcdefgh', 'refresh_token': '12345678', } session._auth_result = auth_result # Generate a dummy exception, wrap it up in an AsynchronousException class DummyError(Exception): pass try: raise DummyError('Testing') except DummyError: res = AsynchronousException() # This should do nothing session._on_http_grid_response(res) # Same keys assert set(auth_result.keys()) == set(session._auth_result.keys()), \ 'Keys mismatch' for key in auth_result.keys(): assert auth_result[key] == session._auth_result[key], \ 'Mismatching key %s' % key
def test_returns_false_if_no_expires_in(self): server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username='******', password='******', client_id='testclient', client_secret='testclientsecret', http_client=dummy_http.DummyHttpClient, http_args={ 'server': server, 'debug': True }) # Inject our own auth result, empty dict. session._auth_result = {} # We should see a False result here assert not session.is_logged_in
def test_impersonate_is_set_in_client_header(self): """ is_logged_in == False if _auth_result is None. """ userId = '12345ab' server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username='******', password='******', client_id='testclient', client_secret='testclientsecret', impersonate=userId, http_client=dummy_http.DummyHttpClient, http_args={ 'server': server, 'debug': True }) session._on_authenticate_done(DummyWsOperation()) assert session._client.headers['X-IMPERSONATE'] is userId
def test_returns_true_if_expires_in_future(self): server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username='******', password='******', client_id='testclient', client_secret='testclientsecret', http_client=dummy_http.DummyHttpClient, http_args={ 'server': server, 'debug': True }) # Inject our own auth result, expiry in the future. session._auth_result = { # Milliseconds here! 'expires_in': (time.time() + 1.0) * 1000.0 } # We should see a True result here assert session.is_logged_in
def test_returns_false_if_no_auth_result(self): """ is_logged_in == False if _auth_result is None. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username='******', password='******', client_id='testclient', client_secret='testclientsecret', http_client=dummy_http.DummyHttpClient, http_args={ 'server': server, 'debug': True }) # Straight off the bat, this should be None assert session._auth_result is None # Therefore, we should see a False result here assert not session.is_logged_in
def server_session(): """ Initialise a HaystackSession and dummy HTTP server instance. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username="******", password="******", 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="""{ "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) assert server.requests() == 0 assert session.is_logged_in return (server, session)
def test_no_op_if_response_not_401(self): """ Function does nothing if the response is not a 401 response. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username="******", password="******", client_id="testclient", client_secret="testclientsecret", http_client=dummy_http.DummyHttpClient, http_args={ "server": server, "debug": True }, ) # Seed this with parameters auth_result = { "expires_in": (time.time() + 3600.0) * 1000.0, "access_token": "abcdefgh", "refresh_token": "12345678", } session._auth_result = auth_result # A dummy response, we don't care much about the content. res = HTTPResponse(status_code=200, headers={}, body="") # This should do nothing session._on_http_grid_response(res) # Same keys assert set(auth_result.keys()) == set( session._auth_result.keys()), "Keys mismatch" for key in auth_result.keys(): assert auth_result[key] == session._auth_result[key], ( "Mismatching key %s" % key)
def test_returns_false_if_expires_in_past(self): server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username="******", password="******", client_id="testclient", client_secret="testclientsecret", http_client=dummy_http.DummyHttpClient, http_args={ "server": server, "debug": True }, ) # Inject our own auth result, expiry in the past. session._auth_result = { # Milliseconds here! "expires_in": (time.time() - 1.0) * 1000.0 } # We should see a False result here assert not session.is_logged_in
def test_no_op_if_response_not_401(self): """ Function does nothing if the response is not a 401 response. """ server = dummy_http.DummyHttpServer() session = widesky.WideskyHaystackSession( uri=BASE_URI, username='******', password='******', client_id='testclient', client_secret='testclientsecret', http_client=dummy_http.DummyHttpClient, http_args={ 'server': server, 'debug': True }) # Seed this with parameters auth_result = { 'expires_in': (time.time() + 3600.0) * 1000.0, 'access_token': 'abcdefgh', 'refresh_token': '12345678', } session._auth_result = auth_result # A dummy response, we don't care much about the content. res = HTTPResponse(status_code=200, headers={}, body='') # This should do nothing session._on_http_grid_response(res) # Same keys assert set(auth_result.keys()) == set(session._auth_result.keys()), \ 'Keys mismatch' for key in auth_result.keys(): assert auth_result[key] == session._auth_result[key], \ 'Mismatching key %s' % key