Skip to content

Commit efcf3f5

Browse files
Merge pull request #234 from lob/identityValidation
add identity validation
2 parents a74980d + 2021a26 commit efcf3f5

8 files changed

+698
-71
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { MultiLineAddress } from "../models";
2+
import { IdentityValidationApi } from "../api/identity-validation-api";
3+
import {
4+
CONFIG_FOR_INTEGRATION,
5+
CONFIG_FOR_INTEGRATION_WITH_LIVE,
6+
} from "./testFixtures";
7+
8+
describe("IdentityValidationApi", () => {
9+
const identityValidationApi = new IdentityValidationApi(
10+
CONFIG_FOR_INTEGRATION_WITH_LIVE
11+
);
12+
13+
it("Identity Validation API can be instantiated", () => {
14+
const api = new IdentityValidationApi(CONFIG_FOR_INTEGRATION_WITH_LIVE);
15+
expect(api).toBeDefined();
16+
expect(typeof api).toEqual("object");
17+
expect(api).toBeInstanceOf(IdentityValidationApi);
18+
});
19+
20+
describe("validate", () => {
21+
const zipCodeIdentityValidationInput = new MultiLineAddress({
22+
recipient: "Lob.com",
23+
primary_line: "210 King St",
24+
zip_code: "94107",
25+
});
26+
27+
it("exists", () => {
28+
expect(identityValidationApi.validate).toBeDefined();
29+
expect(typeof identityValidationApi.validate).toEqual("function");
30+
});
31+
32+
it("validates given city and state", async () => {
33+
const cityStateIdentityValidationInput = new MultiLineAddress({
34+
recipient: "Lob.com",
35+
primary_line: "210 King St",
36+
city: "San Francisco",
37+
state: "CA",
38+
});
39+
40+
const response = await identityValidationApi.validate(
41+
cityStateIdentityValidationInput
42+
);
43+
expect(response.id).toBeDefined();
44+
});
45+
46+
it("validates given zipcode", async () => {
47+
const response = await identityValidationApi.validate(
48+
zipCodeIdentityValidationInput
49+
);
50+
expect(response.id).toBeDefined();
51+
});
52+
53+
it("refuses to validate with test key", async () => {
54+
const response = await new IdentityValidationApi(
55+
CONFIG_FOR_INTEGRATION
56+
).validate(zipCodeIdentityValidationInput);
57+
58+
expect(response.recipient).toEqual("TEST KEYS DO NOT VERIFY ADDRESSES");
59+
});
60+
61+
it("fails on erroneous identityValidation object", async () => {
62+
const invalidValidationInput = new MultiLineAddress({
63+
primary_line: "210 King St",
64+
zip_code: "94107",
65+
});
66+
67+
try {
68+
await identityValidationApi.validate(invalidValidationInput);
69+
fail("should have thrown");
70+
} catch (err: any) {
71+
expect(err.message).toContain("recipient is required");
72+
}
73+
});
74+
});
75+
});
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { MultiLineAddress } from "../models";
2+
import { IdentityValidationApi } from "../api/identity-validation-api";
3+
import { fail } from "./testUtilities";
4+
import {
5+
CONFIG_FOR_UNIT,
6+
CONFIG_WITH_BASE_OPTIONS_FOR_UNIT,
7+
} from "./testFixtures";
8+
9+
// Axios Mock
10+
import axios from "axios";
11+
const axiosRequest: jest.Mock = axios.request as jest.Mock;
12+
jest.mock("axios", () => ({
13+
request: jest.fn(),
14+
}));
15+
16+
describe("IdentityValidationApi", () => {
17+
it("can be instantiated", () => {
18+
const intlApi = new IdentityValidationApi(CONFIG_FOR_UNIT);
19+
expect(intlApi).toBeDefined();
20+
expect(typeof intlApi).toEqual("object");
21+
expect(intlApi).toBeInstanceOf(IdentityValidationApi);
22+
});
23+
24+
it("can be instantiated with base options", () => {
25+
const intlApi = new IdentityValidationApi(
26+
CONFIG_WITH_BASE_OPTIONS_FOR_UNIT
27+
);
28+
expect(intlApi).toBeDefined();
29+
expect(typeof intlApi).toEqual("object");
30+
expect(intlApi).toBeInstanceOf(IdentityValidationApi);
31+
});
32+
33+
describe("validate", () => {
34+
const multilineAddress = new MultiLineAddress({
35+
recipient: "Lob.com",
36+
primary_line: "210 King St",
37+
city: "San Francisco",
38+
state: "CA",
39+
zip_code: "94107",
40+
});
41+
42+
it("exists", () => {
43+
const intlApi = new IdentityValidationApi(CONFIG_FOR_UNIT);
44+
expect(intlApi.validate).toBeDefined();
45+
expect(typeof intlApi.validate).toEqual("function");
46+
});
47+
48+
it("handles errors returned by the api", async () => {
49+
axiosRequest.mockImplementationOnce(async () => {
50+
throw {
51+
message: "error",
52+
response: { data: { error: { message: "error reported by API" } } },
53+
};
54+
});
55+
56+
try {
57+
await new IdentityValidationApi(CONFIG_FOR_UNIT).validate(
58+
multilineAddress
59+
);
60+
} catch (err: any) {
61+
expect(err.message).toEqual("error reported by API");
62+
}
63+
});
64+
65+
it("handles errors returned by the api with missing response.data", async () => {
66+
axiosRequest.mockImplementationOnce(async () => {
67+
throw {
68+
message: "error",
69+
response: {},
70+
};
71+
});
72+
73+
try {
74+
await new IdentityValidationApi(CONFIG_FOR_UNIT).validate(
75+
multilineAddress
76+
);
77+
fail("Should throw");
78+
} catch (err: any) {
79+
expect(err.message).toEqual("error");
80+
}
81+
});
82+
83+
it("handles errors returned by the api with missing response.data.error", async () => {
84+
axiosRequest.mockImplementationOnce(async () => {
85+
throw {
86+
message: "error",
87+
response: { data: {} },
88+
};
89+
});
90+
91+
try {
92+
await new IdentityValidationApi(CONFIG_FOR_UNIT).validate(
93+
multilineAddress
94+
);
95+
fail("Should throw");
96+
} catch (err: any) {
97+
expect(err.message).toEqual("error");
98+
}
99+
});
100+
101+
it("handles errors in making the request", async () => {
102+
axiosRequest.mockImplementationOnce(async () => {
103+
throw new Error("Unknown Error");
104+
});
105+
106+
try {
107+
await new IdentityValidationApi(CONFIG_FOR_UNIT).validate(
108+
multilineAddress
109+
);
110+
fail("Should throw");
111+
} catch (err: any) {
112+
expect(err.message).toEqual("Unknown Error");
113+
}
114+
});
115+
116+
it("validates whether a given name is associated with an address", async () => {
117+
axiosRequest.mockImplementationOnce(async () => ({
118+
data: { id: "id_validation_fakeId" },
119+
}));
120+
121+
const response = await new IdentityValidationApi(
122+
CONFIG_FOR_UNIT
123+
).validate(multilineAddress);
124+
expect(response.id).toBeDefined();
125+
expect(response.id).toEqual("id_validation_fakeId");
126+
});
127+
128+
it("includes custom headers while it validates", async () => {
129+
axiosRequest.mockImplementationOnce(async () => ({
130+
data: { id: "id_validation_fakeId" },
131+
}));
132+
133+
const response = await new IdentityValidationApi(
134+
CONFIG_WITH_BASE_OPTIONS_FOR_UNIT
135+
).validate(multilineAddress);
136+
expect(response.id).toBeDefined();
137+
expect(response.id).toEqual("id_validation_fakeId");
138+
});
139+
});
140+
});

0 commit comments

Comments
 (0)