File size: 3,543 Bytes
1fe7075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import type { INestApplication } from '@nestjs/common';
import { api, createApp, internalToken, resetDatabase, signIn, sql } from './helpers.js';

describe('Generations (e2e)', () => {
  let app: INestApplication;
  let userId: string;
  let headers: Record<string, string>;

  const create = async (jobId: string, token?: string) =>
    api(app)
      .post('/internal/generations')
      .set('Authorization', `Bearer ${token ?? (await internalToken())}`)
      .send({ userId, jobId });

  beforeAll(async () => {
    app = await createApp();
  });
  afterAll(() => app.close());
  beforeEach(async () => {
    await resetDatabase();
    headers = await signIn('user_t');
    userId = (await api(app).get('/api/me').set(headers)).body.data.user.id;
  });

  it('allows 5 generations, then answers 402', async () => {
    for (let n = 1; n <= 5; n++) expect((await create(`job${n}`)).status).toBe(201);
    const refused = await create('job6');
    expect(refused.status).toBe(402);
    expect(refused.body).toEqual({ success: false, error: { code: 'NO_FREE_TRIES', message: expect.any(String) } });
    expect((await api(app).get('/api/me').set(headers)).body.data.usage).toEqual({ plan: 'free', limit: 5, used: 5, remaining: 0, renewsAt: null });
  });

  it('lets admins generate without a limit', async () => {
    await sql("UPDATE users SET role = 'admin' WHERE id = $1", [userId]);
    for (let n = 1; n <= 7; n++) expect((await create(`job${n}`)).status).toBe(201);
    expect((await api(app).get('/api/me').set(headers)).body.data.usage).toEqual({ plan: 'unlimited', limit: null, used: 7, remaining: null, renewsAt: null });
  });

  it('gives a try back once when a generation is refunded', async () => {
    const created = await create('job1');
    expect(created.body.data).toEqual({ generationId: expect.any(String), usage: { plan: 'free', limit: 5, used: 1, remaining: 4, renewsAt: null } });
    const token = await internalToken();
    for (let n = 0; n < 2; n++) {
      await api(app).post(`/internal/generations/${created.body.data.generationId}/refund`).set('Authorization', `Bearer ${token}`).expect(204);
    }
    expect((await api(app).get('/api/me').set(headers)).body.data.usage.used).toBe(0);
  });

  it('never passes the limit with simultaneous jobs', async () => {
    const token = await internalToken();
    const responses = await Promise.all(Array.from({ length: 12 }, (_, n) => create(`burst${n}`, token)));
    expect(responses.filter((response) => response.status === 201)).toHaveLength(5);
    expect(responses.filter((response) => response.status === 402)).toHaveLength(7);
  });

  it('accepts only the internal token', async () => {
    await api(app).post('/internal/generations').send({ userId, jobId: 'j' }).expect(401);
    expect((await create('j', await internalToken('prevision-internal', -120))).status).toBe(401);
    const browserToken = (await api(app).post('/api/processing-token').set(headers)).body.data.token;
    expect((await create('j', browserToken)).status).toBe(401);
  });

  it('validates the request', async () => {
    const token = await internalToken();
    await api(app).post('/internal/generations').set('Authorization', `Bearer ${token}`).send({ userId: 'nope', jobId: 'j' }).expect(400);
    await api(app).post('/internal/generations').set('Authorization', `Bearer ${token}`).send({ userId, jobId: '../x' }).expect(400);
  });

  it('is not reachable through the /api proxy', async () => {
    await api(app).post('/api/internal/generations').expect(404);
  });
});