Repository navigation
Follow next_page_token in SearchEntitlements #156
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
Draft
chinkulkarni
wants to merge
1
commit into
main
Choose a base branch
from
chinmay.kulkarni/search-entitlements-follow-page-token
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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 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,102 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/conductorone/conductorone-sdk-go/pkg/models/shared" | ||
| ) | ||
|
|
||
| // searchPage builds a search response page whose single entitlement points at | ||
| // its own expanded[0] app, mirroring the server's per-response indexing. | ||
| func searchPage(entID, appID, nextToken string) map[string]any { | ||
| page := map[string]any{ | ||
| "list": []any{ | ||
| map[string]any{ | ||
| "entitlement": map[string]any{ | ||
| "appEntitlement": map[string]any{"id": entID, "appId": appID}, | ||
| "appPath": "$.expanded[0]", | ||
| }, | ||
| }, | ||
| }, | ||
| "expanded": []any{ | ||
| map[string]any{"@type": atTypeApp, "id": appID, "displayName": "App " + appID}, | ||
| }, | ||
| } | ||
| if nextToken != "" { | ||
| page["nextPageToken"] = nextToken | ||
| } | ||
| return page | ||
| } | ||
|
|
||
| func TestSearchEntitlementsFollowsPageToken(t *testing.T) { | ||
| var tokens []string | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| var req shared.RequestCatalogSearchServiceSearchEntitlementsRequest | ||
| if err := json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
| t.Errorf("Decode() unexpected error: %v", err) | ||
| } | ||
| if req.PageSize == nil || *req.PageSize != 100 { | ||
| t.Errorf("page size = %v, want 100", req.PageSize) | ||
| } | ||
| tokens = append(tokens, StringFromPtr(req.PageToken)) | ||
|
|
||
| var page map[string]any | ||
| switch StringFromPtr(req.PageToken) { | ||
| case "": | ||
| page = searchPage("e1", "a1", "t1") | ||
| case "t1": | ||
| page = searchPage("e2", "a2", "t2") | ||
| case "t2": | ||
| page = searchPage("e3", "a3", "") | ||
| default: | ||
| t.Errorf("unexpected page token %q", StringFromPtr(req.PageToken)) | ||
| } | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _ = json.NewEncoder(w).Encode(page) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| c := newPaperSecretTestClient(server.URL, server.Client()) | ||
| got, err := c.SearchEntitlements(context.Background(), &SearchEntitlementsFilter{}) | ||
| if err != nil { | ||
| t.Fatalf("SearchEntitlements() unexpected error: %v", err) | ||
| } | ||
|
|
||
| if want := []string{"", "t1", "t2"}; fmt.Sprint(tokens) != fmt.Sprint(want) { | ||
| t.Fatalf("page tokens = %v, want %v", tokens, want) | ||
| } | ||
| if len(got) != 3 { | ||
| t.Fatalf("got %d entitlements, want 3", len(got)) | ||
| } | ||
| for i, id := range []string{"1", "2", "3"} { | ||
| if gotID := StringFromPtr(got[i].Entitlement.ID); gotID != "e"+id { | ||
| t.Errorf("entitlement[%d] id = %q, want e%s", i, gotID, id) | ||
| } | ||
| // Every page uses expanded[0]; each entitlement must resolve to its own page's app. | ||
| app := GetExpanded[shared.App](got[i], ExpandedApp) | ||
| if app == nil || StringFromPtr(app.ID) != "a"+id { | ||
| t.Errorf("entitlement[%d] expanded app = %v, want a%s", i, app, id) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestSearchEntitlementsHonorsContextCancellation(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| // Always advertise another page; cancel after the first response. | ||
| cancel() | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _ = json.NewEncoder(w).Encode(searchPage("e", "a", "next")) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| c := newPaperSecretTestClient(server.URL, server.Client()) | ||
| if _, err := c.SearchEntitlements(ctx, &SearchEntitlementsFilter{}); err == nil { | ||
| t.Fatal("SearchEntitlements() error = nil, want context error") | ||
| } | ||
| } |
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.
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.
🟡 Suggestion (medium confidence): nothing guards against a server that keeps returning the same
next_page_token(or a cycle of tokens). That case loops forever and keeps growingrvuntil ctx is cancelled, and most CLI callers pass a context with no deadline. Consider breaking or erroring when*body.NextPageToken == StringFromPtr(pageToken), and/or capping the page count.