@sssb/tools 0.5.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sssb/tools",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -346,24 +346,85 @@ index 1794523..9fba9eb 100644
346
346
  }
347
347
  diff --git a/src/app/api/items/route.test.ts b/src/app/api/items/route.test.ts
348
348
  deleted file mode 100644
349
- index d0672c3..0000000
349
+ index 30c1f3b..0000000
350
350
  --- a/src/app/api/items/route.test.ts
351
351
  +++ /dev/null
352
- @@ -1,32 +0,0 @@
352
+ @@ -1,93 +0,0 @@
353
353
  -import { beforeEach, describe, expect, test, vi } from 'vitest'
354
- -import { POST } from './route'
355
- -
356
- -const h = vi.hoisted(() => ({ requireUser: vi.fn(), withTenant: vi.fn() }))
354
+ -import type { AuthUser } from '@sssb/kit/ports/auth'
355
+ -import { GET, POST } from './route'
356
+ -
357
+ -// Real authorize (pure abilities) runs — only the adapters and data modules are faked. Low-entropy
358
+ -// fixture ids/slugs (gitleaks): no hex, no UUIDs.
359
+ -const h = vi.hoisted(() => ({
360
+ - requireUser: vi.fn(),
361
+ - tenantIdForSlug: vi.fn(),
362
+ - orgIdForSlug: vi.fn(),
363
+ - listItems: vi.fn(),
364
+ - withTenant: vi.fn(),
365
+ -}))
357
366
  -vi.mock('@sssb/kit/adapters/index', () => ({ auth: { requireUser: h.requireUser }, db: { withTenant: h.withTenant } }))
367
+ -vi.mock('@sssb/kit/db/tenant-lookup', () => ({ tenantIdForSlug: h.tenantIdForSlug }))
368
+ -vi.mock('@sssb/kit/db/org-lookup', () => ({ orgIdForSlug: h.orgIdForSlug }))
369
+ -vi.mock('@/domain/db/items', () => ({ listItems: h.listItems }))
370
+ -
371
+ -function user(overrides: Partial<AuthUser> = {}): AuthUser {
372
+ - return {
373
+ - id: 'user-ada',
374
+ - name: 'Dana Okoye',
375
+ - email: 'dana.okoye@example.test',
376
+ - role: 'member',
377
+ - locale: 'en',
378
+ - tenantSlug: 'northwind',
379
+ - orgSlug: 'frontline',
380
+ - restricted: false,
381
+ - ...overrides,
382
+ - }
383
+ -}
384
+ -
385
+ -const listRequest = (query = '') => new Request(`http://localhost/api/items${query}`)
358
386
  -
359
387
  -function post(body: string): Promise<Response> {
360
388
  - return POST(new Request('http://localhost/api/items', { method: 'POST', body }))
361
389
  -}
362
390
  -
363
391
  -beforeEach(() => {
364
- - h.requireUser.mockReset()
365
- - h.withTenant.mockReset()
366
- - h.requireUser.mockResolvedValue({ id: 'user-one', tenantSlug: 'acme', orgSlug: 'team', restricted: false })
392
+ - for (const fn of Object.values(h)) fn.mockReset()
393
+ - h.requireUser.mockResolvedValue(user())
394
+ - h.tenantIdForSlug.mockResolvedValue('tenant-northwind')
395
+ - h.orgIdForSlug.mockResolvedValue('org-frontline')
396
+ - h.listItems.mockResolvedValue({ items: [], nextCursor: null })
397
+ -})
398
+ -
399
+ -describe('GET /api/items (list)', () => {
400
+ - test('returns one page of the active org items, tenant- and org-scoped', async () => {
401
+ - h.listItems.mockResolvedValue({ items: [{ id: 'item-1', title: 'First' }], nextCursor: null })
402
+ -
403
+ - const response = await GET(listRequest())
404
+ -
405
+ - expect(response.status).toBe(200)
406
+ - expect(await response.json()).toEqual({ items: [{ id: 'item-1', title: 'First' }], nextCursor: null })
407
+ - expect(h.listItems).toHaveBeenCalledWith(expect.anything(), 'tenant-northwind', 'org-frontline', {
408
+ - after: null,
409
+ - limit: expect.any(Number),
410
+ - })
411
+ - })
412
+ -
413
+ - test('a restricted member may still READ the list (Item read = sameOrg; only writes need !restricted)', async () => {
414
+ - h.requireUser.mockResolvedValue(user({ role: 'restricted', restricted: true }))
415
+ -
416
+ - const response = await GET(listRequest())
417
+ -
418
+ - expect(response.status).toBe(200)
419
+ - expect(await response.json()).toEqual({ items: [], nextCursor: null })
420
+ - })
421
+ -
422
+ - test('403 for a person with the role none: no standing in the org, so no org-scoped read', async () => {
423
+ - h.requireUser.mockResolvedValue(user({ role: 'none', restricted: true }))
424
+ -
425
+ - expect((await GET(listRequest())).status).toBe(403)
426
+ - expect(h.listItems).not.toHaveBeenCalled()
427
+ - })
367
428
  -})
368
429
  -
369
430
  -describe('POST /api/items: a malformed body is a 400, never a 500', () => {
@@ -384,10 +445,10 @@ index d0672c3..0000000
384
445
  -})
385
446
  diff --git a/src/app/api/items/route.ts b/src/app/api/items/route.ts
386
447
  deleted file mode 100644
387
- index f1caa3f..0000000
448
+ index b540297..0000000
388
449
  --- a/src/app/api/items/route.ts
389
450
  +++ /dev/null
390
- @@ -1,59 +0,0 @@
451
+ @@ -1,62 +0,0 @@
391
452
  -import { auth, db } from '@sssb/kit/adapters/index'
392
453
  -import { authorize } from '@sssb/kit/authz/authorize'
393
454
  -import { clampKeysetLimit } from '@sssb/kit/core/keyset'
@@ -417,6 +478,9 @@ index f1caa3f..0000000
417
478
  - if (!tenantId) return Response.json({ error: 'unknown tenant' }, { status: 404 })
418
479
  - const orgId = await orgIdForSlug(db, tenantId, user.orgSlug)
419
480
  - if (!orgId) return Response.json({ error: 'unknown org' }, { status: 404 })
481
+ - // A read is authorized as a write is: being placed in an org is not membership of it (a signed-in person with
482
+ - // the role `none` has no standing there), so the session's org alone never decides what it may read.
483
+ - authorize(user, orgId, 'read', { type: 'Item', orgId })
420
484
  - const { items, nextCursor } = await listItems(db, tenantId, orgId, {
421
485
  - after: cursor.position ?? null,
422
486
  - limit: clampKeysetLimit(params.get('limit')),