-
Notifications
You must be signed in to change notification settings - Fork 1
[API-69] Implement managed users endpoint #69
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8ee4133
add endpoint to get managed users
schottra c68fb7a
add tests and fix bad queries
schottra 7a4a4d7
200 test for managed_users endpoint
schottra d242a54
PR feedback
schottra e3ea040
optional bool helper
schottra 1577c72
Merge remote-tracking branch 'origin/main' into grants-endpoints-2
schottra 12dd70a
fix tests with new jsonAssert
schottra 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
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,92 @@ | ||
| package dbv1 | ||
|
|
||
| import ( | ||
| "context" | ||
| ) | ||
|
|
||
| type FullManagerGrant struct { | ||
| GetGrantsForUserIdRow | ||
| GranteeUserID *struct{} `json:"grantee_user_id,omitempty"` | ||
| } | ||
|
|
||
| type FullManagedUserGrant struct { | ||
| GetGrantsForGranteeUserIdRow | ||
| GranteeUserID *struct{} `json:"grantee_user_id,omitempty"` | ||
| } | ||
|
|
||
| type FullManager struct { | ||
| Manager FullUser `json:"manager"` | ||
| Grant FullManagerGrant `json:"grant"` | ||
| } | ||
|
|
||
| type FullManagedUser struct { | ||
| User FullUser `json:"user"` | ||
| Grant FullManagedUserGrant `json:"grant"` | ||
| } | ||
|
|
||
| func (q *Queries) FullManagers(ctx context.Context, params GetGrantsForUserIdParams) ([]FullManager, error) { | ||
|
|
||
| grants, err := q.GetGrantsForUserId(ctx, params) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| user_ids := make([]int32, len(grants)) | ||
| for i, grant := range grants { | ||
| user_ids[i] = int32(grant.GranteeUserID) | ||
| } | ||
|
|
||
| users, err := q.FullUsersKeyed(ctx, GetUsersParams{ | ||
| Ids: user_ids, | ||
| MyID: params.UserID, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| managers := make([]FullManager, len(grants)) | ||
| for i, grant := range grants { | ||
| managers[i] = FullManager{ | ||
| Manager: users[int32(grant.GranteeUserID)], | ||
| Grant: FullManagerGrant{GetGrantsForUserIdRow: grant}, | ||
| } | ||
| } | ||
|
|
||
| return managers, nil | ||
| } | ||
|
|
||
| func (q *Queries) FullManagedUsers(ctx context.Context, params GetGrantsForGranteeUserIdParams) ([]FullManagedUser, error) { | ||
| grants, err := q.GetGrantsForGranteeUserId(ctx, GetGrantsForGranteeUserIdParams{ | ||
| IsRevoked: params.IsRevoked, | ||
| IsApproved: params.IsApproved, | ||
| UserID: params.UserID, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| user_ids := make([]int32, len(grants)) | ||
| for i, grant := range grants { | ||
| user_ids[i] = int32(grant.UserID) | ||
| } | ||
|
|
||
| users, err := q.FullUsersKeyed(ctx, GetUsersParams{ | ||
| Ids: user_ids, | ||
| MyID: params.UserID, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| managedUsers := make([]FullManagedUser, len(grants)) | ||
| for i, grant := range grants { | ||
| managedUsers[i] = FullManagedUser{ | ||
| User: users[int32(grant.UserID)], | ||
| Grant: FullManagedUserGrant{GetGrantsForGranteeUserIdRow: grant}, | ||
| } | ||
| } | ||
|
|
||
| return managedUsers, nil | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "strconv" | ||
|
|
||
| "bridgerton.audius.co/api/dbv1" | ||
| "github.com/gofiber/fiber/v2" | ||
| "github.com/jackc/pgx/v5/pgtype" | ||
| ) | ||
|
|
||
| func (app *ApiServer) v1UsersManagedUsers(c *fiber.Ctx) error { | ||
| // Behavior of this field is a little odd. We only want to filter by it | ||
| // if it is passed, but otherwise not use a default value for either. | ||
| var isApproved *bool | ||
| if approvedStr := c.Query("is_approved"); approvedStr != "" { | ||
| parsed, err := strconv.ParseBool(approvedStr) | ||
| if err != nil { | ||
| return fiber.NewError(fiber.StatusBadRequest, "Invalid value for is_approved") | ||
| } | ||
| isApproved = &parsed | ||
| } | ||
|
|
||
| isRevoked, err := strconv.ParseBool(c.Query("is_revoked", "false")) | ||
| if err != nil { | ||
| return fiber.NewError(fiber.StatusBadRequest, "Invalid value for is_revoked") | ||
| } | ||
| params := dbv1.GetGrantsForGranteeUserIdParams{ | ||
| UserID: int32(c.Locals("userId").(int)), | ||
|
schottra marked this conversation as resolved.
Outdated
|
||
| IsApproved: pgtype.Bool{Bool: isApproved != nil && *isApproved, Valid: isApproved != nil}, | ||
| IsRevoked: isRevoked, | ||
| } | ||
|
|
||
| managedUsers, err := app.queries.FullManagedUsers(c.Context(), params) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return c.JSON(fiber.Map{ | ||
| "data": managedUsers, | ||
| }) | ||
| } | ||
This file contains hidden or 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,61 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "bridgerton.audius.co/api/dbv1" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| // Defaults to all approval status and no revoked managers | ||
| func TestGetManagedUsersNoParams(t *testing.T) { | ||
| var managedUsersResponse struct { | ||
| Data []dbv1.FullManagedUser | ||
| } | ||
| status, _ := testGet(t, "/v1/users/eYZmn/managed_users", &managedUsersResponse) | ||
| assert.Equal(t, 200, status) | ||
| assert.Equal(t, 2, len(managedUsersResponse.Data)) | ||
|
|
||
| assert.Equal(t, 1, int(managedUsersResponse.Data[0].User.ID)) | ||
| assert.Equal(t, false, managedUsersResponse.Data[0].Grant.IsApproved.Bool) | ||
| assert.Equal(t, 2, int(managedUsersResponse.Data[1].User.ID)) | ||
| assert.Equal(t, true, managedUsersResponse.Data[1].Grant.IsApproved.Bool) | ||
| } | ||
|
|
||
| // Should return only approved managers and default to not showing revoked managers | ||
| func TestGetManagedUsersApproved(t *testing.T) { | ||
| var managedUsersResponse struct { | ||
| Data []dbv1.FullManagedUser | ||
| } | ||
| status, _ := testGet(t, "/v1/users/eYZmn/managed_users?is_approved=true", &managedUsersResponse) | ||
| assert.Equal(t, 200, status) | ||
| assert.Equal(t, 1, len(managedUsersResponse.Data)) | ||
| assert.Equal(t, 2, int(managedUsersResponse.Data[0].User.ID)) | ||
| assert.Equal(t, true, managedUsersResponse.Data[0].Grant.IsApproved.Bool) | ||
| } | ||
|
|
||
| func TestGetManagedUsersRevoked(t *testing.T) { | ||
| var managedUsersResponse struct { | ||
| Data []dbv1.FullManagedUser | ||
| } | ||
| status, _ := testGet(t, "/v1/users/eYZmn/managed_users?is_revoked=true", &managedUsersResponse) | ||
| assert.Equal(t, 200, status) | ||
|
schottra marked this conversation as resolved.
|
||
| assert.Equal(t, 2, len(managedUsersResponse.Data)) | ||
| assert.Equal(t, 3, int(managedUsersResponse.Data[0].User.ID)) | ||
| assert.Equal(t, true, managedUsersResponse.Data[0].Grant.IsApproved.Bool) | ||
| assert.Equal(t, true, managedUsersResponse.Data[0].Grant.IsRevoked) | ||
| assert.Equal(t, 4, int(managedUsersResponse.Data[1].User.ID)) | ||
| assert.Equal(t, false, managedUsersResponse.Data[1].Grant.IsApproved.Bool) | ||
| assert.Equal(t, true, managedUsersResponse.Data[1].Grant.IsRevoked) | ||
| } | ||
|
|
||
| func TestGetManagedUsersInvalidParams(t *testing.T) { | ||
| var managedUsersResponse struct { | ||
| Data []dbv1.FullManagedUser | ||
| } | ||
| status, _ := testGet(t, "/v1/users/eYZmn/managed_users?is_approved=invalid", &managedUsersResponse) | ||
| assert.Equal(t, 400, status) | ||
|
|
||
| status, _ = testGet(t, "/v1/users/eYZmn/managed_users?is_revoked=invalid", &managedUsersResponse) | ||
| assert.Equal(t, 400, status) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.