Path: blob/master/src/packages/hub/run/test-create-admin.ts
5704 views
#!/usr/bin/env node12/*3* Script to create a test admin account and API key for CI testing.4* This is used in GitHub Actions to set up cocalc-api tests.5*/67import { v4 as uuidv4 } from "uuid";89import createAccount from "@cocalc/server/accounts/create-account";10import manageApiKeys from "@cocalc/server/api/manage";11import getPool from "@cocalc/database/pool";1213async function main() {14const account_id = uuidv4();15const email = "[email protected]";16const password = "testpassword"; // dummy password17const firstName = "CI";18const lastName = "Admin";1920console.error(`Creating admin account ${account_id}...`);2122// Create the account23await createAccount({24email,25password,26firstName,27lastName,28account_id,29tags: [],30signupReason: "CI testing",31noFirstProject: true,32});3334// Set as admin35const pool = getPool();36await pool.query("UPDATE accounts SET groups=$1 WHERE account_id=$2", [37["admin"],38account_id,39]);4041console.error("Creating API key...");4243// Create API key44const keys = await manageApiKeys({45account_id,46action: "create",47name: "ci-testing",48});4950if (!keys || keys.length === 0) {51throw new Error("Failed to create API key");52}5354const apiKey = keys[0];55if (!apiKey.secret) {56throw new Error("API key secret is missing");57}58console.error(`API key created with id=${apiKey.id}: ${apiKey.secret}`);59console.error(`Last 6 chars: ${apiKey.secret.slice(-6)}`);6061// Output the account_id and API key for CI in format: UUID;api-key62process.stdout.write(`${account_id};${apiKey.secret}`);63}6465main().catch((err) => {66console.error("Error:", err);67process.exit(1);68});697071