def test_status(self): """InsightiqApi - ``renew_session()`` checks the HTTP status code""" fake_post = MagicMock() self.fake_session.post.return_value = fake_post iiq = insightiq_api.InsightiqApi(username='******', password='******') fake_post.raise_for_status.assert_called()
def test_with_statement(self): """InsightiqApi - using in a ``with`` statement auto-handles the session""" with insightiq_api.InsightiqApi(username='******', password='******') as iiq: pass self.fake_session.close.assert_called() self.fake_session.get.assert_called()
def test_end_session(self): """InsightiqApi - ``.end_session()`` closes the TCP socket and logs out of the IIQ session""" iiq = insightiq_api.InsightiqApi(username='******', password='******') iiq.end_session() self.fake_session.get.assert_called() self.fake_session.close.assert_called()
def test_username(self): """InsightiqApi - The supplied username can be read""" iiq = insightiq_api.InsightiqApi(username='******', password='******') value = iiq.username expected = 'pat' self.assertEqual(value, expected)
def test_build_uri_slashs(self): """InsightiqApi - ``_build_uri()`` works with slashes in the endpoint""" iiq = insightiq_api.InsightiqApi(username='******', password='******') value = iiq._build_uri('/someEndpoint') expected = 'https://localhost/someEndpoint' self.assertEqual(value, expected)
def test_connection_error(self, fake_get_session): """InsightiqApi - failure to obtain a session raises ConnectionError""" fake_get_session.side_effect = [requests.exceptions.ConnectionError('testing'), requests.exceptions.ConnectionError('testing'), requests.exceptions.ConnectionError('testing'), requests.exceptions.ConnectionError('testing'),] with self.assertRaises(insightiq_api.ConnectionError): insightiq_api.InsightiqApi(username='******', password='******')
def test_session_post(self): """InsightiqApi - ``renew_session()`` post to create IIQ session""" iiq = insightiq_api.InsightiqApi(username='******', password='******') args, kwargs = self.fake_session.post.call_args expected_url = 'https://localhost/login' expected_data = {'username': '******', 'authform': '+Log+in+', 'password': '******'} self.assertEqual(args[0], expected_url) self.assertEqual(kwargs['data'], expected_data)
def test_session_retries(self, fake_get_session): """InsightiqApi - ``renew_session()`` will try 3 times to get a valid session""" fake_get_session.side_effect = [requests.exceptions.ConnectionError('testing'), requests.exceptions.ConnectionError('testing'), requests.exceptions.ConnectionError('testing'), requests.exceptions.ConnectionError('testing'),] try: insightiq_api.InsightiqApi(username='******', password='******') except Exception as doh: # this will raise connection error, but we're not testing that here pass call_count = fake_get_session.call_count expected = 3 self.assertEqual(call_count, expected)
def test_call(self): """InsightiqApi - ``_call()`` pulls the HTTP method from the session object""" iiq = insightiq_api.InsightiqApi(username='******', password='******') _ = iiq._call('someEndpoint', method='head') self.fake_session.head.assert_called()
def test_username_readonly(self): """InsightiqApi - The username is readonly""" iiq = insightiq_api.InsightiqApi(username='******', password='******') with self.assertRaises(AttributeError): iiq.username = '******'
def test_init(self): """InsightiqApi - Automatically sets up the HTTP session with InsightIQ""" iiq = insightiq_api.InsightiqApi(username='******', password='******') self.fake_renew_session.assert_called()
def test_session(self): """InsightiqApi - ``renew_session()`` returns a requests.Session""" iiq = insightiq_api.InsightiqApi(username='******', password='******') self.assertTrue(iiq._session is self.fake_session)