CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/util/db-schema/name-rules.test.ts
Views: 687
1
// to access the non-exported RESERVED object
2
process.env["NODE_DEV"] = "TEST";
3
4
import * as NR from "@cocalc/util/db-schema/name-rules";
5
6
describe("Check RESERVED", () => {
7
const { RESERVED } = NR as any;
8
const { isReserved } = NR;
9
10
test("reserved names", () => {
11
expect(RESERVED.has("admin")).toBe(true);
12
expect(isReserved("admin")).toBe(true);
13
});
14
15
it.each(Array.from(RESERVED))(
16
"'%s' must be lowercase and has no spaces",
17
(name: string) => {
18
expect(name).not.toEqual("");
19
expect(name.indexOf(" ")).toBe(-1);
20
expect(name.toLowerCase()).toBe(name);
21
// entire name must be of a-z,A-Z,0-9,_,. or -
22
expect(name).toMatch(/^[a-z\d\.\-_]+$/);
23
},
24
);
25
});
26
27
test("checkAccountName", () => {
28
const { checkAccountName } = NR;
29
30
expect(() =>
31
checkAccountName("3b38dd9c-f8bf-48c0-9d26-7cad4bac08eb"),
32
).toThrow(/.*UUID.*/);
33
34
expect(() => checkAccountName("foo--bar")).toThrow(/.*hyphens.*/);
35
36
// not more than 39 characters
37
expect(() =>
38
checkAccountName("1234567890123456789012345678901234567890"),
39
).toThrow(/.*39.*/);
40
41
// not less than 1 character
42
expect(() => checkAccountName("")).toThrow(/.*1.*/);
43
44
// not start with hyphen
45
expect(() => checkAccountName("-foo")).toThrow(/.*hyphen.*/);
46
47
// not be "compute"
48
expect(() => checkAccountName("compute")).toThrow(/.*reserved.*/);
49
});
50
51
test("checkProjectName", () => {
52
const { checkProjectName } = NR;
53
54
// at most 100 characters
55
expect(() =>
56
checkProjectName(
57
"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901",
58
),
59
).toThrow(/.*100.*/);
60
61
// not less than 1 character
62
expect(() => checkProjectName("")).toThrow(/.*1.*/);
63
64
// not start with hyphen
65
expect(() => checkProjectName("-foo")).toThrow(/.*hyphen.*/);
66
67
// name must contain only a-z,A-Z,0-9, . or -, and not start with hyphen or have spaces
68
expect(() => checkProjectName("foo bar")).toThrow(/.*spaces.*/);
69
expect(() => checkProjectName("foo_bar")).toThrow(/.*spaces.*/);
70
expect(() => checkProjectName("foo-bar")).not.toThrow(/.*spaces.*/);
71
expect(() => checkProjectName("foo.bar")).not.toThrow(/.*spaces.*/);
72
73
// allow v4 UUID
74
expect(() =>
75
checkProjectName("3b38dd9c-f8bf-48c0-9d26-7cad4bac08eb"),
76
).not.toThrow(/.*UUID.*/);
77
});
78
79
test("checkPublicPathName", () => {
80
const { checkPublicPathName } = NR;
81
82
// at most 100 characters
83
expect(() =>
84
checkPublicPathName(
85
"12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901",
86
),
87
).toThrow(/.*100.*/);
88
89
// not less than 1 character
90
expect(() => checkPublicPathName("")).toThrow(/.*1.*/);
91
92
// not start with hyphen
93
expect(() => checkPublicPathName("-foo")).toThrow(/.*hyphen.*/);
94
95
// name must contain only a-z,A-Z,0-9, . or -, and not start with hyphen or have spaces
96
expect(() => checkPublicPathName("foo bar")).toThrow(/.*spaces.*/);
97
expect(() => checkPublicPathName("foo_bar")).toThrow(/.*spaces.*/);
98
expect(() => checkPublicPathName("foo-bar")).not.toThrow(/.*spaces.*/);
99
expect(() => checkPublicPathName("foo.bar")).not.toThrow(/.*spaces.*/);
100
101
// allow UUID
102
expect(() =>
103
checkPublicPathName("3b38dd9c-f8bf-48c0-9d26-7cad4bac08eb"),
104
).not.toThrow(/.*UUID.*/);
105
});
106
107