Skip to content
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

Add Support for Nested Fields in isAuthorizedByUserId for Retrieving UserId #185

Merged
merged 6 commits into from
Mar 9, 2022

Conversation

albertlai431
Copy link
Contributor

@albertlai431 albertlai431 commented Mar 2, 2022

Ticket link

Closes #115

Implementation description

  • Created isAuthorizedForCreateShiftSignups check for createShiftSignups and isAuthorizedForUpdateShiftSignups check for updateShiftSignup

Steps to test

  1. Ensure that your local db contains the seed data
  2. docker-compose up --build
  3. Navigate to http://localhost:5000/graphql
  4. Run the following mutations and ensure the results are expected (Credits for the mutations to Joseph from Built out shift signup endpoints #90 🙏).

Sign in as a volunteer and use the access token for the following mutations:

# Create Shift Signups
mutation {
  createShiftSignups(
    shifts: [{ shiftId: 2, userId: 1, numVolunteers: 3, note: "test" }]
  ) {
    shiftId
    userId
    numVolunteers
    note
  }
}
# Expected result: error with "Failed authentication and/or authorization by userId"

mutation {
  createShiftSignups(
    shifts: [{ shiftId: 2, userId: 2, numVolunteers: 3, note: "test" }]
  ) {
    shiftId
    userId
    numVolunteers
    note
  }
}
# Expected result: succeed

# Update Shift Signup
mutation {
  updateShiftSignup(
    shiftId: "2"
    userId: "1"
    update: { status: CONFIRMED, note: "test", numVolunteers: 3 }
  ) {
    shiftId
    userId
    numVolunteers
    note
    status
  }
}
# Expected result: error with "Failed authentication and/or authorization by userId"

mutation {
  updateShiftSignup(
    shiftId: "2"
    userId: "2"
    update: { status: CONFIRMED, note: "test", numVolunteers: 3 }
  ) {
    shiftId
    userId
    numVolunteers
    note
    status
  }
}
# Expected result: succeed

Sign in as an admin and use the access token for the following mutations:

# Create Shift Signups
mutation {
  createShiftSignups(
    shifts: [{ shiftId: 2, userId: 1, numVolunteers: 3, note: "test" }]
  ) {
    shiftId
    userId
    numVolunteers
    note
  }
}
# Expected result: error with "Failed authentication and/or authorization by userId"

# Update Shift Signup
mutation {
  updateShiftSignup(
    shiftId: "2"
    userId: "2"
    update: { status: CONFIRMED, note: "test", numVolunteers: 3 }
  ) {
    shiftId
    userId
    numVolunteers
    note
    status
  }
}
# Expected result: succeed

Sign in as an employee and use the access token for the following mutations:

# Create Shift Signups
mutation {
  createShiftSignups(
    shifts: [{ shiftId: 2, userId: 3, numVolunteers: 3, note: "test" }]
  ) {
    shiftId
    userId
    numVolunteers
    note
  }
}
# Expected result: error with "Failed authentication and/or authorization by userId"

# Update Shift Signup
mutation {
  updateShiftSignup(
    shiftId: "2"
    userId: "3"
    update: { status: CONFIRMED, note: "test", numVolunteers: 3 }
  ) {
    shiftId
    userId
    numVolunteers
    note
    status
  }
}
# Expected result: error with "Failed authentication and/or authorization by userId"

What should reviewers focus on?

  • Ensuring that the mutations above produce the expected result

Checklist

  • My PR name is descriptive and in imperative tense
  • My commit messages are descriptive and in imperative tense. My commits are atomic and trivial commits are squashed or fixup'd into non-trivial commits
  • I have run the appropriate linter(s)
  • I have requested a review from the PL, as well as other devs who have background knowledge on this PR or who will be building on top of this PR

backend/graphql/index.ts Outdated Show resolved Hide resolved
@albertlai431 albertlai431 requested a review from LenaNguyen March 2, 2022 13:48
Copy link
Member

@LenaNguyen LenaNguyen left a comment

Choose a reason for hiding this comment

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

Not sure if this is just me, but it seems like the authorization checks aren't working now? I was able to create a shift for any user but also with any role 🤔 Not sure what's happening yet

backend/middlewares/auth.ts Outdated Show resolved Hide resolved
@albertlai431 albertlai431 requested a review from LenaNguyen March 4, 2022 20:51
Copy link
Member

@LenaNguyen LenaNguyen left a comment

Choose a reason for hiding this comment

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

Everything works great now! Thank you for looking into this! Just some tips on optimizing the code but aside from that it's looking good :)

backend/middlewares/auth.ts Outdated Show resolved Hide resolved
backend/middlewares/auth.ts Outdated Show resolved Hide resolved
backend/middlewares/auth.ts Outdated Show resolved Hide resolved
@albertlai431 albertlai431 requested a review from LenaNguyen March 8, 2022 13:36
Copy link
Member

@LenaNguyen LenaNguyen left a comment

Choose a reason for hiding this comment

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

Thanks for incorporating the changes. This looks great!

@albertlai431 albertlai431 merged commit 52272bf into main Mar 9, 2022
@albertlai431 albertlai431 deleted the albert/middleware-user-id branch March 9, 2022 02:51
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.

Middleware to check user id for nested fields
2 participants