@stonyx/oauth 0.1.1-beta.17 → 0.1.1-beta.171

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +72 -0
  2. package/dist/auth-request.d.ts +97 -0
  3. package/dist/auth-request.js +187 -0
  4. package/dist/main.d.ts +106 -0
  5. package/dist/main.js +177 -0
  6. package/dist/oauth-flow.d.ts +30 -0
  7. package/dist/oauth-flow.js +83 -0
  8. package/dist/providers/discord.d.ts +30 -0
  9. package/dist/providers/discord.js +43 -0
  10. package/dist/session-manager.d.ts +20 -0
  11. package/dist/session-manager.js +30 -0
  12. package/dist/token-manager.d.ts +15 -0
  13. package/dist/token-manager.js +24 -0
  14. package/package.json +45 -9
  15. package/src/auth-request.ts +271 -0
  16. package/src/main.ts +238 -0
  17. package/src/{oauth-flow.js → oauth-flow.ts} +31 -7
  18. package/src/providers/{discord.js → discord.ts} +29 -3
  19. package/src/{session-manager.js → session-manager.ts} +19 -6
  20. package/src/token-manager.ts +35 -0
  21. package/src/types/node.d.ts +19 -0
  22. package/src/types/stonyx-events.d.ts +4 -0
  23. package/src/types/stonyx-rest-server.d.ts +11 -0
  24. package/src/types/stonyx.d.ts +38 -0
  25. package/.github/workflows/ci.yml +0 -16
  26. package/.github/workflows/publish.yml +0 -51
  27. package/src/auth-request.js +0 -74
  28. package/src/main.js +0 -83
  29. package/src/token-manager.js +0 -26
  30. package/test/config/environment.js +0 -18
  31. package/test/integration/oauth-test.js +0 -149
  32. package/test/sample/providers/mock.js +0 -40
  33. package/test/sample/requests/.gitkeep +0 -0
  34. package/test/unit/oauth-flow-test.js +0 -137
  35. package/test/unit/providers/discord-test.js +0 -115
  36. package/test/unit/session-manager-test.js +0 -85
  37. package/test/unit/state-validation-test.js +0 -118
  38. package/test/unit/token-manager-test.js +0 -76
package/src/main.js DELETED
@@ -1,83 +0,0 @@
1
- import config from 'stonyx/config';
2
- import log from 'stonyx/log';
3
- import { waitForModule } from 'stonyx';
4
- import { setup, emit } from '@stonyx/events';
5
- import RestServer from '@stonyx/rest-server';
6
- import TokenManager from './token-manager.js';
7
- import SessionManager from './session-manager.js';
8
- import AuthRequest from './auth-request.js';
9
-
10
- setup(['authenticate']);
11
-
12
- export default class OAuth {
13
- providers = new Map();
14
- pendingStates = new Map();
15
-
16
- constructor() {
17
- if (OAuth.instance) return OAuth.instance;
18
- OAuth.instance = this;
19
- }
20
-
21
- async init() {
22
- const { providers, sessionDuration, frontendCallbackUrl } = config.oauth;
23
- this.frontendCallbackUrl = frontendCallbackUrl;
24
-
25
- for (const [name, providerConfig] of Object.entries(providers)) {
26
- const modulePath = providerConfig.module
27
- ? `${config.rootPath}/${providerConfig.module}`
28
- : `./providers/${name}.js`;
29
- const { default: Provider } = await import(modulePath);
30
- const flow = new Provider(providerConfig);
31
- this.providers.set(name, { flow, tokenManager: new TokenManager(flow) });
32
- }
33
-
34
- this.sessionManager = new SessionManager(sessionDuration);
35
-
36
- await waitForModule('rest-server');
37
- RestServer.instance.mountRoute(AuthRequest, { name: 'auth', options: this });
38
-
39
- log.oauth?.('OAuth module initialized');
40
- }
41
-
42
- getProvider(name) {
43
- const provider = this.providers.get(name);
44
- if (!provider) throw new Error(`OAuth provider "${name}" is not configured`);
45
- return provider;
46
- }
47
-
48
- getAuthorizationUrl(providerName) {
49
- const { flow } = this.getProvider(providerName);
50
- const stateToken = crypto.randomUUID();
51
- this.pendingStates.set(stateToken, Date.now());
52
- return flow.buildAuthorizationUrl(stateToken);
53
- }
54
-
55
- async handleCallback(providerName, code, stateToken) {
56
- if (!stateToken || !this.pendingStates.has(stateToken)) {
57
- throw new Error('Invalid or missing state token');
58
- }
59
-
60
- const stateCreatedAt = this.pendingStates.get(stateToken);
61
- this.pendingStates.delete(stateToken);
62
-
63
- const TEN_MINUTES = 10 * 60 * 1000;
64
- if (Date.now() - stateCreatedAt > TEN_MINUTES) {
65
- throw new Error('State token has expired');
66
- }
67
-
68
- const { flow, tokenManager } = this.getProvider(providerName);
69
- const tokens = await tokenManager.getTokens(code);
70
- const rawUser = await flow.fetchUserInfo(tokens.accessToken);
71
- const user = flow.normalizeUser(rawUser);
72
- await emit('authenticate', user);
73
- return this.sessionManager.create(user, tokens);
74
- }
75
-
76
- getSession(sessionId) {
77
- return this.sessionManager.validate(sessionId);
78
- }
79
-
80
- logout(sessionId) {
81
- this.sessionManager.destroy(sessionId);
82
- }
83
- }
@@ -1,26 +0,0 @@
1
- export default class TokenManager {
2
- constructor(flow) {
3
- this.flow = flow;
4
- }
5
-
6
- async getTokens(code) {
7
- const tokens = await this.flow.exchangeCode(code);
8
- tokens.expiresAt = Date.now() + (tokens.expiresIn * 1000);
9
- return tokens;
10
- }
11
-
12
- async refresh(refreshToken) {
13
- const tokens = await this.flow.refreshAccessToken(refreshToken);
14
- tokens.expiresAt = Date.now() + (tokens.expiresIn * 1000);
15
- return tokens;
16
- }
17
-
18
- async revoke(accessToken) {
19
- return this.flow.revokeToken(accessToken);
20
- }
21
-
22
- isExpired(tokenData) {
23
- if (!tokenData?.expiresAt) return true;
24
- return Date.now() >= tokenData.expiresAt;
25
- }
26
- }
@@ -1,18 +0,0 @@
1
- export default {
2
- restServer: {
3
- dir: './test/sample/requests',
4
- },
5
- oauth: {
6
- providers: {
7
- mock: {
8
- clientId: 'test-client-id',
9
- clientSecret: 'test-client-secret',
10
- redirectUri: 'http://localhost:2666/auth/callback/mock',
11
- scopes: ['identify'],
12
- module: './test/sample/providers/mock.js',
13
- }
14
- },
15
- sessionDuration: 3600,
16
- frontendCallbackUrl: 'http://localhost:4200/auth/callback',
17
- }
18
- };
@@ -1,149 +0,0 @@
1
- import QUnit from 'qunit';
2
- import RestServer from '@stonyx/rest-server';
3
- import config from 'stonyx/config';
4
- import { setupIntegrationTests } from 'stonyx/test-helpers';
5
- import OAuth from '../../src/main.js';
6
-
7
- const { module, test } = QUnit;
8
- let endpoint;
9
-
10
- async function getValidState(endpoint) {
11
- const loginResponse = await fetch(`${endpoint}/auth/login/mock`, { redirect: 'manual' });
12
- const location = loginResponse.headers.get('location');
13
- const url = new URL(location);
14
- return url.searchParams.get('state');
15
- }
16
-
17
- module('[Integration] OAuth', function(hooks) {
18
- setupIntegrationTests(hooks);
19
-
20
- hooks.before(function() {
21
- endpoint = `http://localhost:${config.restServer.port}`;
22
- });
23
-
24
- hooks.after(function() {
25
- RestServer.close();
26
- });
27
-
28
- test('GET /auth/login/mock redirects to provider auth URL', async function(assert) {
29
- const response = await fetch(`${endpoint}/auth/login/mock`, { redirect: 'manual' });
30
-
31
- assert.equal(response.status, 302);
32
-
33
- const location = response.headers.get('location');
34
- assert.ok(location.startsWith('https://mock.provider/oauth/authorize?'));
35
- assert.ok(location.includes('client_id=test-client-id'));
36
- assert.ok(location.includes('response_type=code'));
37
- });
38
-
39
- test('GET /auth/login/nonexistent returns 404', async function(assert) {
40
- const response = await fetch(`${endpoint}/auth/login/nonexistent`, { redirect: 'manual' });
41
-
42
- assert.equal(response.status, 404);
43
- });
44
-
45
- test('GET /auth/callback/mock with valid state redirects to frontend with session', async function(assert) {
46
- const stateToken = await getValidState(endpoint);
47
- const response = await fetch(`${endpoint}/auth/callback/mock?code=test-auth-code&state=${stateToken}`, { redirect: 'manual' });
48
-
49
- assert.equal(response.status, 302);
50
-
51
- const location = response.headers.get('location');
52
- const redirectUrl = new URL(location);
53
- assert.equal(redirectUrl.origin + redirectUrl.pathname, 'http://localhost:4200/auth/callback');
54
- assert.ok(redirectUrl.searchParams.get('sessionId'), 'redirect includes sessionId');
55
- assert.ok(redirectUrl.searchParams.get('expiresAt'), 'redirect includes expiresAt');
56
- });
57
-
58
- test('GET /auth with valid session returns user', async function(assert) {
59
- const stateToken = await getValidState(endpoint);
60
- const callbackResponse = await fetch(`${endpoint}/auth/callback/mock?code=test-code&state=${stateToken}`, { redirect: 'manual' });
61
- const location = callbackResponse.headers.get('location');
62
- const sessionId = new URL(location).searchParams.get('sessionId');
63
-
64
- const response = await fetch(`${endpoint}/auth`, {
65
- headers: { 'session-id': sessionId },
66
- });
67
- const data = await response.json();
68
-
69
- assert.equal(response.status, 200);
70
- assert.equal(data.id, 'mock-user-123');
71
- });
72
-
73
- test('GET /auth without session returns 401', async function(assert) {
74
- const response = await fetch(`${endpoint}/auth`);
75
-
76
- assert.equal(response.status, 401);
77
- });
78
-
79
- test('GET /auth with invalid session returns 401', async function(assert) {
80
- const response = await fetch(`${endpoint}/auth`, {
81
- headers: { 'session-id': 'invalid-session' },
82
- });
83
-
84
- assert.equal(response.status, 401);
85
- });
86
-
87
- test('GET /auth/logout invalidates session', async function(assert) {
88
- const stateToken = await getValidState(endpoint);
89
- const callbackResponse = await fetch(`${endpoint}/auth/callback/mock?code=test-code&state=${stateToken}`, { redirect: 'manual' });
90
- const location = callbackResponse.headers.get('location');
91
- const sessionId = new URL(location).searchParams.get('sessionId');
92
-
93
- // Logout
94
- const logoutResponse = await fetch(`${endpoint}/auth/logout`, {
95
- headers: { 'session-id': sessionId },
96
- });
97
- assert.equal(logoutResponse.status, 200);
98
-
99
- // Verify session is invalid
100
- const authResponse = await fetch(`${endpoint}/auth`, {
101
- headers: { 'session-id': sessionId },
102
- });
103
- assert.equal(authResponse.status, 401);
104
- });
105
-
106
- test('GET /auth/callback/mock rejects missing state token', async function(assert) {
107
- const response = await fetch(`${endpoint}/auth/callback/mock?code=test-auth-code`, { redirect: 'manual' });
108
-
109
- assert.equal(response.status, 302);
110
- const location = response.headers.get('location');
111
- const redirectUrl = new URL(location);
112
- assert.equal(redirectUrl.searchParams.get('error'), 'auth_failed');
113
- });
114
-
115
- test('GET /auth/callback/mock rejects invalid state token', async function(assert) {
116
- const response = await fetch(`${endpoint}/auth/callback/mock?code=test-auth-code&state=bogus-state`, { redirect: 'manual' });
117
-
118
- assert.equal(response.status, 302);
119
- const location = response.headers.get('location');
120
- const redirectUrl = new URL(location);
121
- assert.equal(redirectUrl.searchParams.get('error'), 'auth_failed');
122
- });
123
-
124
- test('GET /auth/callback/mock with error param redirects with error', async function(assert) {
125
- const response = await fetch(`${endpoint}/auth/callback/mock?error=access_denied`, { redirect: 'manual' });
126
-
127
- assert.equal(response.status, 302);
128
- const location = response.headers.get('location');
129
- const redirectUrl = new URL(location);
130
- assert.equal(redirectUrl.origin + redirectUrl.pathname, 'http://localhost:4200/auth/callback');
131
- assert.equal(redirectUrl.searchParams.get('error'), 'access_denied');
132
- });
133
-
134
- test('GET /auth/callback/mock state token cannot be reused', async function(assert) {
135
- const stateToken = await getValidState(endpoint);
136
-
137
- // First use succeeds
138
- const first = await fetch(`${endpoint}/auth/callback/mock?code=test-code&state=${stateToken}`, { redirect: 'manual' });
139
- assert.equal(first.status, 302);
140
- const firstLocation = new URL(first.headers.get('location'));
141
- assert.ok(firstLocation.searchParams.get('sessionId'), 'first use succeeds');
142
-
143
- // Second use fails
144
- const second = await fetch(`${endpoint}/auth/callback/mock?code=test-code&state=${stateToken}`, { redirect: 'manual' });
145
- assert.equal(second.status, 302);
146
- const secondLocation = new URL(second.headers.get('location'));
147
- assert.equal(secondLocation.searchParams.get('error'), 'auth_failed', 'reuse is rejected');
148
- });
149
- });
@@ -1,40 +0,0 @@
1
- import OAuthFlow from '../../../src/oauth-flow.js';
2
-
3
- export default class MockProvider extends OAuthFlow {
4
- constructor(config) {
5
- super({
6
- ...config,
7
- authorizationUrl: 'https://mock.provider/oauth/authorize',
8
- tokenUrl: 'https://mock.provider/oauth/token',
9
- userInfoUrl: 'https://mock.provider/api/me',
10
- });
11
- }
12
-
13
- async exchangeCode(_code) {
14
- return {
15
- accessToken: 'mock-access-token',
16
- refreshToken: 'mock-refresh-token',
17
- expiresIn: 3600,
18
- };
19
- }
20
-
21
- async fetchUserInfo(_accessToken) {
22
- return {
23
- id: 'mock-user-123',
24
- username: 'mockuser',
25
- displayName: 'Mock User',
26
- email: 'mock@test.com',
27
- };
28
- }
29
-
30
- normalizeUser(rawUser) {
31
- return {
32
- id: rawUser.id,
33
- username: rawUser.username,
34
- displayName: rawUser.displayName,
35
- avatar: null,
36
- email: rawUser.email,
37
- raw: rawUser,
38
- };
39
- }
40
- }
File without changes
@@ -1,137 +0,0 @@
1
- import QUnit from 'qunit';
2
- import OAuthFlow from '../../src/oauth-flow.js';
3
-
4
- const { module, test } = QUnit;
5
-
6
- const defaultConfig = {
7
- clientId: 'test-client',
8
- clientSecret: 'test-secret',
9
- redirectUri: 'http://localhost/callback',
10
- scopes: ['read', 'write'],
11
- authorizationUrl: 'https://provider.com/oauth/authorize',
12
- tokenUrl: 'https://provider.com/oauth/token',
13
- userInfoUrl: 'https://provider.com/api/me',
14
- };
15
-
16
- module('[Unit] OAuthFlow', function() {
17
- module('buildAuthorizationUrl', function() {
18
- test('generates a valid authorization URL with all params', function(assert) {
19
- const flow = new OAuthFlow(defaultConfig);
20
- const url = flow.buildAuthorizationUrl('test-state-123');
21
-
22
- assert.ok(url.startsWith('https://provider.com/oauth/authorize?'));
23
- assert.ok(url.includes('client_id=test-client'));
24
- assert.ok(url.includes('redirect_uri='));
25
- assert.ok(url.includes('response_type=code'));
26
- assert.ok(url.includes('scope=read+write'));
27
- assert.ok(url.includes('state=test-state-123'));
28
- });
29
-
30
- test('handles empty scopes', function(assert) {
31
- const flow = new OAuthFlow({ ...defaultConfig, scopes: [] });
32
- const url = flow.buildAuthorizationUrl('state');
33
-
34
- assert.ok(url.includes('scope='));
35
- });
36
- });
37
-
38
- module('exchangeCode', function() {
39
- test('makes a POST request to the token URL', async function(assert) {
40
- const flow = new OAuthFlow(defaultConfig);
41
- const originalFetch = globalThis.fetch;
42
-
43
- globalThis.fetch = async (url, options) => {
44
- assert.equal(url, 'https://provider.com/oauth/token');
45
- assert.equal(options.method, 'POST');
46
- assert.equal(options.headers['Content-Type'], 'application/json');
47
-
48
- const body = JSON.parse(options.body);
49
- assert.equal(body.grant_type, 'authorization_code');
50
- assert.equal(body.code, 'test-code');
51
- assert.equal(body.client_id, 'test-client');
52
-
53
- return {
54
- ok: true,
55
- json: async () => ({ access_token: 'abc', refresh_token: 'def', expires_in: 3600 }),
56
- };
57
- };
58
-
59
- const result = await flow.exchangeCode('test-code');
60
-
61
- assert.equal(result.accessToken, 'abc');
62
- assert.equal(result.refreshToken, 'def');
63
- assert.equal(result.expiresIn, 3600);
64
-
65
- globalThis.fetch = originalFetch;
66
- });
67
-
68
- test('throws on failed token exchange', async function(assert) {
69
- const flow = new OAuthFlow(defaultConfig);
70
- const originalFetch = globalThis.fetch;
71
-
72
- globalThis.fetch = async () => ({ ok: false, status: 400 });
73
-
74
- await assert.rejects(flow.exchangeCode('bad-code'), /Token exchange failed: 400/);
75
-
76
- globalThis.fetch = originalFetch;
77
- });
78
- });
79
-
80
- module('refreshAccessToken', function() {
81
- test('makes a refresh grant request', async function(assert) {
82
- const flow = new OAuthFlow(defaultConfig);
83
- const originalFetch = globalThis.fetch;
84
-
85
- globalThis.fetch = async (_url, options) => {
86
- const body = JSON.parse(options.body);
87
- assert.equal(body.grant_type, 'refresh_token');
88
- assert.equal(body.refresh_token, 'old-refresh');
89
-
90
- return {
91
- ok: true,
92
- json: async () => ({ access_token: 'new-access', expires_in: 7200 }),
93
- };
94
- };
95
-
96
- const result = await flow.refreshAccessToken('old-refresh');
97
-
98
- assert.equal(result.accessToken, 'new-access');
99
- assert.equal(result.refreshToken, 'old-refresh', 'falls back to original refresh token');
100
- assert.equal(result.expiresIn, 7200);
101
-
102
- globalThis.fetch = originalFetch;
103
- });
104
- });
105
-
106
- module('fetchUserInfo', function() {
107
- test('sends a Bearer token in the Authorization header', async function(assert) {
108
- const flow = new OAuthFlow(defaultConfig);
109
- const originalFetch = globalThis.fetch;
110
-
111
- globalThis.fetch = async (url, options) => {
112
- assert.equal(url, 'https://provider.com/api/me');
113
- assert.equal(options.headers.Authorization, 'Bearer my-token');
114
-
115
- return {
116
- ok: true,
117
- json: async () => ({ id: '1', name: 'Test' }),
118
- };
119
- };
120
-
121
- const result = await flow.fetchUserInfo('my-token');
122
- assert.deepEqual(result, { id: '1', name: 'Test' });
123
-
124
- globalThis.fetch = originalFetch;
125
- });
126
- });
127
-
128
- module('normalizeUser', function() {
129
- test('returns raw user by default', function(assert) {
130
- const flow = new OAuthFlow(defaultConfig);
131
- const raw = { id: '1', name: 'Test' };
132
- const result = flow.normalizeUser(raw);
133
-
134
- assert.deepEqual(result, { raw });
135
- });
136
- });
137
- });
@@ -1,115 +0,0 @@
1
- import QUnit from 'qunit';
2
- import DiscordProvider from '../../../src/providers/discord.js';
3
-
4
- const { module, test } = QUnit;
5
-
6
- const defaultConfig = {
7
- clientId: 'discord-client-id',
8
- clientSecret: 'discord-client-secret',
9
- redirectUri: 'http://localhost/auth/callback/discord',
10
- scopes: ['identify', 'email'],
11
- };
12
-
13
- module('[Unit] DiscordProvider', function() {
14
- module('constructor', function() {
15
- test('sets correct Discord OAuth2 URLs', function(assert) {
16
- const provider = new DiscordProvider(defaultConfig);
17
-
18
- assert.equal(provider.authorizationUrl, 'https://discord.com/oauth2/authorize');
19
- assert.equal(provider.tokenUrl, 'https://discord.com/api/oauth2/token');
20
- assert.equal(provider.userInfoUrl, 'https://discord.com/api/users/@me');
21
- });
22
-
23
- test('preserves client config', function(assert) {
24
- const provider = new DiscordProvider(defaultConfig);
25
-
26
- assert.equal(provider.clientId, 'discord-client-id');
27
- assert.equal(provider.clientSecret, 'discord-client-secret');
28
- assert.deepEqual(provider.scopes, ['identify', 'email']);
29
- });
30
- });
31
-
32
- module('exchangeCode', function() {
33
- test('uses form-encoded body for Discord token exchange', async function(assert) {
34
- const provider = new DiscordProvider(defaultConfig);
35
- const originalFetch = globalThis.fetch;
36
-
37
- globalThis.fetch = async (url, options) => {
38
- assert.equal(url, 'https://discord.com/api/oauth2/token');
39
- assert.equal(options.headers['Content-Type'], 'application/x-www-form-urlencoded');
40
- assert.ok(options.body instanceof URLSearchParams);
41
-
42
- const params = Object.fromEntries(options.body);
43
- assert.equal(params.grant_type, 'authorization_code');
44
- assert.equal(params.code, 'discord-auth-code');
45
- assert.equal(params.client_id, 'discord-client-id');
46
-
47
- return {
48
- ok: true,
49
- json: async () => ({ access_token: 'discord-token', refresh_token: 'refresh', expires_in: 604800 }),
50
- };
51
- };
52
-
53
- const result = await provider.exchangeCode('discord-auth-code');
54
-
55
- assert.equal(result.accessToken, 'discord-token');
56
- assert.equal(result.refreshToken, 'refresh');
57
- assert.equal(result.expiresIn, 604800);
58
-
59
- globalThis.fetch = originalFetch;
60
- });
61
- });
62
-
63
- module('normalizeUser', function() {
64
- test('maps Discord user fields correctly', function(assert) {
65
- const provider = new DiscordProvider(defaultConfig);
66
-
67
- const discordUser = {
68
- id: '123456789',
69
- username: 'testuser',
70
- global_name: 'Test User',
71
- avatar: 'abc123',
72
- email: 'test@example.com',
73
- };
74
-
75
- const result = provider.normalizeUser(discordUser);
76
-
77
- assert.equal(result.id, '123456789');
78
- assert.equal(result.username, 'testuser');
79
- assert.equal(result.displayName, 'Test User');
80
- assert.equal(result.avatar, 'https://cdn.discordapp.com/avatars/123456789/abc123.png');
81
- assert.equal(result.email, 'test@example.com');
82
- assert.deepEqual(result.raw, discordUser);
83
- });
84
-
85
- test('handles missing avatar', function(assert) {
86
- const provider = new DiscordProvider(defaultConfig);
87
-
88
- const result = provider.normalizeUser({
89
- id: '1', username: 'user', global_name: 'User', avatar: null, email: null,
90
- });
91
-
92
- assert.equal(result.avatar, null);
93
- });
94
-
95
- test('falls back to username when global_name is missing', function(assert) {
96
- const provider = new DiscordProvider(defaultConfig);
97
-
98
- const result = provider.normalizeUser({
99
- id: '1', username: 'myuser', avatar: null, email: null,
100
- });
101
-
102
- assert.equal(result.displayName, 'myuser');
103
- });
104
-
105
- test('handles missing email', function(assert) {
106
- const provider = new DiscordProvider(defaultConfig);
107
-
108
- const result = provider.normalizeUser({
109
- id: '1', username: 'user', global_name: 'User', avatar: null,
110
- });
111
-
112
- assert.equal(result.email, null);
113
- });
114
- });
115
- });
@@ -1,85 +0,0 @@
1
- import QUnit from 'qunit';
2
- import SessionManager from '../../src/session-manager.js';
3
-
4
- const { module, test } = QUnit;
5
-
6
- const mockUser = { id: '1', username: 'testuser' };
7
- const mockTokens = { accessToken: 'abc', expiresAt: Date.now() + 60000 };
8
-
9
- module('[Unit] SessionManager', function() {
10
- module('create', function() {
11
- test('generates a unique session ID and stores session', function(assert) {
12
- const manager = new SessionManager(3600);
13
- const session1 = manager.create(mockUser, mockTokens);
14
- const session2 = manager.create(mockUser, mockTokens);
15
-
16
- assert.ok(session1.sessionId);
17
- assert.ok(session2.sessionId);
18
- assert.notEqual(session1.sessionId, session2.sessionId);
19
- assert.deepEqual(session1.user, mockUser);
20
- });
21
-
22
- test('sets expiresAt based on duration', function(assert) {
23
- const manager = new SessionManager(3600);
24
- const before = Date.now();
25
- const session = manager.create(mockUser, mockTokens);
26
-
27
- assert.ok(session.expiresAt >= before + 3600 * 1000);
28
- });
29
- });
30
-
31
- module('get', function() {
32
- test('returns session data for a valid session ID', function(assert) {
33
- const manager = new SessionManager(3600);
34
- const { sessionId } = manager.create(mockUser, mockTokens);
35
- const session = manager.get(sessionId);
36
-
37
- assert.ok(session);
38
- assert.deepEqual(session.user, mockUser);
39
- });
40
-
41
- test('returns null for unknown session ID', function(assert) {
42
- const manager = new SessionManager(3600);
43
- const session = manager.get('nonexistent');
44
-
45
- assert.equal(session, null);
46
- });
47
- });
48
-
49
- module('validate', function() {
50
- test('returns user for a valid, non-expired session', function(assert) {
51
- const manager = new SessionManager(3600);
52
- const { sessionId } = manager.create(mockUser, mockTokens);
53
- const user = manager.validate(sessionId);
54
-
55
- assert.deepEqual(user, mockUser);
56
- });
57
-
58
- test('returns null for an expired session and cleans it up', function(assert) {
59
- const manager = new SessionManager(0);
60
- const { sessionId } = manager.create(mockUser, mockTokens);
61
- const user = manager.validate(sessionId);
62
-
63
- assert.equal(user, null);
64
- assert.equal(manager.get(sessionId), null);
65
- });
66
-
67
- test('returns null for nonexistent session', function(assert) {
68
- const manager = new SessionManager(3600);
69
- const user = manager.validate('missing');
70
-
71
- assert.equal(user, null);
72
- });
73
- });
74
-
75
- module('destroy', function() {
76
- test('removes the session', function(assert) {
77
- const manager = new SessionManager(3600);
78
- const { sessionId } = manager.create(mockUser, mockTokens);
79
-
80
- manager.destroy(sessionId);
81
-
82
- assert.equal(manager.get(sessionId), null);
83
- });
84
- });
85
- });