Create Authentik groups from Apps → Authentik

A New group form in the Groups section makes a group with a name and nothing
else: it starts empty and is never one that makes administrators of
Authentik. A name Authentik already has is refused in its own words, and a
name that decides a role in this app (Settings → Sign-in) is said to. Admin
only; audited with who made which group.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
bobbanandClaude Sonnet 5.5 committed 2026-11-01 00:58:33 +01:00
1 parent 6c5a5f0bd6
commit c9385387bc
9 files changed
+152 -12

No files matched your search

+63 -1
View File
@@ -1,5 +1,5 @@
/**
* Looking after Authentik groups: reading them with who is in them, renaming one, putting users in and taking them out — and what is refused (service accounts, the account the token belongs
* Looking after Authentik groups: reading them with who is in them, making one, renaming one, putting users in and taking them out — and what is refused (service accounts, the account the token belongs
* to, the last active administrator losing that, a user whose groups Authentik did not say), that nothing is changed unless all of it may be, how far a failure got, who may, what is in the
* audit log, and which groups decide roles in this app.
*
@@ -8,6 +8,7 @@
import { test, describe, before, after } from "node:test";
import assert from "node:assert/strict";
import { createServer as createHttpServer, type Server } from "node:http";
import { randomUUID } from "node:crypto";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
@@ -123,6 +124,13 @@ before(async () => {
};
const url = new URL(path, "http://x");
if (req.method === "GET" && url.pathname === "/api/v3/core/groups/") return send(200, { results: groupTable });
if (req.method === "POST" && url.pathname === "/api/v3/core/groups/") {
const b = JSON.parse(body) as { name: string; is_superuser?: boolean };
if (groupTable.some((g) => g.name === b.name)) return send(400, { name: ["A group with that name already exists."] });
const made: FakeGroup = { pk: randomUUID(), name: b.name, is_superuser: b.is_superuser === true, users: [] };
groupTable.push(made);
return send(201, made);
}
const grp = /^\/api\/v3\/core\/groups\/([0-9a-f-]{36})\/(add_user\/|remove_user\/)?$/.exec(url.pathname);
if (grp) {
const group = groupTable.find((g) => g.pk === grp[1]);
@@ -189,6 +197,17 @@ describe("the calls", () => {
assert.equal(seen[0].auth, "Bearer write-tok");
});
test("a group is made with a name and as one that does not make administrators — whatever else is asked of the adapter, only that is sent", async () => {
reset();
const made = await adapter().createGroup("Gamers");
assert.deepEqual([made.name, made.admin, made.members], ["Gamers", false, []]);
assert.deepEqual([seen[0].method, seen[0].path, seen[0].body], ["POST", "/api/v3/core/groups/", JSON.stringify({ name: "Gamers", is_superuser: false })]);
await assert.rejects(
() => adapter().createGroup("Gamers"),
(err: Error & { kind?: string }) => err.kind === "invalid" && /name: A group with that name already exists\./.test(err.message),
);
});
test("renaming is a PATCH of the name and nothing else; a member goes in or out with a POST of that one user", async () => {
reset();
const renamed = await adapter().renameGroup(G_FAMILY, "Household");
@@ -221,6 +240,7 @@ describe("the routes", () => {
const as = (sub: string) => ({ "content-type": "application/json", "x-sub": sub });
const details = (sub = "admin") => fetch(`${base}/${id}/group-details`, { headers: as(sub) });
const change = (gid: string, body: unknown, sub = "admin") => fetch(`${base}/${id}/groups/${gid}`, { method: "PATCH", headers: as(sub), body: JSON.stringify(body) });
const make = (body: unknown, sub = "admin") => fetch(`${base}/${id}/groups`, { method: "POST", headers: as(sub), body: JSON.stringify(body) });
const writes = () => seen.filter((s) => s.method !== "GET");
const audit = async (action: string) => (await db.select().from(auditLog)).filter((e) => e.action === action);
const refused = async (res: Response, pattern: RegExp) => {
@@ -378,12 +398,54 @@ describe("the routes", () => {
assert.match(((await (await change(G_OTHER, { membersAdd: [4] })).json()) as { message: string }).message, /0 of 1 changes had been made/);
});
test("making a group: admin only; it is made empty and never as an administrators group, and the audit log says who made which", async () => {
reset();
for (const sub of ["viewer", "operator"]) assert.equal((await make({ name: "Gamers" }, sub)).status, 403, sub);
assert.deepEqual(seen, []);
const res = await make({ name: " Gamers " });
assert.equal(res.status, 201);
const body = (await res.json()) as { group: { id: string; name: string; admin: boolean; members: number[]; decidesRole: string | null } };
assert.deepEqual([body.group.name, body.group.admin, body.group.members, body.group.decidesRole], ["Gamers", false, [], null]);
assert.deepEqual(writes().map((s) => [s.method, s.path, s.body]), [["POST", "/api/v3/core/groups/", JSON.stringify({ name: "Gamers", is_superuser: false })]]);
assert.equal(groupTable.some((g) => g.name === "Gamers" && !g.is_superuser), true);
const [entry] = await audit("create_authentik_group");
assert.deepEqual([entry.category, entry.targetType, entry.targetId], ["integration", "authentik_group", body.group.id]);
assert.deepEqual(JSON.parse(entry.detail ?? "{}"), { integrationId: id, integration: "Front door", group: "Gamers" });
});
test("a new group whose name decides a role here is said to; a name that is taken is Authentik's own words", async () => {
reset();
const res = await make({ name: "Ops" });
assert.equal(res.status, 201);
assert.equal(((await res.json()) as { group: { decidesRole: string | null } }).group.decidesRole, null);
const mapped = await make({ name: "ADMINS-2" });
assert.equal(((await mapped.json()) as { group: { decidesRole: string | null } }).group.decidesRole, null, "only the names in Settings → Sign-in");
await updateSettings({ signIn: { roleFromGroups: true, groupsClaim: "groups", adminGroups: ["admins", "Root Team"], operatorGroups: ["family"], otherwise: "viewer" } });
const root = await make({ name: "root team" });
assert.equal(((await root.json()) as { group: { decidesRole: string | null } }).group.decidesRole, "admin", "compared the way sign-in compares");
seen.length = 0;
const dup = await make({ name: "Family" });
assert.equal(dup.status, 400);
assert.match(((await dup.json()) as { message: string }).message, /name: A group with that name already exists/);
});
test("what is wrong with a new group is said before Authentik is asked", async () => {
reset();
for (const bad of [{}, { name: "" }, { name: " " }, { name: "x".repeat(201) }, { name: 5 }]) {
const res = await make(bad);
assert.equal(res.status, 400, JSON.stringify(bad));
assert.equal(typeof ((await res.json()) as { message: string }).message, "string");
}
assert.deepEqual(seen, []);
});
test("a group that is gone is not found; an Authentik with no token for managing users is not asked anything", async () => {
reset();
assert.equal((await change("99999999-9999-4999-8999-999999999999", { name: "x" })).status, 404);
seen.length = 0;
assert.equal((await fetch(`${base}/${readOnly}/group-details`, { headers: as("admin") })).status, 400);
assert.equal((await fetch(`${base}/${readOnly}/groups/${G_FAMILY}`, { method: "PATCH", headers: as("admin"), body: JSON.stringify({ name: "x" }) })).status, 400);
assert.equal((await fetch(`${base}/${readOnly}/groups`, { method: "POST", headers: as("admin"), body: JSON.stringify({ name: "x" }) })).status, 400);
assert.deepEqual(seen, []);
});
});