File size: 22,138 Bytes
1bd396e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/**

 * @fileoverview `/api/client/workspaces` integration tests: the Mentra App's workspace calls.

 *

 * These run the real app, router, services, models and transactions against a local replica set. The only

 * thing faked is the phone's access-token verification (`verifyAccessToken`), as the phone's token is

 * Mentra's own session token; each person's verified email comes from an `account_emails` row (Core's copy of the account

 * directory's answer, null for an unverified one), as it does in production.

 *

 * Safety: the test connects through `localTestMongoUrl` (loopback only, random database name, ignores

 * `MONGO_URL`) and asserts the live connection is on that database before any destructive call.

 *

 * Run: `CLOUD_V2_TEST_MONGO_URL=mongodb://127.0.0.1:27017 bun test tests/client-workspaces-api.integration.test.ts`

 */

import {afterAll, afterEach, beforeAll, beforeEach, describe, expect, spyOn, test} from "bun:test"
import type {Model} from "mongoose"

import {createApp} from "../packages/core/src/api/app"
import {connectMongo, disconnectMongo, mongoReadinessCheck} from "../packages/core/src/connections/mongo.connection"
import {AuthRateLimitModel} from "../packages/core/src/models/auth-rate-limit.model"
import {AccountEmailModel} from "../packages/core/src/models/account-email.model"
import {UserModel} from "../packages/core/src/models/user.model"
import {WorkspaceAuditCounterModel} from "../packages/core/src/models/workspace-audit-counter.model"
import {WorkspaceAuditEventModel} from "../packages/core/src/models/workspace-audit-event.model"
import {WorkspaceInvitationModel} from "../packages/core/src/models/workspace-invitation.model"
import {WorkspaceMembershipModel} from "../packages/core/src/models/workspace-membership.model"
import {WorkspaceModel} from "../packages/core/src/models/workspace.model"
import * as accountEmails from "../packages/core/src/services/account/account-email.service"
import * as session from "../packages/core/src/services/session.service"
import {createInvitation} from "../packages/core/src/services/workspaces/invitation.service"
import {ensureWorkspace} from "../packages/core/src/services/workspaces/automatic-workspace.service"
import {
  createWorkspace,
  getActiveMembership,
  type Actor,
} from "../packages/core/src/services/workspaces/workspace.service"
import {assertConnectedTo, localTestMongoUrl} from "./support/local-mongo"
import {membershipRow} from "./support/membership-row"

const MODELS: Model<any>[] = [
  AuthRateLimitModel,
  WorkspaceModel,
  WorkspaceMembershipModel,
  WorkspaceInvitationModel,
  WorkspaceAuditCounterModel,
  WorkspaceAuditEventModel,
  AccountEmailModel,
  UserModel,
]

const URL_TEMPLATE = "https://core.example.test/invite/{token}"

let databaseUrl: string
let verified = false
let app: ReturnType<typeof createApp>
const mocks: Array<{mockRestore(): void}> = []

type UserActor = Actor & {kind: "user"}

const owner: UserActor = {
  kind: "user",
  mentraUserId: "mu_owner",
  email: "owner@example.test",
  emailVerified: true,
  isOrganizationAdmin: false,
}

/**

 * A person who can sign in: a users row, and Core's copy of their verified email (null when unverified,

 * which is what the account directory answers for an email nobody has confirmed).

 */
async function person(mentraUserId: string, email: string | null, emailVerified = true) {
  await UserModel.create({mentraUserId, tenantId: "mentra", tenantUserId: `sb-${mentraUserId}`})
  await AccountEmailModel.create({
    mentraUserId,
    email: emailVerified ? email : null,
    checkedAt: new Date(),
  })
}

/** The phone is signed in as `mentraUserId` (its access token verifies to them). */
function signedInAs(mentraUserId: string) {
  mocks.push(
    spyOn(session, "verifyAccessToken").mockResolvedValue({
      mentraUserId,
      tenantId: "mentra",
      sessionId: "s",
      jti: "j",
      exp: Math.floor(Date.now() / 1000) + 600,
    } as never),
  )
}

function call(path: string, init: {method?: string; token?: string | null; body?: string} = {}) {
  const headers: Record<string, string> = {}
  if (init.token !== null) headers.authorization = `Bearer ${init.token ?? "phone-token"}`
  if (init.body !== undefined) headers["content-type"] = "application/json"
  return app.request(`/api/client/workspaces${path}`, {method: init.method ?? "GET", headers, body: init.body})
}

async function workspaceWithOwner(name = "Acme") {
  await person(owner.mentraUserId, owner.email)
  return (await createWorkspace(owner, {name})).workspaceId
}

async function inviteTo(workspaceId: string, email: string, role: "member" | "developer" | "admin" = "member") {
  return createInvitation(owner, workspaceId, {email, role})
}

async function addMember(workspaceId: string, mentraUserId: string, role: string) {
  await WorkspaceMembershipModel.create(
    membershipRow({
      membershipId: `wm_${mentraUserId}`,
      workspaceId,
      mentraUserId,
      email: `${mentraUserId}@example.test`,
      role,
      status: "active",
      startedAt: new Date(),
    }),
  )
}

beforeAll(async () => {
  databaseUrl = localTestMongoUrl("client-workspaces")
  await connectMongo(databaseUrl)
  assertConnectedTo(databaseUrl, WorkspaceModel.db.name)
  verified = true
  for (const model of MODELS) {
    await model.createCollection()
    await model.syncIndexes()
  }
  app = createApp({readinessChecks: [mongoReadinessCheck]})
})

afterAll(async () => {
  if (verified && WorkspaceModel.db.readyState === 1) {
    assertConnectedTo(databaseUrl, WorkspaceModel.db.name)
    await WorkspaceModel.db.dropDatabase()
  }
  await disconnectMongo()
})

beforeEach(async () => {
  assertConnectedTo(databaseUrl, WorkspaceModel.db.name)
  await Promise.all(MODELS.map(model => model.deleteMany({})))
  process.env.CLOUD_CORE_WORKSPACE_INVITE_URL_TEMPLATE = URL_TEMPLATE
  delete process.env.RESEND_API_KEY
})

afterEach(() => {
  for (const mock of mocks.splice(0)) mock.mockRestore()
})

describe("authentication", () => {
  test("no token, and a token Core rejects, are 401 on every route", async () => {
    expect((await call("/mine", {token: null})).status).toBe(401)
    mocks.push(spyOn(session, "verifyAccessToken").mockRejectedValue(new (await import("../packages/core/src/types/oauth.types")).InvalidGrant("expired")))
    for (const [path, method] of [
      ["/mine", "GET"],
      ["/invitations/mine", "GET"],
      ["/invitations/winv_x/accept", "POST"],
      ["/invitations/winv_x/decline", "POST"],
      ["/leave", "POST"],
    ] as const) {
      const res = await call(path, {method})
      expect({path, status: res.status}).toEqual({path, status: 401})
    }
  })

  test("a deleted account is 401, not an empty answer", async () => {
    await person("mu_gone", "gone@example.test")
    await UserModel.updateOne({mentraUserId: "mu_gone"}, {$set: {supportTelemetryDeletedAt: new Date()}})
    signedInAs("mu_gone")
    expect((await call("/mine")).status).toBe(401)
    expect((await call("/invitations/mine")).status).toBe(401)
  })

  test("when the account directory cannot say what the email is and Core has no copy, it is 503", async () => {
    await UserModel.create({mentraUserId: "mu_a", tenantId: "mentra", tenantUserId: "sb-mu_a"})
    signedInAs("mu_a")
    mocks.push(spyOn(accountEmails, "accountEmail").mockRejectedValue(new Error("directory down")))
    const res = await call("/mine")
    expect(res.status).toBe(503)
    expect(((await res.json()) as {error: string}).error).toBe("unavailable")
  })
})

describe("GET /mine", () => {
  test("a person in no workspace gets null", async () => {
    await person("mu_a", "a@example.test")
    signedInAs("mu_a")
    const res = await call("/mine")
    expect(res.status).toBe(200)
    expect(res.headers.get("cache-control")).toBe("no-store")
    expect(await res.json()).toEqual({workspace: null})
  })

  test("a member gets their workspace, their role and their membership", async () => {
    const ws = await workspaceWithOwner("Acme Corp")
    await person("mu_dev", "dev@example.test")
    await addMember(ws, "mu_dev", "developer")
    signedInAs("mu_dev")
    expect(await (await call("/mine")).json()).toEqual({
      workspace: {workspaceId: ws, name: "Acme Corp", role: "developer", membershipId: "wm_mu_dev"},
    })
  })

  test("a deleted workspace is not offered", async () => {
    const ws = await workspaceWithOwner()
    await WorkspaceModel.updateOne({workspaceId: ws}, {$set: {status: "deleted"}})
    signedInAs(owner.mentraUserId)
    expect(await (await call("/mine")).json()).toEqual({workspace: null})
  })
})

describe("invitations", () => {
  test("lists what is addressed to the verified email, with no token", async () => {
    const ws = await workspaceWithOwner("Acme")
    const created = await inviteTo(ws, "new@example.test", "developer")
    await person("mu_new", "New@Example.test")
    signedInAs("mu_new")
    const body = (await (await call("/invitations/mine")).json()) as {invitations: Array<Record<string, unknown>>}
    expect(body.invitations).toHaveLength(1)
    expect(body.invitations[0]).toMatchObject({workspaceName: "Acme", role: "developer"})
    expect(JSON.stringify(body)).not.toContain(created.inviteUrl.split("/").pop()!)
  })

  test("an unverified email is refused (403 email_unverified)", async () => {
    const ws = await workspaceWithOwner()
    await inviteTo(ws, "new@example.test")
    await person("mu_new", "new@example.test", false)
    signedInAs("mu_new")
    const res = await call("/invitations/mine")
    expect(res.status).toBe(403)
    expect(((await res.json()) as {error: string}).error).toBe("email_unverified")
  })

  test("accepting joins the workspace and returns it; the person's workspace is then that one", async () => {
    const ws = await workspaceWithOwner("Acme")
    const created = await inviteTo(ws, "new@example.test", "developer")
    await person("mu_new", "new@example.test")
    signedInAs("mu_new")

    const res = await call(`/invitations/${created.invitationId}/accept`, {method: "POST"})
    expect(res.status).toBe(200)
    const body = (await res.json()) as {workspace: {workspaceId: string; name: string; role: string}}
    expect(body.workspace).toMatchObject({workspaceId: ws, name: "Acme", role: "developer"})
    expect((await getActiveMembership(ws, "mu_new"))?.role).toBe("developer")
    expect(await (await call("/mine")).json()).toMatchObject({workspace: {workspaceId: ws}})
  })

  test("accepting yields an empty automatic workspace of the person's own and joins the invited one", async () => {
    const ws = await workspaceWithOwner("Acme")
    const created = await inviteTo(ws, "new@example.test", "developer")
    await person("mu_new", "new@example.test")
    // The phone user's own empty automatic workspace, as the portal makes when they sign in with none.
    const automatic = (await ensureWorkspace({...owner, mentraUserId: "mu_new", email: "new@example.test"})).workspace.workspaceId
    signedInAs("mu_new")

    const res = await call(`/invitations/${created.invitationId}/accept`, {method: "POST"})
    expect(res.status).toBe(200)
    expect(((await res.json()) as {workspace: {workspaceId: string}}).workspace.workspaceId).toBe(ws)
    expect((await WorkspaceModel.findOne({workspaceId: automatic}).lean())?.status).toBe("deleted")
    expect(await (await call("/mine")).json()).toMatchObject({workspace: {workspaceId: ws}})
  })

  test("accepting while already in a workspace is 409 already_in_workspace and joins nothing", async () => {
    const first = await workspaceWithOwner("First")
    await person("mu_dev", "dev@example.test")
    await addMember(first, "mu_dev", "member")
    const secondOwner: UserActor = {...owner, mentraUserId: "mu_owner2", email: "owner2@example.test"}
    await person(secondOwner.mentraUserId, secondOwner.email)
    const second = (await createWorkspace(secondOwner, {name: "Second"})).workspaceId
    const created = await createInvitation(secondOwner, second, {email: "dev@example.test", role: "member"})
    signedInAs("mu_dev")

    const res = await call(`/invitations/${created.invitationId}/accept`, {method: "POST"})
    expect(res.status).toBe(409)
    expect(((await res.json()) as {error: string}).error).toBe("already_in_workspace")
    expect(await getActiveMembership(second, "mu_dev")).toBeNull()
    const row = await WorkspaceInvitationModel.findOne({invitationId: created.invitationId}).lean()
    expect(row?.status).toBe("pending")
  })

  test("accepting someone else's invitation is 403 email_mismatch", async () => {
    const ws = await workspaceWithOwner()
    const created = await inviteTo(ws, "new@example.test")
    await person("mu_other", "other@example.test")
    signedInAs("mu_other")
    const res = await call(`/invitations/${created.invitationId}/accept`, {method: "POST"})
    expect(res.status).toBe(403)
    expect(((await res.json()) as {error: string}).error).toBe("email_mismatch")
  })

  test("an unknown invitation is 404, an expired one 410", async () => {
    const ws = await workspaceWithOwner()
    const created = await inviteTo(ws, "new@example.test")
    await person("mu_new", "new@example.test")
    signedInAs("mu_new")
    expect((await call("/invitations/winv_missing/accept", {method: "POST"})).status).toBe(404)
    await WorkspaceInvitationModel.updateOne(
      {invitationId: created.invitationId},
      {$set: {expiresAt: new Date(Date.now() - 1000)}},
    )
    expect((await call(`/invitations/${created.invitationId}/accept`, {method: "POST"})).status).toBe(410)
  })

  describe("decline", () => {
    test("marks the invitation declined, joins nothing and records invitation.declined", async () => {
      const ws = await workspaceWithOwner()
      const created = await inviteTo(ws, "new@example.test")
      await person("mu_new", "new@example.test")
      signedInAs("mu_new")
      const membershipEventsBefore = await WorkspaceAuditEventModel.countDocuments({action: {$regex: "^membership\\."}})

      const res = await call(`/invitations/${created.invitationId}/decline`, {method: "POST"})
      expect(res.status).toBe(200)
      expect(await res.json()).toEqual({invitationId: created.invitationId, declined: true})
      expect((await WorkspaceInvitationModel.findOne({invitationId: created.invitationId}).lean())?.status).toBe("declined")
      expect(await getActiveMembership(ws, "mu_new")).toBeNull()
      const events = await WorkspaceAuditEventModel.find({action: "invitation.declined"}).lean()
      expect(events).toHaveLength(1)
      expect(events[0]).toMatchObject({workspaceId: ws, target: {invitationId: created.invitationId, email: "new@example.test"}})
      // Declining opens and closes no membership interval, so the feed carries no membership event for it.
      expect(await WorkspaceAuditEventModel.countDocuments({action: {$regex: "^membership\\."}})).toBe(membershipEventsBefore)
      // It no longer lists, and cannot be accepted.
      expect(((await (await call("/invitations/mine")).json()) as {invitations: unknown[]}).invitations).toEqual([])
      expect((await call(`/invitations/${created.invitationId}/accept`, {method: "POST"})).status).toBe(404)
    })

    test("declining twice is idempotent and records one event", async () => {
      const ws = await workspaceWithOwner()
      const created = await inviteTo(ws, "new@example.test")
      await person("mu_new", "new@example.test")
      signedInAs("mu_new")
      for (let i = 0; i < 2; i++) {
        const res = await call(`/invitations/${created.invitationId}/decline`, {method: "POST"})
        expect(res.status).toBe(200)
      }
      expect(await WorkspaceAuditEventModel.countDocuments({action: "invitation.declined"})).toBe(1)
    })

    test("works on an expired invitation", async () => {
      const ws = await workspaceWithOwner()
      const created = await inviteTo(ws, "new@example.test")
      await WorkspaceInvitationModel.updateOne(
        {invitationId: created.invitationId},
        {$set: {expiresAt: new Date(Date.now() - 1000)}},
      )
      await person("mu_new", "new@example.test")
      signedInAs("mu_new")
      expect((await call(`/invitations/${created.invitationId}/decline`, {method: "POST"})).status).toBe(200)
    })

    test("is refused for someone else's invitation, and for an unverified email", async () => {
      const ws = await workspaceWithOwner()
      const created = await inviteTo(ws, "new@example.test")
      await person("mu_other", "other@example.test")
      signedInAs("mu_other")
      const other = await call(`/invitations/${created.invitationId}/decline`, {method: "POST"})
      expect(other.status).toBe(403)
      expect(((await other.json()) as {error: string}).error).toBe("email_mismatch")

      await person("mu_new", "new@example.test", false)
      mocks.splice(0).forEach(mock => mock.mockRestore())
      signedInAs("mu_new")
      expect((await call(`/invitations/${created.invitationId}/decline`, {method: "POST"})).status).toBe(403)
      expect((await WorkspaceInvitationModel.findOne({invitationId: created.invitationId}).lean())?.status).toBe("pending")
    })

    test("a revoked invitation is not found", async () => {
      const ws = await workspaceWithOwner()
      const created = await inviteTo(ws, "new@example.test")
      await WorkspaceInvitationModel.updateOne({invitationId: created.invitationId}, {$set: {status: "revoked"}})
      await person("mu_new", "new@example.test")
      signedInAs("mu_new")
      expect((await call(`/invitations/${created.invitationId}/decline`, {method: "POST"})).status).toBe(404)
    })

    test("after declining, the workspace can invite the same address again", async () => {
      const ws = await workspaceWithOwner()
      const first = await inviteTo(ws, "new@example.test")
      await person("mu_new", "new@example.test")
      signedInAs("mu_new")
      await call(`/invitations/${first.invitationId}/decline`, {method: "POST"})
      const second = await inviteTo(ws, "new@example.test")
      expect(second.invitationId).not.toBe(first.invitationId)
      expect(((await (await call("/invitations/mine")).json()) as {invitations: unknown[]}).invitations).toHaveLength(1)
    })
  })
})

describe("POST /leave", () => {
  test("a member leaves: 204, no workspace afterwards, the membership ends as left", async () => {
    const ws = await workspaceWithOwner()
    await person("mu_dev", "dev@example.test")
    await addMember(ws, "mu_dev", "developer")
    signedInAs("mu_dev")
    expect((await call("/leave", {method: "POST"})).status).toBe(204)
    expect(await (await call("/mine")).json()).toEqual({workspace: null})
    expect(await getActiveMembership(ws, "mu_dev")).toBeNull()
    expect(await WorkspaceAuditEventModel.countDocuments({action: "membership.left"})).toBe(1)
  })

  test("the only Owner cannot leave (409 last_owner); another Owner can", async () => {
    const ws = await workspaceWithOwner()
    signedInAs(owner.mentraUserId)
    const refused = await call("/leave", {method: "POST"})
    expect(refused.status).toBe(409)
    expect(((await refused.json()) as {error: string}).error).toBe("last_owner")
    expect(await getActiveMembership(ws, owner.mentraUserId)).not.toBeNull()

    await person("mu_co", "co@example.test")
    await addMember(ws, "mu_co", "owner")
    expect((await call("/leave", {method: "POST"})).status).toBe(204)
  })

  test("a leave naming a workspace leaves it only while the person is in it (409 membership_changed otherwise)", async () => {
    const ws = await workspaceWithOwner()
    await person("mu_dev", "dev@example.test")
    await addMember(ws, "mu_dev", "member")
    signedInAs("mu_dev")
    const mismatch = await call("/leave", {method: "POST", body: JSON.stringify({workspaceId: "ws_other"})})
    expect(mismatch.status).toBe(409)
    expect(((await mismatch.json()) as {error: string}).error).toBe("membership_changed")
    expect(await getActiveMembership(ws, "mu_dev")).not.toBeNull()

    const malformed = await call("/leave", {method: "POST", body: JSON.stringify({workspaceId: 7})})
    expect(malformed.status).toBe(400)
    expect(await getActiveMembership(ws, "mu_dev")).not.toBeNull()

    expect((await call("/leave", {method: "POST", body: JSON.stringify({workspaceId: ws})})).status).toBe(204)
    expect(await getActiveMembership(ws, "mu_dev")).toBeNull()
    const again = await call("/leave", {method: "POST", body: JSON.stringify({workspaceId: ws})})
    expect(again.status).toBe(409)
  })

  test("a person in no workspace gets 404", async () => {
    await person("mu_a", "a@example.test")
    signedInAs("mu_a")
    expect((await call("/leave", {method: "POST"})).status).toBe(404)
  })

  test("leaving, then accepting a new invitation, works", async () => {
    const first = await workspaceWithOwner("First")
    await person("mu_dev", "dev@example.test")
    await addMember(first, "mu_dev", "member")
    const secondOwner: UserActor = {...owner, mentraUserId: "mu_owner2", email: "owner2@example.test"}
    await person(secondOwner.mentraUserId, secondOwner.email)
    const second = (await createWorkspace(secondOwner, {name: "Second"})).workspaceId
    const created = await createInvitation(secondOwner, second, {email: "dev@example.test", role: "member"})
    signedInAs("mu_dev")
    expect((await call("/leave", {method: "POST"})).status).toBe(204)
    const accepted = await call(`/invitations/${created.invitationId}/accept`, {method: "POST"})
    expect(accepted.status).toBe(200)
    expect(await (await call("/mine")).json()).toMatchObject({workspace: {workspaceId: second, name: "Second"}})
  })
})