mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-06 03:26:59 +03:00
addUserAsVIP
- add genAnonUser
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import { getHash } from "../../src/utils/getHash";
|
||||
import { HashedUserID } from "../../src/types/user.model";
|
||||
import { client } from "../utils/httpClient";
|
||||
import { db } from "../../src/databases/databases";
|
||||
import assert from "assert";
|
||||
import { genAnonUser, genUsers } from "../utils/genUser";
|
||||
|
||||
// helpers
|
||||
const checkUserVIP = (publicID: string) => db.prepare("get", `SELECT "userID" FROM "vipUsers" WHERE "userID" = ?`, [publicID]);
|
||||
|
||||
const cases = [
|
||||
"vip-1",
|
||||
];
|
||||
const users = genUsers("endpoint", cases);
|
||||
|
||||
// hardcoded into test code
|
||||
const adminPrivateUserID = "testUserId";
|
||||
const permVIP1 = "addVIP_permaVIPOne";
|
||||
const publicPermVIP1 = getHash(permVIP1) as HashedUserID;
|
||||
const permVIP2 = "addVIP_permaVIPTwo";
|
||||
const publicPermVIP2 = getHash(permVIP2) as HashedUserID;
|
||||
const permVIP3 = "addVIP_permaVIPThree";
|
||||
const publicPermVIP3 = getHash(permVIP3) as HashedUserID;
|
||||
|
||||
const endpoint = "/api/addUserAsVIP";
|
||||
const addUserAsVIP = (userID: string, enabled: boolean, adminUserID = adminPrivateUserID) => client({
|
||||
@@ -26,116 +26,68 @@ const addUserAsVIP = (userID: string, enabled: boolean, adminUserID = adminPriva
|
||||
}
|
||||
});
|
||||
|
||||
const testVIPUpdate = (target: HashedUserID, enabled: boolean, adminID: string = adminPrivateUserID) =>
|
||||
addUserAsVIP(target, enabled, adminID)
|
||||
.then(res => assert.strictEqual(res.status, 200))
|
||||
.then(() => checkUserVIP(target))
|
||||
.then(row => assert.ok(Boolean(row) == enabled));
|
||||
|
||||
const statusTest = (status: number, data: Record<string, any>) =>
|
||||
client({
|
||||
method: "POST",
|
||||
url: endpoint,
|
||||
params: data
|
||||
}).then(res => assert.strictEqual(res.status, status));
|
||||
|
||||
describe("addVIP test", function() {
|
||||
it("User should not already be VIP", (done) => {
|
||||
checkUserVIP(publicPermVIP1)
|
||||
.then(result => {
|
||||
assert.ok(!result);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
it("Should be able to add user as VIP", (done) => {
|
||||
addUserAsVIP(publicPermVIP1, true)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const row = await checkUserVIP(publicPermVIP1);
|
||||
assert.ok(row);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
it("Should be able to add second user as VIP", (done) => {
|
||||
addUserAsVIP(publicPermVIP2, true)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const row = await checkUserVIP(publicPermVIP2);
|
||||
assert.ok(row);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
it("Should return 403 with invalid adminID", (done) => {
|
||||
addUserAsVIP(publicPermVIP1, true, "Invalid_Admin_User_ID")
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 403);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
it("Should return 400 with missing adminID", (done) => {
|
||||
client({
|
||||
method: "POST",
|
||||
url: endpoint,
|
||||
params: {
|
||||
userID: publicPermVIP1,
|
||||
enabled: String(true)
|
||||
}
|
||||
it("User should not already be VIP", () =>
|
||||
checkUserVIP(users["vip-1"].pubID)
|
||||
.then(result => assert.ok(!result))
|
||||
);
|
||||
it("Should be able to add user as VIP", () =>
|
||||
testVIPUpdate(users["vip-1"].pubID, true)
|
||||
);
|
||||
it("Should be able to remove VIP", () =>
|
||||
testVIPUpdate(users["vip-1"].pubID, false)
|
||||
);
|
||||
it("Should be able to add second user as VIP", () =>
|
||||
testVIPUpdate(genAnonUser().pubID, true)
|
||||
);
|
||||
it("Should return 403 with invalid adminID", () =>
|
||||
addUserAsVIP(genAnonUser().pubID, true, genAnonUser().privID)
|
||||
.then(res => assert.strictEqual(res.status, 403))
|
||||
);
|
||||
it("Should return 400 with missing adminID", () =>
|
||||
statusTest(400, {
|
||||
userID: genAnonUser().pubID,
|
||||
enabled: String(true)
|
||||
})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
it("Should return 400 with missing userID", (done) => {
|
||||
client({
|
||||
method: "POST",
|
||||
url: endpoint,
|
||||
params: {
|
||||
enabled: String(true),
|
||||
adminUserID: adminPrivateUserID
|
||||
}
|
||||
);
|
||||
it("Should return 400 with missing userID", () =>
|
||||
statusTest(400, {
|
||||
enabled: String(true),
|
||||
adminUserID: adminPrivateUserID
|
||||
})
|
||||
.then(res => {
|
||||
assert.strictEqual(res.status, 400);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
it("Should be able to remove VIP", (done) => {
|
||||
addUserAsVIP(publicPermVIP1, false)
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const row = await checkUserVIP(publicPermVIP1);
|
||||
assert.ok(!row);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
});
|
||||
it("Should remove VIP if enabled is false", (done) => {
|
||||
client({
|
||||
method: "POST",
|
||||
url: endpoint,
|
||||
params: {
|
||||
userID: publicPermVIP2,
|
||||
);
|
||||
it("Should remove VIP if enabled is not true", () => {
|
||||
const user = genAnonUser();
|
||||
return testVIPUpdate(user.pubID, true)
|
||||
.then(() => statusTest(200, {
|
||||
userID: user.pubID,
|
||||
adminUserID: adminPrivateUserID,
|
||||
enabled: "invalid-text"
|
||||
}
|
||||
})
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const row = await checkUserVIP(publicPermVIP2);
|
||||
assert.ok(!row);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
}))
|
||||
.then(() => checkUserVIP(user.pubID))
|
||||
.then(row => assert.ok(!row));
|
||||
});
|
||||
it("Should remove VIP if enabled is missing", (done) => {
|
||||
client({
|
||||
method: "POST",
|
||||
url: endpoint,
|
||||
params: {
|
||||
userID: publicPermVIP3,
|
||||
adminUserID: adminPrivateUserID
|
||||
}
|
||||
})
|
||||
.then(async res => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
const row = await checkUserVIP(publicPermVIP3);
|
||||
assert.ok(!row);
|
||||
done();
|
||||
})
|
||||
.catch(err => done(err));
|
||||
it("Should remove VIP if enabled is missing", () => {
|
||||
const user = genAnonUser();
|
||||
return testVIPUpdate(user.pubID, true)
|
||||
.then(() => statusTest(200, {
|
||||
userID: user.pubID,
|
||||
adminUserID: adminPrivateUserID,
|
||||
}))
|
||||
.then(() => checkUserVIP(user.pubID))
|
||||
.then(row => assert.ok(!row));
|
||||
});
|
||||
});
|
||||
@@ -2,10 +2,12 @@ import { genRandom } from "./getRandom";
|
||||
import { UserID, HashedUserID } from "../../src/types/user.model";
|
||||
import { getHash } from "../../src/utils/getHash";
|
||||
|
||||
type info = Record<string, any>
|
||||
|
||||
export interface User {
|
||||
privID: UserID,
|
||||
pubID: HashedUserID
|
||||
info: Record<string, any>
|
||||
info: info
|
||||
}
|
||||
export type userArray = Record<string, User>
|
||||
|
||||
@@ -14,10 +16,16 @@ export interface UsernameUser extends User {
|
||||
}
|
||||
export type usernameUserArray = Record<string, UsernameUser>
|
||||
|
||||
export const genUser = (fnname: string, testcase: string): User => {
|
||||
export const genUser = (fnname: string, testcase: string, info: info = {}): User => {
|
||||
const privID = `${fnname}-${testcase}-${genRandom(2)}` as UserID;
|
||||
const pubID = getHash(privID);
|
||||
return { privID, pubID, info: {} };
|
||||
return { privID, pubID, info };
|
||||
};
|
||||
|
||||
export const genAnonUser = (info: info = {}): User => {
|
||||
const privID = `user-${genRandom()}` as UserID;
|
||||
const pubID = getHash(privID);
|
||||
return { privID, pubID, info };
|
||||
};
|
||||
|
||||
export const genUsers = (fnname: string, testcase: string[]): userArray => {
|
||||
|
||||
Reference in New Issue
Block a user