|
| 1 | +import { WorkOS } from './index.public'; |
| 2 | + |
| 3 | +describe('WorkOS Public API', () => { |
| 4 | + let workosPublic: WorkOS; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + workosPublic = new WorkOS({ |
| 8 | + clientId: 'client_123', |
| 9 | + apiHostname: 'api.workos.dev', |
| 10 | + }); |
| 11 | + }); |
| 12 | + |
| 13 | + describe('instantiation', () => { |
| 14 | + it('should instantiate without requiring an API key', () => { |
| 15 | + expect(() => new WorkOS()).not.toThrow(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should accept public configuration options', () => { |
| 19 | + const client = new WorkOS({ |
| 20 | + clientId: 'test_client', |
| 21 | + apiHostname: 'test.api.com', |
| 22 | + https: false, |
| 23 | + port: 3000, |
| 24 | + }); |
| 25 | + |
| 26 | + expect(client).toBeDefined(); |
| 27 | + expect(client.version).toBeDefined(); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('exposed services', () => { |
| 32 | + it('should expose webhooks service', () => { |
| 33 | + expect(workosPublic.webhooks).toBeDefined(); |
| 34 | + expect(workosPublic.webhooks.verifyHeader).toBeDefined(); |
| 35 | + expect(workosPublic.webhooks.computeSignature).toBeDefined(); |
| 36 | + expect(workosPublic.webhooks.constructEvent).toBeDefined(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should expose actions service', () => { |
| 40 | + expect(workosPublic.actions).toBeDefined(); |
| 41 | + expect(workosPublic.actions.verifyHeader).toBeDefined(); |
| 42 | + expect(workosPublic.actions.signResponse).toBeDefined(); |
| 43 | + expect(workosPublic.actions.constructAction).toBeDefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should expose client-safe user management methods', () => { |
| 47 | + expect(workosPublic.userManagement).toBeDefined(); |
| 48 | + expect( |
| 49 | + workosPublic.userManagement.authenticateWithCodeAndVerifier, |
| 50 | + ).toBeDefined(); |
| 51 | + expect(workosPublic.userManagement.getAuthorizationUrl).toBeDefined(); |
| 52 | + expect(workosPublic.userManagement.getLogoutUrl).toBeDefined(); |
| 53 | + expect(workosPublic.userManagement.getJwksUrl).toBeDefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should expose client-safe SSO methods', () => { |
| 57 | + expect(workosPublic.sso).toBeDefined(); |
| 58 | + expect(workosPublic.sso.getAuthorizationUrl).toBeDefined(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should expose version', () => { |
| 62 | + expect(workosPublic.version).toBeDefined(); |
| 63 | + expect(typeof workosPublic.version).toBe('string'); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('method behavior', () => { |
| 68 | + it('should be able to call URL generation methods', () => { |
| 69 | + const authUrl = workosPublic.userManagement.getAuthorizationUrl({ |
| 70 | + clientId: 'client_123', |
| 71 | + redirectUri: 'https://example.com/callback', |
| 72 | + provider: 'GoogleOAuth', |
| 73 | + }); |
| 74 | + |
| 75 | + expect(authUrl).toContain('api.workos.dev'); |
| 76 | + expect(authUrl).toContain('client_id=client_123'); |
| 77 | + expect(authUrl).toContain('provider=GoogleOAuth'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should be able to call JWKS URL generation', () => { |
| 81 | + const jwksUrl = workosPublic.userManagement.getJwksUrl('client_123'); |
| 82 | + expect(jwksUrl).toBe('https://api.workos.dev/sso/jwks/client_123'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should be able to call logout URL generation', () => { |
| 86 | + const logoutUrl = workosPublic.userManagement.getLogoutUrl({ |
| 87 | + sessionId: 'session_123', |
| 88 | + returnTo: 'https://example.com', |
| 89 | + }); |
| 90 | + |
| 91 | + expect(logoutUrl).toContain('api.workos.dev'); |
| 92 | + expect(logoutUrl).toContain('session_id=session_123'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should be able to call SSO authorization URL generation', () => { |
| 96 | + const authUrl = workosPublic.sso.getAuthorizationUrl({ |
| 97 | + clientId: 'client_123', |
| 98 | + redirectUri: 'https://example.com/callback', |
| 99 | + provider: 'GoogleOAuth', |
| 100 | + }); |
| 101 | + |
| 102 | + expect(authUrl).toContain('api.workos.dev'); |
| 103 | + expect(authUrl).toContain('client_id=client_123'); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('server-only methods should not be exposed', () => { |
| 108 | + it('should not expose getUser on userManagement', () => { |
| 109 | + expect((workosPublic.userManagement as any).getUser).toBeUndefined(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should not expose createUser on userManagement', () => { |
| 113 | + expect((workosPublic.userManagement as any).createUser).toBeUndefined(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should not expose listUsers on userManagement', () => { |
| 117 | + expect((workosPublic.userManagement as any).listUsers).toBeUndefined(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should not expose updateUser on userManagement', () => { |
| 121 | + expect((workosPublic.userManagement as any).updateUser).toBeUndefined(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should not expose server-only authentication methods', () => { |
| 125 | + expect( |
| 126 | + (workosPublic.userManagement as any).authenticateWithPassword, |
| 127 | + ).toBeUndefined(); |
| 128 | + expect( |
| 129 | + (workosPublic.userManagement as any).authenticateWithMagicAuth, |
| 130 | + ).toBeUndefined(); |
| 131 | + expect( |
| 132 | + (workosPublic.userManagement as any).authenticateWithRefreshToken, |
| 133 | + ).toBeUndefined(); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('type safety', () => { |
| 138 | + it('should provide proper TypeScript types for exposed methods', () => { |
| 139 | + // These should compile without errors |
| 140 | + const authUrl: string = workosPublic.userManagement.getAuthorizationUrl({ |
| 141 | + clientId: 'test', |
| 142 | + redirectUri: 'https://example.com', |
| 143 | + provider: 'GoogleOAuth', |
| 144 | + }); |
| 145 | + |
| 146 | + const jwksUrl: string = |
| 147 | + workosPublic.userManagement.getJwksUrl('client_id'); |
| 148 | + |
| 149 | + expect(typeof authUrl).toBe('string'); |
| 150 | + expect(typeof jwksUrl).toBe('string'); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments