Exemple #1
0
 def setUp(self, mock_request, mock_attributes, mock_method):
     self.mock_get_method = mock_method.return_value
     self.mock_get_method.get_method.return_value = "GET"
     self.mock_attributes = mock_attributes.return_value
     self.mock_attributes.get_headers.return_value = DEFAULT_SESSION_HEADERS
     self.mock_attributes.get_params.return_value = "test_params"
     self.mock_attributes.get_data.return_value = {"test_data": "test_data"}
     self.session = SessionRequest()
    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)
Exemple #3
0
    def get_session_request(self):
        """ Constructs and returns a session request object.

            :return: request - dictionary containing each element of the session request.
        """

        session = SessionRequest(**self.request_parameters)
        director = RequestDirector(**self.request_parameters)
        director.construct_request(session)

        request = self._session_to_dict(session)

        return request
Exemple #4
0
    def get_session_request(self, **request_parameters):
        """ Constructs and returns a session request object.

            :param request_parameters:
                session_id - id for the current session
                metric - specific category of data being requested
                (cpu, gpu, battery, etc.)
                auth_token - authorization token necessary for retrieving
                data.
                params - parameters appended to certain session requests
                (pageSize, timePushed, etc.)
                data - json body appended to certain session requests
                (example: {"apps" : [], "devices" : [], "manufacturers" : []})
            :return: request - dictionary containing each element of the
            session request.
        """

        session = SessionRequest()
        director = RequestDirector()
        director.construct_request(session, **request_parameters)

        request = self._session_to_dict(session)

        return request
Exemple #5
0
class TestSessionRequest(TestCase):
    """ Tests for the SessionRequest concrete builder."""
    @patch(REQUEST_BUILDER_PATH + patch_method)
    @patch(REQUEST_BUILDER_PATH + patch_attributes)
    @patch(REQUEST_BUILDER_PATH + patch_request)
    def setUp(self, mock_request, mock_attributes, mock_method):
        self.mock_get_method = mock_method.return_value
        self.mock_get_method.get_method.return_value = "GET"
        self.mock_attributes = mock_attributes.return_value
        self.mock_attributes.get_headers.return_value = DEFAULT_SESSION_HEADERS
        self.mock_attributes.get_params.return_value = "test_params"
        self.mock_attributes.get_data.return_value = {"test_data": "test_data"}
        self.session = SessionRequest()

    def test_set_session_method(self):
        """ Verify the method is set correctly for a session.

            This test is for session requests that contain a metric.
        """

        self.session.set_method()

        actual = self.session.request.method
        expected = "GET"

        self.assertEqual(expected, actual)

    @patch(TEST_URL_DIRECTOR_PATH)
    def test_set_session_url(self, mock_url):
        """ Verify the proper URL is set for session requests.

            The URLs are created in the URL Builder.
        """
        mock_session_url = mock_url.return_value
        mock_session_url.get_session_url.return_value = DEFAULT_SESSION_URL

        self.session.set_url(mock_session_url)

        actual = self.session.request.url
        expected = DEFAULT_SESSION_URL

        self.assertEqual(expected, actual)

    def test_set_session_headers(self):
        """ Verify the correct headers are set for session requests."""

        self.session.set_headers()

        actual = self.session.request.headers
        expected = DEFAULT_SESSION_HEADERS

        self.assertEqual(expected, actual)

    def test_set_session_params(self):
        """Verify the correct params are set for session requests."""

        self.session.set_params()

        expected = "test_params"
        actual = self.session.request.params

        self.assertEqual(expected, actual)

    def test_set_session_data(self):
        """ Verify the correct data is set for session requests."""

        self.session.set_data()

        actual = self.session.request.data
        expected = {"test_data": "test_data"}

        self.assertEqual(expected, actual)