Beispiel #1
0
    def testClient_sendRequest(self):
        expectedHeaders = {
            'content-length': '0',
            'set-cookie':
            'ipa_session=b99a25695f578c0bb30cafb0932035bf; Domain=ipa.example.test; Path=/ipa; Expires=Sun, 06 Sep 2015 05:12:56 GMT; Secure; HttpOnly',
            'keep-alive': 'timeout=5, max=100',
            'server':
            'Apache/2.4.6 (CentOS) mod_auth_kerb/5.4 mod_nss/2.4.6 NSS/3.16.2.3 Basic ECC mod_wsgi/3.4 Python/2.7.5',
            'connection': 'Keep-Alive',
            'date': 'Sun, 06 Sep 2015 04:52:56 GMT',
            'content-type': 'text/plain; charset=UTF-8'
        }

        expectedIPAResponse = IPAResponse(
            session='b99a25695f578c0bb30cafb0932035bf',
            status_code=200,
            expiration='Sun, 06 Sep 2015 05:12:56 GMT',
            headers=expectedHeaders)

        with open('%s/%s' % (os.path.dirname(__file__),
                             'resources/user_find_result.json')) as f:
            mockJson = json.load(f)
            mockResponse = MockResponse(status_code=200,
                                        headers=expectedHeaders,
                                        jsonValue=mockJson)

            expectedResultIPAResponse = IPAResponse(
                status_code=200,
                headers=expectedHeaders,
                raw_result=mockResponse,
                parsed_json=mockJson['result']['result'],
            )

            ipaAuth = IPAAuth(requests=None, baseUrl=self.baseUrl)
            ipaAuth.authenticate = MagicMock(return_value=expectedIPAResponse)

            requests.post = MagicMock(return_value=mockResponse)

            ipaClient = IPAClient(requests=requests,
                                  baseUrl=self.baseUrl,
                                  sourceUrl=self.sourceUrl,
                                  ipaAuth=ipaAuth)

            response = ipaClient.sendRequest(
                'user_find', ['admin', 'register-marcher', 'smercado'])
            self.assertEquals(expectedResultIPAResponse.raw_result,
                              response.raw_result)
            self.assertEquals(expectedResultIPAResponse.parsed_json,
                              response.parsed_json)
    def testClient_sendRequest_noJsonValue(self):
        expectedIPAResponse = IPAResponse(
            session="b99a25695f578c0bb30cafb0932035bf",
            status_code=200,
            expiration="Sun, 06 Sep 2015 05:12:56 GMT",
            headers=None,
        )

        mockResponse = MockResponse(status_code=500, headers=None)

        ipaAuth = IPAAuth(requests=None, baseUrl=self.baseUrl)
        ipaAuth.authenticate = MagicMock(return_value=expectedIPAResponse)
        requests.post = MagicMock(return_value=mockResponse)
        mockResponse.json = MagicMock(side_effect=ValueError("No JSON object could be decoded"))

        ipaClient = IPAClient(requests=requests, baseUrl=self.baseUrl, sourceUrl=self.sourceUrl, ipaAuth=ipaAuth)

        result = ipaClient.sendRequest("user_find", ["admin", "register-marcher", "smercado"])
        self.assertEquals("No JSON object could be decoded", result.failure)
    def testClient_sendRequest(self):
        expectedHeaders = {
            "content-length": "0",
            "set-cookie": "ipa_session=b99a25695f578c0bb30cafb0932035bf; Domain=ipa.example.test; Path=/ipa; Expires=Sun, 06 Sep 2015 05:12:56 GMT; Secure; HttpOnly",
            "keep-alive": "timeout=5, max=100",
            "server": "Apache/2.4.6 (CentOS) mod_auth_kerb/5.4 mod_nss/2.4.6 NSS/3.16.2.3 Basic ECC mod_wsgi/3.4 Python/2.7.5",
            "connection": "Keep-Alive",
            "date": "Sun, 06 Sep 2015 04:52:56 GMT",
            "content-type": "text/plain; charset=UTF-8",
        }

        expectedIPAResponse = IPAResponse(
            session="b99a25695f578c0bb30cafb0932035bf",
            status_code=200,
            expiration="Sun, 06 Sep 2015 05:12:56 GMT",
            headers=expectedHeaders,
        )

        with open("%s/%s" % (os.path.dirname(__file__), "resources/user_find_result.json")) as f:
            mockJson = json.load(f)
            mockResponse = MockResponse(status_code=200, headers=expectedHeaders, jsonValue=mockJson)

            expectedResultIPAResponse = IPAResponse(
                status_code=200,
                headers=expectedHeaders,
                raw_result=mockResponse,
                parsed_json=mockJson["result"]["result"],
            )

            ipaAuth = IPAAuth(requests=None, baseUrl=self.baseUrl)
            ipaAuth.authenticate = MagicMock(return_value=expectedIPAResponse)

            requests.post = MagicMock(return_value=mockResponse)

            ipaClient = IPAClient(requests=requests, baseUrl=self.baseUrl, sourceUrl=self.sourceUrl, ipaAuth=ipaAuth)

            response = ipaClient.sendRequest("user_find", ["admin", "register-marcher", "smercado"])
            self.assertEquals(expectedResultIPAResponse.raw_result, response.raw_result)
            self.assertEquals(expectedResultIPAResponse.parsed_json, response.parsed_json)
Beispiel #4
0
    def testClient_sendRequest_noJsonValue(self):
        expectedIPAResponse = IPAResponse(
            session='b99a25695f578c0bb30cafb0932035bf',
            status_code=200,
            expiration='Sun, 06 Sep 2015 05:12:56 GMT',
            headers=None)

        mockResponse = MockResponse(status_code=500, headers=None)

        ipaAuth = IPAAuth(requests=None, baseUrl=self.baseUrl)
        ipaAuth.authenticate = MagicMock(return_value=expectedIPAResponse)
        requests.post = MagicMock(return_value=mockResponse)
        mockResponse.json = MagicMock(
            side_effect=ValueError('No JSON object could be decoded'))

        ipaClient = IPAClient(requests=requests,
                              baseUrl=self.baseUrl,
                              sourceUrl=self.sourceUrl,
                              ipaAuth=ipaAuth)

        result = ipaClient.sendRequest(
            'user_find', ['admin', 'register-marcher', 'smercado'])
        self.assertEquals("No JSON object could be decoded", result.failure)