Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
54 changes: 54 additions & 0 deletions src/api.pact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});
});
11 changes: 11 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export class API {
})
.then((r) => new Product(r.data));
}

deleteProduct(id: string): Promise<Product> {
return axios
.delete<ProductData>(`/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);
Loading