SponsorBlockServer/test/cases/getLockCategories.ts

79 lines
2.9 KiB
TypeScript
Raw Normal View History

2021-07-04 23:03:28 +02:00
import fetch from 'node-fetch';
import {Done, getbaseURL} from '../utils';
import {getHash} from '../../src/utils/getHash';
import {db} from '../../src/databases/databases';
2021-07-07 09:47:08 +02:00
import assert from 'assert';
2021-07-04 23:03:28 +02:00
2021-07-05 05:33:12 +02:00
describe('getLockCategories', () => {
2021-07-04 23:03:28 +02:00
before(async () => {
const insertVipUserQuery = 'INSERT INTO "vipUsers" ("userID") VALUES (?)';
await db.prepare("run", insertVipUserQuery, [getHash("VIPUser-getLockCategories")]);
const insertLockCategoryQuery = 'INSERT INTO "lockCategories" ("userID", "videoID", "category") VALUES (?, ?, ?)';
await db.prepare("run", insertLockCategoryQuery, [getHash("VIPUser-getLockCategories"), 'getLock-1', 'sponsor']);
2021-07-05 05:33:12 +02:00
await db.prepare("run", insertLockCategoryQuery, [getHash("VIPUser-getLockCategories"), 'getLock-1', 'interaction']);
2021-07-04 23:03:28 +02:00
await db.prepare("run", insertLockCategoryQuery, [getHash("VIPUser-getLockCategories"), 'getLock-2', 'preview']);
await db.prepare("run", insertLockCategoryQuery, [getHash("VIPUser-getLockCategories"), 'getLock-3', 'nonmusic']);
});
it('Should update the database version when starting the application', async () => {
2021-07-05 05:36:47 +02:00
const version = (await db.prepare('get', 'SELECT key, value FROM config where key = ?', ['version'])).value;
2021-07-04 23:03:28 +02:00
if (version > 1) return;
else return 'Version isn\'t greater than 1. Version is ' + version;
});
it('Should be able to get multiple locks', (done: Done) => {
fetch(getbaseURL() + '/api/lockCategories?videoID=getLock-1')
.then(async res => {
2021-07-07 09:47:08 +02:00
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
categories: [
"sponsor",
"interaction"
]
};
assert.deepStrictEqual(data, expected);
done();
2021-07-04 23:03:28 +02:00
})
2021-07-07 09:47:08 +02:00
.catch(err => done(err));
2021-07-04 23:03:28 +02:00
});
it('Should be able to get single locks', (done: Done) => {
fetch(getbaseURL() + '/api/lockCategories?videoID=getLock-2')
.then(async res => {
2021-07-07 09:47:08 +02:00
assert.strictEqual(res.status, 200);
const data = await res.json();
const expected = {
categories: [
"preview"
]
};
assert.deepStrictEqual(data, expected);
done();
2021-07-04 23:03:28 +02:00
})
2021-07-07 09:47:08 +02:00
.catch(err => done(err));
2021-07-04 23:03:28 +02:00
});
it('should return 404 if no lock exists', (done: Done) => {
fetch(getbaseURL() + '/api/lockCategories?videoID=getLock-0')
.then(res => {
2021-07-07 09:47:08 +02:00
assert.strictEqual(res.status, 404);
done();
2021-07-04 23:03:28 +02:00
})
2021-07-07 09:47:08 +02:00
.catch(err => done(err));
2021-07-04 23:03:28 +02:00
});
it('should return 400 if no videoID specified', (done: Done) => {
fetch(getbaseURL() + '/api/lockCategories')
.then(res => {
2021-07-07 09:47:08 +02:00
assert.strictEqual(res.status, 400);
done();
2021-07-04 23:03:28 +02:00
})
2021-07-07 09:47:08 +02:00
.catch(err => done(err));
2021-07-04 23:03:28 +02:00
});
});