Search code examples
python-requestspact

how to send authorization details in pact Json Generation or to pact verfier in PACT PYTHON


how to send authorization details in MOCK service or to pact verfier in PACT PYTHON

when i call API thorugh soapUI it is working fine but when i run it mock Json through Pact verfier , it is faling since i am not senidng Authorization details in request header or not adding in pact verfier. can u please help me how can send authorization details through request headers in PACT python?

def test_HappyPath (self):
            mockurl = 'http://localhost:1234'
            expected =  {body:true}
            pact.given (
                'Given there is a valid  form'
            ).upon_receiving (
                'fetch all the info '
            ).with_request (
                 'get',
                '/',headers={Authorization:'Bearer 58771381-333e-334f-9604-784'}
            ).will_respond_with(200, body=expected)
  with pact:
                result = callAPI ( mockurl )
            self.assertEqual(result, expected )

Request and Authroization Info:

 GET https: http....com/ /v1/ forms/83359274-7ad6-4
    Accept-Encoding: gzip,deflate
    Authorization: Bearer 58771381-333e-334f-9604-ebf977ed7784
    Content-Length: 0
    Host: company.com
    Connection: Keep-Alive
    User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_162)


OAUTH2.0: 
CSClientUser=username
CSClientPassword=pwd
CSClientIdendification=xxxx
CSClientSecret=fffff
CSAccessTokenURI=company.com/oauth2/token

Solution

  • Please read https://docs.pact.io/faq#how-do-i-test-oauth-or-other-security-headers as a starting point.

    On the consumer side, what you have done is fine (although you might want a matcher if you are generating unique tokens).

    On the provider side, you will want to pass the (as yet undocumented in pact-python) flag --custom-provider-header option to the verification process[1], which allows you to override one or more headers with dynamic values (in your case, a live auth token).

    This is assuming of course that you can't mock out the authentication system during provider verification.

    Under the hood, a separate service[2] is spawned to do this verification. There is more info on this feature in the link provided.