|
| 1 | +import { assert, describe, it } from "@effect/vitest"; |
| 2 | +import { Effect, Exit } from "effect"; |
| 3 | + |
| 4 | +import { |
| 5 | + CloudAuthApiTestContext, |
| 6 | + CloudAuthApiTestContextLayer, |
| 7 | + makeCloudAuthApiTestState, |
| 8 | +} from "./cloud-auth-api.test-context"; |
| 9 | +import { makeWorkOSTestMembership, makeWorkOSTestState } from "./workos.test-layer"; |
| 10 | +import { makeAutumnTestState } from "../services/autumn.test-layer"; |
| 11 | + |
| 12 | +describe("create organization API", () => { |
| 13 | + it.effect("lets a paid user create another organization through the HTTP API client", () => { |
| 14 | + const state = makeCloudAuthApiTestState({ |
| 15 | + workos: makeWorkOSTestState({ |
| 16 | + memberships: [ |
| 17 | + makeWorkOSTestMembership("org_free_1", "active"), |
| 18 | + makeWorkOSTestMembership("org_free_2", "active"), |
| 19 | + makeWorkOSTestMembership("org_paid", "active"), |
| 20 | + ], |
| 21 | + }), |
| 22 | + autumn: makeAutumnTestState({ |
| 23 | + subscriptionsByOrgId: { |
| 24 | + org_paid: [{ planId: "team", status: "active" }], |
| 25 | + }, |
| 26 | + }), |
| 27 | + }); |
| 28 | + |
| 29 | + return Effect.gen(function* () { |
| 30 | + const { client } = yield* CloudAuthApiTestContext; |
| 31 | + |
| 32 | + const result = yield* client.cloudAuth.createOrganization({ |
| 33 | + payload: { name: "Paid Extra Org" }, |
| 34 | + }); |
| 35 | + |
| 36 | + assert.deepEqual(result, { id: "org_created", name: "Paid Extra Org" }); |
| 37 | + assert.deepEqual(state.workos.createdOrganizations, [ |
| 38 | + { id: "org_created", name: "Paid Extra Org" }, |
| 39 | + ]); |
| 40 | + assert.deepEqual(state.workos.createdMemberships, [ |
| 41 | + { organizationId: "org_created", userId: "user_1", roleSlug: "admin" }, |
| 42 | + ]); |
| 43 | + assert.deepEqual(state.userStore.upsertedOrganizations, [ |
| 44 | + { id: "org_created", name: "Paid Extra Org" }, |
| 45 | + ]); |
| 46 | + }).pipe(Effect.provide(CloudAuthApiTestContextLayer(state))); |
| 47 | + }); |
| 48 | + |
| 49 | + it.effect( |
| 50 | + "rejects a free-only user at the free organization limit through the HTTP API client", |
| 51 | + () => { |
| 52 | + const state = makeCloudAuthApiTestState({ |
| 53 | + workos: makeWorkOSTestState({ |
| 54 | + memberships: [ |
| 55 | + makeWorkOSTestMembership("org_free_1", "active"), |
| 56 | + makeWorkOSTestMembership("org_free_2", "active"), |
| 57 | + makeWorkOSTestMembership("org_free_3", "active"), |
| 58 | + ], |
| 59 | + }), |
| 60 | + }); |
| 61 | + |
| 62 | + return Effect.gen(function* () { |
| 63 | + const { client } = yield* CloudAuthApiTestContext; |
| 64 | + |
| 65 | + const exit = yield* Effect.exit( |
| 66 | + client.cloudAuth.createOrganization({ |
| 67 | + payload: { name: "Blocked Org" }, |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + assert.isTrue(Exit.isFailure(exit)); |
| 72 | + assert.deepEqual(state.workos.createdOrganizations, []); |
| 73 | + assert.deepEqual(state.workos.createdMemberships, []); |
| 74 | + assert.deepEqual(state.userStore.upsertedOrganizations, []); |
| 75 | + }).pipe(Effect.provide(CloudAuthApiTestContextLayer(state))); |
| 76 | + }, |
| 77 | + ); |
| 78 | + |
| 79 | + it.effect("does not count pending memberships toward the free organization limit", () => { |
| 80 | + const state = makeCloudAuthApiTestState({ |
| 81 | + workos: makeWorkOSTestState({ |
| 82 | + memberships: [ |
| 83 | + makeWorkOSTestMembership("org_free_1", "active"), |
| 84 | + makeWorkOSTestMembership("org_free_2", "active"), |
| 85 | + makeWorkOSTestMembership("org_invited", "pending"), |
| 86 | + ], |
| 87 | + }), |
| 88 | + }); |
| 89 | + |
| 90 | + return Effect.gen(function* () { |
| 91 | + const { client } = yield* CloudAuthApiTestContext; |
| 92 | + |
| 93 | + const result = yield* client.cloudAuth.createOrganization({ |
| 94 | + payload: { name: "Below Active Limit" }, |
| 95 | + }); |
| 96 | + |
| 97 | + assert.deepEqual(result, { id: "org_created", name: "Below Active Limit" }); |
| 98 | + assert.deepEqual(state.workos.createdOrganizations, [ |
| 99 | + { id: "org_created", name: "Below Active Limit" }, |
| 100 | + ]); |
| 101 | + }).pipe(Effect.provide(CloudAuthApiTestContextLayer(state))); |
| 102 | + }); |
| 103 | +}); |
0 commit comments