-
Notifications
You must be signed in to change notification settings - Fork 990
/
Copy pathaccountImporter.spec.ts
176 lines (157 loc) · 4.96 KB
/
accountImporter.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import * as nock from "nock";
import { expect } from "chai";
import { googleOrigin } from "./api";
import * as accountImporter from "./accountImporter";
describe("accountImporter", () => {
before(() => {
nock.disableNetConnect();
});
after(() => {
nock.enableNetConnect();
});
const transArrayToUser = accountImporter.transArrayToUser;
const validateOptions = accountImporter.validateOptions;
const validateUserJson = accountImporter.validateUserJson;
const serialImportUsers = accountImporter.serialImportUsers;
describe("transArrayToUser", () => {
it("should reject when passwordHash is invalid base64", () => {
expect(transArrayToUser(["123", undefined, undefined, "false"])).to.have.property("error");
});
it("should not reject when passwordHash is valid base64", () => {
expect(
transArrayToUser(["123", undefined, undefined, "Jlf7onfLbzqPNFP/1pqhx6fQF/w="]),
).to.not.have.property("error");
});
});
describe("validateOptions", () => {
it("should reject when unsupported hash algorithm provided", () => {
expect(() => validateOptions({ hashAlgo: "MD2" })).to.throw();
});
it("should reject when missing parameters", () => {
expect(() => validateOptions({ hashAlgo: "HMAC_SHA1" })).to.throw();
});
});
describe("validateUserJson", () => {
it("should reject when unknown fields in user json", () => {
expect(
validateUserJson({
uid: "123",
email: "test@test.org",
}),
).to.have.property("error");
});
it("should reject when unknown fields in providerUserInfo of user json", () => {
expect(
validateUserJson({
localId: "123",
email: "test@test.org",
providerUserInfo: [
{
providerId: "google.com",
googleId: "abc",
email: "test@test.org",
},
],
}),
).to.have.property("error");
});
it("should reject when unknown providerUserInfo of user json", () => {
expect(
validateUserJson({
localId: "123",
email: "test@test.org",
providerUserInfo: [
{
providerId: "otheridp.com",
rawId: "abc",
email: "test@test.org",
},
],
}),
).to.have.property("error");
});
it("should reject when passwordHash is invalid base64", () => {
expect(
validateUserJson({
localId: "123",
passwordHash: "false",
}),
).to.have.property("error");
});
it("should not reject when passwordHash is valid base64", () => {
expect(
validateUserJson({
localId: "123",
passwordHash: "Jlf7onfLbzqPNFP/1pqhx6fQF/w=",
}),
).to.not.have.property("error");
});
});
describe("serialImportUsers", () => {
let batches: { localId: string; email: string }[][] = [];
const hashOptions = {
hashAlgo: "HMAC_SHA1",
hashKey: "a2V5MTIz",
};
let expectedResponse: { status: number; body: any }[] = [];
beforeEach(() => {
for (let i = 0; i < 10; i++) {
batches.push([
{
localId: i.toString(),
email: `test${i}@test.org`,
},
]);
expectedResponse.push({
status: 200,
body: {},
});
}
});
afterEach(() => {
batches = [];
expectedResponse = [];
});
it("should call api.request multiple times", async () => {
for (let i = 0; i < batches.length; i++) {
nock(googleOrigin())
.post("/identitytoolkit/v3/relyingparty/uploadAccount", {
hashAlgorithm: "HMAC_SHA1",
signerKey: "a2V5MTIz",
targetProjectId: "test-project-id",
users: [{ email: `test${i}@test.org`, localId: i.toString() }],
})
.once()
.reply(expectedResponse[i].status, expectedResponse[i].body);
}
await serialImportUsers("test-project-id", hashOptions, batches, 0);
expect(nock.isDone()).to.be.true;
});
it("should continue when some request's response is 200 but has `error` in response", async () => {
expectedResponse[5] = {
status: 200,
body: {
error: [
{
index: 0,
message: "some error message",
},
],
},
};
for (let i = 0; i < batches.length; i++) {
nock(googleOrigin())
.post("/identitytoolkit/v3/relyingparty/uploadAccount", {
hashAlgorithm: "HMAC_SHA1",
signerKey: "a2V5MTIz",
targetProjectId: "test-project-id",
users: [{ email: `test${i}@test.org`, localId: i.toString() }],
})
.once()
.reply(expectedResponse[i].status, expectedResponse[i].body);
}
await serialImportUsers("test-project-id", hashOptions, batches, 0);
expect(nock.isDone()).to.be.true;
});
});
});