diff --git a/Makefile b/Makefile index 19fe3d28..15d72570 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ # It's set as a secure environment variable in the .travis.yml file GITHUB_ORG="pactflow" PACTICIPANT="pactflow-example-consumer" +PACT_PROVIDER?=pactflow-example-provider GITHUB_WEBHOOK_UUID := "04510dc1-7f0a-4ed2-997d-114bfa86f8ad" PACT_CLI="docker run --rm -v ${PWD}:${PWD} -e PACT_BROKER_BASE_URL -e PACT_BROKER_TOKEN pactfoundation/pact-cli" @@ -18,12 +19,19 @@ ENVIRONMENT?=production ifeq ($(GIT_BRANCH),master) ENVIRONMENT=production DEPLOY_TARGET=deploy + # On master: verify against what is deployed in production + CID_FLAGS=--to-environment $(ENVIRONMENT) else ifeq ($(GIT_BRANCH),test) ENVIRONMENT=test DEPLOY_TARGET=deploy + CID_FLAGS=--to-environment $(ENVIRONMENT) else DEPLOY_TARGET=no_deploy + # On feature branches: verify against the provider's master branch, + # not a deployed environment. This ensures PRs get fast feedback + # without requiring the provider to already be in production. + CID_FLAGS=--pacticipant $(PACT_PROVIDER) --latest --branch master endif endif @@ -72,7 +80,7 @@ can_i_deploy: .env @"${PACT_CLI}" broker can-i-deploy \ --pacticipant ${PACTICIPANT} \ --version ${GIT_COMMIT} \ - --to-environment ${ENVIRONMENT} \ + ${CID_FLAGS} \ --retry-while-unknown 30 \ --retry-interval 10 diff --git a/src/api.pact.spec.ts b/src/api.pact.spec.ts index 4d0a2674..cba47a12 100644 --- a/src/api.pact.spec.ts +++ b/src/api.pact.spec.ts @@ -109,4 +109,58 @@ describe("API Pact test", () => { }); }); }); + + describe("deleting a product", () => { + it("ID 10 exists", async () => { + const expectedProduct = { + id: "10", + type: "CREDIT_CARD", + name: "28 Degrees", + }; + + await mockProvider + .addInteraction() + .given("a product with ID 10 exists") + .uponReceiving("a request to delete a product") + .withRequest("DELETE", "/product/10", (builder) => { + builder.headers({ + Authorization: like("Bearer 2019-01-14T11:34:18.045Z"), + }); + }) + .willRespondWith(200, (builder) => { + builder.headers({ + "Content-Type": "application/json; charset=utf-8", + }); + builder.jsonBody(like(expectedProduct)); + }) + .executeTest(async (mockserver) => { + const api = new API(mockserver.url); + const product = await api.deleteProduct("10"); + + expect(product).toStrictEqual(new Product(expectedProduct)); + return; + }); + }); + + it("product does not exist", async () => { + await mockProvider + .addInteraction() + .given("a product with ID 11 does not exist") + .uponReceiving("a request to delete a product") + .withRequest("DELETE", "/product/11", (builder) => { + builder.headers({ + Authorization: like("Bearer 2019-01-14T11:34:18.045Z"), + }); + }) + .willRespondWith(404) + .executeTest(async (mockserver) => { + const api = new API(mockserver.url); + + await expect(api.deleteProduct("11")).rejects.toThrow( + "Request failed with status code 404" + ); + return; + }); + }); + }); }); diff --git a/src/api.ts b/src/api.ts index 347fea4c..2b10fdc8 100644 --- a/src/api.ts +++ b/src/api.ts @@ -33,6 +33,17 @@ export class API { }) .then((r) => new Product(r.data)); } + + deleteProduct(id: string): Promise { + return axios + .delete(`/product/${id}`, { + baseURL: this.baseURL, + headers: { + Authorization: this.generateAuthToken(), + }, + }) + .then((r) => new Product(r.data)); + } } export default new API(import.meta.env.VITE_API_BASE_URL);