def test_get_session_request(self):
        """ Test to check that get_session_request returns expected dict."""

        error_message = "\nTest: {} \nExpected: {}\nActual:   {}\n"
        test_params = METRIC_TEST_PARAMS

        for test, params in test_params.items():
            request_parameters = {
                'method': params['method'],
                'session_id': params["session_id"],
                'metric': params["metric"],
                'auth_token': AUTH_TOKEN,
                "params": "",
                "data": ''
            }
            expected = {
                "method":
                params["method"],
                "url":
                BASE_URL + VERSION + SESSION_SUFFIX + '/' +
                params["session_id"] + params["metric"],
                "attributes": {
                    "headers": params["headers"],
                    "params": "",
                    "data": ''
                }
            }

            self.director = RequestDirector(**request_parameters)
            actual = self.director.get_session_request()

            self.assertEqual(expected, actual,
                             error_message.format(test, expected, actual))
    def test_get_session_request_no_metric(self):
        """ Test to check that get_session_request returns expected dict when session_id is empty."""

        test_params = {
            'method': 'POST',
            'session_id': '',
            'metric': '',
            'auth_token': AUTH_TOKEN,
            "params": "test_params",
            "data": '{"test_data": "test_data"}'
        }

        expected = {
            "method": "POST",
            "url": BASE_URL + VERSION + SESSION_SUFFIX,
            "attributes": {
                "headers": NO_METRIC_HEADERS,
                "params": "test_params",
                "data": '{"test_data": "test_data"}'
            }
        }

        self.director = RequestDirector(**test_params)
        actual = self.director.get_session_request()

        self.assertEqual(
            expected,
            actual,
        )
class TestRequestBuilder(TestCase):
    """ Tests for the Request Builder."""
    def setUp(self):
        self.request_director = RequestDirector()

    def test_auth_request(self):
        """ Test to check that each property of the auth request is
            built and set correctly.
        """

        auth = AuthRequest()
        self.request_director.construct_request(auth, **AUTH_DATA)

        expected = {
            "method": "POST",
            "url": AUTH_URL,
            "headers": AUTH_HEADERS,
            "data": AUTH_DATA
        }
        actual = {
            "method": auth.request.method,
            "url": auth.request.url,
            "headers": auth.request.headers,
            "data": auth.request.data
        }

        self.assertEqual(expected, actual)

    def test_basic_session_request(self):
        """ Test to check that each property of the auth request is
            built and set correctly.
        """

        request_parameters = DEFAULT_REQUEST_PARAMS

        session = SessionRequest()
        self.request_director.construct_request(session, **request_parameters)

        expected = {
            "method": "GET",
            "url": DEFAULT_SESSION_URL,
            "headers": DEFAULT_SESSION_HEADERS,
            "params": "test_params",
            "data": {
                "test_data": "test_data"
            }
        }
        actual = {
            "method": session.request.method,
            "url": session.request.url,
            "headers": session.request.headers,
            "params": session.request.params,
            "data": session.request.data
        }

        self.assertEqual(expected, actual)
Beispiel #4
0
class TestGetAuthRequest(TestCase):
    """ Tests for the GetAuth Request class."""
    def setUp(self):
        self.director = RequestDirector()

    def test_get_auth_request(self):
        """ Test to check that get_auth_request returns expected dict."""
        data = AUTH_DATA

        expected = {
            "method": "POST",
            "url": AUTH_URL,
            "attributes": {
                "headers": AUTH_HEADERS,
                "auth": (USERNAME, PASSWORD)
            }
        }
        actual = self.director.get_auth_request(**data)

        self.assertEqual(expected, actual)

    def test_get_session_request(self):
        """ Test to check that get_session_request returns expected dict."""

        error_message = "\nTest: {} \nExpected: {}\nActual:   {}\n"
        test_params = METRIC_TEST_PARAMS

        for test, params in test_params.items():
            request_parameters = {
                'method': params['method'],
                'session_id': params["session_id"],
                'metric': params["metric"],
                'auth_token': AUTH_TOKEN,
                "params": "test_params",
                "data": {
                    "test_data": "test_data"
                }
            }
            expected = {
                "method":
                params["method"],
                "url":
                BASE_URL + VERSION + SESSION_SUFFIX + params["session_id"] +
                params["metric"],
                "attributes": {
                    "headers": params["headers"],
                    "params": "test_params",
                    "data": {
                        "test_data": "test_data"
                    }
                }
            }
            actual = self.director.get_session_request(**request_parameters)

            self.assertEqual(expected, actual,
                             error_message.format(test, expected, actual))
class TestRequestDirector(TestCase):
    """ Tests for the Request Director module."""
    def setUp(self):
        self.director = RequestDirector()

    def test_init_creates_builder_object(self):
        """ Verify the init simply creates a _builder variable."""

        self.assertEqual(self.director._builder, None)

    def test_auth_to_dict(self):
        """ Verify the helper method correctly turns the object into a dictionary."""

        mock = Mock()
        mock.request.method = 'POST'
        mock.request.url = AUTH_URL
        mock.request.headers = AUTH_HEADERS
        mock.request.data = AUTH_DATA

        actual = self.director._auth_to_dict(mock)
        expected = {
            'method': mock.request.method,
            'url': mock.request.url,
            'attributes': {
                'headers':
                mock.request.headers,
                'auth':
                (mock.request.data['username'], mock.request.data['password'])
            }
        }

        self.assertEqual(actual, expected)

    def test_session_to_dict(self):
        """ Verify the helper method correctly turns the object into a dictionary."""

        mock = Mock()
        mock.request.method = 'GET'
        mock.request.url = DEFAULT_SESSION_URL
        mock.request.headers = DEFAULT_SESSION_HEADERS
        mock.request.params = "test_params"
        mock.request.data = SESSION_DATA

        actual = self.director._session_to_dict(mock)
        expected = {
            'method': mock.request.method,
            'url': mock.request.url,
            'attributes': {
                'headers': mock.request.headers,
                'params': mock.request.params,
                'data': mock.request.data
            }
        }

        self.assertEqual(actual, expected)
    def test_get_auth_request(self):
        """ Test to check that get_auth_request returns expected dict."""

        data = AUTH_DATA
        self.director = RequestDirector(**data)
        expected = {
            "method": "POST",
            "url": AUTH_URL,
            "attributes": {
                "headers": AUTH_HEADERS,
                "data":
                f'{{ "username": "******", "password": "******" }}'
            }
        }
        actual = self.director.get_auth_request()

        self.assertEqual(expected, actual)
class TestGetAuthRequest(TestCase):
    """ Tests for the GetAuth Request class."""
    def test_get_auth_request(self):
        """ Test to check that get_auth_request returns expected dict."""

        data = AUTH_DATA
        self.director = RequestDirector(**data)
        expected = {
            "method": "POST",
            "url": AUTH_URL,
            "attributes": {
                "headers": AUTH_HEADERS,
                "data":
                f'{{ "username": "******", "password": "******" }}'
            }
        }
        actual = self.director.get_auth_request()

        self.assertEqual(expected, actual)

    def test_get_session_request(self):
        """ Test to check that get_session_request returns expected dict."""

        error_message = "\nTest: {} \nExpected: {}\nActual:   {}\n"
        test_params = METRIC_TEST_PARAMS

        for test, params in test_params.items():
            request_parameters = {
                'method': params['method'],
                'session_id': params["session_id"],
                'metric': params["metric"],
                'auth_token': AUTH_TOKEN,
                "params": "",
                "data": ''
            }
            expected = {
                "method":
                params["method"],
                "url":
                BASE_URL + VERSION + SESSION_SUFFIX + '/' +
                params["session_id"] + params["metric"],
                "attributes": {
                    "headers": params["headers"],
                    "params": "",
                    "data": ''
                }
            }

            self.director = RequestDirector(**request_parameters)
            actual = self.director.get_session_request()

            self.assertEqual(expected, actual,
                             error_message.format(test, expected, actual))

    def test_get_session_request_no_metric(self):
        """ Test to check that get_session_request returns expected dict when session_id is empty."""

        test_params = {
            'method': 'POST',
            'session_id': '',
            'metric': '',
            'auth_token': AUTH_TOKEN,
            "params": "test_params",
            "data": '{"test_data": "test_data"}'
        }

        expected = {
            "method": "POST",
            "url": BASE_URL + VERSION + SESSION_SUFFIX,
            "attributes": {
                "headers": NO_METRIC_HEADERS,
                "params": "test_params",
                "data": '{"test_data": "test_data"}'
            }
        }

        self.director = RequestDirector(**test_params)
        actual = self.director.get_session_request()

        self.assertEqual(
            expected,
            actual,
        )
 def setUp(self):
     self.director = RequestDirector()
 def __init__(self, **request_parameters):
     self.request_parameters = request_parameters
     self.director = RequestDirector()
     self.request = None
     self.adapter = None
     self.response = None