Skip to content

Commit

Permalink
Team endpoint middleware refactor (#625)
Browse files Browse the repository at this point in the history
* Add db method to get user team role info

* Update teamUserOnly middleware to get team user role info with new query

* Update teamAdminOnly middleware to chain off teamUserOnly middleware

* Add e2e seeder class in prep for adding e2e tests for middleware changes

* Add teamUserOnly middleware unit tests

* format go

* Add unit tests for teamAdminOnly middleware
  • Loading branch information
StevenWeathers authored Sep 22, 2024
1 parent a1614c6 commit 072183e
Show file tree
Hide file tree
Showing 83 changed files with 3,657 additions and 1,344 deletions.
32 changes: 11 additions & 21 deletions e2e/fixtures/db/admin-user.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
import { ThunderdomeSeeder } from "./seeder";

const adminUser = {
name: "E2E ADMIN",
email: "[email protected]",
password: "kentRules!",
hashedPass: "$2a$10$3CvuzyoGIme3dJ4v9BnvyOIKFxEaYyjV2Lfunykv0VokGf/twxi9m",
rank: "ADMIN",
type: "ADMIN",
};

const seed = async (pool) => {
const newUser = await pool.query(
`SELECT userid, verifyid FROM thunderdome.user_register($1, $2, $3, $4);`,
[adminUser.name, adminUser.email, adminUser.hashedPass, adminUser.rank],
const seeder = new ThunderdomeSeeder(pool);
const { id } = await seeder.createUser(
adminUser.name,
adminUser.email,
adminUser.type,
true,
);

await pool.query("call thunderdome.user_account_verify($1);", [
newUser.rows[0].verifyid,
]);
const id = newUser.rows[0].userid;

return {
...adminUser,
id,
};
};

const teardown = async (pool) => {
const oldUser = await pool.query(
`SELECT id FROM thunderdome.users WHERE email = $1;`,
[adminUser.email],
);

if (oldUser.rows.length) {
await pool.query("DELETE FROM thunderdome.users WHERE id = $1;", [
oldUser.rows[0].id,
]);
}
const seeder = new ThunderdomeSeeder(pool);
await seeder.deleteUserByEmail(adminUser.email);

return {};
};
Expand Down
50 changes: 16 additions & 34 deletions e2e/fixtures/db/adminapi-user.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
import { ThunderdomeSeeder } from "./seeder";

export const adminAPIUser = {
name: "E2E Admin API User",
name: "E2EAdminAPIUser",
email: "[email protected]",
password: "kentRules!",
hashedPass: "$2a$10$3CvuzyoGIme3dJ4v9BnvyOIKFxEaYyjV2Lfunykv0VokGf/twxi9m",
rank: "ADMIN",
type: "ADMIN",
apikey: "Gssy-ffy.okeTA-3AJhCnY1sqeUvRPRHiNYIVUxs4",
};

const seed = async (pool) => {
const newUser = await pool.query(
`SELECT userid, verifyid FROM thunderdome.user_register($1, $2, $3, $4);`,
[
adminAPIUser.name,
adminAPIUser.email,
adminAPIUser.hashedPass,
adminAPIUser.rank,
],
const seeder = new ThunderdomeSeeder(pool);
const { id } = await seeder.createUser(
adminAPIUser.name,
adminAPIUser.email,
adminAPIUser.type,
true,
);
const id = newUser.rows[0].userid;

await pool.query("call thunderdome.user_account_verify($1);", [
newUser.rows[0].verifyid,
]);

await pool.query(
`INSERT INTO thunderdome.api_key (id, user_id, name, active) VALUES ($1, $2, $3, TRUE);`,
[
"Gssy-ffy.e170ffced2ae5806aebc103f30255dc5cc1b9e203d6035aa817f2b7e6638f223",
id,
"test api key 2",
],
await seeder.addUserAPIKey(
id,
"Gssy-ffy.e170ffced2ae5806aebc103f30255dc5cc1b9e203d6035aa817f2b7e6638f223",
"test api key 2",
);

return {
Expand All @@ -39,16 +29,8 @@ const seed = async (pool) => {
};

const teardown = async (pool) => {
const oldUser = await pool.query(
`SELECT id FROM thunderdome.users WHERE email = $1;`,
[adminAPIUser.email],
);

if (oldUser.rows.length) {
await pool.query("DELETE FROM thunderdome.users WHERE id = $1;", [
oldUser.rows[0].id,
]);
}
const seeder = new ThunderdomeSeeder(pool);
await seeder.deleteUserByEmail(adminAPIUser.email);

return {};
};
Expand Down
45 changes: 16 additions & 29 deletions e2e/fixtures/db/api-user.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import { ThunderdomeSeeder } from "./seeder";

export const apiUser = {
name: "E2E API User",
name: "E2EAPIUser",
email: "[email protected]",
password: "kentRules!",
hashedPass: "$2a$10$3CvuzyoGIme3dJ4v9BnvyOIKFxEaYyjV2Lfunykv0VokGf/twxi9m",
rank: "REGISTERED",
type: "REGISTERED",
apikey: "8MenPkY8.Vqvkh030vv7$rSyYs1gt++L0v7wKuVgR",
};

const seed = async (pool) => {
const newUser = await pool.query(
`SELECT userid, verifyid FROM thunderdome.user_register($1, $2, $3, $4);`,
[apiUser.name, apiUser.email, apiUser.hashedPass, apiUser.rank],
const seeder = new ThunderdomeSeeder(pool);
const { id } = await seeder.createUser(
apiUser.name,
apiUser.email,
apiUser.type,
true,
);
const id = newUser.rows[0].userid;

await pool.query("call thunderdome.user_account_verify($1);", [
newUser.rows[0].verifyid,
]);

await pool.query(
`INSERT INTO thunderdome.api_key (id, user_id, name, active) VALUES ($1, $2, $3, TRUE);`,
[
"8MenPkY8.cd737cbc4bdca1838bdcf1685b00a9a778261255c10193714d9ba1630b55b63c",
id,
"test apikey",
],
await seeder.addUserAPIKey(
id,
"8MenPkY8.cd737cbc4bdca1838bdcf1685b00a9a778261255c10193714d9ba1630b55b63c",
"test apikey",
);

return {
Expand All @@ -34,16 +29,8 @@ const seed = async (pool) => {
};

const teardown = async (pool) => {
const oldUser = await pool.query(
`SELECT id FROM thunderdome.users WHERE email = $1;`,
[apiUser.email],
);

if (oldUser.rows.length) {
await pool.query("DELETE FROM thunderdome.users WHERE id = $1;", [
oldUser.rows[0].id,
]);
}
const seeder = new ThunderdomeSeeder(pool);
await seeder.deleteUserByEmail(apiUser.email);

return {};
};
Expand Down
33 changes: 11 additions & 22 deletions e2e/fixtures/db/registered-delete-user.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { ThunderdomeSeeder } from "./seeder";

export const registeredDeleteUser = {
name: "E2E Delete User",
email: "[email protected]",
password: "kentRules!",
hashedPass: "$2a$10$3CvuzyoGIme3dJ4v9BnvyOIKFxEaYyjV2Lfunykv0VokGf/twxi9m",
rank: "REGISTERED",
type: "REGISTERED",
};

const seed = async (pool) => {
const newUser = await pool.query(
`SELECT userid, verifyid FROM thunderdome.user_register($1, $2, $3, $4);`,
[
registeredDeleteUser.name,
registeredDeleteUser.email,
registeredDeleteUser.hashedPass,
registeredDeleteUser.rank,
],
const seeder = new ThunderdomeSeeder(pool);
const { id } = await seeder.createUser(
registeredDeleteUser.name,
registeredDeleteUser.email,
registeredDeleteUser.type,
false,
);
const id = newUser.rows[0].userid;

return {
...registeredDeleteUser,
Expand All @@ -25,16 +22,8 @@ const seed = async (pool) => {
};

const teardown = async (pool) => {
const oldUser = await pool.query(
`SELECT id FROM thunderdome.users WHERE email = $1;`,
[registeredDeleteUser.email],
);

if (oldUser.rows.length) {
await pool.query("DELETE FROM thunderdome.users WHERE id = $1;", [
oldUser.rows[0].id,
]);
}
const seeder = new ThunderdomeSeeder(pool);
await seeder.deleteUserByEmail(registeredDeleteUser.email);

return {};
};
Expand Down
33 changes: 11 additions & 22 deletions e2e/fixtures/db/registered-user.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { ThunderdomeSeeder } from "./seeder";

export const registeredUser = {
name: "E2E Registered User",
email: "[email protected]",
password: "kentRules!",
hashedPass: "$2a$10$3CvuzyoGIme3dJ4v9BnvyOIKFxEaYyjV2Lfunykv0VokGf/twxi9m",
rank: "REGISTERED",
type: "REGISTERED",
};

const seed = async (pool) => {
const newUser = await pool.query(
`SELECT userid, verifyid FROM thunderdome.user_register($1, $2, $3, $4);`,
[
registeredUser.name,
registeredUser.email,
registeredUser.hashedPass,
registeredUser.rank,
],
const seeder = new ThunderdomeSeeder(pool);
const { id } = await seeder.createUser(
registeredUser.name,
registeredUser.email,
registeredUser.type,
false,
);
const id = newUser.rows[0].userid;

return {
...registeredUser,
Expand All @@ -25,16 +22,8 @@ const seed = async (pool) => {
};

const teardown = async (pool) => {
const oldUser = await pool.query(
`SELECT id FROM thunderdome.users WHERE email = $1;`,
[registeredUser.email],
);

if (oldUser.rows.length) {
await pool.query("DELETE FROM thunderdome.users WHERE id = $1;", [
oldUser.rows[0].id,
]);
}
const seeder = new ThunderdomeSeeder(pool);
await seeder.deleteUserByEmail(registeredUser.email);

return {};
};
Expand Down
Loading

0 comments on commit 072183e

Please sign in to comment.