SponsorBlockServer/test/cases/postWarning.ts

130 lines
4.2 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';
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) => {
let json = {
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 => {
if (res.status === 200) {
2021-03-07 06:21:56 +01:00
let row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
2021-01-06 01:43:28 +01:00
if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID)) {
done();
2020-12-29 19:31:15 +01:00
} else {
2021-01-06 01:43:28 +01:00
done("Warning missing from database");
2020-12-29 19:31:15 +01:00
}
2021-01-06 01:43:28 +01:00
} else {
const body = await res.text();
console.log(body);
done("Status code was " + res.status);
}
})
.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) => {
let json = {
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 => {
if (res.status === 409) {
2021-03-07 06:21:56 +01:00
let row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
2021-01-06 01:43:28 +01:00
if (row?.enabled == 1 && row?.issuerUserID == getHash(json.issuerUserID)) {
done();
2020-10-17 20:56:54 +02:00
} else {
2021-01-06 01:43:28 +01:00
done("Warning missing from database");
2020-10-17 20:56:54 +02:00
}
2021-01-06 01:43:28 +01:00
} else {
const body = await res.text();
console.log(body);
done("Status code was " + res.status);
}
})
.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) => {
let json = {
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 => {
if (res.status === 200) {
2021-03-07 06:21:56 +01:00
let row = await db.prepare('get', `SELECT "userID", "issueTime", "issuerUserID", enabled FROM warnings WHERE "userID" = ?`, [json.userID]);
2021-01-06 01:43:28 +01:00
if (row?.enabled == 0) {
done();
2020-12-29 06:18:50 +01:00
} else {
2021-01-06 01:43:28 +01:00
done("Warning missing from database");
2020-12-29 06:18:50 +01:00
}
2021-01-06 01:43:28 +01:00
} else {
const body = await res.text();
console.log(body);
done("Status code was " + res.status);
}
})
.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) => {
2020-10-17 20:56:54 +02:00
let json = {
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),
})
.then(async res => {
if (res.status === 403) {
done();
} else {
const body = await res.text();
console.log(body);
done("Status code was " + res.status);
}
})
.catch(err => done(err));
2020-10-17 20:56:54 +02:00
});
});