Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[contract-tests] fix: Adding delete scenario #56

Merged
merged 2 commits into from
Jun 16, 2024
Merged
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
4 changes: 3 additions & 1 deletion .github/workflows/backend-contract-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches: [ "main" ]
paths:
- "backend/**"
- "frontend/src/api/**"
- "frontend/contract-tests/**"

defaults:
run:
Expand All @@ -17,7 +19,7 @@ jobs:
API_URL: http://localhost:5000
CONTRACT_TEST_AUTH_TOKEN: ${{ secrets.CONTRACT_TEST_AUTH_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
Expand Down
217 changes: 115 additions & 102 deletions frontend/contract-tests/consumer.pact.spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import path from "path";
import path from 'path';
import {
PactV3,
MatchersV3,
SpecificationVersion,
} from "@pact-foundation/pact";
import * as API from "../src/api/apiService";
import { AxiosResponse } from "axios";
} from '@pact-foundation/pact';
import * as API from '../src/api/apiService';
import { AxiosResponse } from 'axios';
const { eachLike, like } = MatchersV3;

const provider = new PactV3({
consumer: "NotesFEService",
provider: "NotesBEService",
logLevel: "debug",
dir: path.resolve(process.cwd(), "pacts"),
consumer: 'NotesFEService',
provider: 'NotesBEService',
logLevel: 'debug',
dir: path.resolve(process.cwd(), 'pacts'),
spec: SpecificationVersion.SPECIFICATION_VERSION_V2,
host: "127.0.0.1",
host: '127.0.0.1',
port: 5000,
});

describe("Pact with NotesBEService", () => {
it("returns the users notes", async () => {
describe('Pact with NotesBEService', () => {
it('returns the users notes', async () => {
provider.addInteraction({
states: [{ description: "notes are returned" }],
uponReceiving: "a request to get notes",
states: [{ description: 'notes are returned' }],
uponReceiving: 'a request to get notes',
withRequest: {
method: "GET",
path: "/api/notes",
method: 'GET',
path: '/api/notes',
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json',
},
body: eachLike({
id: "0a97f1c3-294e-43e8-b78f-60209e972ee9",
title: "Note Title",
content: "Note Content",
createdAt: "2021-01-01T00:00:00.000Z",
updatedAt: "2021-01-01T00:00:00.000Z",
userID: "618005a7-bbca-4d1f-83cd-1fb1d5511d06",
id: '0a97f1c3-294e-43e8-b78f-60209e972ee9',
title: 'Note Title',
content: 'Note Content',
createdAt: '2021-01-01T00:00:00.000Z',
updatedAt: '2021-01-01T00:00:00.000Z',
userID: '618005a7-bbca-4d1f-83cd-1fb1d5511d06',
}),
},
});
Expand All @@ -48,147 +48,160 @@ describe("Pact with NotesBEService", () => {

expect(response.data).toStrictEqual([
{
id: "0a97f1c3-294e-43e8-b78f-60209e972ee9",
title: "Note Title",
content: "Note Content",
createdAt: "2021-01-01T00:00:00.000Z",
updatedAt: "2021-01-01T00:00:00.000Z",
userID: "618005a7-bbca-4d1f-83cd-1fb1d5511d06",
id: '0a97f1c3-294e-43e8-b78f-60209e972ee9',
title: 'Note Title',
content: 'Note Content',
createdAt: '2021-01-01T00:00:00.000Z',
updatedAt: '2021-01-01T00:00:00.000Z',
userID: '618005a7-bbca-4d1f-83cd-1fb1d5511d06',
},
]);
});
});

it("add a note", async () => {
it('add a note', async () => {
provider.addInteraction({
states: [{ description: "note is added" }],
uponReceiving: "a request to create a note",
states: [{ description: 'note is added' }],
uponReceiving: 'a request to create a note',
withRequest: {
method: "POST",
path: "/api/notes",
method: 'POST',
path: '/api/notes',
body: {
title: "New Note Title",
content: "New Note Content",
title: 'New Note Title',
content: 'New Note Content',
},
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json',
},
body: like ({
id: "0a97f1c3-294e-43e8-b78f-60209e972ee9",
title: "New Note Title",
content: "New Note Content",
createdAt: "2021-01-01T00:00:00.000Z",
updatedAt: "2021-01-01T00:00:00.000Z",
userID: "618005a7-bbca-4d1f-83cd-1fb1d5511d06",
body: like({
id: '0a97f1c3-294e-43e8-b78f-60209e972ee9',
title: 'New Note Title',
content: 'New Note Content',
createdAt: '2021-01-01T00:00:00.000Z',
updatedAt: '2021-01-01T00:00:00.000Z',
userID: '618005a7-bbca-4d1f-83cd-1fb1d5511d06',
}),
},
});

await provider.executeTest(async (mockService) => {
const response = await API.postNote({
title: "New Note Title",
content: "New Note Content",
});
title: 'New Note Title',
content: 'New Note Content',
});

expect(response.data).toStrictEqual(
{
id: "0a97f1c3-294e-43e8-b78f-60209e972ee9",
title: "New Note Title",
content: "New Note Content",
createdAt: "2021-01-01T00:00:00.000Z",
updatedAt: "2021-01-01T00:00:00.000Z",
userID: "618005a7-bbca-4d1f-83cd-1fb1d5511d06",
},
);
expect(response.data).toStrictEqual({
id: '0a97f1c3-294e-43e8-b78f-60209e972ee9',
title: 'New Note Title',
content: 'New Note Content',
createdAt: '2021-01-01T00:00:00.000Z',
updatedAt: '2021-01-01T00:00:00.000Z',
userID: '618005a7-bbca-4d1f-83cd-1fb1d5511d06',
});
});
});

it("update a note", async () => {
it('update a note', async () => {
provider.addInteraction({
states: [{ description: "note is updated" }],
uponReceiving: "a request to update a note",
states: [{ description: 'note is updated' }],
uponReceiving: 'a request to update a note',
withRequest: {
method: "PUT",
path: "/api/notes/a37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
method: 'PUT',
path: '/api/notes/a37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
body: {
title: "Updated Note Title",
content: "Updated Note Content",
title: 'Updated Note Title',
content: 'Updated Note Content',
},
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json',
},
body: like ( {
"id": "a37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
"title": "Updated Note Title",
"content": "Updated Note Content",
"createdAt": "2024-05-21T22:58:55.743Z",
"updatedAt": "2024-05-26T19:43:58.742Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
}),
body: like({
id: 'a37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
title: 'Updated Note Title',
content: 'Updated Note Content',
createdAt: '2024-05-21T22:58:55.743Z',
updatedAt: '2024-05-26T19:43:58.742Z',
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
}),
},
});

await provider.executeTest(async (mockService) => {
const response = await API.patchNote({
id: "a37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
title: "Updated Note Title",
content: "Updated Note Content",
id: 'a37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
title: 'Updated Note Title',
content: 'Updated Note Content',
});

expect((response as AxiosResponse<any>).data).toStrictEqual(
{
"id": "a37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
"title": "Updated Note Title",
"content": "Updated Note Content",
"createdAt": "2024-05-21T22:58:55.743Z",
"updatedAt": "2024-05-26T19:43:58.742Z",
"userID": "ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272"
},
);
expect((response as AxiosResponse<any>).data).toStrictEqual({
id: 'a37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
title: 'Updated Note Title',
content: 'Updated Note Content',
createdAt: '2024-05-21T22:58:55.743Z',
updatedAt: '2024-05-26T19:43:58.742Z',
userID: 'ccf89a7e-b941-4f17-bbe0-4e0c8b2cd272',
});
});
});
it("update non-existant note", async () => {
it('update non-existant note', async () => {
provider.addInteraction({
states: [{ description: "note does not exist" }],
uponReceiving: "a request to update a non-existant note",
states: [{ description: 'note does not exist' }],
uponReceiving: 'a request to update a non-existant note',
withRequest: {
method: "PUT",
path: "/api/notes/b37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
method: 'PUT',
path: '/api/notes/b37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
body: {
title: "Updated Note Title",
content: "Updated Note Content",
title: 'Updated Note Title',
content: 'Updated Note Content',
},
},
willRespondWith: {
status: 404,
headers: {
"Content-Type": "application/json"
'Content-Type': 'application/json',
},
body: like ( { error: "Note not found" }),
body: like({ error: 'Note not found' }),
},
});

await provider.executeTest(async (mockService) => {
const response = await API.patchNote({
id: "b37f39bc-9e4f-45f2-b1d6-fe668bba2b55",
title: "Updated Note Title",
content: "Updated Note Content",
id: 'b37f39bc-9e4f-45f2-b1d6-fe668bba2b55',
title: 'Updated Note Title',
content: 'Updated Note Content',
});

expect(response.status).toBe(404);

expect(response).toStrictEqual(
{
error: "Note not found",
status: 404,
expect(response).toStrictEqual({
error: 'Note not found',
status: 404,
});
});
});
it('delete a note', async () => {
provider.addInteraction({
states: [{ description: 'note is deleted' }],
uponReceiving: 'a request to delete a note',
withRequest: {
method: 'DELETE',
path: '/api/notes/0a97f1c3-294e-43e8-b78f-60209e972ee9',
},
willRespondWith: {
status: 200,
headers: {
'Content-Type': 'application/json',
},
);
body: like({
status: 'ok',
}),
},
});
});
});
2 changes: 1 addition & 1 deletion frontend/pacts/NotesFEService-NotesBEService.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
],
"metadata": {
"pact-js": {
"version": "12.5.0"
"version": "12.5.2"
},
"pactRust": {
"ffi": "0.4.20",
Expand Down
Loading