Skip to content

Conversation

@danielp1218
Copy link
Contributor

@danielp1218 danielp1218 commented Dec 16, 2025

Added some middlewares and utils we can use for enforcing role-specific access to certain routes. (e.x. admin-only, or hacker-only routes). Also adapted existing systems to the new system + fixed up some minor dev details.

Specifically:

  • Added a requireRoles() middleware to enforce access on a specific route
  • Added isUserType() for checking if a request has a certain level of access permissions

This comment was marked as resolved.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@hack-the-6ix hack-the-6ix deleted a comment from Copilot AI Dec 16, 2025
@danielp1218 danielp1218 changed the title Daniel/auth handling Authorization middlewares and utils Dec 16, 2025
Copy link
Member

@BlazingAsher BlazingAsher left a comment

Choose a reason for hiding this comment

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

tbh I am not a general fan of mocking so much. I think you should better separate usage of the db and the service functions.

for example, for isUserType, you should have a function called "fetchUser" or something similar that is called inside the function to retrieve the user. Then, using that output, perform logic on it.

that way, you can have an integration test (with a real db) for the fetchUser function, then mock only fetchUser to return users depending on that you're trying to test. this provides a much better separation of concerns and should make the tests way less verbose.

TLDR: I would move DB access outside service functions, so there's a more clear line for where you need a unit vs integration test.

also the route tests I would always make to be an integration test.

src/lib/auth.ts Outdated

if (userType === UserType.User) return true; // TODO: check if userId exists in system

// need to check admin table separately since admins are not season-specific (NOTE: this feels bad for consistency, would it be better to have season-specific admins?)
Copy link
Member

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 enough. You'll always need a global admin role anyways.

@danielp1218 danielp1218 mentioned this pull request Dec 27, 2025
Lindsay-X and others added 2 commits December 27, 2025 14:01
* fix: change to composite keys and add form names to form

* fix: fix format

* fix: added form composite keys, remove specific user id

* fix: format

* fix: added missing keys and constraints

* fix: format fix
@danielp1218
Copy link
Contributor Author

for example, for isUserType, you should have a function called "fetchUser" or something similar that is called inside the function to retrieve the user. Then, using that output, perform logic on it.

I already had a queryUserType, I just forgot to update my tests to do them individually...

More generally, our tests are really bloated because doing unit tests on DB access is really annoying and integration testing is not so easy either (bun doesn't support testcontainers which is the only easy way I know 😭). I agree that mocking so much feels really pointless though. Will likely have to fix later, just depends on priorities.

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.

4 participants