-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auth User Middleware #11
Merged
Merged
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
94dd9db
first draft
owens1127 0ee9d0a
doc
owens1127 78afd39
Added script to seed the database and then run the tests
jpraissman 50f340a
Created tests for get-authenticated-user
jpraissman 1418098
Created mock for cookies
jpraissman bf9f0b0
Updated README
jpraissman 44314e3
Merge remote-tracking branch 'origin/main' into auth-user-middleware
jpraissman 0e72982
Github Actions runs the seed command before running the tests
jpraissman 86583d3
Removed test:seed command because that is not needed
jpraissman 7385fe9
Removed test:seed command because that is not needed
jpraissman 48d1643
Updated MockNextCookies to have an apply method
jpraissman a5a5532
Updated tests to use the new apply method on the mock cookies and to …
jpraissman 86a308f
Delete database records after now
jpraissman c6c3bed
Reverted changes
jpraissman d8804d9
Removed console.log statement
jpraissman d66e879
Merged main into branch
jpraissman d09de21
Updated auth builder and tests to use id instead of token
jpraissman 4fc50e8
Merged in main
jpraissman 10a265a
Updated get method to fix return value issue
jpraissman e91faa1
sessionToken -> sessionId, uses Promise.all(), and added test to make…
jpraissman fb01079
Fixed schema and all related things
jpraissman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { authenticatedProcedureBuilder } from "../internal/init"; | ||
|
||
export const getAuthenticatedUserProcedure = | ||
authenticatedProcedureBuilder.query(({ ctx }) => { | ||
return ctx.user; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,16 @@ | ||
.. | ||
# Tests | ||
|
||
## Mocks | ||
|
||
### MockNextCookies | ||
|
||
This is a mock for the cookies function in next/headers. In your tests, just instantiate | ||
a new MockNextCookies object, set the cookies you would like, and run the apply() method | ||
on the newly created object. | ||
|
||
Example usage: | ||
|
||
<pre>const cookies = new MockNextCookies(); | ||
cookies.set("myKey", "myValue"); | ||
cookies.apply(); | ||
... rest of your test</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { afterAll, beforeAll, expect, test } from "bun:test"; | ||
|
||
import { prisma } from "@good-dog/db"; | ||
import { _trpcCaller } from "@good-dog/trpc/server"; | ||
|
||
import { MockNextCookies } from "../mocks/MockNextCookies"; | ||
|
||
// Seeds the database before running the tests | ||
beforeAll(async () => { | ||
const person1 = await prisma.user.upsert({ | ||
where: { email: "person1@prisma.io" }, | ||
update: {}, | ||
create: { | ||
email: "person1@prisma.io", | ||
name: "Person 1", | ||
password: "person1Password", | ||
}, | ||
}); | ||
await prisma.session.upsert({ | ||
where: { id: 500 }, | ||
update: { | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
create: { | ||
userId: person1.id, | ||
id: 500, | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
}); | ||
|
||
const person2 = await prisma.user.upsert({ | ||
where: { email: "person2@gmail.com" }, | ||
update: {}, | ||
create: { | ||
email: "person2@gmail.com", | ||
name: "Person2 Jones", | ||
password: "person2Password", | ||
}, | ||
}); | ||
await prisma.session.upsert({ | ||
where: { id: 501 }, | ||
update: { | ||
expiresAt: new Date(new Date().setFullYear(new Date().getFullYear() - 1)), | ||
}, | ||
create: { | ||
userId: person2.id, | ||
id: 501, | ||
expiresAt: new Date(new Date().setFullYear(new Date().getFullYear() - 1)), | ||
}, | ||
}); | ||
await prisma.session.upsert({ | ||
where: { id: 502 }, | ||
update: { | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
create: { | ||
userId: person2.id, | ||
id: 502, | ||
expiresAt: new Date( | ||
new Date().setFullYear(new Date().getFullYear() + 10), | ||
), | ||
}, | ||
}); | ||
}); | ||
|
||
test("Correct user is returned when they have a valid session.", async () => { | ||
// Set the cookies | ||
const cookies = new MockNextCookies(); | ||
cookies.set("sessionToken", "500"); | ||
await cookies.apply(); | ||
|
||
const user = await _trpcCaller.user(); | ||
|
||
expect(user.email).toEqual("person1@prisma.io"); | ||
}); | ||
|
||
test("Correct user is returned when they have multiple sessions and one is valid.", async () => { | ||
// Set the cookies | ||
const cookies = new MockNextCookies(); | ||
cookies.set("sessionToken", "502"); | ||
await cookies.apply(); | ||
|
||
const user = await _trpcCaller.user(); | ||
|
||
expect(user.email).toEqual("person2@gmail.com"); | ||
}); | ||
|
||
test("'UNAUTHORIZED' error is thrown when no session is found for the token.", async () => { | ||
// Set the cookies | ||
const cookies = new MockNextCookies(); | ||
cookies.set("sessionToken", "503"); | ||
await cookies.apply(); | ||
|
||
const getUser = async () => await _trpcCaller.user(); | ||
|
||
expect(getUser).toThrow("UNAUTHORIZED"); | ||
}); | ||
|
||
test("'UNAUTHORIZED' error is thrown when there is no 'sessionToken' cookie.", async () => { | ||
const cookies = new MockNextCookies(); | ||
await cookies.apply(); | ||
|
||
const getUser = async () => await _trpcCaller.user(); | ||
|
||
expect(getUser).toThrow("UNAUTHORIZED"); | ||
}); | ||
|
||
test("'UNAUTHORIZED' error is thrown when session is expired.", async () => { | ||
// Set the cookies | ||
const cookies = new MockNextCookies(); | ||
cookies.set("sessionToken", "501"); | ||
await cookies.apply(); | ||
|
||
const getUser = async () => await _trpcCaller.user(); | ||
|
||
expect(getUser).toThrow("UNAUTHORIZED"); | ||
}); | ||
|
||
// Delete the records created for these tests | ||
afterAll(async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can wrap this in a |
||
await prisma.session.delete({ | ||
where: { id: 500 }, | ||
}); | ||
await prisma.session.delete({ | ||
where: { id: 501 }, | ||
}); | ||
await prisma.session.delete({ | ||
where: { id: 502 }, | ||
}); | ||
await prisma.user.delete({ | ||
where: { email: "person1@prisma.io" }, | ||
}); | ||
await prisma.user.delete({ | ||
where: { email: "person2@gmail.com" }, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { mock } from "bun:test"; | ||
|
||
// A mock for the cookies function in the NextJS next/header module. | ||
export class MockNextCookies { | ||
private cookiesMap: Map<string, string>; | ||
|
||
constructor() { | ||
this.cookiesMap = new Map<string, string>(); | ||
} | ||
|
||
// Applies this mock to be the cookies used by the next/header module. This method | ||
// must be called in order for this mock to be applied. | ||
async apply(): Promise<void> { | ||
await mock.module("next/headers", () => ({ | ||
cookies: () => this, | ||
})); | ||
} | ||
|
||
set(key: string, value: string): void { | ||
this.cookiesMap.set(key, value); | ||
} | ||
|
||
get(key: string): { value: string | undefined } { | ||
const requestCookie = { | ||
value: this.cookiesMap.get(key), | ||
}; | ||
return requestCookie; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's