|
| 1 | +import { VerifyFunction } from 'passport-oauth2'; |
| 2 | + |
| 3 | +import { DiscordStrategyConfig } from './DiscordStrategyConfig'; |
| 4 | +import DiscordStrategy from './Strategy'; |
| 5 | +import { DiscordPermissionScope, Profile } from './types'; |
| 6 | + |
| 7 | +describe('DiscordStrategy', () => { |
| 8 | + let strategy: DiscordStrategy; |
| 9 | + const verify: VerifyFunction = jest.fn(); |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + const config: DiscordStrategyConfig = { |
| 13 | + clientID: 'test-client-id', |
| 14 | + clientSecret: 'test-client-secret', |
| 15 | + callbackUrl: 'http://localhost:3000/callback', |
| 16 | + scope: [ |
| 17 | + DiscordPermissionScope.Email, |
| 18 | + DiscordPermissionScope.Identify, |
| 19 | + DiscordPermissionScope.Connections, |
| 20 | + // DiscordPermissionScope.Bot, // Not allowed scope |
| 21 | + ], |
| 22 | + prompt: 'consent', |
| 23 | + }; |
| 24 | + |
| 25 | + strategy = new DiscordStrategy(config, verify); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should be defined', () => { |
| 29 | + expect(strategy).toBeDefined(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should have the correct name', () => { |
| 33 | + expect(strategy.name).toBe('discord'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should validate config', async () => { |
| 37 | + const config: DiscordStrategyConfig = { |
| 38 | + clientID: 'test-client-id', |
| 39 | + clientSecret: 'test-client-secret', |
| 40 | + callbackUrl: 'http://localhost:3000/callback', |
| 41 | + scope: [DiscordPermissionScope.Email, DiscordPermissionScope.Identify], |
| 42 | + prompt: 'consent', |
| 43 | + }; |
| 44 | + |
| 45 | + await expect(strategy['validateConfig'](config)).resolves.toBeUndefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should make API request', async () => { |
| 49 | + const mockGet = jest.fn((url, accessToken, callback) => { |
| 50 | + callback(null, JSON.stringify({ id: '123' })); |
| 51 | + }); |
| 52 | + |
| 53 | + strategy['_oauth2'].get = mockGet; |
| 54 | + |
| 55 | + const result = await strategy['makeApiRequest']<{ id: string }>( |
| 56 | + 'https://discord.com/api/users/@me', |
| 57 | + 'test-access-token', |
| 58 | + ); |
| 59 | + |
| 60 | + expect(result).toEqual({ id: '123' }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should fetch user data', async () => { |
| 64 | + const mockMakeApiRequest = jest.fn().mockResolvedValue({ id: '123' }); |
| 65 | + strategy['makeApiRequest'] = mockMakeApiRequest; |
| 66 | + |
| 67 | + const result = await strategy['fetchUserData']('test-access-token'); |
| 68 | + |
| 69 | + expect(result).toEqual({ id: '123' }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should build profile', () => { |
| 73 | + const profileData = { |
| 74 | + id: '123', |
| 75 | + username: 'testuser', |
| 76 | + displayName: 'Test User', |
| 77 | + avatar: 'avatar.png', |
| 78 | + banner: 'banner.png', |
| 79 | + |
| 80 | + verified: true, |
| 81 | + mfa_enabled: true, |
| 82 | + public_flags: 1, |
| 83 | + flags: 1, |
| 84 | + locale: 'en-US', |
| 85 | + global_name: 'testuser#1234', |
| 86 | + premium_type: 1, |
| 87 | + connections: [], |
| 88 | + guilds: [], |
| 89 | + } as unknown as Profile; |
| 90 | + |
| 91 | + const profile = strategy['buildProfile'](profileData, 'test-access-token'); |
| 92 | + |
| 93 | + expect(profile).toMatchObject({ |
| 94 | + provider: 'discord', |
| 95 | + id: '123', |
| 96 | + username: 'testuser', |
| 97 | + displayName: 'Test User', |
| 98 | + avatar: 'avatar.png', |
| 99 | + banner: 'banner.png', |
| 100 | + |
| 101 | + verified: true, |
| 102 | + mfa_enabled: true, |
| 103 | + public_flags: 1, |
| 104 | + flags: 1, |
| 105 | + locale: 'en-US', |
| 106 | + global_name: 'testuser#1234', |
| 107 | + premium_type: 1, |
| 108 | + connections: [], |
| 109 | + guilds: [], |
| 110 | + access_token: 'test-access-token', |
| 111 | + fetchedAt: expect.any(Date), |
| 112 | + createdAt: expect.any(Date), |
| 113 | + _raw: JSON.stringify(profileData), |
| 114 | + _json: profileData, |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should fetch scope data', async () => { |
| 119 | + const mockMakeApiRequest = jest.fn().mockResolvedValue([{ id: '123' }]); |
| 120 | + strategy['makeApiRequest'] = mockMakeApiRequest; |
| 121 | + |
| 122 | + const result = await strategy['fetchScopeData']( |
| 123 | + DiscordPermissionScope.Connections, |
| 124 | + 'test-access-token', |
| 125 | + ); |
| 126 | + |
| 127 | + expect(result).toEqual([{ id: '123' }]); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should no fetch out of scope data', async () => { |
| 131 | + const mockMakeApiRequest = jest.fn().mockResolvedValue([{ id: '123' }]); |
| 132 | + strategy['makeApiRequest'] = mockMakeApiRequest; |
| 133 | + |
| 134 | + const result = await strategy['fetchScopeData']( |
| 135 | + DiscordPermissionScope.Bot, |
| 136 | + 'test-access-token', |
| 137 | + ); |
| 138 | + |
| 139 | + expect(result).toEqual(null); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should enrich profile with scopes', async () => { |
| 143 | + const profile = { |
| 144 | + id: '123', |
| 145 | + connections: [], |
| 146 | + guilds: [], |
| 147 | + } as unknown as Profile; |
| 148 | + |
| 149 | + const mockFetchScopeData = jest |
| 150 | + .fn() |
| 151 | + .mockResolvedValueOnce([{ id: 'connection1' }]) |
| 152 | + .mockResolvedValueOnce([{ id: 'guild1' }]); |
| 153 | + |
| 154 | + strategy['fetchScopeData'] = mockFetchScopeData; |
| 155 | + |
| 156 | + await strategy['enrichProfileWithScopes'](profile, 'test-access-token'); |
| 157 | + |
| 158 | + expect(profile.connections).toEqual([{ id: 'connection1' }]); |
| 159 | + expect(profile.guilds).toEqual([{ id: 'guild1' }]); |
| 160 | + expect(profile.fetchedAt).toBeInstanceOf(Date); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should calculate creation date', () => { |
| 164 | + const id = '123456789012345678'; |
| 165 | + const date = strategy['calculateCreationDate'](id); |
| 166 | + |
| 167 | + expect(date).toBeInstanceOf(Date); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should return authorization params', () => { |
| 171 | + const options = { prompt: 'consent' }; |
| 172 | + const params = strategy.authorizationParams(options); |
| 173 | + |
| 174 | + expect(params).toMatchObject({ |
| 175 | + prompt: 'consent', |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments