Skip to content

Commit

Permalink
Merge pull request #729 from companieshouse/feature/lp-284-patch-email
Browse files Browse the repository at this point in the history
lp-284 : add patchLimitedPartnership method to limited partnership service
  • Loading branch information
lduranteau authored Dec 9, 2024
2 parents 4096cb8 + 199e663 commit 07ce03c
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 51 deletions.
22 changes: 21 additions & 1 deletion src/services/limited-partnerships/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { LimitedPartnership, LimitedPartnershipCreated } from "./types";
import Resource, { ApiErrorResponse } from "../resource";

export default class LimitedPartnershipsService {
constructor (private readonly client: IHttpClient) { }
constructor (private readonly client: IHttpClient) {}

public async postLimitedPartnership (
transactionId: string,
Expand All @@ -26,4 +26,24 @@ export default class LimitedPartnershipsService {
resource.resource = { ...response.body };
return resource;
}

public async patchLimitedPartnership (
transactionId: string,
submissionId: string,
body: { type: string; data: Record<string, any> }
): Promise<Resource<void> | ApiErrorResponse> {
const URL = `/transactions/${transactionId}/limited-partnership/partnership/${submissionId}`;
const response: HttpResponse = await this.client.httpPatch(URL, body);

if (response.error) {
return {
httpStatusCode: response.status,
errors: [response.error]
};
}

return {
httpStatusCode: response.status
};
}
}
22 changes: 16 additions & 6 deletions test/services/limited-partnerships/limited.partnerships.mock.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
import { RequestClient } from "../../../src";
import { LimitedPartnership, LimitedPartnershipCreated, NameEndingType } from "../../../src/services/limited-partnerships";
import {
LimitedPartnership,
LimitedPartnershipCreated,
NameEndingType
} from "../../../src/services/limited-partnerships";

export const requestClient = new RequestClient({ baseUrl: "URL_NOT_USED", oauthToken: "TOKEN_NOT_USED" });
export const requestClient = new RequestClient({
baseUrl: "URL_NOT_USED",
oauthToken: "TOKEN_NOT_USED"
});

export const LIMITED_PARTNERSHIP_OBJECT_MOCK: LimitedPartnership = {
data: {
partnership_name: "Legalised Asset Stashing",
name_ending: NameEndingType.LIMITED_PARTNERSHIP
}
}
};

export const TRANSACTION_ID = "12345";
export const SUBMISSION_ID = "09876";
export const LIMITED_PARTNERSHIP_ID = "00112233";
export const UNAUTHORISED = "Unauthorised";
export const BAD_REQUEST = "Bad Request";

export const mockLimitedPartnershipCreatedResource: LimitedPartnershipCreated = {
id: LIMITED_PARTNERSHIP_ID
};
export const mockLimitedPartnershipCreatedResource: LimitedPartnershipCreated =
{
id: LIMITED_PARTNERSHIP_ID
};

export const mockPostLimitedPartnershipResponse = {
200: { status: 200 },
201: { status: 201, body: mockLimitedPartnershipCreatedResource },
400: { status: 400, error: BAD_REQUEST },
401: { status: 401, error: UNAUTHORISED }
Expand Down
193 changes: 149 additions & 44 deletions test/services/limited-partnerships/limited.partnerships.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { expect } from "chai";
import sinon from "sinon";

import * as mockValues from "./limited.partnerships.mock";
import { LimitedPartnershipCreated, LimitedPartnershipsService, NameEndingType } from "../../../src/services/limited-partnerships";
import {
LimitedPartnershipCreated,
LimitedPartnershipsService,
NameEndingType
} from "../../../src/services/limited-partnerships";
import Resource, { ApiErrorResponse } from "../../../src/services/resource";

describe("LimitedPartnershipsService POST Tests suite", () => {
Expand All @@ -12,62 +16,163 @@ describe("LimitedPartnershipsService POST Tests suite", () => {
sinon.restore();
});

afterEach(done => {
afterEach((done) => {
sinon.reset();
sinon.restore();
done();
});

it("should return object Id for postLimitedPartnership method", async () => {
const mockRequest = sinon.stub(mockValues.requestClient, "httpPost").resolves(mockValues.mockPostLimitedPartnershipResponse[201]);
const service = new LimitedPartnershipsService(mockValues.requestClient);
const response = (await service.postLimitedPartnership(
mockValues.TRANSACTION_ID,
mockValues.LIMITED_PARTNERSHIP_OBJECT_MOCK
)) as Resource<LimitedPartnershipCreated>;

expect(mockRequest).to.have.been.calledOnce;
expect(
mockRequest.calledWith("/transactions/12345/limited-partnership/partnership",
{
data: {
partnership_name: "Legalised Asset Stashing",
name_ending: "Limited Partnership"
describe("postLimitedPartnership", () => {
it("should return object Id for postLimitedPartnership method", async () => {
const mockRequest = sinon
.stub(mockValues.requestClient, "httpPost")
.resolves(mockValues.mockPostLimitedPartnershipResponse[201]);
const service = new LimitedPartnershipsService(
mockValues.requestClient
);
const response = (await service.postLimitedPartnership(
mockValues.TRANSACTION_ID,
mockValues.LIMITED_PARTNERSHIP_OBJECT_MOCK
)) as Resource<LimitedPartnershipCreated>;

expect(mockRequest).to.have.been.calledOnce;
expect(
mockRequest.calledWith(
"/transactions/12345/limited-partnership/partnership",
{
data: {
partnership_name: "Legalised Asset Stashing",
name_ending: "Limited Partnership"
}
}
})
).to.be.true;
)
).to.be.true;

expect(response.httpStatusCode).to.equal(201);
expect(response.resource?.id).to.equal(mockValues.mockLimitedPartnershipCreatedResource.id);
});
expect(response.httpStatusCode).to.equal(201);
expect(response.resource?.id).to.equal(
mockValues.mockLimitedPartnershipCreatedResource.id
);
});

it("should return error 401 (Unauthorised) for postLimitedPartnership method", async () => {
const mockRequest = sinon.stub(mockValues.requestClient, "httpPost").resolves(mockValues.mockPostLimitedPartnershipResponse[401]);
it("should return error 401 (Unauthorised) for postLimitedPartnership method", async () => {
const mockRequest = sinon
.stub(mockValues.requestClient, "httpPost")
.resolves(mockValues.mockPostLimitedPartnershipResponse[401]);

const service = new LimitedPartnershipsService(mockValues.requestClient);
const response = await service.postLimitedPartnership(mockValues.TRANSACTION_ID, { data: {} }) as ApiErrorResponse;
const service = new LimitedPartnershipsService(
mockValues.requestClient
);
const response = (await service.postLimitedPartnership(
mockValues.TRANSACTION_ID,
{ data: {} }
)) as ApiErrorResponse;

expect(mockRequest).to.have.been.calledOnce;
expect(
mockRequest.calledWith("/transactions/12345/limited-partnership/partnership", { data: {} })
).to.be.true;
expect(mockRequest).to.have.been.calledOnce;
expect(
mockRequest.calledWith(
"/transactions/12345/limited-partnership/partnership",
{ data: {} }
)
).to.be.true;

expect(response.httpStatusCode).to.equal(401);
expect(response.errors?.[0]).to.equal(mockValues.UNAUTHORISED);
});
expect(response.httpStatusCode).to.equal(401);
expect(response.errors?.[0]).to.equal(mockValues.UNAUTHORISED);
});

it("should return error 400 (Bad Request) for postLimitedPartnership method", async () => {
const mockRequest = sinon.stub(mockValues.requestClient, "httpPost").resolves(mockValues.mockPostLimitedPartnershipResponse[400]);
it("should return error 400 (Bad Request) for postLimitedPartnership method", async () => {
const mockRequest = sinon
.stub(mockValues.requestClient, "httpPost")
.resolves(mockValues.mockPostLimitedPartnershipResponse[400]);

const service = new LimitedPartnershipsService(mockValues.requestClient);
const response = await service.postLimitedPartnership(mockValues.TRANSACTION_ID, { data: { name_ending: NameEndingType.LP } }) as ApiErrorResponse;
const service = new LimitedPartnershipsService(
mockValues.requestClient
);
const response = (await service.postLimitedPartnership(
mockValues.TRANSACTION_ID,
{ data: { name_ending: NameEndingType.LP } }
)) as ApiErrorResponse;

expect(mockRequest).to.have.been.calledOnce;
expect(
mockRequest.calledWith("/transactions/12345/limited-partnership/partnership", { data: { name_ending: NameEndingType.LP } })
).to.be.true;
expect(mockRequest).to.have.been.calledOnce;
expect(
mockRequest.calledWith(
"/transactions/12345/limited-partnership/partnership",
{ data: { name_ending: NameEndingType.LP } }
)
).to.be.true;

expect(response.httpStatusCode).to.equal(400);
expect(response.errors?.[0]).to.equal(mockValues.BAD_REQUEST);
});
expect(response.httpStatusCode).to.equal(400);
expect(response.errors?.[0]).to.equal(mockValues.BAD_REQUEST);
});
})

describe("patchLimitedPartnership", () => {
it("should return a status 200", async () => {
const mockRequest = sinon
.stub(mockValues.requestClient, "httpPatch")
.resolves(mockValues.mockPostLimitedPartnershipResponse[200]);

const service = new LimitedPartnershipsService(
mockValues.requestClient
);

const response = await service.patchLimitedPartnership(
mockValues.TRANSACTION_ID,
mockValues.SUBMISSION_ID,
{
type: "email",
data: {
email: "[email protected]"
}
}
);

expect(mockRequest).to.have.been.calledOnce;
expect(
mockRequest.calledWith(
"/transactions/12345/limited-partnership/partnership/09876",
{
type: "email",
data: {
email: "[email protected]"
}
}
)
).to.be.true;
expect(response.httpStatusCode).to.equal(200);
});

it("should return error 400 (Bad Request)", async () => {
const mockRequest = sinon
.stub(mockValues.requestClient, "httpPatch")
.resolves(mockValues.mockPostLimitedPartnershipResponse[400]);

const service = new LimitedPartnershipsService(
mockValues.requestClient
);

const response = await service.patchLimitedPartnership(
mockValues.TRANSACTION_ID,
mockValues.SUBMISSION_ID,
{
type: "email",
data: {}
}
) as ApiErrorResponse;

expect(mockRequest).to.have.been.calledOnce;
expect(
mockRequest.calledWith(
"/transactions/12345/limited-partnership/partnership/09876",
{
type: "email",
data: {}

}
)
).to.be.true;

expect(response.httpStatusCode).to.equal(400);
expect(response.errors?.[0]).to.equal(mockValues.BAD_REQUEST);
});
})
});

0 comments on commit 07ce03c

Please sign in to comment.