Skip to content

Commit add55ec

Browse files
committed
Import utils as a group in tests
This should simplify testing new utils a bit.
1 parent ba0364a commit add55ec

File tree

2 files changed

+74
-95
lines changed

2 files changed

+74
-95
lines changed

test/unit/node/util.test.ts

+42-52
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
import {
2-
hash,
3-
isHashMatch,
4-
handlePasswordValidation,
5-
PasswordMethod,
6-
getPasswordMethod,
7-
hashLegacy,
8-
isHashLegacyMatch,
9-
isCookieValid,
10-
sanitizeString,
11-
} from "../../../src/node/util"
1+
import * as util from "../../../src/node/util"
122

133
describe("getEnvPaths", () => {
144
describe("on darwin", () => {
@@ -161,176 +151,176 @@ describe("getEnvPaths", () => {
161151
describe("hash", () => {
162152
it("should return a hash of the string passed in", async () => {
163153
const plainTextPassword = "mySecretPassword123"
164-
const hashed = await hash(plainTextPassword)
154+
const hashed = await util.hash(plainTextPassword)
165155
expect(hashed).not.toBe(plainTextPassword)
166156
})
167157
})
168158

169159
describe("isHashMatch", () => {
170160
it("should return true if the password matches the hash", async () => {
171161
const password = "codeserver1234"
172-
const _hash = await hash(password)
173-
const actual = await isHashMatch(password, _hash)
162+
const _hash = await util.hash(password)
163+
const actual = await util.isHashMatch(password, _hash)
174164
expect(actual).toBe(true)
175165
})
176166
it("should return false if the password does not match the hash", async () => {
177167
const password = "password123"
178-
const _hash = await hash(password)
179-
const actual = await isHashMatch("otherPassword123", _hash)
168+
const _hash = await util.hash(password)
169+
const actual = await util.isHashMatch("otherPassword123", _hash)
180170
expect(actual).toBe(false)
181171
})
182172
it("should return true with actual hash", async () => {
183173
const password = "password123"
184174
const _hash = "$argon2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"
185-
const actual = await isHashMatch(password, _hash)
175+
const actual = await util.isHashMatch(password, _hash)
186176
expect(actual).toBe(true)
187177
})
188178
it("should return false if the password is empty", async () => {
189179
const password = ""
190180
const _hash = "$argon2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"
191-
const actual = await isHashMatch(password, _hash)
181+
const actual = await util.isHashMatch(password, _hash)
192182
expect(actual).toBe(false)
193183
})
194184
it("should return false if the hash is empty", async () => {
195185
const password = "hellowpasssword"
196186
const _hash = ""
197-
const actual = await isHashMatch(password, _hash)
187+
const actual = await util.isHashMatch(password, _hash)
198188
expect(actual).toBe(false)
199189
})
200190
})
201191

202192
describe("hashLegacy", () => {
203193
it("should return a hash of the string passed in", () => {
204194
const plainTextPassword = "mySecretPassword123"
205-
const hashed = hashLegacy(plainTextPassword)
195+
const hashed = util.hashLegacy(plainTextPassword)
206196
expect(hashed).not.toBe(plainTextPassword)
207197
})
208198
})
209199

210200
describe("isHashLegacyMatch", () => {
211201
it("should return true if is match", () => {
212202
const password = "password123"
213-
const _hash = hashLegacy(password)
214-
expect(isHashLegacyMatch(password, _hash)).toBe(true)
203+
const _hash = util.hashLegacy(password)
204+
expect(util.isHashLegacyMatch(password, _hash)).toBe(true)
215205
})
216206
it("should return false if is match", () => {
217207
const password = "password123"
218-
const _hash = hashLegacy(password)
219-
expect(isHashLegacyMatch("otherPassword123", _hash)).toBe(false)
208+
const _hash = util.hashLegacy(password)
209+
expect(util.isHashLegacyMatch("otherPassword123", _hash)).toBe(false)
220210
})
221211
it("should return true if hashed from command line", () => {
222212
const password = "password123"
223213
// Hashed using printf "password123" | sha256sum | cut -d' ' -f1
224214
const _hash = "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f"
225-
expect(isHashLegacyMatch(password, _hash)).toBe(true)
215+
expect(util.isHashLegacyMatch(password, _hash)).toBe(true)
226216
})
227217
})
228218

229219
describe("getPasswordMethod", () => {
230220
it("should return PLAIN_TEXT for no hashed password", () => {
231221
const hashedPassword = undefined
232-
const passwordMethod = getPasswordMethod(hashedPassword)
233-
const expected: PasswordMethod = "PLAIN_TEXT"
222+
const passwordMethod = util.getPasswordMethod(hashedPassword)
223+
const expected: util.PasswordMethod = "PLAIN_TEXT"
234224
expect(passwordMethod).toEqual(expected)
235225
})
236226
it("should return ARGON2 for password with 'argon2'", () => {
237227
const hashedPassword =
238228
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY"
239-
const passwordMethod = getPasswordMethod(hashedPassword)
240-
const expected: PasswordMethod = "ARGON2"
229+
const passwordMethod = util.getPasswordMethod(hashedPassword)
230+
const expected: util.PasswordMethod = "ARGON2"
241231
expect(passwordMethod).toEqual(expected)
242232
})
243233
it("should return SHA256 for password with legacy hash", () => {
244234
const hashedPassword = "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af"
245-
const passwordMethod = getPasswordMethod(hashedPassword)
246-
const expected: PasswordMethod = "SHA256"
235+
const passwordMethod = util.getPasswordMethod(hashedPassword)
236+
const expected: util.PasswordMethod = "SHA256"
247237
expect(passwordMethod).toEqual(expected)
248238
})
249239
})
250240

251241
describe("handlePasswordValidation", () => {
252242
it("should return true with a hashedPassword for a PLAIN_TEXT password", async () => {
253243
const p = "password"
254-
const passwordValidation = await handlePasswordValidation({
244+
const passwordValidation = await util.handlePasswordValidation({
255245
passwordMethod: "PLAIN_TEXT",
256246
passwordFromRequestBody: p,
257247
passwordFromArgs: p,
258248
hashedPasswordFromArgs: undefined,
259249
})
260250

261-
const matchesHash = await isHashMatch(p, passwordValidation.hashedPassword)
251+
const matchesHash = await util.isHashMatch(p, passwordValidation.hashedPassword)
262252

263253
expect(passwordValidation.isPasswordValid).toBe(true)
264254
expect(matchesHash).toBe(true)
265255
})
266256
it("should return false when PLAIN_TEXT password doesn't match args", async () => {
267257
const p = "password"
268-
const passwordValidation = await handlePasswordValidation({
258+
const passwordValidation = await util.handlePasswordValidation({
269259
passwordMethod: "PLAIN_TEXT",
270260
passwordFromRequestBody: "password1",
271261
passwordFromArgs: p,
272262
hashedPasswordFromArgs: undefined,
273263
})
274264

275-
const matchesHash = await isHashMatch(p, passwordValidation.hashedPassword)
265+
const matchesHash = await util.isHashMatch(p, passwordValidation.hashedPassword)
276266

277267
expect(passwordValidation.isPasswordValid).toBe(false)
278268
expect(matchesHash).toBe(false)
279269
})
280270
it("should return true with a hashedPassword for a SHA256 password", async () => {
281271
const p = "helloworld"
282-
const passwordValidation = await handlePasswordValidation({
272+
const passwordValidation = await util.handlePasswordValidation({
283273
passwordMethod: "SHA256",
284274
passwordFromRequestBody: p,
285275
passwordFromArgs: undefined,
286276
hashedPasswordFromArgs: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af",
287277
})
288278

289-
const matchesHash = isHashLegacyMatch(p, passwordValidation.hashedPassword)
279+
const matchesHash = util.isHashLegacyMatch(p, passwordValidation.hashedPassword)
290280

291281
expect(passwordValidation.isPasswordValid).toBe(true)
292282
expect(matchesHash).toBe(true)
293283
})
294284
it("should return false when SHA256 password doesn't match hash", async () => {
295285
const p = "helloworld1"
296-
const passwordValidation = await handlePasswordValidation({
286+
const passwordValidation = await util.handlePasswordValidation({
297287
passwordMethod: "SHA256",
298288
passwordFromRequestBody: p,
299289
passwordFromArgs: undefined,
300290
hashedPasswordFromArgs: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af",
301291
})
302292

303-
const matchesHash = isHashLegacyMatch(p, passwordValidation.hashedPassword)
293+
const matchesHash = util.isHashLegacyMatch(p, passwordValidation.hashedPassword)
304294

305295
expect(passwordValidation.isPasswordValid).toBe(false)
306296
expect(matchesHash).toBe(false)
307297
})
308298
it("should return true with a hashedPassword for a ARGON2 password", async () => {
309299
const p = "password"
310-
const passwordValidation = await handlePasswordValidation({
300+
const passwordValidation = await util.handlePasswordValidation({
311301
passwordMethod: "ARGON2",
312302
passwordFromRequestBody: p,
313303
passwordFromArgs: undefined,
314304
hashedPasswordFromArgs:
315305
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
316306
})
317307

318-
const matchesHash = await isHashMatch(p, passwordValidation.hashedPassword)
308+
const matchesHash = await util.isHashMatch(p, passwordValidation.hashedPassword)
319309

320310
expect(passwordValidation.isPasswordValid).toBe(true)
321311
expect(matchesHash).toBe(true)
322312
})
323313
it("should return false when ARGON2 password doesn't match hash", async () => {
324314
const p = "password1"
325-
const passwordValidation = await handlePasswordValidation({
315+
const passwordValidation = await util.handlePasswordValidation({
326316
passwordMethod: "ARGON2",
327317
passwordFromRequestBody: p,
328318
passwordFromArgs: undefined,
329319
hashedPasswordFromArgs:
330320
"$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
331321
})
332322

333-
const matchesHash = await isHashMatch(p, passwordValidation.hashedPassword)
323+
const matchesHash = await util.isHashMatch(p, passwordValidation.hashedPassword)
334324

335325
expect(passwordValidation.isPasswordValid).toBe(false)
336326
expect(matchesHash).toBe(false)
@@ -339,7 +329,7 @@ describe("handlePasswordValidation", () => {
339329

340330
describe("isCookieValid", () => {
341331
it("should be valid if hashed-password for SHA256 matches cookie.key", async () => {
342-
const isValid = await isCookieValid({
332+
const isValid = await util.isCookieValid({
343333
passwordMethod: "SHA256",
344334
cookieKey: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af",
345335
hashedPasswordFromArgs: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af",
@@ -348,7 +338,7 @@ describe("isCookieValid", () => {
348338
expect(isValid).toBe(true)
349339
})
350340
it("should be invalid if hashed-password for SHA256 does not match cookie.key", async () => {
351-
const isValid = await isCookieValid({
341+
const isValid = await util.isCookieValid({
352342
passwordMethod: "SHA256",
353343
cookieKey: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb9442bb6f8f8f07af",
354344
hashedPasswordFromArgs: "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af",
@@ -357,7 +347,7 @@ describe("isCookieValid", () => {
357347
expect(isValid).toBe(false)
358348
})
359349
it("should be valid if hashed-password for ARGON2 matches cookie.key", async () => {
360-
const isValid = await isCookieValid({
350+
const isValid = await util.isCookieValid({
361351
passwordMethod: "ARGON2",
362352
cookieKey: "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
363353
hashedPasswordFromArgs:
@@ -367,7 +357,7 @@ describe("isCookieValid", () => {
367357
expect(isValid).toBe(true)
368358
})
369359
it("should be invalid if hashed-password for ARGON2 does not match cookie.key", async () => {
370-
const isValid = await isCookieValid({
360+
const isValid = await util.isCookieValid({
371361
passwordMethod: "ARGON2",
372362
cookieKey: "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9H",
373363
hashedPasswordFromArgs:
@@ -377,7 +367,7 @@ describe("isCookieValid", () => {
377367
expect(isValid).toBe(false)
378368
})
379369
it("should be valid if password for PLAIN_TEXT matches cookie.key", async () => {
380-
const isValid = await isCookieValid({
370+
const isValid = await util.isCookieValid({
381371
passwordMethod: "PLAIN_TEXT",
382372
cookieKey: "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9HYY",
383373
passwordFromArgs: "password",
@@ -386,7 +376,7 @@ describe("isCookieValid", () => {
386376
expect(isValid).toBe(true)
387377
})
388378
it("should be invalid if hashed-password for PLAIN_TEXT does not match cookie.key", async () => {
389-
const isValid = await isCookieValid({
379+
const isValid = await util.isCookieValid({
390380
passwordMethod: "PLAIN_TEXT",
391381
cookieKey: "$argon2i$v=19$m=4096,t=3,p=1$0qR/o+0t00hsbJFQCKSfdQ$oFcM4rL6o+B7oxpuA4qlXubypbBPsf+8L531U7P9H",
392382
passwordFromArgs: "password1234",
@@ -398,12 +388,12 @@ describe("isCookieValid", () => {
398388

399389
describe("sanitizeString", () => {
400390
it("should return an empty string if passed a type other than a string", () => {
401-
expect(sanitizeString({} as string)).toBe("")
391+
expect(util.sanitizeString({} as string)).toBe("")
402392
})
403393
it("should trim whitespace", () => {
404-
expect(sanitizeString(" hello ")).toBe("hello")
394+
expect(util.sanitizeString(" hello ")).toBe("hello")
405395
})
406396
it("should always return an empty string", () => {
407-
expect(sanitizeString(" ")).toBe("")
397+
expect(util.sanitizeString(" ")).toBe("")
408398
})
409399
})

0 commit comments

Comments
 (0)