-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): add channel validation (#4734)
* fix(core): add channel validation * test: use enum in valiadtions * fix: allow test numbers
- Loading branch information
Showing
8 changed files
with
192 additions
and
28 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
102 changes: 102 additions & 0 deletions
102
core/api/test/unit/domain/phone-provider/checked-to-channel.spec.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,102 @@ | ||
import { checkedToChannel, ChannelType } from "@/domain/phone-provider" | ||
import { | ||
InvalidPhoneNumber, | ||
InvalidChannel, | ||
InvalidChannelForCountry, | ||
} from "@/domain/errors" | ||
|
||
jest.mock("@/config", () => ({ | ||
...jest.requireActual("@/config"), | ||
getSmsAuthUnsupportedCountries: () => ["GB"], | ||
getWhatsAppAuthUnsupportedCountries: () => ["US"], | ||
})) | ||
|
||
describe("checkedToChannel", () => { | ||
const validUsPhone = "+16505554321" | ||
const validUkPhone = "+447874482105" | ||
|
||
it("fails with empty phone", () => { | ||
const result = checkedToChannel("", "sms") | ||
expect(result).toBeInstanceOf(InvalidPhoneNumber) | ||
}) | ||
|
||
it("fails with empty channel", () => { | ||
const result = checkedToChannel(validUsPhone, "") | ||
expect(result).toBeInstanceOf(InvalidChannel) | ||
}) | ||
|
||
it("fails with invalid phone number", () => { | ||
const result = checkedToChannel("+1", "sms") | ||
expect(result).toBeInstanceOf(InvalidPhoneNumber) | ||
}) | ||
|
||
it("fails with non-phone-number input", () => { | ||
const result = checkedToChannel("not-a-phone", "sms") | ||
expect(result).toBeInstanceOf(InvalidPhoneNumber) | ||
}) | ||
|
||
it("fails with invalid channel type", () => { | ||
const result = checkedToChannel(validUsPhone, "invalid-channel") | ||
expect(result).toBeInstanceOf(InvalidChannel) | ||
}) | ||
|
||
describe("SMS channel", () => { | ||
it("succeeds for supported country", () => { | ||
const result = checkedToChannel(validUsPhone, "sms") | ||
expect(result).toBe(ChannelType.Sms) | ||
}) | ||
|
||
it("fails for unsupported country", () => { | ||
const result = checkedToChannel(validUkPhone, "sms") | ||
expect(result).toBeInstanceOf(InvalidChannelForCountry) | ||
}) | ||
|
||
it("is case insensitive", () => { | ||
const result = checkedToChannel(validUsPhone, "SMS") | ||
expect(result).toBe(ChannelType.Sms) | ||
}) | ||
}) | ||
|
||
describe("WhatsApp channel", () => { | ||
it("succeeds for supported country", () => { | ||
const result = checkedToChannel(validUkPhone, "whatsapp") | ||
expect(result).toBe(ChannelType.Whatsapp) | ||
}) | ||
|
||
it("fails for unsupported country", () => { | ||
const result = checkedToChannel(validUsPhone, "whatsapp") | ||
expect(result).toBeInstanceOf(InvalidChannelForCountry) | ||
}) | ||
|
||
it("is case insensitive", () => { | ||
const result = checkedToChannel(validUkPhone, "WhatsApp") | ||
expect(result).toBe(ChannelType.Whatsapp) | ||
}) | ||
}) | ||
|
||
describe("edge cases", () => { | ||
it("handles undefined phone", () => { | ||
// @ts-expect-error Testing undefined case | ||
const result = checkedToChannel(undefined, "sms") | ||
expect(result).toBeInstanceOf(InvalidPhoneNumber) | ||
}) | ||
|
||
it("handles undefined channel", () => { | ||
// @ts-expect-error Testing undefined case | ||
const result = checkedToChannel(validUsPhone, undefined) | ||
expect(result).toBeInstanceOf(InvalidChannel) | ||
}) | ||
|
||
it("handles null phone", () => { | ||
// @ts-expect-error Testing null case | ||
const result = checkedToChannel(null, "sms") | ||
expect(result).toBeInstanceOf(InvalidPhoneNumber) | ||
}) | ||
|
||
it("handles null channel", () => { | ||
// @ts-expect-error Testing null case | ||
const result = checkedToChannel(validUsPhone, null) | ||
expect(result).toBeInstanceOf(InvalidChannel) | ||
}) | ||
}) | ||
}) |