From 9f5fa6504dafe0b9bd2495500b9878dd67a629bb Mon Sep 17 00:00:00 2001 From: Lucas Charrier Date: Tue, 28 Jan 2025 17:27:57 +0100 Subject: [PATCH] tests: added create sentry team test --- __tests__/test-worker.ts | 104 +++++++++++++++++++ src/app/api/services/actions.ts | 16 ++- src/models/actionEvent/actionEvent.ts | 5 + src/models/actionEvent/serviceActionEvent.ts | 12 +++ src/models/actions/service.ts | 2 +- 5 files changed, 137 insertions(+), 2 deletions(-) diff --git a/__tests__/test-worker.ts b/__tests__/test-worker.ts index 2722a7a45..2d8e0a3a8 100644 --- a/__tests__/test-worker.ts +++ b/__tests__/test-worker.ts @@ -5,13 +5,16 @@ import sinon from "sinon"; import testUsers from "./users.json"; import utils from "./utils"; import { db } from "@/lib/kysely"; +import { EventCode } from "@/models/actionEvent"; import { MATOMO_SITE_TYPE } from "@/models/actions/service"; import { CreateOrUpdateMatomoAccountDataSchemaType, CreateSentryAccountDataSchemaType, + CreateSentryTeamDataSchemaType, } from "@/models/jobs/services"; import { ACCOUNT_SERVICE_STATUS, SERVICES } from "@/models/services"; import { createSentryServiceAccount } from "@/server/queueing/workers/create-sentry-account"; +import { createSentryTeam } from "@/server/queueing/workers/create-sentry-team"; import { createOrUpdateMatomoServiceAccount } from "@/server/queueing/workers/create-update-matomo-account"; import * as controllerUtils from "@controllers/utils"; @@ -113,6 +116,7 @@ describe("Service account creation by worker", () => { describe("sentry account service consumer", () => { let service_accounts; let user; + let newStartup; before(async function () { await utils.createUsers(testUsers); user = await db @@ -130,6 +134,14 @@ describe("Service account creation by worker", () => { status: ACCOUNT_SERVICE_STATUS.ACCOUNT_CREATION_PENDING, }) .executeTakeFirstOrThrow(); + newStartup = await db + .insertInto("startups") + .values({ + name: "a-startup", + ghid: "a-startup", + }) + .returning("uuid") + .executeTakeFirstOrThrow(); }); after(async function () { await db @@ -137,6 +149,10 @@ describe("Service account creation by worker", () => { .where("uuid", "=", service_accounts.uuid) .executeTakeFirstOrThrow(); await utils.deleteUsers(testUsers); + await db + .deleteFrom("startups") + .where("uuid", "=", newStartup.uuid) + .execute(); }); it("should create sentry service account", async () => { await createSentryServiceAccount({ @@ -164,14 +180,53 @@ describe("Service account creation by worker", () => { ACCOUNT_SERVICE_STATUS.ACCOUNT_INVITATION_SENT ); }); + it("should create sentry team", async () => { + await createSentryTeam({ + data: { + email: "membre.actif@betagouv.ovh", + userLogin: "membre.actif@betagouv.ovh", + username: "membre.actif", + userUuid: user?.uuid, + startupId: newStartup.uuid, + teams: [ + { + teamSlug: "a-startup", + role: "admin", + }, + ], + }, + } as unknown as PgBoss.Job); + + const result = await db + .selectFrom("events") + .selectAll() + .orderBy("created_at desc") + .executeTakeFirst(); + result?.action_code.should.equal( + EventCode.MEMBER_SERVICE_TEAM_CREATED + ); + }); }); describe("sentry account producer", () => { + let newStartup; before(async () => { await utils.createUsers(testUsers); + newStartup = await db + .insertInto("startups") + .values({ + name: "a-startup", + ghid: "a-startup", + }) + .returning("uuid") + .executeTakeFirstOrThrow(); }); after(async () => { await utils.deleteUsers(testUsers); + await db + .deleteFrom("startups") + .where("uuid", "=", newStartup.uuid) + .execute(); }); it("should create sentry worker tasks", async () => { @@ -213,5 +268,54 @@ describe("Service account creation by worker", () => { .executeTakeFirstOrThrow(); account.should.exist; }); + + it("should create sentry worker tasks create account and create team", async () => { + const user = await db + .selectFrom("users") + .where("username", "=", "membre.actif") + .selectAll() + .executeTakeFirst(); + const mockSession = { + user: { + id: "membre.actif", + isAdmin: false, + uuid: user?.uuid, + }, + }; + let getServerSessionStub = sinon.stub(); + const askAccountCreationForService = proxyquire( + "@/app/api/services/actions", + { + "next-auth": { getServerSession: getServerSessionStub }, + } + ).askAccountCreationForService; + getServerSessionStub.resolves(mockSession); + await askAccountCreationForService({ + service: SERVICES.SENTRY, + data: { + newTeam: { + startupId: newStartup.uuid, + }, + }, + }); + const account = await db + .selectFrom("service_accounts") + .selectAll() + .where("user_id", "=", user?.uuid!) + .where("account_type", "=", "sentry") + .executeTakeFirstOrThrow(); + const events = await db + .selectFrom("events") + .selectAll() + .orderBy("created_at desc") + .execute(); + events[0].action_code.should.equal( + EventCode.MEMBER_SERVICE_ACCOUNT_REQUESTED + ); + events[1].action_code.should.equal( + EventCode.MEMBER_SERVICE_TEAM_CREATION_REQUESTED + ); + account.should.exist; + }); }); }); diff --git a/src/app/api/services/actions.ts b/src/app/api/services/actions.ts index 80a2be0a8..6cf6dc69d 100644 --- a/src/app/api/services/actions.ts +++ b/src/app/api/services/actions.ts @@ -131,9 +131,23 @@ const createOrUpdateSentryAccount = async ( startupId: sentryData.newTeam.startupId, }) ); - teams.push({ + const newTeam = { teamSlug: slugify(startup.name), teamRole: SentryRole.admin, + }; + teams.push(newTeam); + await addEvent({ + action_code: EventCode.MEMBER_SERVICE_TEAM_CREATION_REQUESTED, + action_metadata: { + service: SERVICES.SENTRY, + startupId: sentryData.newTeam.startupId, + requestId: requestId, + team: { + teamSlug: newTeam.teamSlug, + }, + }, + action_on_username: user.username, + created_by_username: user.username, }); } if (accountAlreadyExists) { diff --git a/src/models/actionEvent/actionEvent.ts b/src/models/actionEvent/actionEvent.ts index 06b778b59..3bb4f459e 100644 --- a/src/models/actionEvent/actionEvent.ts +++ b/src/models/actionEvent/actionEvent.ts @@ -12,6 +12,7 @@ import { EventSentryAccountUpdateRequestedPayload, EventServiceAccountDeletedPayload, EventSentryCreateTeamPayload, + EventSentryCreateTeamRequestedTeamPayload, } from "./serviceActionEvent"; import { memberInfoUpdateSchema, @@ -55,6 +56,7 @@ export enum EventCode { MEMBER_SERVICE_ACCOUNT_UPDATE_REQUESTED = "MEMBER_SERVICE_ACCOUNT_UPDATE_REQUESTED", MEMBER_SERVICE_ACCOUNT_UPDATED = "MEMBER_SERVICE_ACCOUNT_UPDATED", MEMBER_SERVICE_TEAM_CREATED = "MEMBER_SERVICE_TEAM_CREATED", + MEMBER_SERVICE_TEAM_CREATION_REQUESTED = "MEMBER_SERVICE_TEAM_CREATION_REQUESTED", } export const EventCodeToReadable: Record = { @@ -95,6 +97,8 @@ export const EventCodeToReadable: Record = { "Compte de service mise à jour demandée", [EventCode.MEMBER_SERVICE_ACCOUNT_UPDATED]: "Compte de service mis à jour", [EventCode.MEMBER_SERVICE_TEAM_CREATED]: "Equipe sentry créée", + [EventCode.MEMBER_SERVICE_TEAM_CREATION_REQUESTED]: + "Equipe sentry demandée", }; export const SYSTEM_NAME = "system"; @@ -387,6 +391,7 @@ export type EventPayloads = | z.infer | z.infer | z.infer + | z.infer | z.infer; // | z.infer // | z.infer diff --git a/src/models/actionEvent/serviceActionEvent.ts b/src/models/actionEvent/serviceActionEvent.ts index 24147e0a3..f7dde3eae 100644 --- a/src/models/actionEvent/serviceActionEvent.ts +++ b/src/models/actionEvent/serviceActionEvent.ts @@ -76,6 +76,18 @@ export const EventSentryAccountUpdatedPayload = z.object({ action_metadata: sentryActionMetadataSchema, }); +export const EventSentryCreateTeamRequestedTeamPayload = z.object({ + action_code: z.literal(EventCode.MEMBER_SERVICE_TEAM_CREATION_REQUESTED), + action_metadata: z.object({ + service: z.literal(SERVICES.SENTRY), + requestId: z.string().uuid(), + startupId: z.string(), + team: z.object({ + teamSlug: z.string(), + }), + }), +}); + export const EventSentryCreateTeamPayload = z.object({ action_code: z.literal(EventCode.MEMBER_SERVICE_TEAM_CREATED), action_metadata: z.object({ diff --git a/src/models/actions/service.ts b/src/models/actions/service.ts index 5a122aad4..b4be96047 100644 --- a/src/models/actions/service.ts +++ b/src/models/actions/service.ts @@ -61,7 +61,7 @@ export type matomoAccountRequestSchemaType = z.infer< export const sentryAccountCreateRequestSchema = z.object({ newTeam: z.object({ - startupId: z.string(), + startupId: z.string().uuid(), }), });