/* eslint-disable @typescript-eslint/no-non-null-assertion */ import { LicenseStatus, LICENSE_STATUS_PATH, LICENSE_ACTIVE_PATH, LicenseActiveRequest, LicenseActiveResponse, } from './api-types.js'; import {expect} from 'chai'; import {suite, test, setup} from 'mocha'; import * as request from 'supertest'; import pkg from '@prisma/client'; const {LicenseStatus: PrismaLicenseStatus} = pkg; import {dbDateToday, prisma} from './db.js'; import {resetDB, newAppForTest, apiVersionTest} from './test-helpers.js'; const app = newAppForTest(); suite('Server: License', () => { suite('licenseActive', () => { const existingLicenseKey = 'abc-123'; setup(async () => { await resetDB(prisma); await prisma.license.create({ data: { key: existingLicenseKey, status: PrismaLicenseStatus.VALID, commercial: true, acceptedTOS: true, customer: { create: { name: 'Test Customer', email: 'foo@example.com', }, }, }, }); }); type LicenseActiveTestCase = { req: LicenseActiveRequest; expResp: LicenseActiveResponse; expStatus: number; expectRecordCreated: boolean; }; async function licenseActiveTest(tc: LicenseActiveTestCase) { const activeCountBefore = await prisma.active.count(); const response = await request .default(app) .post(LICENSE_ACTIVE_PATH) .set({ // eslint-disable-next-line @typescript-eslint/naming-convention 'Accept': 'appplication/json', // eslint-disable-next-line @typescript-eslint/naming-convention 'Content-Type': 'application/json', }) .send(tc.req); expect(response.statusCode).to.equal(tc.expStatus); expect(response.body).to.deep.equal(tc.expResp); const activeCountAfter = await prisma.active.count(); if (tc.expectRecordCreated) { expect(activeCountAfter).to.equal(activeCountBefore + 1); const now = await dbDateToday(); // Yes this test will flake if run at midnight UTC. ¯\_(ツ)_/¯ const active = await prisma.active.findFirst({ where: { licenseKey: tc.req.licenseKey, profileID: tc.req.profileID, date: now, }, }); expect(active).to.not.be.null; } else { expect(activeCountAfter).to.equal(activeCountBefore); } } test('licenseActive with good license succeeds', async () => { await licenseActiveTest({ req: { licenseKey: existingLicenseKey, profileID: 'test-profile', }, expResp: {}, expStatus: 200, expectRecordCreated: true, }); }); test('licenseActive with good license dup record succeeds', async () => { await licenseActiveTest({ req: { licenseKey: existingLicenseKey, profileID: 'test-profile', }, expResp: {}, expStatus: 200, expectRecordCreated: true, }); await licenseActiveTest({ req: { licenseKey: existingLicenseKey, profileID: 'test-profile', }, expResp: {}, expStatus: 200, expectRecordCreated: true, }); }); test('licenseActive with bad license fails', async () => { await licenseActiveTest({ req: { licenseKey: 'no-such-license', profileID: 'test-profile', }, expResp: {}, expStatus: 500, expectRecordCreated: false, }); }); test('CORS', async () => { const response = await request .default(app) .options(LICENSE_ACTIVE_PATH) .send(); expect(response.statusCode).to.equal(204); expect(response.header['access-control-allow-origin']).to.equal('*'); expect(response.header['access-control-allow-methods']).to.include([ 'POST', ]); }); test('API version', async () => { const errText = 'Unsupported API version'; await apiVersionTest( '2000.1', app, LICENSE_ACTIVE_PATH, {}, 400, errText, ); await apiVersionTest( '1.123124', app, LICENSE_ACTIVE_PATH, {}, 400, errText, ); }); }); suite('getLicenseStatus', () => { setup(async () => { await resetDB(prisma); }); const expectedStatus = new Map(); expectedStatus.set(PrismaLicenseStatus.VALID, LicenseStatus.Valid); expectedStatus.set(PrismaLicenseStatus.INVALID, LicenseStatus.Invalid); let status: keyof typeof PrismaLicenseStatus; for (status in PrismaLicenseStatus) { test(`get license status ${status}`, async () => { const key = 'key-' + status; const license = await prisma.license.create({ data: { key, status, customer: { create: { name: 'name-' + status, email: 'foo@example.com', }, }, }, }); expect(license).to.not.be.undefined; const response = await request .default(app) .post(LICENSE_STATUS_PATH) .send({licenseKey: key}) .set({ // eslint-disable-next-line @typescript-eslint/naming-convention 'Accept': 'appplication/json', // eslint-disable-next-line @typescript-eslint/naming-convention 'Content-Type': 'application/json', }); expect(response.statusCode).to.equal(200); expect(response.body.status).to.equal(expectedStatus.get(status)); expect(response.body.disable).to.be.false; }); } test('get license status UNKNOWN', async () => { const response = await request .default(app) .post(LICENSE_STATUS_PATH) .send({licenseKey: 'NO-SUCH-KEY'}) .set({ // eslint-disable-next-line @typescript-eslint/naming-convention 'Accept': 'appplication/json', // eslint-disable-next-line @typescript-eslint/naming-convention 'Content-Type': 'application/json', }); expect(response.statusCode).to.equal(200); expect(response.body.status).to.equal(LicenseStatus.Unknown); expect(response.body.disable).to.be.false; }); test('CORS', async () => { const response = await request .default(app) .options(LICENSE_STATUS_PATH) .send(); expect(response.statusCode).to.equal(204); expect(response.header['access-control-allow-origin']).to.equal('*'); expect(response.header['access-control-allow-methods']).to.include([ 'POST', ]); }); }); test('API version', async () => { const errText = 'Unsupported API version'; await apiVersionTest('2000.1', app, LICENSE_STATUS_PATH, {}, 400, errText); await apiVersionTest( '1.123124', app, LICENSE_STATUS_PATH, {}, 400, errText, ); }); });