pact-foundation / pact-python

Python version of Pact. Enables consumer driven contract testing, providing a mock service and DSL for the consumer project, and interaction playback and verification for the service provider project.
http://pact.io
MIT License
571 stars 138 forks source link

No way to verify with a specific pact #332

Open ims-swilkinson opened 1 year ago

ims-swilkinson commented 1 year ago

I'm using pact-python version 1.7.0. There doesn't seem to be a way to verify with a specific pact by passing in a pact URL.

I tried passing a pact_url kwarg into MessageProvider.verify_with_broker but this always gives me the error pact.verify_wrapper.PactException: Pact urls or Pact broker required.

YOU54F commented 1 year ago

Hey @ims-swilkinson

can you confirm if the request was to verify a pact by URL, to be fetched from a broker (with a view to verify, and optionally publish the results back) as a result of webhook triggered provider jobs

https://docs.pact.io/pact_broker/webhooks#events

or was there also a request to be able to read files from a local file system (in which case, using a named function verify_with_broker - it wouldn't make sense to publish results of local files verification to a broker, unless it was retrieved from the broker.

Hopefully we can clarify and then get this feature out to you!

YOU54F commented 1 year ago

So there are at least three use cases for verifying

and 4 file sources

Rust distinguishes between file and dir, I would need to test if the ruby provider verifier will also accept a dir as as a arg (assume it will)

Verify with a file source

Verify a local file, provided by a file or dir, verification results should never be published and therefore this step does not require a broker

Verify by pact url (consumer change)

Verify a pact from a remote url, which will be retrieve from a pact broker, for a contract that has changed and requires verification.

Verify by selectors (provider change)

Verify remote pacts, provided by querying the Pact Broker via consumer version selectors, verification results may be published

State of Pact Python

Pact Python appears to

Smallest step forward

Real world examples - Pact JS

Verify by pact url (consumer change)

https://github.com/pactflow/example-provider/blob/master/src/product/product.consumerChange.pact.test.js

uses these options

    const opts = {
      ...baseOpts,
      pactUrls: [process.env.PACT_URL],
      stateHandlers: stateHandlers,
      requestFilter: requestFilter
    };

Verify by selectors (provider change)

https://github.com/pactflow/example-provider/blob/master/src/product/product.providerChange.pact.test.js

    const fetchPactsDynamicallyOpts = {
      provider: 'pactflow-example-provider',
      consumerVersionSelectors: [
        { mainBranch: true },
        { deployed: true },
        { matchingBranch: true }
      ],
      pactBrokerUrl: process.env.PACT_BROKER_BASE_URL,
      // https://docs.pact.io/pact_broker/advanced_topics/pending_pacts
      enablePending: true,
      // https://docs.pact.io/pact_broker/advanced_topics/wip_pacts
      includeWipPactsSince: '2020-01-01'
    };

    const opts = {
      ...baseOpts,
      ...fetchPactsDynamicallyOpts,
      stateHandlers: stateHandlers,
      requestFilter: requestFilter
    };

There is some commonality between the modes the verifying with a broker

https://github.com/pactflow/example-provider/blob/master/src/product/pact.setup.js

const baseOpts = {
  logLevel: "INFO",
  providerBaseUrl: "http://localhost:8080",
  providerVersion: process.env.GIT_COMMIT,
  providerVersionBranch: process.env.GIT_BRANCH, // the recommended way of publishing verification results with the branch property
  verbose: process.env.VERBOSE === "true",
};

Real world examples - .NET

Verify by pact local file

https://github.com/pactflow/example-provider-dotnet/blob/34c615db8b828dcca1947e3f6d1c81a84c5e4b2c/tests/ProviderApiTests.cs#L72

Verify by pact url (consumer change)

https://github.com/pactflow/example-provider-dotnet/blob/34c615db8b828dcca1947e3f6d1c81a84c5e4b2c/tests/ProviderApiTests.cs#LL81C1-L99C27

Verify by selectors (provider change)

https://github.com/pactflow/example-provider-dotnet/blob/34c615db8b828dcca1947e3f6d1c81a84c5e4b2c/tests/ProviderApiTests.cs#L105

YOU54F commented 1 year ago

so this func

https://github.com/pact-foundation/pact-python/blob/fc6ced8b08aed733ed2406a98bef9554a9da399a/pact/verifier.py#L36

should probably have never allowed for setting wip pacts, or enable pending if its not for verifying with a broker.

There is a function called verify_with_broker, one would assume for that reason.

I've given this a go in #356

I am not a pythonista at all, so happy for reviews, feedback, additional commits 👍🏾

ims-swilkinson commented 1 year ago

Hey @ims-swilkinson

can you confirm if the request was to verify a pact by URL, to be fetched from a broker (with a view to verify, and optionally publish the results back) as a result of webhook triggered provider jobs

https://docs.pact.io/pact_broker/webhooks#events

or was there also a request to be able to read files from a local file system (in which case, using a named function verify_with_broker - it wouldn't make sense to publish results of local files verification to a broker, unless it was retrieved from the broker.

Hopefully we can clarify and then get this feature out to you!

Sorry for the late reply. Yes this is for verifying using the URL passed in a webhook request, not for local files.

mefellows commented 1 year ago

Thanks. Would you be up for a PR as per Yousaf's comments?