SponsorBlockServer/test/cases/postWarning.ts

121 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-01-06 01:43:28 +01:00
import fetch from 'node-fetch';
2020-10-17 20:56:54 +02:00
import {Done, getbaseURL} from '../utils';
import {db} from '../../src/databases/databases';
import {getHash} from '../../src/utils/getHash';
2021-07-10 22:30:30 +02:00
import assert from 'assert';
2020-10-17 20:56:54 +02:00
describe('postWarning', () => {
2021-03-07 06:21:56 +01:00
before(async () => {
2021-05-07 01:51:11 +02:00
await db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [getHash("warning-vip")]);
2020-10-17 20:56:54 +02:00
});
it('Should be able to create warning if vip (exp 200)', (done: Done) => {
2021-07-04 07:34:31 +02:00
const json = {
2020-10-17 20:56:54 +02:00
issuerUserID: 'warning-vip',
userID: 'warning-0',
2021-06-30 03:59:20 +02:00
reason: 'warning-reason-0'
2020-10-17 20:56:54 +02:00
};
2021-01-06 01:43:28 +01:00
fetch(getbaseURL()
+ "/api/warnUser", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(json),
})
.then(async res => {
2021-07-07 09:47:08 +02:00
assert.strictEqual(res.status, 200);
const row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(row.enabled, 1);
assert.strictEqual(row.issuerUserID, getHash(json.issuerUserID));
assert.strictEqual(row.reason, json.reason);
done();
2021-01-06 01:43:28 +01:00
})
.catch(err => done(err));
2020-12-29 19:31:15 +01:00
});
it('Should be not be able to create a duplicate warning if vip', (done: Done) => {
2021-07-04 07:34:31 +02:00
const json = {
2020-12-29 19:31:15 +01:00
issuerUserID: 'warning-vip',
userID: 'warning-0',
};
2021-01-06 01:43:28 +01:00
fetch(getbaseURL()
+ "/api/warnUser", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(json),
})
.then(async res => {
2021-07-07 09:47:08 +02:00
assert.strictEqual(res.status, 409);
const row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(row.enabled, 1);
assert.strictEqual(row.issuerUserID, getHash(json.issuerUserID));
done();
2021-01-06 01:43:28 +01:00
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2020-12-29 06:18:50 +01:00
it('Should be able to remove warning if vip', (done: Done) => {
2021-07-04 07:34:31 +02:00
const json = {
2020-12-29 06:18:50 +01:00
issuerUserID: 'warning-vip',
userID: 'warning-0',
enabled: false
};
2021-01-06 01:43:28 +01:00
fetch(getbaseURL()
+ "/api/warnUser", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(json),
})
.then(async res => {
2021-07-07 09:47:08 +02:00
assert.strictEqual(res.status, 200);
const row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
assert.strictEqual(row.enabled, 0);
done();
2021-01-06 01:43:28 +01:00
})
.catch(err => done(err));
2020-12-29 06:18:50 +01:00
});
2021-06-18 01:09:24 +02:00
it('Should not be able to create warning if not vip (exp 403)', (done: Done) => {
2021-07-04 07:34:31 +02:00
const json = {
2020-10-17 20:56:54 +02:00
issuerUserID: 'warning-not-vip',
userID: 'warning-1',
};
2021-01-06 01:43:28 +01:00
fetch(getbaseURL()
+ "/api/warnUser", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(json),
})
2021-07-07 09:47:08 +02:00
.then(res => {
assert.strictEqual(res.status, 403);
done();
2021-01-06 01:43:28 +01:00
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
2021-07-10 22:30:30 +02:00
it('Should return 400 if missing body', (done: Done) => {
fetch(getbaseURL()
+ "/api/warnUser", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
})
.then(async res => {
assert.strictEqual(res.status, 400);
done();
})
.catch(err => done(err));
});
2020-10-17 20:56:54 +02:00
});