Skip to content

Commit

Permalink
tests: added create sentry team test
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasCharrier committed Jan 28, 2025
1 parent a6d7c9c commit 9f5fa65
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
104 changes: 104 additions & 0 deletions __tests__/test-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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
Expand All @@ -130,13 +134,25 @@ 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
.deleteFrom("service_accounts")
.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({
Expand Down Expand Up @@ -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<CreateSentryTeamDataSchemaType>);

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 () => {
Expand Down Expand Up @@ -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;
});
});
});
16 changes: 15 additions & 1 deletion src/app/api/services/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/models/actionEvent/actionEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
EventSentryAccountUpdateRequestedPayload,
EventServiceAccountDeletedPayload,
EventSentryCreateTeamPayload,
EventSentryCreateTeamRequestedTeamPayload,
} from "./serviceActionEvent";
import {
memberInfoUpdateSchema,
Expand Down Expand Up @@ -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<EventCode, string> = {
Expand Down Expand Up @@ -95,6 +97,8 @@ export const EventCodeToReadable: Record<EventCode, string> = {
"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";
Expand Down Expand Up @@ -387,6 +391,7 @@ export type EventPayloads =
| z.infer<typeof EventSentryAccountUpdateRequestedPayload>
| z.infer<typeof EventSentryAccountUpdatedPayload>
| z.infer<typeof EventSentryCreateTeamPayload>
| z.infer<typeof EventSentryCreateTeamRequestedTeamPayload>
| z.infer<typeof EventMatomoAccountPayloadSchema>;
// | z.infer<typeof EventMatomoAccountRequestedPayload>
// | z.infer<typeof EventMatomoAccountCreatedPayload>
Expand Down
12 changes: 12 additions & 0 deletions src/models/actionEvent/serviceActionEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/models/actions/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type matomoAccountRequestSchemaType = z.infer<

export const sentryAccountCreateRequestSchema = z.object({
newTeam: z.object({
startupId: z.string(),
startupId: z.string().uuid(),
}),
});

Expand Down

0 comments on commit 9f5fa65

Please sign in to comment.