-
Notifications
You must be signed in to change notification settings - Fork 0
Authorization middlewares and utils #4
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
BlazingAsher
left a comment
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.
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?) |
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.
I think it's fine enough. You'll always need a global admin role anyways.
* 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
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. |
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:
requireRoles()middleware to enforce access on a specific routeisUserType()for checking if a request has a certain level of access permissions