-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAU: Introduce missing service tests
These were previously untested. This will make it easier to prove changes to these files are correct in future. This also introduces a common test helper for service tests, since there is a lot of repetition between the tests"
- Loading branch information
Showing
12 changed files
with
827 additions
and
12 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/components/account-intervention/tests/account-intervention-service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { describe } from "mocha"; | ||
import sinon, { SinonStub } from "sinon"; | ||
import { AccountInterventionsInterface } from "../types"; | ||
import { accountInterventionService } from "../account-intervention-service"; | ||
import { | ||
checkApiCallMadeWithExpectedBodyAndHeaders, | ||
commonVariables, | ||
expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
resetApiKeyAndBaseUrlEnvVars, | ||
setupApiKeyAndBaseUrlEnvVars, | ||
} from "../../../../test/helpers/service-test-helper"; | ||
import { API_ENDPOINTS } from "../../../app.constants"; | ||
import { Http } from "../../../utils/http"; | ||
|
||
describe("account interventions service", () => { | ||
const httpInstance = new Http(); | ||
const service: AccountInterventionsInterface = | ||
accountInterventionService(httpInstance); | ||
let postStub: SinonStub; | ||
|
||
beforeEach(() => { | ||
setupApiKeyAndBaseUrlEnvVars(); | ||
postStub = sinon.stub(httpInstance.client, "post"); | ||
}); | ||
|
||
afterEach(() => { | ||
postStub.reset(); | ||
resetApiKeyAndBaseUrlEnvVars(); | ||
}); | ||
|
||
it("successfully calls the API to check a user's account interventions status", async () => { | ||
const axiosResponse = Promise.resolve({ | ||
data: {}, | ||
status: 200, | ||
statusText: "OK", | ||
}); | ||
postStub.resolves(axiosResponse); | ||
const { sessionId, clientSessionId, email, ip, diPersistentSessionId } = | ||
commonVariables; | ||
|
||
const result = await service.accountInterventionStatus( | ||
sessionId, | ||
email, | ||
ip, | ||
clientSessionId, | ||
diPersistentSessionId | ||
); | ||
|
||
const expectedApiCallDetails = { | ||
expectedPath: API_ENDPOINTS.ACCOUNT_INTERVENTIONS, | ||
expectedHeaders: expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
expectedBody: { email: commonVariables.email }, | ||
}; | ||
|
||
checkApiCallMadeWithExpectedBodyAndHeaders( | ||
result, | ||
postStub, | ||
true, | ||
expectedApiCallDetails | ||
); | ||
}); | ||
}); |
68 changes: 56 additions & 12 deletions
68
src/components/check-reauth-users/tests/check-reauth-user-service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,66 @@ | ||
import { expect } from "chai"; | ||
import { describe } from "mocha"; | ||
|
||
import { supportReauthentication } from "../../../config"; | ||
import { Http } from "../../../utils/http"; | ||
import { sinon } from "../../../../test/utils/test-utils"; | ||
import { API_ENDPOINTS } from "../../../app.constants"; | ||
import { SinonStub } from "sinon"; | ||
import { checkReauthUsersService } from "../check-reauth-users-service"; | ||
import { CheckReauthServiceInterface } from "../types"; | ||
import { | ||
checkApiCallMadeWithExpectedBodyAndHeaders, | ||
commonVariables, | ||
expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
resetApiKeyAndBaseUrlEnvVars, | ||
setupApiKeyAndBaseUrlEnvVars, | ||
} from "../../../../test/helpers/service-test-helper"; | ||
|
||
describe("re-authentication service", () => { | ||
describe("with auth re-authentication feature flag on", () => { | ||
it("should return true", async () => { | ||
process.env.SUPPORT_REAUTHENTICATION = "1"; | ||
const httpInstance = new Http(); | ||
const service: CheckReauthServiceInterface = | ||
checkReauthUsersService(httpInstance); | ||
const SUBJECT = "123"; | ||
let postStub: SinonStub; | ||
|
||
expect(supportReauthentication()).to.be.true; | ||
}); | ||
beforeEach(() => { | ||
setupApiKeyAndBaseUrlEnvVars(); | ||
postStub = sinon.stub(httpInstance.client, "post"); | ||
}); | ||
|
||
describe("with auth re-authentication feature flag off", () => { | ||
it("should return false", async () => { | ||
process.env.SUPPORT_REAUTHENTICATION = "0"; | ||
afterEach(() => { | ||
postStub.reset(); | ||
resetApiKeyAndBaseUrlEnvVars(); | ||
}); | ||
|
||
expect(supportReauthentication()).to.be.false; | ||
it("successfully calls the API to check a reauth user", async () => { | ||
const axiosResponse = Promise.resolve({ | ||
data: {}, | ||
status: 200, | ||
statusText: "OK", | ||
}); | ||
postStub.resolves(axiosResponse); | ||
const { sessionId, email, ip, clientSessionId, diPersistentSessionId } = | ||
commonVariables; | ||
|
||
const result = await service.checkReauthUsers( | ||
sessionId, | ||
email, | ||
SUBJECT, | ||
ip, | ||
clientSessionId, | ||
diPersistentSessionId | ||
); | ||
|
||
const expectedApiCallDetails = { | ||
expectedPath: API_ENDPOINTS.CHECK_REAUTH_USER, | ||
expectedHeaders: expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
expectedBody: { email: commonVariables.email, rpPairwiseId: SUBJECT }, | ||
validateStatus: true, | ||
}; | ||
|
||
checkApiCallMadeWithExpectedBodyAndHeaders( | ||
result, | ||
postStub, | ||
true, | ||
expectedApiCallDetails | ||
); | ||
}); | ||
}); |
62 changes: 62 additions & 0 deletions
62
src/components/common/account-recovery/tests/account-recovery-service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { describe } from "mocha"; | ||
import sinon, { SinonStub } from "sinon"; | ||
import { Http } from "../../../../utils/http"; | ||
import { AccountRecoveryInterface } from "../types"; | ||
import { accountRecoveryService } from "../account-recovery-service"; | ||
import { | ||
checkApiCallMadeWithExpectedBodyAndHeaders, | ||
commonVariables, | ||
expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
resetApiKeyAndBaseUrlEnvVars, | ||
setupApiKeyAndBaseUrlEnvVars, | ||
} from "../../../../../test/helpers/service-test-helper"; | ||
import { API_ENDPOINTS } from "../../../../app.constants"; | ||
|
||
describe("account recovery service", () => { | ||
const httpInstance = new Http(); | ||
const service: AccountRecoveryInterface = | ||
accountRecoveryService(httpInstance); | ||
let postStub: SinonStub; | ||
|
||
beforeEach(() => { | ||
setupApiKeyAndBaseUrlEnvVars(); | ||
postStub = sinon.stub(httpInstance.client, "post"); | ||
}); | ||
|
||
afterEach(() => { | ||
postStub.reset(); | ||
resetApiKeyAndBaseUrlEnvVars(); | ||
}); | ||
|
||
it("successfully calls the API to perform an account recovery request", async () => { | ||
const axiosResponse = Promise.resolve({ | ||
data: {}, | ||
status: 200, | ||
statusText: "OK", | ||
}); | ||
postStub.resolves(axiosResponse); | ||
const { sessionId, clientSessionId, email, ip, diPersistentSessionId } = | ||
commonVariables; | ||
|
||
const result = await service.accountRecovery( | ||
sessionId, | ||
clientSessionId, | ||
email, | ||
ip, | ||
diPersistentSessionId | ||
); | ||
|
||
const expectedApiCallDetails = { | ||
expectedPath: API_ENDPOINTS.ACCOUNT_RECOVERY, | ||
expectedHeaders: expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
expectedBody: { email: commonVariables.email }, | ||
}; | ||
|
||
checkApiCallMadeWithExpectedBodyAndHeaders( | ||
result, | ||
postStub, | ||
true, | ||
expectedApiCallDetails | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { describe } from "mocha"; | ||
import sinon, { SinonStub } from "sinon"; | ||
import { Http } from "../../../../utils/http"; | ||
import { MfaServiceInterface } from "../types"; | ||
import { mfaService } from "../mfa-service"; | ||
import { | ||
checkApiCallMadeWithExpectedBodyAndHeaders, | ||
commonVariables, | ||
expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
resetApiKeyAndBaseUrlEnvVars, | ||
setupApiKeyAndBaseUrlEnvVars, | ||
} from "../../../../../test/helpers/service-test-helper"; | ||
import { | ||
API_ENDPOINTS, | ||
HTTP_STATUS_CODES, | ||
JOURNEY_TYPE, | ||
} from "../../../../app.constants"; | ||
|
||
describe("mfa service", () => { | ||
const httpInstance = new Http(); | ||
const service: MfaServiceInterface = mfaService(httpInstance); | ||
let postStub: SinonStub; | ||
|
||
beforeEach(() => { | ||
setupApiKeyAndBaseUrlEnvVars(); | ||
postStub = sinon.stub(httpInstance.client, "post"); | ||
}); | ||
|
||
afterEach(() => { | ||
postStub.reset(); | ||
resetApiKeyAndBaseUrlEnvVars(); | ||
}); | ||
|
||
it("successfully calls the API to make a request to reset a password ", async () => { | ||
const axiosResponse = Promise.resolve({ | ||
data: {}, | ||
status: HTTP_STATUS_CODES.NO_CONTENT, | ||
statusText: "OK", | ||
}); | ||
postStub.resolves(axiosResponse); | ||
const { email, sessionId, clientSessionId, ip, diPersistentSessionId } = | ||
commonVariables; | ||
const userLanguage = "cy"; | ||
const journeyType = JOURNEY_TYPE.SIGN_IN; | ||
const isResendCodeRequest = true; | ||
|
||
const expectedApiCallDetails = { | ||
expectedPath: API_ENDPOINTS.MFA, | ||
expectedHeaders: { | ||
...expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
"User-Language": userLanguage, | ||
}, | ||
expectedBody: { email, isResendCodeRequest, journeyType }, | ||
}; | ||
|
||
const result = await service.sendMfaCode( | ||
sessionId, | ||
clientSessionId, | ||
email, | ||
ip, | ||
diPersistentSessionId, | ||
isResendCodeRequest, | ||
userLanguage, | ||
journeyType | ||
); | ||
|
||
checkApiCallMadeWithExpectedBodyAndHeaders( | ||
result, | ||
postStub, | ||
true, | ||
expectedApiCallDetails | ||
); | ||
}); | ||
}); |
109 changes: 109 additions & 0 deletions
109
src/components/common/send-notification/tests/send-notification-service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { describe } from "mocha"; | ||
import sinon, { SinonStub } from "sinon"; | ||
import { Http } from "../../../../utils/http"; | ||
import { | ||
checkApiCallMadeWithExpectedBodyAndHeaders, | ||
commonVariables, | ||
expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
resetApiKeyAndBaseUrlEnvVars, | ||
setupApiKeyAndBaseUrlEnvVars, | ||
} from "../../../../../test/helpers/service-test-helper"; | ||
import { API_ENDPOINTS, NOTIFICATION_TYPE } from "../../../../app.constants"; | ||
import { SendNotificationServiceInterface } from "../types"; | ||
import { sendNotificationService } from "../send-notification-service"; | ||
import { JOURNEY_TYPE } from "../../constants"; | ||
|
||
describe("send notification service", () => { | ||
let postStub: SinonStub; | ||
let service: SendNotificationServiceInterface; | ||
const axiosResponse = Promise.resolve({ | ||
data: {}, | ||
status: 200, | ||
statusText: "OK", | ||
}); | ||
const { sessionId, clientSessionId, email, ip, diPersistentSessionId } = | ||
commonVariables; | ||
const notificationType = NOTIFICATION_TYPE.VERIFY_EMAIL; | ||
const userLanguage = "cy"; | ||
const expectedHeaders = { | ||
...expectedHeadersFromCommonVarsWithoutSecurityHeaders, | ||
"User-Language": userLanguage, | ||
}; | ||
|
||
beforeEach(() => { | ||
const httpInstance = new Http(); | ||
service = sendNotificationService(httpInstance); | ||
postStub = sinon.stub(httpInstance.client, "post"); | ||
setupApiKeyAndBaseUrlEnvVars(); | ||
postStub.resolves(axiosResponse); | ||
}); | ||
|
||
afterEach(() => { | ||
postStub.reset(); | ||
resetApiKeyAndBaseUrlEnvVars(); | ||
}); | ||
|
||
it("successfully calls the API to send a notification", async () => { | ||
const result = await service.sendNotification( | ||
sessionId, | ||
clientSessionId, | ||
email, | ||
notificationType, | ||
ip, | ||
diPersistentSessionId, | ||
userLanguage | ||
); | ||
|
||
const expectedApiCallDetails = { | ||
expectedPath: API_ENDPOINTS.SEND_NOTIFICATION, | ||
expectedHeaders, | ||
expectedBody: { email, notificationType }, | ||
validateStatus: true, | ||
}; | ||
|
||
checkApiCallMadeWithExpectedBodyAndHeaders( | ||
result, | ||
postStub, | ||
true, | ||
expectedApiCallDetails | ||
); | ||
}); | ||
|
||
it("adds the additional details to the payload when these are included", async () => { | ||
const journeyType = JOURNEY_TYPE.CREATE_ACCOUNT; | ||
const phoneNumber = "123456"; | ||
const requestNewCode = true; | ||
const result = await service.sendNotification( | ||
sessionId, | ||
clientSessionId, | ||
email, | ||
notificationType, | ||
ip, | ||
diPersistentSessionId, | ||
userLanguage, | ||
journeyType, | ||
phoneNumber, | ||
requestNewCode | ||
); | ||
|
||
const expectedApiCallDetails = { | ||
expectedPath: API_ENDPOINTS.SEND_NOTIFICATION, | ||
expectedHeaders, | ||
expectedBody: { | ||
email, | ||
notificationType, | ||
journeyType, | ||
phoneNumber, | ||
requestNewCode, | ||
}, | ||
validateStatus: true, | ||
}; | ||
|
||
checkApiCallMadeWithExpectedBodyAndHeaders( | ||
result, | ||
postStub, | ||
true, | ||
expectedApiCallDetails | ||
); | ||
}); | ||
}); |
Oops, something went wrong.