|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { |
| 3 | + ATPROTO_SCOPE, |
| 4 | + TRANSITION_SCOPES, |
| 5 | + TransitionScopeSchema, |
| 6 | + AccountAttrSchema, |
| 7 | + AccountActionSchema, |
| 8 | + RepoActionSchema, |
| 9 | + IdentityAttrSchema, |
| 10 | + MimeTypeSchema, |
| 11 | + NsidSchema, |
| 12 | +} from "../../src/auth/permissions.js"; |
| 13 | + |
| 14 | +describe("Permission Constants", () => { |
| 15 | + it("should export ATPROTO_SCOPE constant", () => { |
| 16 | + expect(ATPROTO_SCOPE).toBe("atproto"); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should export TRANSITION_SCOPES with correct values", () => { |
| 20 | + expect(TRANSITION_SCOPES.GENERIC).toBe("transition:generic"); |
| 21 | + expect(TRANSITION_SCOPES.CHAT).toBe("transition:chat.bsky"); |
| 22 | + expect(TRANSITION_SCOPES.EMAIL).toBe("transition:email"); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe("TransitionScopeSchema", () => { |
| 27 | + it("should accept valid transitional scopes", () => { |
| 28 | + expect(TransitionScopeSchema.parse("transition:generic")).toBe("transition:generic"); |
| 29 | + expect(TransitionScopeSchema.parse("transition:chat.bsky")).toBe("transition:chat.bsky"); |
| 30 | + expect(TransitionScopeSchema.parse("transition:email")).toBe("transition:email"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should reject invalid transitional scopes", () => { |
| 34 | + expect(() => TransitionScopeSchema.parse("atproto")).toThrow(); |
| 35 | + expect(() => TransitionScopeSchema.parse("transition:invalid")).toThrow(); |
| 36 | + expect(() => TransitionScopeSchema.parse("invalid")).toThrow(); |
| 37 | + expect(() => TransitionScopeSchema.parse("")).toThrow(); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("AccountAttrSchema", () => { |
| 42 | + it("should accept valid account attributes", () => { |
| 43 | + expect(AccountAttrSchema.parse("email")).toBe("email"); |
| 44 | + expect(AccountAttrSchema.parse("repo")).toBe("repo"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should reject invalid account attributes", () => { |
| 48 | + expect(() => AccountAttrSchema.parse("invalid")).toThrow(); |
| 49 | + expect(() => AccountAttrSchema.parse("handle")).toThrow(); |
| 50 | + expect(() => AccountAttrSchema.parse("")).toThrow(); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe("AccountActionSchema", () => { |
| 55 | + it("should accept valid account actions", () => { |
| 56 | + expect(AccountActionSchema.parse("read")).toBe("read"); |
| 57 | + expect(AccountActionSchema.parse("manage")).toBe("manage"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("should reject invalid account actions", () => { |
| 61 | + expect(() => AccountActionSchema.parse("create")).toThrow(); |
| 62 | + expect(() => AccountActionSchema.parse("delete")).toThrow(); |
| 63 | + expect(() => AccountActionSchema.parse("invalid")).toThrow(); |
| 64 | + expect(() => AccountActionSchema.parse("")).toThrow(); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("RepoActionSchema", () => { |
| 69 | + it("should accept valid repository actions", () => { |
| 70 | + expect(RepoActionSchema.parse("create")).toBe("create"); |
| 71 | + expect(RepoActionSchema.parse("update")).toBe("update"); |
| 72 | + expect(RepoActionSchema.parse("delete")).toBe("delete"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should reject invalid repository actions", () => { |
| 76 | + expect(() => RepoActionSchema.parse("read")).toThrow(); |
| 77 | + expect(() => RepoActionSchema.parse("manage")).toThrow(); |
| 78 | + expect(() => RepoActionSchema.parse("invalid")).toThrow(); |
| 79 | + expect(() => RepoActionSchema.parse("")).toThrow(); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe("IdentityAttrSchema", () => { |
| 84 | + it("should accept valid identity attributes", () => { |
| 85 | + expect(IdentityAttrSchema.parse("handle")).toBe("handle"); |
| 86 | + expect(IdentityAttrSchema.parse("*")).toBe("*"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should reject invalid identity attributes", () => { |
| 90 | + expect(() => IdentityAttrSchema.parse("email")).toThrow(); |
| 91 | + expect(() => IdentityAttrSchema.parse("repo")).toThrow(); |
| 92 | + expect(() => IdentityAttrSchema.parse("invalid")).toThrow(); |
| 93 | + expect(() => IdentityAttrSchema.parse("")).toThrow(); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("MimeTypeSchema", () => { |
| 98 | + it("should accept valid MIME type patterns", () => { |
| 99 | + expect(MimeTypeSchema.parse("image/*")).toBe("image/*"); |
| 100 | + expect(MimeTypeSchema.parse("video/*")).toBe("video/*"); |
| 101 | + expect(MimeTypeSchema.parse("audio/*")).toBe("audio/*"); |
| 102 | + expect(MimeTypeSchema.parse("text/html")).toBe("text/html"); |
| 103 | + expect(MimeTypeSchema.parse("application/json")).toBe("application/json"); |
| 104 | + expect(MimeTypeSchema.parse("image/png")).toBe("image/png"); |
| 105 | + expect(MimeTypeSchema.parse("video/mp4")).toBe("video/mp4"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should accept MIME types with numbers and hyphens", () => { |
| 109 | + expect(MimeTypeSchema.parse("application/json-ld")).toBe("application/json-ld"); |
| 110 | + expect(MimeTypeSchema.parse("application/ld+json")).toBe("application/ld+json"); |
| 111 | + expect(MimeTypeSchema.parse("image/svg+xml")).toBe("image/svg+xml"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should reject invalid MIME type patterns", () => { |
| 115 | + expect(() => MimeTypeSchema.parse("invalid")).toThrow(); |
| 116 | + expect(() => MimeTypeSchema.parse("image")).toThrow(); |
| 117 | + expect(() => MimeTypeSchema.parse("/png")).toThrow(); |
| 118 | + expect(() => MimeTypeSchema.parse("image/")).toThrow(); |
| 119 | + expect(() => MimeTypeSchema.parse("")).toThrow(); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should accept MIME types in any case (case-insensitive)", () => { |
| 123 | + // MIME types are case-insensitive per RFC 2045 |
| 124 | + expect(MimeTypeSchema.parse("IMAGE/*")).toBe("IMAGE/*"); |
| 125 | + expect(MimeTypeSchema.parse("Image/Png")).toBe("Image/Png"); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should provide helpful error message for invalid MIME types", () => { |
| 129 | + try { |
| 130 | + MimeTypeSchema.parse("invalid"); |
| 131 | + expect.fail("Should have thrown"); |
| 132 | + } catch (error) { |
| 133 | + const zodError = error as { issues: Array<{ message: string }> }; |
| 134 | + expect(zodError.issues[0].message).toContain("Invalid MIME type pattern"); |
| 135 | + expect(zodError.issues[0].message).toContain("type/subtype"); |
| 136 | + } |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +describe("NsidSchema", () => { |
| 141 | + it("should accept valid NSID formats", () => { |
| 142 | + expect(NsidSchema.parse("app.bsky.feed.post")).toBe("app.bsky.feed.post"); |
| 143 | + expect(NsidSchema.parse("com.example.myrecord")).toBe("com.example.myrecord"); |
| 144 | + expect(NsidSchema.parse("org.example.test.nested.record")).toBe("org.example.test.nested.record"); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should accept NSIDs with hyphens", () => { |
| 148 | + expect(NsidSchema.parse("com.example-app.record")).toBe("com.example-app.record"); |
| 149 | + expect(NsidSchema.parse("app.my-test.record")).toBe("app.my-test.record"); |
| 150 | + }); |
| 151 | + |
| 152 | + it("should reject invalid NSID formats", () => { |
| 153 | + expect(() => NsidSchema.parse("InvalidNSID")).toThrow(); |
| 154 | + expect(() => NsidSchema.parse("example")).toThrow(); // Need at least one dot |
| 155 | + expect(() => NsidSchema.parse(".example.com")).toThrow(); // Can't start with dot |
| 156 | + expect(() => NsidSchema.parse("example.com.")).toThrow(); // Can't end with dot |
| 157 | + expect(() => NsidSchema.parse("Example.com")).toThrow(); // Must be lowercase |
| 158 | + expect(() => NsidSchema.parse("example..com")).toThrow(); // No consecutive dots |
| 159 | + expect(() => NsidSchema.parse("")).toThrow(); |
| 160 | + }); |
| 161 | + |
| 162 | + it("should provide helpful error message for invalid NSIDs", () => { |
| 163 | + try { |
| 164 | + NsidSchema.parse("InvalidNSID"); |
| 165 | + expect.fail("Should have thrown"); |
| 166 | + } catch (error) { |
| 167 | + const zodError = error as { issues: Array<{ message: string }> }; |
| 168 | + expect(zodError.issues[0].message).toContain("Invalid NSID format"); |
| 169 | + expect(zodError.issues[0].message).toContain("reverse-DNS"); |
| 170 | + } |
| 171 | + }); |
| 172 | +}); |
0 commit comments