|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { tokenCreatorHasCollectionAccess } from "./access-token-runtime"; |
| 3 | + |
| 4 | +const PREV_OWNER_EMAILS = process.env.OWNER_EMAILS; |
| 5 | + |
| 6 | +describe("tokenCreatorHasCollectionAccess", () => { |
| 7 | + beforeEach(() => { |
| 8 | + process.env.OWNER_EMAILS = "owner@example.com"; |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + if (PREV_OWNER_EMAILS === undefined) delete process.env.OWNER_EMAILS; |
| 13 | + else process.env.OWNER_EMAILS = PREV_OWNER_EMAILS; |
| 14 | + }); |
| 15 | + |
| 16 | + it("allows tokens created by owners", () => { |
| 17 | + expect( |
| 18 | + tokenCreatorHasCollectionAccess({ |
| 19 | + creatorUserId: "user_a", |
| 20 | + creatorEmail: "owner@example.com", |
| 21 | + collectionCreatedById: "user_b", |
| 22 | + hasDirectGrant: false, |
| 23 | + }), |
| 24 | + ).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it("allows tokens created by the collection creator", () => { |
| 28 | + expect( |
| 29 | + tokenCreatorHasCollectionAccess({ |
| 30 | + creatorUserId: "user_a", |
| 31 | + creatorEmail: "user@example.com", |
| 32 | + collectionCreatedById: "user_a", |
| 33 | + hasDirectGrant: false, |
| 34 | + }), |
| 35 | + ).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it("allows tokens while a direct grant exists", () => { |
| 39 | + expect( |
| 40 | + tokenCreatorHasCollectionAccess({ |
| 41 | + creatorUserId: "user_a", |
| 42 | + creatorEmail: "user@example.com", |
| 43 | + collectionCreatedById: "user_b", |
| 44 | + hasDirectGrant: true, |
| 45 | + }), |
| 46 | + ).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it("denies tokens after creator access is revoked", () => { |
| 50 | + expect( |
| 51 | + tokenCreatorHasCollectionAccess({ |
| 52 | + creatorUserId: "user_a", |
| 53 | + creatorEmail: "user@example.com", |
| 54 | + collectionCreatedById: "user_b", |
| 55 | + hasDirectGrant: false, |
| 56 | + }), |
| 57 | + ).toBe(false); |
| 58 | + }); |
| 59 | +}); |
0 commit comments