Skip to content

Commit 10d78e3

Browse files
committed
test: add unit tests for DiscordStrategy functionality and configuration validation
1 parent 5c2cade commit 10d78e3

File tree

1 file changed

+160
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)