Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/hub/run/test-create-admin.ts
5704 views
1
#!/usr/bin/env node
2
3
/*
4
* Script to create a test admin account and API key for CI testing.
5
* This is used in GitHub Actions to set up cocalc-api tests.
6
*/
7
8
import { v4 as uuidv4 } from "uuid";
9
10
import createAccount from "@cocalc/server/accounts/create-account";
11
import manageApiKeys from "@cocalc/server/api/manage";
12
import getPool from "@cocalc/database/pool";
13
14
async function main() {
15
const account_id = uuidv4();
16
const email = "[email protected]";
17
const password = "testpassword"; // dummy password
18
const firstName = "CI";
19
const lastName = "Admin";
20
21
console.error(`Creating admin account ${account_id}...`);
22
23
// Create the account
24
await createAccount({
25
email,
26
password,
27
firstName,
28
lastName,
29
account_id,
30
tags: [],
31
signupReason: "CI testing",
32
noFirstProject: true,
33
});
34
35
// Set as admin
36
const pool = getPool();
37
await pool.query("UPDATE accounts SET groups=$1 WHERE account_id=$2", [
38
["admin"],
39
account_id,
40
]);
41
42
console.error("Creating API key...");
43
44
// Create API key
45
const keys = await manageApiKeys({
46
account_id,
47
action: "create",
48
name: "ci-testing",
49
});
50
51
if (!keys || keys.length === 0) {
52
throw new Error("Failed to create API key");
53
}
54
55
const apiKey = keys[0];
56
if (!apiKey.secret) {
57
throw new Error("API key secret is missing");
58
}
59
console.error(`API key created with id=${apiKey.id}: ${apiKey.secret}`);
60
console.error(`Last 6 chars: ${apiKey.secret.slice(-6)}`);
61
62
// Output the account_id and API key for CI in format: UUID;api-key
63
process.stdout.write(`${account_id};${apiKey.secret}`);
64
}
65
66
main().catch((err) => {
67
console.error("Error:", err);
68
process.exit(1);
69
});
70
71