From 947454f163b6fe1c0e11edfd69c75b442a486b33 Mon Sep 17 00:00:00 2001 From: Pascal Fischer Date: Mon, 23 Dec 2024 18:25:25 +0100 Subject: [PATCH] moved files --- .../setupkeys_handler_benchmark_test.go | 66 ++--- .../setupkeys_handler_integration_test.go | 268 +++++++++--------- .../{ => testing}/testdata/setup_keys.sql | 0 .../testing_tools}/tools.go | 2 +- 4 files changed, 168 insertions(+), 168 deletions(-) rename management/server/http/{tests_benchmark => testing/benchmarks}/setupkeys_handler_benchmark_test.go (71%) rename management/server/http/{tests_integration => testing/integration}/setupkeys_handler_integration_test.go (75%) rename management/server/http/{ => testing}/testdata/setup_keys.sql (100%) rename management/server/http/{tests_tools => testing/testing_tools}/tools.go (99%) diff --git a/management/server/http/tests_benchmark/setupkeys_handler_benchmark_test.go b/management/server/http/testing/benchmarks/setupkeys_handler_benchmark_test.go similarity index 71% rename from management/server/http/tests_benchmark/setupkeys_handler_benchmark_test.go rename to management/server/http/testing/benchmarks/setupkeys_handler_benchmark_test.go index 90efe357161..5e2895bcc37 100644 --- a/management/server/http/tests_benchmark/setupkeys_handler_benchmark_test.go +++ b/management/server/http/testing/benchmarks/setupkeys_handler_benchmark_test.go @@ -1,4 +1,4 @@ -package tests_benchmark +package benchmarks import ( "encoding/json" @@ -15,11 +15,11 @@ import ( "github.com/netbirdio/netbird/management/server" "github.com/netbirdio/netbird/management/server/http/api" - "github.com/netbirdio/netbird/management/server/http/tests_tools" + "github.com/netbirdio/netbird/management/server/http/testing/testing_tools" ) // Map to store peers, groups, users, and setupKeys by name -var benchCasesSetupKeys = map[string]tests_tools.BenchmarkCase{ +var benchCasesSetupKeys = map[string]testing_tools.BenchmarkCase{ "Setup Keys - XS": {Peers: 10000, Groups: 10000, Users: 10000, SetupKeys: 5}, "Setup Keys - S": {Peers: 5, Groups: 5, Users: 5, SetupKeys: 100}, "Setup Keys - M": {Peers: 100, Groups: 20, Users: 20, SetupKeys: 1000}, @@ -31,7 +31,7 @@ var benchCasesSetupKeys = map[string]tests_tools.BenchmarkCase{ } func BenchmarkCreateSetupKey(b *testing.B) { - var expectedMetrics = map[string]tests_tools.PerformanceMetrics{ + var expectedMetrics = map[string]testing_tools.PerformanceMetrics{ "Setup Keys - XS": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 16}, "Setup Keys - S": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 16}, "Setup Keys - M": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 16}, @@ -49,16 +49,16 @@ func BenchmarkCreateSetupKey(b *testing.B) { for name, bc := range benchCasesSetupKeys { b.Run(name, func(b *testing.B) { - apiHandler, am, _ := tests_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) - tests_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, bc.SetupKeys) + apiHandler, am, _ := testing_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) + testing_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, bc.SetupKeys) b.ResetTimer() start := time.Now() for i := 0; i < b.N; i++ { requestBody := api.CreateSetupKeyRequest{ - AutoGroups: []string{tests_tools.TestGroupId}, - ExpiresIn: tests_tools.ExpiresIn, - Name: tests_tools.NewKeyName + strconv.Itoa(i), + AutoGroups: []string{testing_tools.TestGroupId}, + ExpiresIn: testing_tools.ExpiresIn, + Name: testing_tools.NewKeyName + strconv.Itoa(i), Type: "reusable", UsageLimit: 0, } @@ -67,17 +67,17 @@ func BenchmarkCreateSetupKey(b *testing.B) { body, err := json.Marshal(requestBody) assert.NoError(b, err) - req := tests_tools.BuildRequest(b, body, http.MethodPost, "/api/setup-keys", tests_tools.TestAdminId) + req := testing_tools.BuildRequest(b, body, http.MethodPost, "/api/setup-keys", testing_tools.TestAdminId) apiHandler.ServeHTTP(recorder, req) } - tests_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) + testing_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) }) } } func BenchmarkUpdateSetupKey(b *testing.B) { - var expectedMetrics = map[string]tests_tools.PerformanceMetrics{ + var expectedMetrics = map[string]testing_tools.PerformanceMetrics{ "Setup Keys - XS": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 3, MinMsPerOpCICD: 2, MaxMsPerOpCICD: 19}, "Setup Keys - S": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 3, MinMsPerOpCICD: 2, MaxMsPerOpCICD: 19}, "Setup Keys - M": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 3, MinMsPerOpCICD: 2, MaxMsPerOpCICD: 19}, @@ -95,15 +95,15 @@ func BenchmarkUpdateSetupKey(b *testing.B) { for name, bc := range benchCasesSetupKeys { b.Run(name, func(b *testing.B) { - apiHandler, am, _ := tests_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) - tests_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, bc.SetupKeys) + apiHandler, am, _ := testing_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) + testing_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, bc.SetupKeys) b.ResetTimer() start := time.Now() for i := 0; i < b.N; i++ { - groupId := tests_tools.TestGroupId + groupId := testing_tools.TestGroupId if i%2 == 0 { - groupId = tests_tools.NewGroupId + groupId = testing_tools.NewGroupId } requestBody := api.SetupKeyRequest{ AutoGroups: []string{groupId}, @@ -114,17 +114,17 @@ func BenchmarkUpdateSetupKey(b *testing.B) { body, err := json.Marshal(requestBody) assert.NoError(b, err) - req := tests_tools.BuildRequest(b, body, http.MethodPut, "/api/setup-keys/"+tests_tools.TestKeyId, tests_tools.TestAdminId) + req := testing_tools.BuildRequest(b, body, http.MethodPut, "/api/setup-keys/"+testing_tools.TestKeyId, testing_tools.TestAdminId) apiHandler.ServeHTTP(recorder, req) } - tests_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) + testing_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) }) } } func BenchmarkGetOneSetupKey(b *testing.B) { - var expectedMetrics = map[string]tests_tools.PerformanceMetrics{ + var expectedMetrics = map[string]testing_tools.PerformanceMetrics{ "Setup Keys - XS": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 16}, "Setup Keys - S": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 16}, "Setup Keys - M": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 16}, @@ -142,23 +142,23 @@ func BenchmarkGetOneSetupKey(b *testing.B) { for name, bc := range benchCasesSetupKeys { b.Run(name, func(b *testing.B) { - apiHandler, am, _ := tests_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) - tests_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, bc.SetupKeys) + apiHandler, am, _ := testing_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) + testing_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, bc.SetupKeys) b.ResetTimer() start := time.Now() for i := 0; i < b.N; i++ { - req := tests_tools.BuildRequest(b, nil, http.MethodGet, "/api/setup-keys/"+tests_tools.TestKeyId, tests_tools.TestAdminId) + req := testing_tools.BuildRequest(b, nil, http.MethodGet, "/api/setup-keys/"+testing_tools.TestKeyId, testing_tools.TestAdminId) apiHandler.ServeHTTP(recorder, req) } - tests_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) + testing_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) }) } } func BenchmarkGetAllSetupKeys(b *testing.B) { - var expectedMetrics = map[string]tests_tools.PerformanceMetrics{ + var expectedMetrics = map[string]testing_tools.PerformanceMetrics{ "Setup Keys - XS": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 12}, "Setup Keys - S": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 15}, "Setup Keys - M": {MinMsPerOpLocal: 5, MaxMsPerOpLocal: 10, MinMsPerOpCICD: 5, MaxMsPerOpCICD: 40}, @@ -176,23 +176,23 @@ func BenchmarkGetAllSetupKeys(b *testing.B) { for name, bc := range benchCasesSetupKeys { b.Run(name, func(b *testing.B) { - apiHandler, am, _ := tests_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) - tests_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, bc.SetupKeys) + apiHandler, am, _ := testing_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) + testing_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, bc.SetupKeys) b.ResetTimer() start := time.Now() for i := 0; i < b.N; i++ { - req := tests_tools.BuildRequest(b, nil, http.MethodGet, "/api/setup-keys", tests_tools.TestAdminId) + req := testing_tools.BuildRequest(b, nil, http.MethodGet, "/api/setup-keys", testing_tools.TestAdminId) apiHandler.ServeHTTP(recorder, req) } - tests_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) + testing_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) }) } } func BenchmarkDeleteSetupKey(b *testing.B) { - var expectedMetrics = map[string]tests_tools.PerformanceMetrics{ + var expectedMetrics = map[string]testing_tools.PerformanceMetrics{ "Setup Keys - XS": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 16}, "Setup Keys - S": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 16}, "Setup Keys - M": {MinMsPerOpLocal: 0.5, MaxMsPerOpLocal: 2, MinMsPerOpCICD: 1, MaxMsPerOpCICD: 16}, @@ -210,17 +210,17 @@ func BenchmarkDeleteSetupKey(b *testing.B) { for name, bc := range benchCasesSetupKeys { b.Run(name, func(b *testing.B) { - apiHandler, am, _ := tests_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) - tests_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, 1000) + apiHandler, am, _ := testing_tools.BuildApiBlackBoxWithDBState(b, "../testdata/setup_keys.sql", nil) + testing_tools.PopulateTestData(b, am.(*server.DefaultAccountManager), bc.Peers, bc.Groups, bc.Users, 1000) b.ResetTimer() start := time.Now() for i := 0; i < b.N; i++ { - req := tests_tools.BuildRequest(b, nil, http.MethodDelete, "/api/setup-keys/"+"oldkey-"+strconv.Itoa(i), tests_tools.TestAdminId) + req := testing_tools.BuildRequest(b, nil, http.MethodDelete, "/api/setup-keys/"+"oldkey-"+strconv.Itoa(i), testing_tools.TestAdminId) apiHandler.ServeHTTP(recorder, req) } - tests_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) + testing_tools.EvaluateBenchmarkResults(b, name, time.Since(start), expectedMetrics[name], recorder) }) } } diff --git a/management/server/http/tests_integration/setupkeys_handler_integration_test.go b/management/server/http/testing/integration/setupkeys_handler_integration_test.go similarity index 75% rename from management/server/http/tests_integration/setupkeys_handler_integration_test.go rename to management/server/http/testing/integration/setupkeys_handler_integration_test.go index fc8b9a84643..193c0fb022d 100644 --- a/management/server/http/tests_integration/setupkeys_handler_integration_test.go +++ b/management/server/http/testing/integration/setupkeys_handler_integration_test.go @@ -1,4 +1,4 @@ -package tests_integration +package integration import ( "context" @@ -14,7 +14,7 @@ import ( "github.com/netbirdio/netbird/management/server/http/api" "github.com/netbirdio/netbird/management/server/http/handlers/setup_keys" - "github.com/netbirdio/netbird/management/server/http/tests_tools" + "github.com/netbirdio/netbird/management/server/http/testing/testing_tools" ) func Test_SetupKeys_Create(t *testing.T) { @@ -27,42 +27,42 @@ func Test_SetupKeys_Create(t *testing.T) { }{ { name: "Regular user", - userId: tests_tools.TestUserId, + userId: testing_tools.TestUserId, expectResponse: false, }, { name: "Admin user", - userId: tests_tools.TestAdminId, + userId: testing_tools.TestAdminId, expectResponse: true, }, { name: "Owner user", - userId: tests_tools.TestOwnerId, + userId: testing_tools.TestOwnerId, expectResponse: true, }, { name: "Regular service user", - userId: tests_tools.TestServiceUserId, + userId: testing_tools.TestServiceUserId, expectResponse: false, }, { name: "Admin service user", - userId: tests_tools.TestServiceAdminId, + userId: testing_tools.TestServiceAdminId, expectResponse: true, }, { name: "Blocked user", - userId: tests_tools.BlockedUserId, + userId: testing_tools.BlockedUserId, expectResponse: false, }, { name: "Other user", - userId: tests_tools.OtherUserId, + userId: testing_tools.OtherUserId, expectResponse: false, }, { name: "Invalid token", - userId: tests_tools.InvalidToken, + userId: testing_tools.InvalidToken, expectResponse: false, }, } @@ -82,8 +82,8 @@ func Test_SetupKeys_Create(t *testing.T) { requestPath: "/api/setup-keys", requestBody: &api.CreateSetupKeyRequest{ AutoGroups: nil, - ExpiresIn: tests_tools.ExpiresIn, - Name: tests_tools.NewKeyName, + ExpiresIn: testing_tools.ExpiresIn, + Name: testing_tools.NewKeyName, Type: "reusable", UsageLimit: 0, }, @@ -95,7 +95,7 @@ func Test_SetupKeys_Create(t *testing.T) { Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.NewKeyName, + Name: testing_tools.NewKeyName, Revoked: false, State: "valid", Type: "reusable", @@ -111,8 +111,8 @@ func Test_SetupKeys_Create(t *testing.T) { requestPath: "/api/setup-keys", requestBody: &api.CreateSetupKeyRequest{ AutoGroups: nil, - ExpiresIn: tests_tools.ExpiresIn, - Name: tests_tools.ExistingKeyName, + ExpiresIn: testing_tools.ExpiresIn, + Name: testing_tools.ExistingKeyName, Type: "one-off", UsageLimit: 0, }, @@ -124,7 +124,7 @@ func Test_SetupKeys_Create(t *testing.T) { Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: false, State: "valid", Type: "one-off", @@ -140,8 +140,8 @@ func Test_SetupKeys_Create(t *testing.T) { requestPath: "/api/setup-keys", requestBody: &api.CreateSetupKeyRequest{ AutoGroups: nil, - ExpiresIn: tests_tools.ExpiresIn, - Name: tests_tools.NewKeyName, + ExpiresIn: testing_tools.ExpiresIn, + Name: testing_tools.NewKeyName, Type: "one-off", UsageLimit: 3, }, @@ -153,7 +153,7 @@ func Test_SetupKeys_Create(t *testing.T) { Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.NewKeyName, + Name: testing_tools.NewKeyName, Revoked: false, State: "valid", Type: "one-off", @@ -169,8 +169,8 @@ func Test_SetupKeys_Create(t *testing.T) { requestPath: "/api/setup-keys", requestBody: &api.CreateSetupKeyRequest{ AutoGroups: nil, - ExpiresIn: -tests_tools.ExpiresIn, - Name: tests_tools.NewKeyName, + ExpiresIn: -testing_tools.ExpiresIn, + Name: testing_tools.NewKeyName, Type: "one-off", UsageLimit: 0, }, @@ -182,21 +182,21 @@ func Test_SetupKeys_Create(t *testing.T) { requestType: http.MethodPost, requestPath: "/api/setup-keys", requestBody: &api.CreateSetupKeyRequest{ - AutoGroups: []string{tests_tools.TestGroupId}, - ExpiresIn: tests_tools.ExpiresIn, - Name: tests_tools.NewKeyName, + AutoGroups: []string{testing_tools.TestGroupId}, + ExpiresIn: testing_tools.ExpiresIn, + Name: testing_tools.NewKeyName, Type: "reusable", UsageLimit: 1, }, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.NewKeyName, + Name: testing_tools.NewKeyName, Revoked: false, State: "valid", Type: "reusable", @@ -212,8 +212,8 @@ func Test_SetupKeys_Create(t *testing.T) { requestPath: "/api/setup-keys", requestBody: &api.CreateSetupKeyRequest{ AutoGroups: []string{}, - ExpiresIn: tests_tools.ExpiresIn, - Name: tests_tools.NewKeyName, + ExpiresIn: testing_tools.ExpiresIn, + Name: testing_tools.NewKeyName, Type: "reusable", Ephemeral: &truePointer, UsageLimit: 1, @@ -226,7 +226,7 @@ func Test_SetupKeys_Create(t *testing.T) { Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.NewKeyName, + Name: testing_tools.NewKeyName, Revoked: false, State: "valid", Type: "reusable", @@ -242,8 +242,8 @@ func Test_SetupKeys_Create(t *testing.T) { requestPath: "/api/setup-keys", requestBody: &api.CreateSetupKeyRequest{ AutoGroups: []string{"someGroupID"}, - ExpiresIn: tests_tools.ExpiresIn, - Name: tests_tools.NewKeyName, + ExpiresIn: testing_tools.ExpiresIn, + Name: testing_tools.NewKeyName, Type: "reusable", UsageLimit: 0, }, @@ -256,8 +256,8 @@ func Test_SetupKeys_Create(t *testing.T) { requestPath: "/api/setup-keys", requestBody: &api.CreateSetupKeyRequest{ AutoGroups: nil, - ExpiresIn: tests_tools.ExpiresIn, - Name: tests_tools.NewKeyName, + ExpiresIn: testing_tools.ExpiresIn, + Name: testing_tools.NewKeyName, Type: "reusable", UsageLimit: 0, }, @@ -269,7 +269,7 @@ func Test_SetupKeys_Create(t *testing.T) { Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.NewKeyName, + Name: testing_tools.NewKeyName, Revoked: false, State: "valid", Type: "reusable", @@ -284,19 +284,19 @@ func Test_SetupKeys_Create(t *testing.T) { for _, tc := range tt { for _, user := range users { t.Run(user.name+" - "+tc.name, func(t *testing.T) { - apiHandler, am, done := tests_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) + apiHandler, am, done := testing_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) body, err := json.Marshal(tc.requestBody) if err != nil { t.Fatalf("Failed to marshal request body: %v", err) } - req := tests_tools.BuildRequest(t, body, tc.requestType, tc.requestPath, user.userId) + req := testing_tools.BuildRequest(t, body, tc.requestType, tc.requestPath, user.userId) recorder := httptest.NewRecorder() apiHandler.ServeHTTP(recorder, req) - content, expectResponse := tests_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) + content, expectResponse := testing_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) if !expectResponse { return } @@ -307,7 +307,7 @@ func Test_SetupKeys_Create(t *testing.T) { validateCreatedKey(t, tc.expectedResponse, got) - key, err := am.GetSetupKey(context.Background(), tests_tools.TestAccountId, tests_tools.TestUserId, got.Id) + key, err := am.GetSetupKey(context.Background(), testing_tools.TestAccountId, testing_tools.TestUserId, got.Id) if err != nil { return } @@ -332,42 +332,42 @@ func Test_SetupKeys_Update(t *testing.T) { }{ { name: "Regular user", - userId: tests_tools.TestUserId, + userId: testing_tools.TestUserId, expectResponse: false, }, { name: "Admin user", - userId: tests_tools.TestAdminId, + userId: testing_tools.TestAdminId, expectResponse: true, }, { name: "Owner user", - userId: tests_tools.TestOwnerId, + userId: testing_tools.TestOwnerId, expectResponse: true, }, { name: "Regular service user", - userId: tests_tools.TestServiceUserId, + userId: testing_tools.TestServiceUserId, expectResponse: false, }, { name: "Admin service user", - userId: tests_tools.TestServiceAdminId, + userId: testing_tools.TestServiceAdminId, expectResponse: true, }, { name: "Blocked user", - userId: tests_tools.BlockedUserId, + userId: testing_tools.BlockedUserId, expectResponse: false, }, { name: "Other user", - userId: tests_tools.OtherUserId, + userId: testing_tools.OtherUserId, expectResponse: false, }, { name: "Invalid token", - userId: tests_tools.InvalidToken, + userId: testing_tools.InvalidToken, expectResponse: false, }, } @@ -385,20 +385,20 @@ func Test_SetupKeys_Update(t *testing.T) { name: "Add existing Group to existing Setup Key", requestType: http.MethodPut, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.TestKeyId, + requestId: testing_tools.TestKeyId, requestBody: &api.SetupKeyRequest{ - AutoGroups: []string{tests_tools.TestGroupId, tests_tools.NewGroupId}, + AutoGroups: []string{testing_tools.TestGroupId, testing_tools.NewGroupId}, Revoked: false, }, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId, tests_tools.NewGroupId}, + AutoGroups: []string{testing_tools.TestGroupId, testing_tools.NewGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: false, State: "valid", Type: "one-off", @@ -412,9 +412,9 @@ func Test_SetupKeys_Update(t *testing.T) { name: "Add non-existing Group to existing Setup Key", requestType: http.MethodPut, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.TestKeyId, + requestId: testing_tools.TestKeyId, requestBody: &api.SetupKeyRequest{ - AutoGroups: []string{tests_tools.TestGroupId, "someGroupId"}, + AutoGroups: []string{testing_tools.TestGroupId, "someGroupId"}, Revoked: false, }, expectedStatus: http.StatusUnprocessableEntity, @@ -426,7 +426,7 @@ func Test_SetupKeys_Update(t *testing.T) { requestPath: "/api/setup-keys/{id}", requestId: "someId", requestBody: &api.SetupKeyRequest{ - AutoGroups: []string{tests_tools.TestGroupId, tests_tools.NewGroupId}, + AutoGroups: []string{testing_tools.TestGroupId, testing_tools.NewGroupId}, Revoked: false, }, expectedStatus: http.StatusNotFound, @@ -436,7 +436,7 @@ func Test_SetupKeys_Update(t *testing.T) { name: "Remove existing Group from existing Setup Key", requestType: http.MethodPut, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.TestKeyId, + requestId: testing_tools.TestKeyId, requestBody: &api.SetupKeyRequest{ AutoGroups: []string{}, Revoked: false, @@ -449,7 +449,7 @@ func Test_SetupKeys_Update(t *testing.T) { Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: false, State: "valid", Type: "one-off", @@ -475,20 +475,20 @@ func Test_SetupKeys_Update(t *testing.T) { name: "Revoke existing valid Setup Key", requestType: http.MethodPut, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.TestKeyId, + requestId: testing_tools.TestKeyId, requestBody: &api.SetupKeyRequest{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Revoked: true, }, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: true, State: "revoked", Type: "one-off", @@ -502,20 +502,20 @@ func Test_SetupKeys_Update(t *testing.T) { name: "Revoke existing revoked Setup Key", requestType: http.MethodPut, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.RevokedKeyId, + requestId: testing_tools.RevokedKeyId, requestBody: &api.SetupKeyRequest{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Revoked: true, }, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: true, State: "revoked", Type: "reusable", @@ -529,9 +529,9 @@ func Test_SetupKeys_Update(t *testing.T) { name: "Un-Revoke existing revoked Setup Key", requestType: http.MethodPut, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.RevokedKeyId, + requestId: testing_tools.RevokedKeyId, requestBody: &api.SetupKeyRequest{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Revoked: false, }, expectedStatus: http.StatusUnprocessableEntity, @@ -541,20 +541,20 @@ func Test_SetupKeys_Update(t *testing.T) { name: "Revoke existing expired Setup Key", requestType: http.MethodPut, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.ExpiredKeyId, + requestId: testing_tools.ExpiredKeyId, requestBody: &api.SetupKeyRequest{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Revoked: true, }, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: true, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: true, State: "expired", Type: "reusable", @@ -569,20 +569,20 @@ func Test_SetupKeys_Update(t *testing.T) { for _, tc := range tt { for _, user := range users { t.Run(tc.name, func(t *testing.T) { - apiHandler, am, done := tests_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) + apiHandler, am, done := testing_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) body, err := json.Marshal(tc.requestBody) if err != nil { t.Fatalf("Failed to marshal request body: %v", err) } - req := tests_tools.BuildRequest(t, body, tc.requestType, strings.Replace(tc.requestPath, "{id}", tc.requestId, 1), user.userId) + req := testing_tools.BuildRequest(t, body, tc.requestType, strings.Replace(tc.requestPath, "{id}", tc.requestId, 1), user.userId) recorder := httptest.NewRecorder() apiHandler.ServeHTTP(recorder, req) - content, expectResponse := tests_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) + content, expectResponse := testing_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) if !expectResponse { return } @@ -593,7 +593,7 @@ func Test_SetupKeys_Update(t *testing.T) { validateCreatedKey(t, tc.expectedResponse, got) - key, err := am.GetSetupKey(context.Background(), tests_tools.TestAccountId, tests_tools.TestUserId, got.Id) + key, err := am.GetSetupKey(context.Background(), testing_tools.TestAccountId, testing_tools.TestUserId, got.Id) if err != nil { return } @@ -618,42 +618,42 @@ func Test_SetupKeys_Get(t *testing.T) { }{ { name: "Regular user", - userId: tests_tools.TestUserId, + userId: testing_tools.TestUserId, expectResponse: false, }, { name: "Admin user", - userId: tests_tools.TestAdminId, + userId: testing_tools.TestAdminId, expectResponse: true, }, { name: "Owner user", - userId: tests_tools.TestOwnerId, + userId: testing_tools.TestOwnerId, expectResponse: true, }, { name: "Regular service user", - userId: tests_tools.TestServiceUserId, + userId: testing_tools.TestServiceUserId, expectResponse: false, }, { name: "Admin service user", - userId: tests_tools.TestServiceAdminId, + userId: testing_tools.TestServiceAdminId, expectResponse: true, }, { name: "Blocked user", - userId: tests_tools.BlockedUserId, + userId: testing_tools.BlockedUserId, expectResponse: false, }, { name: "Other user", - userId: tests_tools.OtherUserId, + userId: testing_tools.OtherUserId, expectResponse: false, }, { name: "Invalid token", - userId: tests_tools.InvalidToken, + userId: testing_tools.InvalidToken, expectResponse: false, }, } @@ -670,16 +670,16 @@ func Test_SetupKeys_Get(t *testing.T) { name: "Get existing valid Setup Key", requestType: http.MethodGet, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.TestKeyId, + requestId: testing_tools.TestKeyId, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: false, State: "valid", Type: "one-off", @@ -693,16 +693,16 @@ func Test_SetupKeys_Get(t *testing.T) { name: "Get existing expired Setup Key", requestType: http.MethodGet, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.ExpiredKeyId, + requestId: testing_tools.ExpiredKeyId, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: true, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: false, State: "expired", Type: "reusable", @@ -716,16 +716,16 @@ func Test_SetupKeys_Get(t *testing.T) { name: "Get existing revoked Setup Key", requestType: http.MethodGet, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.RevokedKeyId, + requestId: testing_tools.RevokedKeyId, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: true, State: "revoked", Type: "reusable", @@ -748,15 +748,15 @@ func Test_SetupKeys_Get(t *testing.T) { for _, tc := range tt { for _, user := range users { t.Run(tc.name, func(t *testing.T) { - apiHandler, am, done := tests_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) + apiHandler, am, done := testing_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) - req := tests_tools.BuildRequest(t, []byte{}, tc.requestType, strings.Replace(tc.requestPath, "{id}", tc.requestId, 1), user.userId) + req := testing_tools.BuildRequest(t, []byte{}, tc.requestType, strings.Replace(tc.requestPath, "{id}", tc.requestId, 1), user.userId) recorder := httptest.NewRecorder() apiHandler.ServeHTTP(recorder, req) - content, expectRespnose := tests_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) + content, expectRespnose := testing_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) if !expectRespnose { return } @@ -767,7 +767,7 @@ func Test_SetupKeys_Get(t *testing.T) { validateCreatedKey(t, tc.expectedResponse, got) - key, err := am.GetSetupKey(context.Background(), tests_tools.TestAccountId, tests_tools.TestUserId, got.Id) + key, err := am.GetSetupKey(context.Background(), testing_tools.TestAccountId, testing_tools.TestUserId, got.Id) if err != nil { return } @@ -792,42 +792,42 @@ func Test_SetupKeys_GetAll(t *testing.T) { }{ { name: "Regular user", - userId: tests_tools.TestUserId, + userId: testing_tools.TestUserId, expectResponse: false, }, { name: "Admin user", - userId: tests_tools.TestAdminId, + userId: testing_tools.TestAdminId, expectResponse: true, }, { name: "Owner user", - userId: tests_tools.TestOwnerId, + userId: testing_tools.TestOwnerId, expectResponse: true, }, { name: "Regular service user", - userId: tests_tools.TestServiceUserId, + userId: testing_tools.TestServiceUserId, expectResponse: false, }, { name: "Admin service user", - userId: tests_tools.TestServiceAdminId, + userId: testing_tools.TestServiceAdminId, expectResponse: true, }, { name: "Blocked user", - userId: tests_tools.BlockedUserId, + userId: testing_tools.BlockedUserId, expectResponse: false, }, { name: "Other user", - userId: tests_tools.OtherUserId, + userId: testing_tools.OtherUserId, expectResponse: false, }, { name: "Invalid token", - userId: tests_tools.InvalidToken, + userId: testing_tools.InvalidToken, expectResponse: false, }, } @@ -846,13 +846,13 @@ func Test_SetupKeys_GetAll(t *testing.T) { expectedStatus: http.StatusOK, expectedResponse: []*api.SetupKey{ { - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: false, State: "valid", Type: "one-off", @@ -862,13 +862,13 @@ func Test_SetupKeys_GetAll(t *testing.T) { Valid: true, }, { - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: true, State: "revoked", Type: "reusable", @@ -878,13 +878,13 @@ func Test_SetupKeys_GetAll(t *testing.T) { Valid: false, }, { - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: true, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: false, State: "expired", Type: "reusable", @@ -900,15 +900,15 @@ func Test_SetupKeys_GetAll(t *testing.T) { for _, tc := range tt { for _, user := range users { t.Run(tc.name, func(t *testing.T) { - apiHandler, am, done := tests_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) + apiHandler, am, done := testing_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) - req := tests_tools.BuildRequest(t, []byte{}, tc.requestType, tc.requestPath, user.userId) + req := testing_tools.BuildRequest(t, []byte{}, tc.requestType, tc.requestPath, user.userId) recorder := httptest.NewRecorder() apiHandler.ServeHTTP(recorder, req) - content, expectResponse := tests_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) + content, expectResponse := testing_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) if !expectResponse { return } @@ -928,7 +928,7 @@ func Test_SetupKeys_GetAll(t *testing.T) { for i, _ := range tc.expectedResponse { validateCreatedKey(t, tc.expectedResponse[i], &got[i]) - key, err := am.GetSetupKey(context.Background(), tests_tools.TestAccountId, tests_tools.TestUserId, got[i].Id) + key, err := am.GetSetupKey(context.Background(), testing_tools.TestAccountId, testing_tools.TestUserId, got[i].Id) if err != nil { return } @@ -954,42 +954,42 @@ func Test_SetupKeys_Delete(t *testing.T) { }{ { name: "Regular user", - userId: tests_tools.TestUserId, + userId: testing_tools.TestUserId, expectResponse: false, }, { name: "Admin user", - userId: tests_tools.TestAdminId, + userId: testing_tools.TestAdminId, expectResponse: true, }, { name: "Owner user", - userId: tests_tools.TestOwnerId, + userId: testing_tools.TestOwnerId, expectResponse: true, }, { name: "Regular service user", - userId: tests_tools.TestServiceUserId, + userId: testing_tools.TestServiceUserId, expectResponse: false, }, { name: "Admin service user", - userId: tests_tools.TestServiceAdminId, + userId: testing_tools.TestServiceAdminId, expectResponse: true, }, { name: "Blocked user", - userId: tests_tools.BlockedUserId, + userId: testing_tools.BlockedUserId, expectResponse: false, }, { name: "Other user", - userId: tests_tools.OtherUserId, + userId: testing_tools.OtherUserId, expectResponse: false, }, { name: "Invalid token", - userId: tests_tools.InvalidToken, + userId: testing_tools.InvalidToken, expectResponse: false, }, } @@ -1006,16 +1006,16 @@ func Test_SetupKeys_Delete(t *testing.T) { name: "Delete existing valid Setup Key", requestType: http.MethodDelete, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.TestKeyId, + requestId: testing_tools.TestKeyId, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: false, State: "valid", Type: "one-off", @@ -1029,16 +1029,16 @@ func Test_SetupKeys_Delete(t *testing.T) { name: "Delete existing expired Setup Key", requestType: http.MethodDelete, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.ExpiredKeyId, + requestId: testing_tools.ExpiredKeyId, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: true, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: false, State: "expired", Type: "reusable", @@ -1052,16 +1052,16 @@ func Test_SetupKeys_Delete(t *testing.T) { name: "Delete existing revoked Setup Key", requestType: http.MethodDelete, requestPath: "/api/setup-keys/{id}", - requestId: tests_tools.RevokedKeyId, + requestId: testing_tools.RevokedKeyId, expectedStatus: http.StatusOK, expectedResponse: &api.SetupKey{ - AutoGroups: []string{tests_tools.TestGroupId}, + AutoGroups: []string{testing_tools.TestGroupId}, Ephemeral: false, Expires: time.Time{}, Id: "", Key: "", LastUsed: time.Time{}, - Name: tests_tools.ExistingKeyName, + Name: testing_tools.ExistingKeyName, Revoked: true, State: "revoked", Type: "reusable", @@ -1084,15 +1084,15 @@ func Test_SetupKeys_Delete(t *testing.T) { for _, tc := range tt { for _, user := range users { t.Run(tc.name, func(t *testing.T) { - apiHandler, am, done := tests_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) + apiHandler, am, done := testing_tools.BuildApiBlackBoxWithDBState(t, "../testdata/setup_keys.sql", nil) - req := tests_tools.BuildRequest(t, []byte{}, tc.requestType, strings.Replace(tc.requestPath, "{id}", tc.requestId, 1), user.userId) + req := testing_tools.BuildRequest(t, []byte{}, tc.requestType, strings.Replace(tc.requestPath, "{id}", tc.requestId, 1), user.userId) recorder := httptest.NewRecorder() apiHandler.ServeHTTP(recorder, req) - content, expectResponse := tests_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) + content, expectResponse := testing_tools.ReadResponse(t, recorder, tc.expectedStatus, user.expectResponse) if !expectResponse { return } @@ -1101,7 +1101,7 @@ func Test_SetupKeys_Delete(t *testing.T) { t.Fatalf("Sent content is not in correct json format; %v", err) } - _, err := am.GetSetupKey(context.Background(), tests_tools.TestAccountId, tests_tools.TestUserId, got.Id) + _, err := am.GetSetupKey(context.Background(), testing_tools.TestAccountId, testing_tools.TestUserId, got.Id) assert.Errorf(t, err, "Expected error when trying to get deleted key") select { @@ -1117,7 +1117,7 @@ func Test_SetupKeys_Delete(t *testing.T) { func validateCreatedKey(t *testing.T, expectedKey *api.SetupKey, got *api.SetupKey) { t.Helper() - if got.Expires.After(time.Now().Add(-1*time.Minute)) && got.Expires.Before(time.Now().Add(tests_tools.ExpiresIn*time.Second)) || + if got.Expires.After(time.Now().Add(-1*time.Minute)) && got.Expires.Before(time.Now().Add(testing_tools.ExpiresIn*time.Second)) || got.Expires.After(time.Date(2300, 01, 01, 0, 0, 0, 0, time.Local)) || got.Expires.Before(time.Date(1950, 01, 01, 0, 0, 0, 0, time.Local)) { got.Expires = time.Time{} diff --git a/management/server/http/testdata/setup_keys.sql b/management/server/http/testing/testdata/setup_keys.sql similarity index 100% rename from management/server/http/testdata/setup_keys.sql rename to management/server/http/testing/testdata/setup_keys.sql diff --git a/management/server/http/tests_tools/tools.go b/management/server/http/testing/testing_tools/tools.go similarity index 99% rename from management/server/http/tests_tools/tools.go rename to management/server/http/testing/testing_tools/tools.go index 7323f978598..78c7a4fb5c3 100644 --- a/management/server/http/tests_tools/tools.go +++ b/management/server/http/testing/testing_tools/tools.go @@ -1,4 +1,4 @@ -package tests_tools +package testing_tools import ( "bytes"