Skip to content

[#152] Test: created integration test for supabase/middleware#402

Open
alexappleget wants to merge 21 commits intodevelopfrom
alex/152-create-integration-test-for-supabase-middleware
Open

[#152] Test: created integration test for supabase/middleware#402
alexappleget wants to merge 21 commits intodevelopfrom
alex/152-create-integration-test-for-supabase-middleware

Conversation

@alexappleget
Copy link
Copy Markdown
Collaborator

@alexappleget alexappleget commented May 12, 2025

Closes #152

All tests pass and I double checked by forcing them to fail to make sure it was testing correctly.

And since I touched the middleware file to move it into its own folder and write a test for it, I handled the eslint errors for the middleware.ts as well.

IMPORTANT: I edited the middleware.ts file because it was needed to even make any of these tests pass and be accurate.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
elecretanta ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 1, 2025 10:24pm
elecretanta-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 1, 2025 10:24pm
elecretanta-unit-test ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 1, 2025 10:24pm

Comment thread lib/supabase/middleware/middleware.ts
Comment thread lib/supabase/middleware/middleware.ts
Copy link
Copy Markdown
Contributor

@mathematiCode mathematiCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to mock the page redirects, but I do think we should have an actual test database with test users to make sure they can only access the appropriate pages. Otherwise, really nice job on this!

Comment thread lib/supabase/middleware/middleware.test.ts Outdated

describe('Middleware', () => {
it('redirects unauthenticated user back to /', async () => {
const request = new NextRequest('https://localhost:4000/dashboard');
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're testing local or staging?

Copy link
Copy Markdown
Member

@nickytonline nickytonline Jun 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also make the base URL an environment variable or at a minimum a constant and then you can do something like this.

Suggested change
const request = new NextRequest('https://localhost:4000/dashboard');
const request = new NextRequest(`${new URL('/dashboard', BASE_APP_URL)}`);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't make this change yet as the staging/production url has not been set up yet on vercel


describe('Middleware', () => {
it('redirects unauthenticated user back to /', async () => {
const request = new NextRequest('https://localhost:4000/dashboard');
Copy link
Copy Markdown
Member

@nickytonline nickytonline Jun 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also make the base URL an environment variable or at a minimum a constant and then you can do something like this.

Suggested change
const request = new NextRequest('https://localhost:4000/dashboard');
const request = new NextRequest(`${new URL('/dashboard', BASE_APP_URL)}`);

const {
data: { user },
} = await supabase.auth.getUser();
const accessToken = request.cookies.get('sb-access-token')?.value;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I’ve read, @supabase/ssr doesn’t include the createMiddlewareClient helper like the older @supabase/auth-helpers-nextjs package did. So manually reading the cookie to get the access token is the correct approach here.

That said, does this mean middleware-based auth hasn’t been working since we switched to @supabase/ssr? Just wondering if we had a gap in session handling before this change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just following up on this @alexappleget

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I was writing the test, I had to log in via the email way so that way we can use test users and not the google auth. When doing so, I noticed that the test kept throwing null for the user and that it couldn't find it. After digging, I found that the issue was happening here with fetching the user. So i added in that the helper function could grab the accesstoken itself, and if so then it fetches the user. Thinking about it, I do believe supabase had a gap in the auth and my test caught it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auth was working for google auth, but i believe we need to clean up our auth context as well. it's not handling things correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create integration test for supabase/middleware.ts

4 participants