-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
221 lines (195 loc) · 4.73 KB
/
test.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const test = require("ava");
const sandbox = require("sinon").createSandbox();
const uuid = require("uuid");
const { OAuth2Client } = require("google-auth-library");
const microAuthGoogle = require(".");
const oAuth2ClientStub = sandbox.stub(OAuth2Client.prototype);
const uuidV4Stub = sandbox.spy(uuid, "v4");
const mockServer = (req, res, ...args) => async fn => {
if (!fn) return;
try {
return fn(req, res, ...args);
} catch (error) {
console.log(error);
return fn(req, res, { err: error });
}
};
test.afterEach(() => {
sandbox.reset();
});
test("should fail if no argument is supplied", t => {
t.throws(() => microAuthGoogle(), { instanceOf: Error });
});
test("should fail if clientId is not supplied", t => {
t.throws(
() => {
microAuthGoogle({});
},
{
message: /Must provide a clientId/
}
);
});
test("should fail if clientSecret is not supplied", t => {
t.throws(
() => {
microAuthGoogle({
clientId: "foo"
});
},
{
message: /Must provide a clientSecret/
}
);
});
test("should fail if callbackUrl is not supplied", t => {
t.throws(
() => {
microAuthGoogle({
clientId: "foo",
clientSecret: "bar",
callbackUrl: null
});
},
{
message: /Must provide a callbackUrl/
}
);
});
test("should fail if path is not supplied", t => {
t.throws(
() => {
microAuthGoogle({
clientId: "foo",
clientSecret: "bar",
callbackUrl: "http://localhost:3000/callback",
path: null
});
},
{
message: /Must provide an url path/
}
);
});
test("shoud fail if callbackUrl is not a valid full url", t => {
const invalidUrl = "baz";
t.throws(
() => {
microAuthGoogle({
clientId: "foo",
clientSecret: "bar",
callbackUrl: "baz"
});
},
{
message: `Invalid URL: ${invalidUrl}`
}
);
});
test("should fail if path is the same as callbackUrl", t => {
t.throws(
() => {
microAuthGoogle({
clientId: "foo",
clientSecret: "bar",
callbackUrl: "http://localhost:3000/",
path: "/"
});
},
{
message: /Service path cannot be the same as callback path/
}
);
});
test("should return a valid micro function", t => {
const service = microAuthGoogle({
clientId: "foo",
clientSecret: "bar",
callbackUrl: "http://localhost:3000/callback"
});
t.is(typeof service, "function");
});
test("should throw an error if, somehow, there's a ill formed url", async t => {
const service = microAuthGoogle({
clientId: "foo",
clientSecret: "bar",
callbackUrl: "http://localhost:3000/callback"
});
const req = {
url: ":3000"
};
const res = {};
const mock = mockServer(req, res);
await mock(
service(async (req, res, { err }) => {
t.is(err.code, "ERR_INVALID_URL");
})
);
});
test.serial("should generate a valid auth url and redirect", async t => {
const service = microAuthGoogle({
clientId: "foo",
clientSecret: "bar",
callbackUrl: "http://localhost:3000/callback"
});
const req = {
url: "/"
};
const res = {
statusCode: 0,
setHeader: sandbox.stub(),
end: sandbox.stub()
};
const mock = mockServer(req, res);
const redirectUrl = "https://google.com/where?account=1";
const authStub = oAuth2ClientStub.generateAuthUrl.returns(redirectUrl);
await mock(service(() => {}));
t.is(uuidV4Stub.callCount, 1);
t.is(authStub.callCount, 1);
t.is(res.statusCode, 307);
t.is(res.setHeader.callCount, 1);
t.true(res.setHeader.calledWith("Location", redirectUrl));
t.is(res.end.callCount, 1);
});
test.serial("should fail to generate a valid auth url", async t => {
const service = microAuthGoogle({
clientId: "foo",
clientSecret: "bar",
callbackUrl: "http://localhost:3000/callback"
});
const req = {
url: "/"
};
const res = {};
const mock = mockServer(req, res);
const authStub = oAuth2ClientStub.generateAuthUrl.throws(new Error("fubar"));
await mock(
service(async (req, res, { err }) => {
t.is(authStub.callCount, 1);
t.is(err.message, "fubar");
})
);
});
test.serial("should fail due to no pairing state", async t => {
const service = microAuthGoogle({
clientId: "foo",
clientSecret: "bar",
callbackUrl: "http://localhost:3000/callback"
});
const req = {
url: "/callback?code=1&state=1"
};
const res = {};
const mock = mockServer(req, res);
oAuth2ClientStub.getToken.returns(Promise.resolve("token"));
oAuth2ClientStub.requestAsync.returns(
Promise.resolve({
name: "foo"
})
);
await mock(
service(async (req, res, { err }) => {
t.is(err.message, "Invalid state");
})
);
});