From bb82daa52978c800fd7dafef5c9b59fd5c7e30bf Mon Sep 17 00:00:00 2001 From: Taras Kushnir Date: Thu, 8 Jan 2026 08:34:01 +0200 Subject: [PATCH] Make global variables in tests consistent --- AGENTS.md | 3 +- pkg/api/gc_test.go | 2 +- pkg/api/jobs_test.go | 2 +- pkg/api/org_test.go | 26 +++++----- pkg/api/property_test.go | 102 +++++++++++++++++++------------------- pkg/api/puzzle_test.go | 2 +- pkg/api/server_test.go | 8 +-- pkg/api/task_test.go | 4 +- pkg/api/verify_test.go | 14 +++--- pkg/db/utils.go | 8 --- pkg/puzzle/puzzle_test.go | 4 ++ 11 files changed, 86 insertions(+), 89 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 17c748d2..dc6807f1 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -50,12 +50,13 @@ ## Testing instructions -- To run all Go unit tests, run `make test-unit` (unit tests always run with "enterprise" tag) +- To run all Go unit tests, run `make test-unit`. Unit tests always run with "enterprise" tag. You can use `make test-unit` also as a "shortcut" to check if everything builds. - To run JS widget tests, run `make test-widget-unit` - To run a single Go integration test, run `make test-docker-light TEST_NAME=` (prefer running a single test for debugging). Docker is required. - To run all Go integration tests, run `make test-docker-light`. Docker is required. - Do not use underscores in Golang test names - To get unit tests code coverage, run `make test-unit-cover` - To get integration tests code coverage, after running integration tests, open `coverage_integration/` directory in repository root +- Integration tests for Portal and API have global variables `store` (Postgres `db.BusinessStore`), `timeSeries` (ClickHouse, `common.TimeSeriesStore`) and `server` (respective server resource) that can be used instead of creating new resources. - For exact HTTP routes to endpoints always check how they are setup in `server.go` and `server_enterprise.go` - Always make sure all unit and integration tests pass before sending a PR diff --git a/pkg/api/gc_test.go b/pkg/api/gc_test.go index 4deca2b4..208dcd08 100644 --- a/pkg/api/gc_test.go +++ b/pkg/api/gc_test.go @@ -17,7 +17,7 @@ func gcDataTestSuite(ctx context.Context, property *dbgen.Property, deleter func tnow := time.Now() for i := 0; i < requests; i++ { - s.Levels.Difficulty(common.RandomFingerprint(), property, tnow.Add(time.Duration(i)*10*time.Second)) + server.Levels.Difficulty(common.RandomFingerprint(), property, tnow.Add(time.Duration(i)*10*time.Second)) } // we need to wait for the timeout in the ProcessAccessLog() diff --git a/pkg/api/jobs_test.go b/pkg/api/jobs_test.go index 890f61fb..d65db2b1 100644 --- a/pkg/api/jobs_test.go +++ b/pkg/api/jobs_test.go @@ -79,7 +79,7 @@ func TestAsyncJob(t *testing.T) { t.Fatal(err) } - if _, err := s.BusinessDB.Impl().CreateNewAsyncTask(ctx, request, handlerID, user, time.Now().UTC().Add(-1*time.Second), t.Name()); err != nil { + if _, err := server.BusinessDB.Impl().CreateNewAsyncTask(ctx, request, handlerID, user, time.Now().UTC().Add(-1*time.Second), t.Name()); err != nil { t.Fatal(err) } diff --git a/pkg/api/org_test.go b/pkg/api/org_test.go index 2c42d049..3f352303 100644 --- a/pkg/api/org_test.go +++ b/pkg/api/org_test.go @@ -23,7 +23,7 @@ import ( func apiRequestSuite(ctx context.Context, request interface{}, method, endpoint, apiKey string) (*http.Response, error) { srv := http.NewServeMux() - s.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) + server.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) //srv.HandleFunc("/", catchAll) @@ -127,7 +127,7 @@ func TestAPICreateOrg(t *testing.T) { t.Fatal(err) } - if _, err := s.BusinessDB.Impl().SoftDeleteOrganization(ctx, baseOrg, user); err != nil { + if _, err := server.BusinessDB.Impl().SoftDeleteOrganization(ctx, baseOrg, user); err != nil { t.Fatal(err) } @@ -166,7 +166,7 @@ func TestAPIDeleteOrg(t *testing.T) { } input := &apiOrgInput{ - ID: s.IDHasher.Encrypt(int(org.ID)), + ID: server.IDHasher.Encrypt(int(org.ID)), } _, meta, err := requestResponseAPISuite[json.RawMessage](ctx, input, http.MethodDelete, "/"+common.OrgEndpoint, apiKey) @@ -178,7 +178,7 @@ func TestAPIDeleteOrg(t *testing.T) { t.Fatalf("Unexpected status code: %v", meta.Description) } - if _, err := s.BusinessDB.Impl().RetrieveUserOrganization(t.Context(), user, org.ID); (err != db.ErrSoftDeleted) && (err != db.ErrNegativeCacheHit) { + if _, err := server.BusinessDB.Impl().RetrieveUserOrganization(t.Context(), user, org.ID); (err != db.ErrSoftDeleted) && (err != db.ErrNegativeCacheHit) { t.Fatalf("Unexpected error when retrieving deleted org: %v", err) } } @@ -196,7 +196,7 @@ func TestAPIUpdateOrg(t *testing.T) { } input := &apiOrgInput{ - ID: s.IDHasher.Encrypt(int(org.ID)), + ID: server.IDHasher.Encrypt(int(org.ID)), Name: "Org Update " + xid.New().String(), } @@ -209,7 +209,7 @@ func TestAPIUpdateOrg(t *testing.T) { t.Fatalf("Unexpected status code: %v", meta.Description) } - org, err = s.BusinessDB.Impl().RetrieveUserOrganization(ctx, user, org.ID) + org, err = server.BusinessDB.Impl().RetrieveUserOrganization(ctx, user, org.ID) if err != nil { t.Fatalf("Unexpected error when retrieving org: %v", err) } @@ -245,7 +245,7 @@ func TestAPIUpdateOrgEmptyID(t *testing.T) { t.Fatalf("Unexpected status code: %v", meta.Description) } - org, err = s.BusinessDB.Impl().RetrieveUserOrganization(ctx, user, org.ID) + org, err = server.BusinessDB.Impl().RetrieveUserOrganization(ctx, user, org.ID) if err != nil { t.Fatalf("Unexpected error when retrieving org: %v", err) } @@ -300,7 +300,7 @@ func TestAPIOrgPermissions(t *testing.T) { resp, err := apiRequestSuite(ctx, nil, http.MethodDelete, - fmt.Sprintf("/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org.ID))), + fmt.Sprintf("/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org.ID))), apiKey) if err != nil { t.Fatal(err) @@ -396,7 +396,7 @@ func TestAPIDeleteOrgReadOnlyKey(t *testing.T) { } input := &apiOrgInput{ - ID: s.IDHasher.Encrypt(int(org.ID)), + ID: server.IDHasher.Encrypt(int(org.ID)), } resp, err := apiRequestSuite(ctx, input, http.MethodDelete, "/"+common.OrgEndpoint, apiKey) @@ -446,7 +446,7 @@ func TestAPIUpdateOrgReadOnlyKey(t *testing.T) { } input := &apiOrgInput{ - ID: s.IDHasher.Encrypt(int(org.ID)), + ID: server.IDHasher.Encrypt(int(org.ID)), Name: "Org Update " + xid.New().String(), } @@ -554,7 +554,7 @@ func TestAPIDeleteOrgAPIKeyOrgScope(t *testing.T) { } input := &apiOrgInput{ - ID: s.IDHasher.Encrypt(int(org2.ID)), + ID: server.IDHasher.Encrypt(int(org2.ID)), } resp, err := apiRequestSuite(ctx, input, http.MethodDelete, "/"+common.OrgEndpoint, apiKey) @@ -585,7 +585,7 @@ func TestAPIUpdateOrgAPIKeyOrgScope(t *testing.T) { } input := &apiOrgInput{ - ID: s.IDHasher.Encrypt(int(org2.ID)), + ID: server.IDHasher.Encrypt(int(org2.ID)), Name: "Org Update", } @@ -672,7 +672,7 @@ func TestAPIOrgInvalidRequests(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { srv := http.NewServeMux() - s.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) + server.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) req, err := http.NewRequestWithContext(ctx, tt.method, tt.endpoint, bytes.NewReader(tt.body)) if err != nil { diff --git a/pkg/api/property_test.go b/pkg/api/property_test.go index 8129452f..dc7ade88 100644 --- a/pkg/api/property_test.go +++ b/pkg/api/property_test.go @@ -215,7 +215,7 @@ func TestApiPostProperties(t *testing.T) { output, meta, err := requestResponseAPISuite[*apiAsyncTaskOutput](ctx, inputs, http.MethodPost, - fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint), + fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint), apiKey) if err != nil { t.Fatal(err) @@ -249,7 +249,7 @@ func TestApiPostProperties(t *testing.T) { t.Fatal("Async task did not complete within timeout") } - properties, _, err := s.BusinessDB.Impl().RetrieveOrgProperties(ctx, org, 0, db.MaxOrgPropertiesPageSize) + properties, _, err := server.BusinessDB.Impl().RetrieveOrgProperties(ctx, org, 0, db.MaxOrgPropertiesPageSize) if err != nil { t.Fatal(err) } @@ -303,7 +303,7 @@ func TestApiPostPropertiesNoSubscription(t *testing.T) { resp, err := apiRequestSuite(ctx, inputs, http.MethodPost, - fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint), + fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint), apiKeyStr) if err != nil { t.Fatal(err) @@ -344,7 +344,7 @@ func TestApiPostPropertiesOtherOrg(t *testing.T) { resp, err := apiRequestSuite(ctx, inputs, http.MethodPost, - fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint), + fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint), apiKey) if err != nil { t.Fatal(err) @@ -368,32 +368,32 @@ func TestApiDeleteProperties(t *testing.T) { } // Create another org for the same user - org2, _, err := s.BusinessDB.Impl().CreateNewOrganization(ctx, t.Name()+"_org2", user.ID) + org2, _, err := server.BusinessDB.Impl().CreateNewOrganization(ctx, t.Name()+"_org2", user.ID) if err != nil { t.Fatal(err) } // Create properties // P1 in Org1 - p1, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p1.com"), org1) + p1, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p1.com"), org1) if err != nil { t.Fatal(err) } // P2 in Org2 - p2, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p2.com"), org2) + p2, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p2.com"), org2) if err != nil { t.Fatal(err) } // P3 in Org1 (should not be deleted) - p3, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p3.com"), org1) + p3, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p3.com"), org1) if err != nil { t.Fatal(err) } // Prepare request idsToDelete := []string{ - s.IDHasher.Encrypt(int(p1.ID)), - s.IDHasher.Encrypt(int(p2.ID)), + server.IDHasher.Encrypt(int(p1.ID)), + server.IDHasher.Encrypt(int(p2.ID)), } output, meta, err := requestResponseAPISuite[*apiAsyncTaskOutput](ctx, idsToDelete, @@ -433,7 +433,7 @@ func TestApiDeleteProperties(t *testing.T) { } // Verify P1 deleted - props1, _, err := s.BusinessDB.Impl().RetrieveOrgProperties(ctx, org1, 0, db.MaxOrgPropertiesPageSize) + props1, _, err := server.BusinessDB.Impl().RetrieveOrgProperties(ctx, org1, 0, db.MaxOrgPropertiesPageSize) if err != nil { t.Fatal(err) } @@ -456,7 +456,7 @@ func TestApiDeleteProperties(t *testing.T) { } // Verify P2 deleted - props2, _, err := s.BusinessDB.Impl().RetrieveOrgProperties(ctx, org2, 0, db.MaxOrgPropertiesPageSize) + props2, _, err := server.BusinessDB.Impl().RetrieveOrgProperties(ctx, org2, 0, db.MaxOrgPropertiesPageSize) if err != nil { t.Fatal(err) } @@ -510,19 +510,19 @@ func TestApiUpdateProperties(t *testing.T) { } // Create another org for the same user - org2, _, err := s.BusinessDB.Impl().CreateNewOrganization(ctx, t.Name()+"_org2", user.ID) + org2, _, err := server.BusinessDB.Impl().CreateNewOrganization(ctx, t.Name()+"_org2", user.ID) if err != nil { t.Fatal(err) } // Create properties // P1 in Org1 - p1, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p1.com"), org1) + p1, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p1.com"), org1) if err != nil { t.Fatal(err) } // P2 in Org2 - p2, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p2.com"), org2) + p2, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "p2.com"), org2) if err != nil { t.Fatal(err) } @@ -530,7 +530,7 @@ func TestApiUpdateProperties(t *testing.T) { // Prepare update request updates := []*apiUpdatePropertyInput{ { - ID: s.IDHasher.Encrypt(int(p1.ID)), + ID: server.IDHasher.Encrypt(int(p1.ID)), apiPropertySettings: apiPropertySettings{ Name: "Updated Property 1", Level: int(common.DifficultyLevelHigh), @@ -542,7 +542,7 @@ func TestApiUpdateProperties(t *testing.T) { }, }, { - ID: s.IDHasher.Encrypt(int(p2.ID)), + ID: server.IDHasher.Encrypt(int(p2.ID)), apiPropertySettings: apiPropertySettings{ Name: "Updated Property 2", Level: int(common.DifficultyLevelSmall), @@ -592,14 +592,14 @@ func TestApiUpdateProperties(t *testing.T) { } // Verify P1 updated - updatedP1, err := s.BusinessDB.Impl().RetrieveOrgProperty(ctx, org1, p1.ID) + updatedP1, err := server.BusinessDB.Impl().RetrieveOrgProperty(ctx, org1, p1.ID) if err != nil { t.Fatal(err) } verifyPropertyUpdate(t, updatedP1, updates[0]) // Verify P2 updated - updatedP2, err := s.BusinessDB.Impl().RetrieveOrgProperty(ctx, org2, p2.ID) + updatedP2, err := server.BusinessDB.Impl().RetrieveOrgProperty(ctx, org2, p2.ID) if err != nil { t.Fatal(err) } @@ -618,13 +618,13 @@ func TestApiGetProperties(t *testing.T) { } for i := 0; i < 3*db.MaxOrgPropertiesPageSize/2; i++ { - if _, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_tests.CreateNewPropertyParams(user.ID, fmt.Sprintf("example%v.com", i)), org); err != nil { + if _, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_tests.CreateNewPropertyParams(user.ID, fmt.Sprintf("example%v.com", i)), org); err != nil { t.Fatalf("Failed to create new property: %v", err) } } // with api key 1 it should work - endpoint := fmt.Sprintf("/%s/%v/%s?page=1&per_page=%d", common.OrgEndpoint, s.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint, db.MaxOrgPropertiesPageSize/2-1) + endpoint := fmt.Sprintf("/%s/%v/%s?page=1&per_page=%d", common.OrgEndpoint, server.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint, db.MaxOrgPropertiesPageSize/2-1) properties, meta, err := requestResponseAPISuite[[]*apiOrgPropertyOutput](ctx, nil, http.MethodGet, endpoint, apiKey) if err != nil { t.Fatal(err) @@ -651,12 +651,12 @@ func TestApiGetPropertyInvalidOrgID(t *testing.T) { t.Fatal(err) } - property, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org) + property, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org) if err != nil { t.Fatal(err) } - propertyID := s.IDHasher.Encrypt(int(property.ID)) + propertyID := server.IDHasher.Encrypt(int(property.ID)) _, meta, err := requestResponseAPISuite[APIResponse](ctx, nil, http.MethodGet, fmt.Sprintf("/%s/%s/%s/%s", common.OrgEndpoint, "qwerty123", @@ -683,14 +683,14 @@ func TestApiGetPropertyInvalidPropertyID(t *testing.T) { t.Fatal(err) } - if _, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org); err != nil { + if _, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org); err != nil { t.Fatal(err) } propertyID := "qwerty123" _, meta, err := requestResponseAPISuite[APIResponse](ctx, nil, http.MethodGet, - fmt.Sprintf("/%s/%s/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org.ID)), + fmt.Sprintf("/%s/%s/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org.ID)), common.PropertyEndpoint, propertyID), apiKey) if err != nil { @@ -714,15 +714,15 @@ func TestApiGetProperty(t *testing.T) { t.Fatal(err) } - property, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org) + property, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org) if err != nil { t.Fatal(err) } - propertyID := s.IDHasher.Encrypt(int(property.ID)) + propertyID := server.IDHasher.Encrypt(int(property.ID)) output, meta, err := requestResponseAPISuite[*apiPropertyOutput](ctx, nil, http.MethodGet, - fmt.Sprintf("/%s/%s/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org.ID)), + fmt.Sprintf("/%s/%s/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org.ID)), common.PropertyEndpoint, propertyID), apiKey) if err != nil { @@ -782,7 +782,7 @@ func TestApiGetPropertyPermissions(t *testing.T) { t.Fatal(err) } - property, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(owner.ID, "example.com"), org) + property, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(owner.ID, "example.com"), org) if err != nil { t.Fatal(err) } @@ -792,11 +792,11 @@ func TestApiGetPropertyPermissions(t *testing.T) { t.Fatal(err) } - propertyID := s.IDHasher.Encrypt(int(property.ID)) + propertyID := server.IDHasher.Encrypt(int(property.ID)) resp, err := apiRequestSuite(ctx, nil, http.MethodGet, - fmt.Sprintf("/%s/%s/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org.ID)), + fmt.Sprintf("/%s/%s/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org.ID)), common.PropertyEndpoint, propertyID), apiKey) if err != nil { @@ -825,16 +825,16 @@ func TestApiGetPropertyAPIKeyOrgScope(t *testing.T) { t.Fatalf("Failed to create extra org: %v", err) } - property, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org2) + property, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org2) if err != nil { t.Fatal(err) } - propertyID := s.IDHasher.Encrypt(int(property.ID)) + propertyID := server.IDHasher.Encrypt(int(property.ID)) resp, err := apiRequestSuite(ctx, nil, http.MethodGet, - fmt.Sprintf("/%s/%s/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org2.ID)), + fmt.Sprintf("/%s/%s/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org2.ID)), common.PropertyEndpoint, propertyID), apiKey) if err != nil { @@ -867,7 +867,7 @@ func TestApiPostPropertiesInvalidKey(t *testing.T) { // We need a valid path structure even if auth fails, usually. // The route is /org/{org}/properties. // We can use a dummy org ID. - dummyOrgID := s.IDHasher.Encrypt(123) + dummyOrgID := server.IDHasher.Encrypt(123) resp, err := apiRequestSuite(ctx, inputs, http.MethodPost, @@ -905,7 +905,7 @@ func TestApiPostPropertiesReadOnlyKey(t *testing.T) { resp, err := apiRequestSuite(ctx, inputs, http.MethodPost, - fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint), + fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org.ID)), common.PropertiesEndpoint), apiKey) if err != nil { t.Fatal(err) @@ -951,13 +951,13 @@ func TestApiDeletePropertiesReadOnlyKey(t *testing.T) { t.Fatal(err) } - property, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org) + property, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org) if err != nil { t.Fatal(err) } idsToDelete := []string{ - s.IDHasher.Encrypt(int(property.ID)), + server.IDHasher.Encrypt(int(property.ID)), } resp, err := apiRequestSuite(ctx, idsToDelete, @@ -1015,14 +1015,14 @@ func TestApiUpdatePropertiesReadOnlyKey(t *testing.T) { t.Fatal(err) } - property, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org) + property, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org) if err != nil { t.Fatal(err) } updates := []*apiUpdatePropertyInput{ { - ID: s.IDHasher.Encrypt(int(property.ID)), + ID: server.IDHasher.Encrypt(int(property.ID)), apiPropertySettings: apiPropertySettings{ Name: "Updated Property 1", Level: int(common.DifficultyLevelHigh), @@ -1055,7 +1055,7 @@ func TestApiGetPropertiesInvalidKey(t *testing.T) { ctx := t.Context() apiKey := db.UUIDToSecret(*randomUUID()) - dummyOrgID := s.IDHasher.Encrypt(123) + dummyOrgID := server.IDHasher.Encrypt(123) endpoint := fmt.Sprintf("/%s/%v/%s", common.OrgEndpoint, dummyOrgID, common.PropertiesEndpoint) resp, err := apiRequestSuite(ctx, nil, http.MethodGet, endpoint, apiKey) @@ -1075,8 +1075,8 @@ func TestApiGetPropertyInvalidKey(t *testing.T) { ctx := common.TraceContext(t.Context(), t.Name()) apiKey := db.UUIDToSecret(*randomUUID()) - dummyOrgID := s.IDHasher.Encrypt(123) - dummyPropID := s.IDHasher.Encrypt(456) + dummyOrgID := server.IDHasher.Encrypt(123) + dummyPropID := server.IDHasher.Encrypt(456) resp, err := apiRequestSuite(ctx, nil, http.MethodGet, @@ -1108,7 +1108,7 @@ func TestApiGetPropertiesAPIKeyOrgScope(t *testing.T) { t.Fatalf("Failed to create extra org: %v", err) } - endpoint := fmt.Sprintf("/%s/%v/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org2.ID)), common.PropertiesEndpoint) + endpoint := fmt.Sprintf("/%s/%v/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org2.ID)), common.PropertiesEndpoint) resp, err := apiRequestSuite(ctx, nil, http.MethodGet, endpoint, apiKey) if err != nil { t.Fatal(err) @@ -1147,7 +1147,7 @@ func TestApiPostPropertiesAPIKeyOrgScope(t *testing.T) { resp, err := apiRequestSuite(ctx, inputs, http.MethodPost, - fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, s.IDHasher.Encrypt(int(org2.ID)), common.PropertiesEndpoint), + fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, server.IDHasher.Encrypt(int(org2.ID)), common.PropertiesEndpoint), apiKey) if err != nil { t.Fatal(err) @@ -1175,13 +1175,13 @@ func TestApiDeletePropertiesAPIKeyOrgScope(t *testing.T) { t.Fatalf("Failed to create extra org: %v", err) } - property, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org2) + property, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org2) if err != nil { t.Fatal(err) } idsToDelete := []string{ - s.IDHasher.Encrypt(int(property.ID)), + server.IDHasher.Encrypt(int(property.ID)), } output, meta, err := requestResponseAPISuite[*apiAsyncTaskOutput](ctx, idsToDelete, @@ -1249,14 +1249,14 @@ func TestApiUpdatePropertiesAPIKeyOrgScope(t *testing.T) { t.Fatalf("Failed to create extra org: %v", err) } - property, _, err := s.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org2) + property, _, err := server.BusinessDB.Impl().CreateNewProperty(ctx, db_test.CreateNewPropertyParams(user.ID, "example.com"), org2) if err != nil { t.Fatal(err) } updates := []*apiUpdatePropertyInput{ { - ID: s.IDHasher.Encrypt(int(property.ID)), + ID: server.IDHasher.Encrypt(int(property.ID)), apiPropertySettings: apiPropertySettings{ Name: "Updated Property", }, @@ -1323,7 +1323,7 @@ func TestAPIPropertyInvalidRequests(t *testing.T) { t.Fatal(err) } - orgID := s.IDHasher.Encrypt(int(org.ID)) + orgID := server.IDHasher.Encrypt(int(org.ID)) createEndpoint := fmt.Sprintf("/%s/%s/%s", common.OrgEndpoint, orgID, common.PropertiesEndpoint) tests := []struct { @@ -1387,7 +1387,7 @@ func TestAPIPropertyInvalidRequests(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { srv := http.NewServeMux() - s.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) + server.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) req, err := http.NewRequestWithContext(ctx, tt.method, tt.endpoint, bytes.NewReader(tt.body)) if err != nil { diff --git a/pkg/api/puzzle_test.go b/pkg/api/puzzle_test.go index 7fc80eeb..2ce77811 100644 --- a/pkg/api/puzzle_test.go +++ b/pkg/api/puzzle_test.go @@ -31,7 +31,7 @@ func puzzleSuite(ctx context.Context, sitekey, domain string) (*http.Response, e func puzzleSuiteEx(ctx context.Context, method, sitekey, domain string) (*http.Response, error) { slog.Log(ctx, common.LevelTrace, "Running puzzle suite", "domain", domain, "sitekey", sitekey) srv := http.NewServeMux() - s.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) + server.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) //srv.HandleFunc("/", catchAll) diff --git a/pkg/api/server_test.go b/pkg/api/server_test.go index 5813d208..819c40a6 100644 --- a/pkg/api/server_test.go +++ b/pkg/api/server_test.go @@ -21,7 +21,7 @@ import ( ) var ( - s *Server + server *Server cfg common.ConfigStore cache common.Cache[db.CacheKey, any] timeSeries common.TimeSeriesStore @@ -80,7 +80,7 @@ func TestMain(m *testing.M) { planService := billing.NewPlanService(nil) testPlan = planService.GetInternalTrialPlan() - s = &Server{ + server = &Server{ Stage: common.StageTest, BusinessDB: store, TimeSeries: timeSeries, @@ -96,10 +96,10 @@ func TestMain(m *testing.M) { IDHasher: common.NewIDHasher(cfg.Get(common.IDHasherSaltKey)), AsyncTasks: maintenance.NewAsyncTasksJob(store), } - if err := s.Init(context.TODO(), verifyFlushInterval, authBackfillDelay); err != nil { + if err := server.Init(context.TODO(), verifyFlushInterval, authBackfillDelay); err != nil { panic(err) } - defer s.Shutdown() + defer server.Shutdown() // TODO: seed data diff --git a/pkg/api/task_test.go b/pkg/api/task_test.go index a903c7b8..7b740de0 100644 --- a/pkg/api/task_test.go +++ b/pkg/api/task_test.go @@ -27,7 +27,7 @@ func TestGetAsyncTaskPermissions(t *testing.T) { handlerID := xid.New().String() request := struct{}{} - task, err := s.BusinessDB.Impl().CreateNewAsyncTask(ctx, request, handlerID, user1, time.Now().UTC().Add(24*time.Hour), t.Name()) + task, err := server.BusinessDB.Impl().CreateNewAsyncTask(ctx, request, handlerID, user1, time.Now().UTC().Add(24*time.Hour), t.Name()) if err != nil { t.Fatal(err) } @@ -91,7 +91,7 @@ func TestGetAsyncTaskReadOnlyKey(t *testing.T) { handlerID := xid.New().String() request := struct{}{} - task, err := s.BusinessDB.Impl().CreateNewAsyncTask(ctx, request, handlerID, user, time.Now().UTC().Add(24*time.Hour), t.Name()) + task, err := server.BusinessDB.Impl().CreateNewAsyncTask(ctx, request, handlerID, user, time.Now().UTC().Add(24*time.Hour), t.Name()) if err != nil { t.Fatal(err) } diff --git a/pkg/api/verify_test.go b/pkg/api/verify_test.go index 9e214b0c..8e3a27ad 100644 --- a/pkg/api/verify_test.go +++ b/pkg/api/verify_test.go @@ -44,7 +44,7 @@ func TestSerializeResponse(t *testing.T) { func verifySuite(response, secret, sitekey string) (*http.Response, error) { srv := http.NewServeMux() - s.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) + server.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) //srv.HandleFunc("/", catchAll) @@ -68,7 +68,7 @@ func verifySuite(response, secret, sitekey string) (*http.Response, error) { func siteVerifySuite(response, secret, sitekey string, headers ...map[string][]string) (*http.Response, error) { srv := http.NewServeMux() - s.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) + server.Setup("", true /*verbose*/, common.NoopMiddleware).Register(srv) //srv.HandleFunc("/", catchAll) @@ -760,24 +760,24 @@ func TestVerifyTestShortcut(t *testing.T) { ctx := t.Context() solver := &puzzle.ComputeSolver{} - solutions, _ := solver.Solve(s.Verifier.TestPuzzle) + solutions, _ := solver.Solve(server.Verifier.TestPuzzle) var buf bytes.Buffer buf.WriteString(solutions.String()) buf.Write([]byte(".")) - s.Verifier.WriteTestPuzzle(&buf) + server.Verifier.WriteTestPuzzle(&buf) - payload, err := s.Verifier.ParseSolutionPayload(ctx, buf.Bytes()) + payload, err := server.Verifier.ParseSolutionPayload(ctx, buf.Bytes()) if err != nil { t.Fatal(err) } - if payload.Puzzle() != s.Verifier.TestPuzzle { + if payload.Puzzle() != server.Verifier.TestPuzzle { t.Fatal("verify result is not short circuited") } - if result, _ := s.Verifier.Verify(ctx, payload, nil /*expectedOwner*/, time.Now().UTC()); result.Error != puzzle.TestPropertyError { + if result, _ := server.Verifier.Verify(ctx, payload, nil /*expectedOwner*/, time.Now().UTC()); result.Error != puzzle.TestPropertyError { t.Errorf("Unexpected verification result: %v", result.Error.String()) } } diff --git a/pkg/db/utils.go b/pkg/db/utils.go index e76ac794..4935f893 100644 --- a/pkg/db/utils.go +++ b/pkg/db/utils.go @@ -74,14 +74,6 @@ func Timestampz(t time.Time) pgtype.Timestamptz { } } -func Date(t time.Time) pgtype.Date { - return pgtype.Date{ - Time: t, - InfinityModifier: pgtype.Finite, - Valid: true, - } -} - func UUIDToSiteKey(uuid pgtype.UUID) string { if !uuid.Valid { return "" diff --git a/pkg/puzzle/puzzle_test.go b/pkg/puzzle/puzzle_test.go index da7a7a5e..2ab8b0fe 100644 --- a/pkg/puzzle/puzzle_test.go +++ b/pkg/puzzle/puzzle_test.go @@ -189,6 +189,8 @@ func TestPuzzlePayloadSuffix(t *testing.T) { } func TestValidityIntervalFromIndex(t *testing.T) { + t.Parallel() + ctx := t.Context() tests := []struct { @@ -221,6 +223,8 @@ func TestValidityIntervalFromIndex(t *testing.T) { } func TestValidityIntervalToIndex(t *testing.T) { + t.Parallel() + tests := []struct { duration time.Duration expected int