Skip to content

Commit e888281

Browse files
committed
refactor(util): pass in process.platform
1 parent 9d0c040 commit e888281

File tree

3 files changed

+1062
-1901
lines changed

3 files changed

+1062
-1901
lines changed

src/node/util.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { logger } from "@coder/logger"
2-
import { hash as _hash, verify } from "@node-rs/argon2"
2+
import * as argon2 from "@node-rs/argon2"
33
import * as cp from "child_process"
44
import * as crypto from "crypto"
55
import envPaths from "env-paths"
@@ -58,10 +58,10 @@ export const paths = getEnvPaths()
5858
* On MacOS this function gets the standard XDG directories instead of using the native macOS
5959
* ones. Most CLIs do this as in practice only GUI apps use the standard macOS directories.
6060
*/
61-
export function getEnvPaths(): Paths {
61+
export function getEnvPaths(platform = process.platform): Paths {
6262
const paths = envPaths("code-server", { suffix: "" })
6363
const append = (p: string): string => path.join(p, "code-server")
64-
switch (process.platform) {
64+
switch (platform) {
6565
case "darwin":
6666
return {
6767
// envPaths uses native directories so force Darwin to use the XDG spec
@@ -158,7 +158,7 @@ export const generatePassword = async (length = 24): Promise<string> => {
158158
*/
159159
export const hash = async (password: string): Promise<string> => {
160160
try {
161-
return await _hash(password)
161+
return await argon2.hash(password)
162162
} catch (error: any) {
163163
logger.error(error)
164164
return ""
@@ -173,7 +173,7 @@ export const isHashMatch = async (password: string, hash: string) => {
173173
return false
174174
}
175175
try {
176-
return await verify(hash, password)
176+
return await argon2.verify(hash, password)
177177
} catch (error: any) {
178178
logger.error(error)
179179
return false

test/unit/node/util.test.ts

+7-63
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,8 @@ import { generateUuid } from "../../../src/common/util"
55
import { tmpdir } from "../../../src/node/constants"
66
import * as util from "../../../src/node/util"
77

8-
describe.skip("getEnvPaths", () => {
8+
describe("getEnvPaths", () => {
99
describe("on darwin", () => {
10-
let ORIGINAL_PLATFORM = ""
11-
12-
beforeAll(() => {
13-
ORIGINAL_PLATFORM = process.platform
14-
15-
Object.defineProperty(process, "platform", {
16-
value: "darwin",
17-
})
18-
})
19-
2010
beforeEach(() => {
2111
jest.resetModules()
2212
jest.mock("env-paths", () => {
@@ -27,23 +17,14 @@ describe.skip("getEnvPaths", () => {
2717
})
2818
})
2919
})
30-
31-
afterAll(() => {
32-
// Restore old platform
33-
34-
Object.defineProperty(process, "platform", {
35-
value: ORIGINAL_PLATFORM,
36-
})
37-
})
38-
3920
it("should return the env paths using xdgBasedir", () => {
4021
jest.mock("xdg-basedir", () => ({
4122
data: "/home/usr/.local/share",
4223
config: "/home/usr/.config",
4324
runtime: "/tmp/runtime",
4425
}))
4526
const getEnvPaths = require("../../../src/node/util").getEnvPaths
46-
const envPaths = getEnvPaths()
27+
const envPaths = getEnvPaths("darwin")
4728

4829
expect(envPaths.data).toEqual("/home/usr/.local/share/code-server")
4930
expect(envPaths.config).toEqual("/home/usr/.config/code-server")
@@ -53,24 +34,14 @@ describe.skip("getEnvPaths", () => {
5334
it("should return the env paths using envPaths when xdgBasedir is undefined", () => {
5435
jest.mock("xdg-basedir", () => ({}))
5536
const getEnvPaths = require("../../../src/node/util").getEnvPaths
56-
const envPaths = getEnvPaths()
37+
const envPaths = getEnvPaths("darwin")
5738

5839
expect(envPaths.data).toEqual("/home/envPath/.local/share")
5940
expect(envPaths.config).toEqual("/home/envPath/.config")
6041
expect(envPaths.runtime).toEqual("/tmp/envPath/runtime")
6142
})
6243
})
6344
describe("on win32", () => {
64-
let ORIGINAL_PLATFORM = ""
65-
66-
beforeAll(() => {
67-
ORIGINAL_PLATFORM = process.platform
68-
69-
Object.defineProperty(process, "platform", {
70-
value: "win32",
71-
})
72-
})
73-
7445
beforeEach(() => {
7546
jest.resetModules()
7647
jest.mock("env-paths", () => {
@@ -82,34 +53,16 @@ describe.skip("getEnvPaths", () => {
8253
})
8354
})
8455

85-
afterAll(() => {
86-
// Restore old platform
87-
88-
Object.defineProperty(process, "platform", {
89-
value: ORIGINAL_PLATFORM,
90-
})
91-
})
92-
9356
it("should return the env paths using envPaths", () => {
9457
const getEnvPaths = require("../../../src/node/util").getEnvPaths
95-
const envPaths = getEnvPaths()
58+
const envPaths = getEnvPaths("win32")
9659

9760
expect(envPaths.data).toEqual("/windows/envPath/.local/share")
9861
expect(envPaths.config).toEqual("/windows/envPath/.config")
9962
expect(envPaths.runtime).toEqual("/tmp/envPath/runtime")
10063
})
10164
})
10265
describe("on other platforms", () => {
103-
let ORIGINAL_PLATFORM = ""
104-
105-
beforeAll(() => {
106-
ORIGINAL_PLATFORM = process.platform
107-
108-
Object.defineProperty(process, "platform", {
109-
value: "linux",
110-
})
111-
})
112-
11366
beforeEach(() => {
11467
jest.resetModules()
11568
jest.mock("env-paths", () => {
@@ -121,20 +74,12 @@ describe.skip("getEnvPaths", () => {
12174
})
12275
})
12376

124-
afterAll(() => {
125-
// Restore old platform
126-
127-
Object.defineProperty(process, "platform", {
128-
value: ORIGINAL_PLATFORM,
129-
})
130-
})
131-
13277
it("should return the runtime using xdgBasedir if it exists", () => {
13378
jest.mock("xdg-basedir", () => ({
13479
runtime: "/tmp/runtime",
13580
}))
13681
const getEnvPaths = require("../../../src/node/util").getEnvPaths
137-
const envPaths = getEnvPaths()
82+
const envPaths = getEnvPaths("linux")
13883

13984
expect(envPaths.data).toEqual("/linux/envPath/.local/share")
14085
expect(envPaths.config).toEqual("/linux/envPath/.config")
@@ -144,7 +89,7 @@ describe.skip("getEnvPaths", () => {
14489
it("should return the env paths using envPaths when xdgBasedir is undefined", () => {
14590
jest.mock("xdg-basedir", () => ({}))
14691
const getEnvPaths = require("../../../src/node/util").getEnvPaths
147-
const envPaths = getEnvPaths()
92+
const envPaths = getEnvPaths("linux")
14893

14994
expect(envPaths.data).toEqual("/linux/envPath/.local/share")
15095
expect(envPaths.config).toEqual("/linux/envPath/.config")
@@ -192,10 +137,9 @@ describe("isHashMatch", () => {
192137
const actual = await util.isHashMatch(password, _hash)
193138
expect(actual).toBe(false)
194139
})
195-
it("should return false and not throw an error if the hash doesn't start with a $", async () => {
140+
it("should return false if the hash doesn't start with a $", async () => {
196141
const password = "hellowpasssword"
197142
const _hash = "n2i$v=19$m=4096,t=3,p=1$EAoczTxVki21JDfIZpTUxg$rkXgyrW4RDGoDYrxBFD4H2DlSMEhP4h+Api1hXnGnFY"
198-
expect(async () => await util.isHashMatch(password, _hash)).not.toThrow()
199143
expect(await util.isHashMatch(password, _hash)).toBe(false)
200144
})
201145
it("should return false if the password and hash don't match", async () => {

0 commit comments

Comments
 (0)