Skip to content

Commit

Permalink
✅ test: Add ACCESS_CODE testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mushan0x0 committed Dec 15, 2023
1 parent 5d60cdd commit 4f96487
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/app/api/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from 'vitest';

import { checkAuth } from './auth';
import { getPreferredRegion } from './config';

// Stub the global process object to safely mock environment variables
Expand Down Expand Up @@ -40,3 +41,53 @@ describe('getPreferredRegion', () => {
expect(preferredRegion).toStrictEqual(['ida1', 'sfo1']);
});
});

describe('ACCESS_CODE', () => {
let auth = false;

beforeEach(() => {
auth = false;
process.env.ACCESS_CODE = undefined;
// Reset environment variables before each test case
vi.restoreAllMocks();
});

it('set multiple access codes', () => {
process.env.ACCESS_CODE = ',code1,code2,code3';
({ auth } = checkAuth({ accessCode: 'code1' }));
expect(auth).toBe(true);
({ auth } = checkAuth({ accessCode: 'code2' }));
expect(auth).toBe(true);
({ auth } = checkAuth({ accessCode: 'code1,code2' }));
expect(auth).toBe(false);
});

it('set individual access code', () => {
process.env.ACCESS_CODE = 'code1';
({ auth } = checkAuth({ accessCode: 'code1' }));
expect(auth).toBe(true);
({ auth } = checkAuth({ accessCode: 'code2' }));
expect(auth).toBe(false);
});

it('no access code', () => {
({ auth } = checkAuth({ accessCode: 'code1' }));
expect(auth).toBe(true);
({ auth } = checkAuth({}));
expect(auth).toBe(true);
});

it('empty access code', () => {
process.env.ACCESS_CODE = '';
({ auth } = checkAuth({ accessCode: 'code1' }));
expect(auth).toBe(true);
({ auth } = checkAuth({}));
expect(auth).toBe(true);

process.env.ACCESS_CODE = ',,';
({ auth } = checkAuth({ accessCode: 'code1' }));
expect(auth).toBe(true);
({ auth } = checkAuth({}));
expect(auth).toBe(true);
});
});

0 comments on commit 4f96487

Please sign in to comment.