Ejemplo n.º 1
0
class PactVerifyTestCase(PactTestCase):
    def setUp(self):
        super(PactVerifyTestCase, self).setUp()
        self.addCleanup(patch.stopall)
        self.mock_requests = patch('requests.api.request').start()
        self.target = Pact(self.consumer, self.provider)
        (self.target.given('I am creating a new pact using the Pact class').
         upon_receiving('a specific request to the server').with_request(
             'GET', '/path').will_respond_with(200, body='success'))
        self.get_verification_call = call(
            'get',
            'http://localhost:1234/interactions/verification',
            allow_redirects=True,
            headers={'X-Pact-Mock-Service': 'true'},
            verify=False,
            params=None)

        self.post_publish_pacts_call = call(
            'post',
            'http://localhost:1234/pact',
            data=None,
            headers={'X-Pact-Mock-Service': 'true'},
            verify=False,
            json=None)

    def test_success(self):
        self.mock_requests.side_effect = iter([Mock(status_code=200)] * 2)
        self.target.verify()

        self.assertEqual(self.mock_requests.call_count, 2)
        self.mock_requests.assert_has_calls(
            [self.get_verification_call, self.post_publish_pacts_call])

    def test_error_verifying_interactions(self):
        self.mock_requests.side_effect = iter(
            [Mock(status_code=500, text='verification error')])

        with self.assertRaises(AssertionError) as e:
            self.target.verify()

        self.assertEqual(str(e.exception), 'verification error')
        self.assertEqual(self.mock_requests.call_count, 1)
        self.mock_requests.assert_has_calls([self.get_verification_call])

    def test_error_writing_pacts_to_file(self):
        self.mock_requests.side_effect = iter([
            Mock(status_code=200),
            Mock(status_code=500, text='error writing pact to file')
        ])

        with self.assertRaises(AssertionError) as e:
            self.target.verify()

        self.assertEqual(str(e.exception), 'error writing pact to file')
        self.assertEqual(self.mock_requests.call_count, 2)
        self.mock_requests.assert_has_calls(
            [self.get_verification_call, self.post_publish_pacts_call])
Ejemplo n.º 2
0
    def test_definition_all_options(self):
        target = Pact(self.consumer, self.provider)
        (target.given('I am creating a new pact using the Pact class').
         upon_receiving('a specific request to the server').with_request(
             'GET',
             '/path',
             body={
                 'key': 'value'
             },
             headers={
                 'Accept': 'application/json'
             },
             query={
                 'search': 'test'
             }).will_respond_with(200,
                                  body='success',
                                  headers={'Content-Type':
                                           'application/json'}))

        self.assertEqual(target._interactions[0]['provider_state'],
                         'I am creating a new pact using the Pact class')

        self.assertEqual(target._interactions[0]['description'],
                         'a specific request to the server')

        self.assertEqual(
            target._interactions[0]['request'], {
                'path': '/path',
                'method': 'GET',
                'body': {
                    'key': 'value'
                },
                'headers': {
                    'Accept': 'application/json'
                },
                'query': {
                    'search': 'test'
                }
            })
        self.assertEqual(
            target._interactions[0]['response'], {
                'status': 200,
                'body': 'success',
                'headers': {
                    'Content-Type': 'application/json'
                }
            })
Ejemplo n.º 3
0
    def test_definition_sparse(self):
        target = Pact(self.consumer, self.provider)
        (target.given('I am creating a new pact using the Pact class').
         upon_receiving('a specific request to the server').with_request(
             'GET', '/path').will_respond_with(200, body='success'))

        self.assertEqual(len(target._interactions), 1)

        self.assertEqual(target._interactions[0]['provider_state'],
                         'I am creating a new pact using the Pact class')

        self.assertEqual(target._interactions[0]['description'],
                         'a specific request to the server')

        self.assertEqual(target._interactions[0]['request'], {
            'path': '/path',
            'method': 'GET'
        })
        self.assertEqual(target._interactions[0]['response'], {
            'status': 200,
            'body': 'success'
        })
Ejemplo n.º 4
0
class PactSetupTestCase(PactTestCase):
    def setUp(self):
        super(PactSetupTestCase, self).setUp()
        self.addCleanup(patch.stopall)
        self.mock_requests = patch('requests.api.request').start()
        self.target = Pact(self.consumer, self.provider)
        (self.target.given('I am creating a new pact using the Pact class').
         upon_receiving('a specific request to the server').with_request(
             'GET', '/path').will_respond_with(200, body='success'))

        self.delete_call = call('delete',
                                'http://localhost:1234/interactions',
                                headers={'X-Pact-Mock-Service': 'true'})

        self.put_interactions_call = call(
            'put',
            'http://localhost:1234/interactions',
            data=None,
            headers={'X-Pact-Mock-Service': 'true'},
            json={
                'interactions': [{
                    'response': {
                        'status': 200,
                        'body': 'success'
                    },
                    'request': {
                        'path': '/path',
                        'method': 'GET'
                    },
                    'description':
                    'a specific request to the server',
                    'provider_state':
                    'I am creating a new pact using the '
                    'Pact class'
                }]
            })

    def test_error_deleting_interactions(self):
        self.mock_requests.side_effect = iter(
            [Mock(status_code=500, text='deletion error')])

        with self.assertRaises(AssertionError) as e:
            self.target.setup()

        self.assertEqual(str(e.exception), 'deletion error')
        self.assertEqual(self.mock_requests.call_count, 1)
        self.mock_requests.assert_has_calls([self.delete_call])

    def test_error_posting_interactions(self):
        self.mock_requests.side_effect = iter([
            Mock(status_code=200),
            Mock(status_code=500, text='post interactions error')
        ])

        with self.assertRaises(AssertionError) as e:
            self.target.setup()

        self.assertEqual(str(e.exception), 'post interactions error')
        self.assertEqual(self.mock_requests.call_count, 2)
        self.mock_requests.assert_has_calls(
            [self.delete_call, self.put_interactions_call])

    def test_successful(self):
        self.mock_requests.side_effect = iter([Mock(status_code=200)] * 4)
        self.target.setup()

        self.assertEqual(self.mock_requests.call_count, 2)
        self.mock_requests.assert_has_calls(
            [self.delete_call, self.put_interactions_call])