SponsorBlockServer/test/cases/getUserInfo.ts

125 lines
6.4 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('getUserInfo', () => {
2021-03-02 03:37:35 +01:00
before(async () => {
2021-03-07 06:21:56 +01:00
let startOfUserNamesQuery = `INSERT INTO "userNames" ("userID", "userName") VALUES`;
2021-03-02 03:37:35 +01:00
await db.prepare("run", startOfUserNamesQuery + "('" + getHash("getuserinfo_user_01") + "', 'Username user 01')");
2021-03-07 06:21:56 +01:00
let startOfSponsorTimesQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "UUID", "userID", "timeSubmitted", views, category, "shadowHidden") VALUES';
2021-03-02 03:37:35 +01:00
await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000001', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)");
await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000002', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)");
await db.prepare("run", startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -1, 'uuid000003', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 0)");
await db.prepare("run", startOfSponsorTimesQuery + "('yyyxxxzzz', 1, 11, -2, 'uuid000004', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 1)");
await db.prepare("run", startOfSponsorTimesQuery + "('xzzzxxyyy', 1, 11, -5, 'uuid000005', '" + getHash("getuserinfo_user_01") + "', 0, 10, 'sponsor', 1)");
await db.prepare("run", startOfSponsorTimesQuery + "('zzzxxxyyy', 1, 11, 2, 'uuid000006', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 0)");
await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000007', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 1)");
await db.prepare("run", startOfSponsorTimesQuery + "('xxxyyyzzz', 1, 11, 2, 'uuid000008', '" + getHash("getuserinfo_user_02") + "', 0, 10, 'sponsor', 1)");
2020-10-17 20:56:54 +02:00
2021-03-02 03:37:35 +01:00
2021-03-07 06:21:56 +01:00
await db.prepare("run", `INSERT INTO warnings ("userID", "issueTime", "issuerUserID", enabled) VALUES ('` + getHash('getuserinfo_warning_0') + "', 10, 'getuserinfo_vip', 1)");
await db.prepare("run", `INSERT INTO warnings ("userID", "issueTime", "issuerUserID", enabled) VALUES ('` + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)");
await db.prepare("run", `INSERT INTO warnings ("userID", "issueTime", "issuerUserID", enabled) VALUES ('` + getHash('getuserinfo_warning_1') + "', 10, 'getuserinfo_vip', 1)");
2020-10-17 20:56:54 +02:00
});
it('Should be able to get a 200', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_01')
.then(res => {
if (res.status !== 200) done('non 200 (' + res.status + ')');
2021-03-02 03:37:35 +01:00
else done(); // pass
2021-01-06 01:43:28 +01:00
})
.catch(err => done('couldn\'t call endpoint'));
2020-10-17 20:56:54 +02:00
});
it('Should be able to get a 400 (No userID parameter)', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + '/api/getUserInfo')
.then(res => {
if (res.status !== 400) done('non 400 (' + res.status + ')');
else done(); // pass
})
.catch(err => done('couldn\'t call endpoint'));
2020-10-17 20:56:54 +02:00
});
2021-03-02 03:37:35 +01:00
it('Should done(info', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_01')
.then(async res => {
if (res.status !== 200) {
done("non 200");
} else {
const data = await res.json();
if (data.userName !== 'Username user 01') {
done('Returned incorrect userName "' + data.userName + '"');
} else if (data.minutesSaved !== 5) {
done('Returned incorrect minutesSaved "' + data.minutesSaved + '"');
} else if (data.viewCount !== 30) {
done('Returned incorrect viewCount "' + data.viewCount + '"');
} else if (data.segmentCount !== 3) {
done('Returned incorrect segmentCount "' + data.segmentCount + '"');
2020-10-17 20:56:54 +02:00
} else {
2021-01-06 01:43:28 +01:00
done(); // pass
2020-10-17 20:56:54 +02:00
}
2021-01-06 01:43:28 +01:00
}
})
2021-03-02 03:37:35 +01:00
.catch(err => ("couldn't call endpoint"));
2020-10-17 20:56:54 +02:00
});
it('Should get warning data', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_warning_0')
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
} else {
const data = await res.json();;
if (data.warnings !== 1) done('wrong number of warnings: ' + data.warnings + ', not ' + 1);
else done(); // pass
}
})
2021-03-02 03:37:35 +01:00
.catch(err => ("couldn't call endpoint"));
2020-10-17 20:56:54 +02:00
});
it('Should get multiple warnings', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_warning_1')
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
} else {
const data = await res.json();
if (data.warnings !== 2) done('wrong number of warnings: ' + data.warnings + ', not ' + 2);
else done(); // pass
}
})
2021-03-02 03:37:35 +01:00
.catch(err => ("couldn't call endpoint"));
2020-10-17 20:56:54 +02:00
});
it('Should not get warnings if noe', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_warning_2')
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
} else {
const data = await res.json();
if (data.warnings !== 0) done('wrong number of warnings: ' + data.warnings + ', not ' + 0);
else done(); // pass
}
})
2021-03-02 03:37:35 +01:00
.catch(err => ("couldn't call endpoint"));
2020-10-17 20:56:54 +02:00
});
2021-03-02 03:37:35 +01:00
it('Should done(userID for userName (No userName set)', (done: Done) => {
2021-01-06 01:43:28 +01:00
fetch(getbaseURL() + '/api/getUserInfo?userID=getuserinfo_user_02')
.then(async res => {
if (res.status !== 200) {
done('non 200 (' + res.status + ')');
} else {
const data = await res.json();
if (data.userName !== 'c2a28fd225e88f74945794ae85aef96001d4a1aaa1022c656f0dd48ac0a3ea0f') {
2021-03-02 03:37:35 +01:00
done('Did not done(userID for userName');
2020-10-17 20:56:54 +02:00
}
2021-01-06 01:43:28 +01:00
done(); // pass
}
})
2021-03-02 03:37:35 +01:00
.catch(err => ('couldn\'t call endpoint'));
2020-10-17 20:56:54 +02:00
});
});