|
|
|
|
@ -68,7 +68,11 @@ func (r *inMemoryRepo) IngestEntry(
|
|
|
|
|
}
|
|
|
|
|
return &cp, false, nil
|
|
|
|
|
}
|
|
|
|
|
func (r *inMemoryRepo) ListTop(ctx context.Context, filter domain.TopFilter, limit int) ([]*domain.LeaderboardEntry, error) {
|
|
|
|
|
func (r *inMemoryRepo) ListTop(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
filter domain.TopFilter,
|
|
|
|
|
limit int,
|
|
|
|
|
) ([]*domain.LeaderboardEntry, error) {
|
|
|
|
|
if len(r.entries) < limit {
|
|
|
|
|
limit = len(r.entries)
|
|
|
|
|
}
|
|
|
|
|
@ -170,12 +174,18 @@ func TestUpdateAndTop10(t *testing.T) {
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/leaderboard/update", bytes.NewReader(payload))
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
req.Header.Set("Authorization", "Bearer service")
|
|
|
|
|
resp := sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
sharedhttpx.AssertStatusAndClose(t, resp, http.StatusOK, "update failed")
|
|
|
|
|
{
|
|
|
|
|
resp := sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
assertStatus(t, resp, http.StatusOK, "update failed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/leaderboard/top10", nil)
|
|
|
|
|
resp = sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
sharedhttpx.AssertStatusAndClose(t, resp, http.StatusOK, "top10 failed")
|
|
|
|
|
{
|
|
|
|
|
resp := sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
assertStatus(t, resp, http.StatusOK, "top10 failed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPlayerAuthAndStats(t *testing.T) {
|
|
|
|
|
@ -183,22 +193,41 @@ func TestPlayerAuthAndStats(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/leaderboard/players/user-1", nil)
|
|
|
|
|
req.Header.Set("Authorization", "Bearer player")
|
|
|
|
|
resp := sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
sharedhttpx.AssertStatusAndClose(t, resp, http.StatusNotFound, "expected not found before update")
|
|
|
|
|
{
|
|
|
|
|
resp := sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
assertStatus(t, resp, http.StatusNotFound, "expected not found before update")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/leaderboard/players/user-2", nil)
|
|
|
|
|
req.Header.Set("Authorization", "Bearer player")
|
|
|
|
|
resp = sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
sharedhttpx.AssertStatusAndClose(t, resp, http.StatusForbidden, "expected forbidden for other player")
|
|
|
|
|
{
|
|
|
|
|
resp := sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
assertStatus(t, resp, http.StatusForbidden, "expected forbidden for other player")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req = httptest.NewRequest(http.MethodGet, "/leaderboard/stats", nil)
|
|
|
|
|
resp = sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
sharedhttpx.AssertStatusAndClose(t, resp, http.StatusOK, "stats failed")
|
|
|
|
|
{
|
|
|
|
|
resp := sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
assertStatus(t, resp, http.StatusOK, "stats failed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMetricsEndpoint(t *testing.T) {
|
|
|
|
|
app := setupApp(t)
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
|
|
|
|
|
resp := sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
sharedhttpx.AssertStatusAndClose(t, resp, http.StatusOK, "metrics failed")
|
|
|
|
|
{
|
|
|
|
|
resp := sharedhttpx.MustTest(t, app, req)
|
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
assertStatus(t, resp, http.StatusOK, "metrics failed")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func assertStatus(t *testing.T, resp *http.Response, want int, msg string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
if resp.StatusCode != want {
|
|
|
|
|
t.Fatalf("%s: status=%d want=%d", msg, resp.StatusCode, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|