-
Notifications
You must be signed in to change notification settings - Fork 4
api/v1: public stats and recent reversals endpoints #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
Open
ZukwiZ
wants to merge
1
commit into
master
Choose a base branch
from
feat/public-stats-api
base: master
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.
Open
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
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,240 @@ | ||
| package reversals | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "reverse-watch/domain/models" | ||
| "reverse-watch/domain/models/constants" | ||
| "reverse-watch/errors" | ||
| "reverse-watch/internal/testutil" | ||
| "reverse-watch/middleware" | ||
| "reverse-watch/repository/factory" | ||
| "reverse-watch/secret" | ||
| "reverse-watch/util" | ||
|
|
||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| func buildRecentHandlerStack(t *testing.T) (http.Handler, *gorm.DB) { | ||
| t.Helper() | ||
|
|
||
| db := testutil.NewTestDB(t) | ||
| keygen := secret.NewKeyGenerator(constants.EnvironmentDevelopment) | ||
| f, err := factory.NewFactoryWithConfig(&factory.Config{ | ||
| PrivateDB: db, | ||
| PublicDB: db, | ||
| KeyGen: keygen, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("NewFactoryWithConfig(): %v", err) | ||
| } | ||
|
|
||
| handler := http.HandlerFunc(listRecentHandler) | ||
| return middleware.FactoryMiddleware(f)(handler), db | ||
| } | ||
|
|
||
| func TestListRecentHandler(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| handler, db := buildRecentHandlerStack(t) | ||
|
|
||
| base := models.Epoch + 1000 | ||
|
|
||
| // 5 rows, monotonically increasing CreatedAt. Row id=3 is expunged. | ||
| testutil.Insert(t, db, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 1, CreatedAt: base + 100}, | ||
| SteamID: models.SteamID(76561197960287930), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 2, CreatedAt: base + 200}, | ||
| SteamID: models.SteamID(76561197960287931), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 3, CreatedAt: base + 300}, | ||
| SteamID: models.SteamID(76561197960287932), | ||
| MarketplaceSlug: "csfloat", | ||
| ExpungedAt: util.Ptr(base + 400), | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 4, CreatedAt: base + 500}, | ||
| SteamID: models.SteamID(76561197960287933), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 5, CreatedAt: base + 600}, | ||
| SteamID: models.SteamID(76561197960287934), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| ) | ||
|
|
||
| r := httptest.NewRequest(http.MethodGet, "/recent", nil) | ||
| w := httptest.NewRecorder() | ||
| handler.ServeHTTP(w, r) | ||
|
|
||
| resp := w.Result() | ||
| if resp.StatusCode != http.StatusOK { | ||
| t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK) | ||
| } | ||
|
|
||
| var body listRecentResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
|
|
||
| wantSteamIDs := []models.SteamID{ | ||
| 76561197960287934, // id=5 | ||
| 76561197960287933, // id=4 | ||
| 76561197960287931, // id=2 | ||
| 76561197960287930, // id=1 | ||
| } | ||
| if len(body.Data) != len(wantSteamIDs) { | ||
| t.Fatalf("len(data) = %d, want %d", len(body.Data), len(wantSteamIDs)) | ||
| } | ||
| for i, want := range wantSteamIDs { | ||
| if body.Data[i].SteamID != want { | ||
| t.Errorf("data[%d].SteamID = %d, want %d", i, body.Data[i].SteamID, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestListRecentHandler_RespectsLimit(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| handler, db := buildRecentHandlerStack(t) | ||
|
|
||
| base := models.Epoch + 1000 | ||
| testutil.Insert(t, db, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 1, CreatedAt: base + 100}, | ||
| SteamID: models.SteamID(76561197960287930), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 2, CreatedAt: base + 200}, | ||
| SteamID: models.SteamID(76561197960287931), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 3, CreatedAt: base + 300}, | ||
| SteamID: models.SteamID(76561197960287932), | ||
| MarketplaceSlug: "csfloat", | ||
| }, | ||
| ) | ||
|
|
||
| r := httptest.NewRequest(http.MethodGet, "/recent?limit=2", nil) | ||
| w := httptest.NewRecorder() | ||
| handler.ServeHTTP(w, r) | ||
|
|
||
| resp := w.Result() | ||
| if resp.StatusCode != http.StatusOK { | ||
| t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK) | ||
| } | ||
| var body listRecentResponse | ||
| if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
| if len(body.Data) != 2 { | ||
| t.Errorf("len(data) = %d, want 2", len(body.Data)) | ||
| } | ||
| } | ||
|
|
||
| func TestListRecentHandler_InvalidLimit(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| handler, _ := buildRecentHandlerStack(t) | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| limit string | ||
| }{ | ||
| {name: "zero", limit: "0"}, | ||
| {name: "negative", limit: "-1"}, | ||
| {name: "overMax", limit: "101"}, | ||
| {name: "nonNumeric", limit: "abc"}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| r := httptest.NewRequest(http.MethodGet, "/recent?limit="+tc.limit, nil) | ||
| w := httptest.NewRecorder() | ||
| handler.ServeHTTP(w, r) | ||
|
|
||
| resp := w.Result() | ||
| if resp.StatusCode != http.StatusBadRequest { | ||
| t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusBadRequest) | ||
| } | ||
| var body errors.Error | ||
| if err := json.NewDecoder(resp.Body).Decode(&body); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
| if body.Details != "limit must be between 1 and 100" { | ||
| t.Errorf("details = %q, want %q", body.Details, "limit must be between 1 and 100") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestListRecentHandler_ResponseShape(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| handler, db := buildRecentHandlerStack(t) | ||
|
|
||
| base := models.Epoch + 1000 | ||
| testutil.Insert(t, db, | ||
| &models.Reversal{ | ||
| Model: models.Model{ID: 1, CreatedAt: base + 100}, | ||
| SteamID: models.SteamID(76561197960287930), | ||
| MarketplaceSlug: "csfloat", | ||
| ReversedAt: base + 50, | ||
| }, | ||
| ) | ||
|
|
||
| r := httptest.NewRequest(http.MethodGet, "/recent", nil) | ||
| w := httptest.NewRecorder() | ||
| handler.ServeHTTP(w, r) | ||
|
|
||
| // Decode as raw JSON to assert the exact wire shape (especially steam_id as a string). | ||
| var raw struct { | ||
| Data []map[string]interface{} `json:"data"` | ||
| } | ||
| if err := json.NewDecoder(w.Result().Body).Decode(&raw); err != nil { | ||
| t.Fatalf("decode: %v", err) | ||
| } | ||
| if len(raw.Data) != 1 { | ||
| t.Fatalf("len(data) = %d, want 1", len(raw.Data)) | ||
| } | ||
| row := raw.Data[0] | ||
|
|
||
| expectedKeys := []string{"marketplace_slug", "steam_id", "reversed_at", "created_at"} | ||
| for _, k := range expectedKeys { | ||
| if _, ok := row[k]; !ok { | ||
| t.Errorf("missing key %q in response", k) | ||
| } | ||
| } | ||
| for k := range row { | ||
| ok := false | ||
| for _, want := range expectedKeys { | ||
| if k == want { | ||
| ok = true | ||
| break | ||
| } | ||
| } | ||
| if !ok { | ||
| t.Errorf("unexpected key %q in response", k) | ||
| } | ||
| } | ||
|
|
||
| steamIDValue, ok := row["steam_id"].(string) | ||
| if !ok { | ||
| t.Errorf("steam_id should be a JSON string, got %T", row["steam_id"]) | ||
| } | ||
| if steamIDValue != "76561197960287930" { | ||
| t.Errorf("steam_id = %q, want %q", steamIDValue, "76561197960287930") | ||
| } | ||
| } |
Oops, something went wrong.
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.
I believe we can use the
List(opts)function for this functionality instead of creating a bespokeListRecentfunction.The options also allow the use of a cursor for pagination, which will be useful when the user wants to click "Load More."