Skip to content

Commit

Permalink
Add tests for authenticated access to para controller routes
Browse files Browse the repository at this point in the history
  • Loading branch information
canjalal committed Oct 17, 2024
1 parent 46ce087 commit 5b382d7
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/backend/routers/para.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ test("getParaById", async (t) => {
t.is(para?.user_id, user_id);
});

test("getParaById - paras do not have access", async (t) => {
const { trpc } = await getTestServer(t, { authenticateAs: UserType.Para });

const error = await t.throwsAsync(async () => {
await trpc.para.getParaById.query({
user_id: "user_id",
});
});

t.is(
error?.message,
"UNAUTHORIZED",
"Expected an 'unauthorized' error message"
);
});

test("getParaByEmail", async (t) => {
const { trpc, db } = await getTestServer(t, {
authenticateAs: UserType.CaseManager,
Expand All @@ -42,6 +58,22 @@ test("getParaByEmail", async (t) => {
t.is(para?.email, email);
});

test("getParaByEmail - paras do not have access", async (t) => {
const { trpc } = await getTestServer(t, { authenticateAs: UserType.Para });

const error = await t.throwsAsync(async () => {
await trpc.para.getParaByEmail.query({
email: "[email protected]",
});
});

t.is(
error?.message,
"UNAUTHORIZED",
"Expected an 'unauthorized' error message"
);
});

test("createPara", async (t) => {
const { trpc, db, nodemailerMock } = await getTestServer(t, {
authenticateAs: UserType.CaseManager,
Expand All @@ -68,6 +100,24 @@ test("createPara", async (t) => {
);
});

test("createPara - paras do not have access", async (t) => {
const { trpc } = await getTestServer(t, { authenticateAs: UserType.Para });

const error = await t.throwsAsync(async () => {
await trpc.para.createPara.mutate({
first_name: "Foo",
last_name: "Bar",
email: "[email protected]",
});
});

t.is(
error?.message,
"UNAUTHORIZED",
"Expected an 'unauthorized' error message"
);
});

test("paras are deduped by email", async (t) => {
const { trpc, db } = await getTestServer(t, {
authenticateAs: UserType.CaseManager,
Expand Down Expand Up @@ -200,3 +250,25 @@ test("getMyTasks", async (t) => {
t.is(task[0].instructions, INSTRUCTIONS);
t.is(task[0].trial_count, TRIAL_COUNT);
});

test("getMyTasks - paras do have access", async (t) => {
const { trpc } = await getTestServer(t, { authenticateAs: UserType.Para });

await t.notThrowsAsync(async () => {
await trpc.para.getMyTasks.query();
});
});

test("getMyTasks - regular users don't have access", async (t) => {
const { trpc } = await getTestServer(t, {});

const error = await t.throwsAsync(async () => {
await trpc.para.getMyTasks.query();
});

t.is(
error?.message,
"UNAUTHORIZED",
"Expected an 'unauthorized' error message"
);
});

0 comments on commit 5b382d7

Please sign in to comment.