File size: 1,973 Bytes
c7d34c1
 
 
b1cfe1b
c7d34c1
 
 
 
32151b6
 
c7d34c1
 
 
 
 
 
 
 
 
 
32151b6
c7d34c1
 
 
 
 
 
 
 
 
 
32151b6
c7d34c1
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { GET } from './image/[filename]/route';
import { PAGE_PASSWORD_AUTH_ERROR_CODES } from '@/lib/page-password-auth';
import { createAccessToken } from '@/lib/server-runtime';
import { NextRequest } from 'next/server';
import assert from 'node:assert/strict';
import { afterEach, describe, it } from 'node:test';

const originalAppPassword = process.env.APP_PASSWORD;
const PAGE_PASSWORD_FIXTURE = ['customer', 'access', 'code'].join('-');
const OTHER_PAGE_PASSWORD_FIXTURE = ['other', 'access', 'code'].join('-');

afterEach(() => {
    if (originalAppPassword === undefined) {
        delete process.env.APP_PASSWORD;
    } else {
        process.env.APP_PASSWORD = originalAppPassword;
    }
});

describe('GET /api/image/[filename]', () => {
    it('returns a missing page access code error when the access cookie is absent', async () => {
        process.env.APP_PASSWORD = PAGE_PASSWORD_FIXTURE;
        const request = new NextRequest('http://localhost/api/image/sample.png');

        const response = await GET(request, { params: Promise.resolve({ filename: 'sample.png' }) });
        const result = (await response.json()) as { code?: string };

        assert.equal(response.status, 401);
        assert.equal(result.code, PAGE_PASSWORD_AUTH_ERROR_CODES.missing);
    });

    it('returns an invalid page access code error when the access cookie is wrong', async () => {
        process.env.APP_PASSWORD = PAGE_PASSWORD_FIXTURE;
        const request = new NextRequest('http://localhost/api/image/sample.png', {
            headers: {
                Cookie: `gptImageAccess=${createAccessToken(OTHER_PAGE_PASSWORD_FIXTURE)}`
            }
        });

        const response = await GET(request, { params: Promise.resolve({ filename: 'sample.png' }) });
        const result = (await response.json()) as { code?: string };

        assert.equal(response.status, 401);
        assert.equal(result.code, PAGE_PASSWORD_AUTH_ERROR_CODES.invalid);
    });
});