Skip to content

Commit

Permalink
Merge pull request #561 from mchangrh/test-helpers
Browse files Browse the repository at this point in the history
long overdue test helpers (partial)
  • Loading branch information
ajayyy authored Oct 15, 2023
2 parents 9dd8b28 + 68bb39c commit 5714f51
Show file tree
Hide file tree
Showing 25 changed files with 1,298 additions and 1,622 deletions.
4 changes: 2 additions & 2 deletions src/utils/getSubmissionUUID.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getHash } from "./getHash";
import { HashedValue } from "../types/hash.model";
import { ActionType, VideoID, Service, Category } from "../types/segments.model";
import { UserID } from "../types/user.model";
import { HashedUserID } from "../types/user.model";

export function getSubmissionUUID(
videoID: VideoID,
category: Category,
actionType: ActionType,
description: string,
userID: UserID,
userID: HashedUserID,
startTime: number,
endTime: number,
service: Service
Expand Down
22 changes: 22 additions & 0 deletions test/case_boilerplate.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { db } from "../../src/databases/databases";
import assert from "assert";
import { client } from "../utils/httpClient";
import { genUsers, User } from "../utils/genUser";
import { insertSegment, insertVip } from "../utils/queryGen";

const endpoint = "/api/endpoint";

const postTestEndpoint = () => client({
method: "POST",
url: endpoint,
data: {
}
});

const cases = [
"firstCase",
"secondCase",
"thirdCase"
];
const users = genUsers("endpoint", cases);
const vipUser = genUser("endpoint", "vip");
69 changes: 29 additions & 40 deletions test/cases/addFeatures.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,57 @@
import assert from "assert";
import { db } from "../../src/databases/databases";
import { Feature, HashedUserID } from "../../src/types/user.model";
import { Feature } from "../../src/types/user.model";
import { hasFeature } from "../../src/utils/features";
import { getHash } from "../../src/utils/getHash";
import { client } from "../utils/httpClient";
import { grantFeature, insertVip } from "../utils/queryGen";
import { User, genUser, genUsers } from "../utils/genUser";

const endpoint = "/api/feature";

const postAddFeatures = (userID: string, adminUserID: string, feature: Feature, enabled: string) => client({
const postAddFeatures = (userID: string, adminUserID: string, feature: Feature, enabled: boolean) => client({
method: "POST",
url: endpoint,
data: {
userID,
feature,
enabled,
enabled: String(enabled),
adminUserID
}
});

const privateVipUserID = "VIPUser-addFeatures";
const vipUserID = getHash(privateVipUserID);
const cases = [
"grant",
"remove",
"update"
];
const users = genUsers("addFeatures", cases);
const vipUser = genUser("addFeatures", "vip");

const hashedUserID1 = "user1-addFeatures" as HashedUserID;
const hashedUserID2 = "user2-addFeatures" as HashedUserID;
const hashedUserID3 = "user3-addFeatures" as HashedUserID;
const testedFeature = Feature.ChapterSubmitter;
const validFeatures = [testedFeature];

const validFeatures = [Feature.ChapterSubmitter];
const updateValidateFeature = (user: User, feature: Feature, grant: boolean, issuer: User): Promise<void> =>
postAddFeatures(user.pubID, issuer.privID, feature, grant)
.then(res => assert.strictEqual(res.status, 200)) // ensure request was successful
.then(() => hasFeature(user.pubID, feature))
.then(result => assert.strictEqual(result, grant)); // ensure user has new feature

describe("addFeatures", () => {
before(() => {
const userFeatureQuery = `INSERT INTO "userFeatures" ("userID", "feature", "issuerUserID", "timeSubmitted") VALUES(?, ?, ?, ?)`;

return Promise.all([
db.prepare("run", `INSERT INTO "vipUsers" ("userID") VALUES (?)`, [vipUserID]),

db.prepare("run", userFeatureQuery, [hashedUserID2, Feature.ChapterSubmitter, "some-user", 0]),
db.prepare("run", userFeatureQuery, [hashedUserID3, Feature.ChapterSubmitter, "some-user", 0])
]);
before(async () => {
await insertVip(db, vipUser.pubID);
await grantFeature(db, users["remove"].pubID, testedFeature, vipUser.pubID);
await grantFeature(db, users["update"].pubID, testedFeature, vipUser.pubID);
});

it("can add features", async () => {
it("can add features", (done) => {
for (const feature of validFeatures) {
const result = await postAddFeatures(hashedUserID1, privateVipUserID, feature, "true");
assert.strictEqual(result.status, 200);

assert.strictEqual(await hasFeature(hashedUserID1, feature), true);
updateValidateFeature(users["grant"], feature, true, vipUser)
.catch(err => done(err));
}
done();
});

it("can remove features", async () => {
const feature = Feature.ChapterSubmitter;
it("can remove features", () => updateValidateFeature(users["remove"], testedFeature, false, vipUser));

const result = await postAddFeatures(hashedUserID2, privateVipUserID, feature, "false");
assert.strictEqual(result.status, 200);

assert.strictEqual(await hasFeature(hashedUserID2, feature), false);
});

it("can update features", async () => {
const feature = Feature.ChapterSubmitter;

const result = await postAddFeatures(hashedUserID3, privateVipUserID, feature, "true");
assert.strictEqual(result.status, 200);

assert.strictEqual(await hasFeature(hashedUserID3, feature), true);
});
it("can update features", () => updateValidateFeature(users["update"], testedFeature, true, vipUser));
});
176 changes: 64 additions & 112 deletions test/cases/addUserAsVIP.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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));
});
});
Loading

0 comments on commit 5714f51

Please sign in to comment.