|
1 | 1 | import * as cp from "child_process"
|
| 2 | +import { logger } from "@coder/logger" |
2 | 3 | import { promises as fs } from "fs"
|
3 | 4 | import * as path from "path"
|
4 | 5 | import { generateUuid } from "../../../src/common/util"
|
5 | 6 | import { tmpdir } from "../../../src/node/constants"
|
6 | 7 | import * as util from "../../../src/node/util"
|
| 8 | +import { mockLogger } from "../../utils/helpers" |
7 | 9 |
|
8 | 10 | describe("getEnvPaths", () => {
|
9 | 11 | describe("on darwin", () => {
|
@@ -99,11 +101,31 @@ describe("getEnvPaths", () => {
|
99 | 101 | })
|
100 | 102 |
|
101 | 103 | describe("hash", () => {
|
| 104 | + beforeAll(() => { |
| 105 | + mockLogger() |
| 106 | + }) |
| 107 | + |
| 108 | + afterEach(() => { |
| 109 | + jest.clearAllMocks() |
| 110 | + }) |
| 111 | + |
102 | 112 | it("should return a hash of the string passed in", async () => {
|
103 | 113 | const plainTextPassword = "mySecretPassword123"
|
104 | 114 | const hashed = await util.hash(plainTextPassword)
|
105 | 115 | expect(hashed).not.toBe(plainTextPassword)
|
106 | 116 | })
|
| 117 | + it("should log an error and return an empty string on error", async () => { |
| 118 | + // Although we don't expect to pass the wrong type to our `hash` |
| 119 | + // function, we do want to make sure our function handles errors correctly. |
| 120 | + // Therefore, we mock a scenario where the password is `undefined` |
| 121 | + // which gets us into the catch block and checks that we handle errors by |
| 122 | + // logging it and returning an empty string (as opposed to throwing an Error) |
| 123 | + // and crashing our program for a silly password error. |
| 124 | + // @ts-expect-error See description above |
| 125 | + const hashed = await util.hash(undefined) |
| 126 | + expect(hashed).toBe("") |
| 127 | + expect(logger.error).toHaveBeenCalled() |
| 128 | + }) |
107 | 129 | })
|
108 | 130 |
|
109 | 131 | describe("isHashMatch", () => {
|
|
0 commit comments