Ejemplo n.º 1
0
    def test_deepsea_invalid_url_error(self):
        with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session:
            mock_requests_session().post.side_effect = InvalidURL("test")

            api = DeepSea('', 8000, 'auto', 'hello', 'world')
            with self.assertRaisesRegexp(RequestException, "^test$") as context:
                api._login()
                
            self.assertEqual(context.exception.status_code, None)
            self.assertTrue(mock_requests_session().post.called)
Ejemplo n.º 2
0
    def test_deepsea_login_connection_error(self):
        with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session:
            mock_requests_session().post.side_effect = ConnectionError()

            api = DeepSea('localhost', 8000, 'auto', 'hello', 'world')
            with self.assertRaises(RequestException) as context:
                api._login()

            self.assertEqual(context.exception.status_code, None)
            self.assertTrue(mock_requests_session().post.called)
            self.assertEqual(mock_requests_session().post.call_args[1]['data'],
                             {'username': '******', 'password': '******', 'eauth': 'auto'})
            self.assertFalse(api._is_logged_in())
            self.assertEqual(api.token, None)
Ejemplo n.º 3
0
    def test_deepsea_login_fail_401(self):
        with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session:
            resp = mock.MagicMock()
            resp.ok = False
            resp.status_code = 401
            mock_requests_session().post.return_value = resp

            api = DeepSea('localhost', 8000, 'auto', 'hello', 'world')
            with self.assertRaises(RequestException) as context:
                api._login()

            self.assertEqual(context.exception.status_code, 401)
            self.assertTrue(mock_requests_session().post.called)
            self.assertEqual(mock_requests_session().post.call_args[1]['data'],
                             {'username': '******', 'password': '******', 'eauth': 'auto'})
            self.assertFalse(api._is_logged_in())
            self.assertEqual(api.token, None)
Ejemplo n.º 4
0
    def test_deepsea_login_success(self):
        with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session:
            resp = mock.MagicMock()
            resp.ok = True
            resp.status_code = 200
            resp.json.return_value = {
                'return': [{'token': 'validtoken'}]
            }
            mock_requests_session().post.return_value = resp

            api = DeepSea('localhost', 8000, 'auto', 'hello', 'world')
            api._login()

            self.assertTrue(mock_requests_session().post.called)
            self.assertEqual(mock_requests_session().post.call_args[1]['data'],
                             {'username': '******', 'password': '******', 'eauth': 'auto'})
            self.assertTrue(api._is_logged_in())
            self.assertEqual(api.token, 'validtoken')
Ejemplo n.º 5
0
    def test_deepsea_login_response_format_error(self):
        with mock.patch("rest_client.TimeoutRequestsSession") as mock_requests_session:
            resp = mock.MagicMock()
            resp.ok = True
            resp.status_code = 200
            resp.json.return_value = {
                'return': {'invalidtoken': 'validtoken'}
            }
            mock_requests_session().post.return_value = resp

            api = DeepSea('localhost', 8000, 'auto', 'hello', 'world')
            with self.assertRaises(BadResponseFormatException):
                api._login()

            self.assertTrue(mock_requests_session().post.called)
            self.assertEqual(mock_requests_session().post.call_args[1]['data'],
                             {'username': '******', 'password': '******', 'eauth': 'auto'})
            self.assertFalse(api._is_logged_in())
            self.assertEqual(api.token, None)