diff --git a/server/e2e/common.go b/server/e2e/common.go index 396a11753..889d9861e 100644 --- a/server/e2e/common.go +++ b/server/e2e/common.go @@ -37,11 +37,6 @@ func init() { mongotest.Env = "REEARTH_DB" } -func StartServer(t *testing.T, cfg *config.Config, useMongo bool, seeder Seeder) *httpexpect.Expect { - e, _, _ := StartServerAndRepos(t, cfg, useMongo, seeder) - return e -} - func initRepos(t *testing.T, useMongo bool, seeder Seeder) (repos *repo.Container) { ctx := context.Background() @@ -68,13 +63,26 @@ func initGateway() *gateway.Container { } } -func StartServerAndRepos(t *testing.T, cfg *config.Config, useMongo bool, seeder Seeder) (*httpexpect.Expect, *repo.Container, *gateway.Container) { - repos := initRepos(t, useMongo, seeder) +func initAccountGateway(ctx context.Context) *accountgateway.Container { + return &accountgateway.Container{ + Mailer: mailer.New(ctx, &mailer.Config{}), + } +} + +func initServerWithAccountGateway(cfg *config.Config, repos *repo.Container, ctx context.Context) (*app.WebServer, *gateway.Container, *accountgateway.Container) { gateways := initGateway() - return StartServerWithRepos(t, cfg, repos, gateways), repos, gateways + accountGateway := initAccountGateway(ctx) + return app.NewServer(ctx, &app.ServerConfig{ + Config: cfg, + Repos: repos, + AccountRepos: repos.AccountRepos(), + Gateways: gateways, + AccountGateways: accountGateway, + Debug: true, + }), gateways, accountGateway } -func StartServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Container, gateways *gateway.Container) *httpexpect.Expect { +func StartGQLServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Container) (*httpexpect.Expect, *gateway.Container, *accountgateway.Container) { t.Helper() if testing.Short() { @@ -82,19 +90,12 @@ func StartServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Containe } ctx := context.Background() - l, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("server failed to listen: %v", err) } - srv := app.NewServer(ctx, &app.ServerConfig{ - Config: cfg, - Repos: repos, - Gateways: gateways, - Debug: true, - AccountRepos: repos.AccountRepos(), - }) + srv, gateways, accountGateway := initServerWithAccountGateway(cfg, repos, ctx) ch := make(chan error) go func() { @@ -103,7 +104,6 @@ func StartServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Containe } close(ch) }() - t.Cleanup(func() { if err := srv.Shutdown(context.Background()); err != nil { t.Fatalf("server shutdown: %v", err) @@ -113,14 +113,7 @@ func StartServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Containe t.Fatalf("server serve: %v", err) } }) - - return httpexpect.Default(t, "http://"+l.Addr().String()) -} - -type GraphQLRequest struct { - OperationName string `json:"operationName"` - Query string `json:"query"` - Variables map[string]any `json:"variables"` + return httpexpect.Default(t, "http://"+l.Addr().String()), gateways, accountGateway } func StartGQLServerAndRepos(t *testing.T, seeder Seeder) (*httpexpect.Expect, *accountrepo.Container) { @@ -131,35 +124,36 @@ func StartGQLServerAndRepos(t *testing.T, seeder Seeder) (*httpexpect.Expect, *a }, } repos := initRepos(t, true, seeder) - acRepos := repos.AccountRepos() - return StartGQLServerWithRepos(t, cfg, repos, acRepos), acRepos + e, _, _ := StartGQLServerWithRepos(t, cfg, repos) + return e, repos.AccountRepos() } -func StartGQLServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Container, accountrepos *accountrepo.Container) *httpexpect.Expect { +func initServer(cfg *config.Config, repos *repo.Container, ctx context.Context) (*app.WebServer, *gateway.Container) { + gateways := initGateway() + return app.NewServer(ctx, &app.ServerConfig{ + Config: cfg, + Repos: repos, + AccountRepos: repos.AccountRepos(), + Gateways: gateways, + Debug: true, + }), gateways +} + +func StartServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Container) (*httpexpect.Expect, *gateway.Container) { t.Helper() if testing.Short() { - t.SkipNow() + t.Skip("skipping test in short mode.") } ctx := context.Background() + l, err := net.Listen("tcp", ":0") if err != nil { t.Fatalf("server failed to listen: %v", err) } - srv := app.NewServer(ctx, &app.ServerConfig{ - Config: cfg, - Repos: repos, - AccountRepos: accountrepos, - Gateways: &gateway.Container{ - File: lo.Must(fs.NewFile(afero.NewMemMapFs(), "https://example.com")), - }, - AccountGateways: &accountgateway.Container{ - Mailer: mailer.New(ctx, &mailer.Config{}), - }, - Debug: true, - }) + srv, gateways := initServer(cfg, repos, ctx) ch := make(chan error) go func() { @@ -177,7 +171,18 @@ func StartGQLServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Conta t.Fatalf("server serve: %v", err) } }) - return httpexpect.Default(t, "http://"+l.Addr().String()) + return httpexpect.Default(t, "http://"+l.Addr().String()), gateways +} + +func StartServerAndRepos(t *testing.T, cfg *config.Config, useMongo bool, seeder Seeder) (*httpexpect.Expect, *repo.Container, *gateway.Container) { + repos := initRepos(t, useMongo, seeder) + e, gateways := StartServerWithRepos(t, cfg, repos) + return e, repos, gateways +} + +func StartServer(t *testing.T, cfg *config.Config, useMongo bool, seeder Seeder) *httpexpect.Expect { + e, _, _ := StartServerAndRepos(t, cfg, useMongo, seeder) + return e } func Server(t *testing.T, seeder Seeder) *httpexpect.Expect { @@ -204,6 +209,12 @@ func ServerLanguage(t *testing.T, lang language.Tag) *httpexpect.Expect { ) } +type GraphQLRequest struct { + OperationName string `json:"operationName"` + Query string `json:"query"` + Variables map[string]any `json:"variables"` +} + func Request(e *httpexpect.Expect, user string, requestBody GraphQLRequest) *httpexpect.Value { return e.POST("/api/graphql"). WithHeader("Origin", "https://example.com"). diff --git a/server/e2e/dataset_export_test.go b/server/e2e/dataset_export_test.go index cf5a37edd..bb71472ab 100644 --- a/server/e2e/dataset_export_test.go +++ b/server/e2e/dataset_export_test.go @@ -4,16 +4,11 @@ import ( "net/http" "testing" - "github.com/reearth/reearth/server/internal/app/config" "github.com/reearth/reearth/server/pkg/dataset" ) func TestDatasetExport(t *testing.T) { - e := StartServer(t, &config.Config{ - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) e.GET("/api/datasets/test"). Expect(). diff --git a/server/e2e/gql_asset_test.go b/server/e2e/gql_asset_test.go index 6cf37930c..fcb5c539b 100644 --- a/server/e2e/gql_asset_test.go +++ b/server/e2e/gql_asset_test.go @@ -2,7 +2,6 @@ package e2e import ( "context" - "net/http" "os" "testing" @@ -216,12 +215,5 @@ func getAssets(e *httpexpect.Expect, teamId string) *httpexpect.Value { }, }, } - return e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON() + return Request(e, uID.String(), requestBody) } diff --git a/server/e2e/gql_featureCollection_test.go b/server/e2e/gql_featureCollection_test.go index 706dad42f..01e69ad98 100644 --- a/server/e2e/gql_featureCollection_test.go +++ b/server/e2e/gql_featureCollection_test.go @@ -1,11 +1,9 @@ package e2e import ( - "net/http" "testing" "github.com/gavv/httpexpect/v2" - "github.com/reearth/reearth/server/internal/app/config" ) func addGeoJSONFeature( @@ -72,14 +70,7 @@ func addGeoJSONFeature( }, } - res := e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON() + res := Request(e, uID.String(), requestBody) featureId := res.Path("$.data.addGeoJSONFeature.id").Raw().(string) return requestBody, res, featureId @@ -150,14 +141,7 @@ func updateGeoJSONFeature( }, } - res := e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON() + res := Request(e, uID.String(), requestBody) fId := res.Path("$.data.updateGeoJSONFeature.id").Raw().(string) return requestBody, res, fId @@ -183,26 +167,14 @@ func deleteGeoJSONFeature( }, } - res := e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON() + res := Request(e, uID.String(), requestBody) fId := res.Path("$.data.deleteGeoJSONFeature.deletedFeatureId").Raw().(string) return requestBody, res, fId } func TestFeatureCollectionCRUD(t *testing.T) { - e := StartServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) pId := createProject(e, "test") _, _, sId := createScene(e, pId) diff --git a/server/e2e/gql_me_test.go b/server/e2e/gql_me_test.go index 25eb38b9e..7331f8a1c 100644 --- a/server/e2e/gql_me_test.go +++ b/server/e2e/gql_me_test.go @@ -1,7 +1,6 @@ package e2e import ( - "net/http" "testing" "github.com/reearth/reearth/server/internal/app/config" @@ -22,15 +21,7 @@ func TestMe(t *testing.T) { Variables: map[string]any{}, } - e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - // WithHeader("authorization", "Bearer test"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON(). + Request(e, uID.String(), requestBody). Object(). Value("data").Object(). Value("me").Object(). diff --git a/server/e2e/gql_project_export_test.go b/server/e2e/gql_project_export_test.go index 40cf62c1b..5b9d2f704 100644 --- a/server/e2e/gql_project_export_test.go +++ b/server/e2e/gql_project_export_test.go @@ -7,7 +7,6 @@ import ( "testing" "github.com/gavv/httpexpect/v2" - "github.com/reearth/reearth/server/internal/app/config" "github.com/stretchr/testify/assert" ) @@ -16,12 +15,7 @@ import ( func TestCallExportProject(t *testing.T) { - e := StartServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) pID := createProjectWithExternalImage(e, "test") @@ -86,14 +80,7 @@ func createProjectWithExternalImage(e *httpexpect.Expect, name string) string { "coreSupport": true, }, } - res := e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON() + res := Request(e, uID.String(), requestBody) return res.Path("$.data.createProject.project.id").Raw().(string) } @@ -105,15 +92,7 @@ func exporProject(t *testing.T, e *httpexpect.Expect, p string) string { "projectId": p, }, } - r := e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("authorization", "Bearer test"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON(). + r := Request(e, uID.String(), requestBody). Object() downloadPath := r. Value("data").Object(). diff --git a/server/e2e/gql_project_import_test.go b/server/e2e/gql_project_import_test.go index 821540f81..3cde15f47 100644 --- a/server/e2e/gql_project_import_test.go +++ b/server/e2e/gql_project_import_test.go @@ -1,12 +1,10 @@ package e2e import ( - "net/http" "os" "testing" "github.com/gavv/httpexpect/v2" - "github.com/reearth/reearth/server/internal/app/config" "github.com/stretchr/testify/assert" "golang.org/x/text/language" ) @@ -16,12 +14,7 @@ import ( func TestCallImportProject(t *testing.T) { - e := StartServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) filePath := "test.zip" @@ -84,15 +77,7 @@ func getScene(e *httpexpect.Expect, s string, l string) *httpexpect.Object { "lang": l, }, } - r := e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("authorization", "Bearer test"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON(). + r := Request(e, uID.String(), requestBody). Object() v := r.Value("data").Object().Value("node") v.NotNull() diff --git a/server/e2e/gql_project_test.go b/server/e2e/gql_project_test.go index a04382612..f524c917a 100644 --- a/server/e2e/gql_project_test.go +++ b/server/e2e/gql_project_test.go @@ -3,7 +3,6 @@ package e2e import ( "context" "fmt" - "net/http" "net/url" "testing" @@ -18,12 +17,7 @@ import ( ) func TestCreateAndGetProject(t *testing.T) { - e := StartServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) testData(e) @@ -60,12 +54,7 @@ func TestCreateAndGetProject(t *testing.T) { } func TestSortByName(t *testing.T) { - e := StartServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) createProject(e, "a-project") createProject(e, "b-project") @@ -163,12 +152,7 @@ func TestSortByName(t *testing.T) { func TestFindStarredByWorkspace(t *testing.T) { - e := StartServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) project1ID := createProject(e, "Project 1") project2ID := createProject(e, "Project 2") project3ID := createProject(e, "Project 3") @@ -322,12 +306,7 @@ fragment ProjectFragment on Project { func TestSortByUpdatedAt(t *testing.T) { - e := StartServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) createProject(e, "project1-test") project2ID := createProject(e, "project2-test") @@ -385,12 +364,7 @@ func TestSortByUpdatedAt(t *testing.T) { func TestDeleteProjects(t *testing.T) { - e := StartServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) testData(e) @@ -477,15 +451,7 @@ func createGraphQLRequest(name string, coreSupport bool) GraphQLRequest { } func callRequest(e *httpexpect.Expect, requestBody GraphQLRequest) *httpexpect.Object { - return e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("authorization", "Bearer test"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON(). + return Request(e, uID.String(), requestBody). Object(). Value("data").Object() } diff --git a/server/e2e/gql_style_test.go b/server/e2e/gql_style_test.go index df2ab70c7..aac8014c9 100644 --- a/server/e2e/gql_style_test.go +++ b/server/e2e/gql_style_test.go @@ -1,11 +1,9 @@ package e2e import ( - "net/http" "testing" "github.com/gavv/httpexpect/v2" - "github.com/reearth/reearth/server/internal/app/config" ) func addStyle(e *httpexpect.Expect, sId, name string) (GraphQLRequest, *httpexpect.Value, string) { @@ -51,12 +49,7 @@ func addStyle(e *httpexpect.Expect, sId, name string) (GraphQLRequest, *httpexpe }, } - res := e.POST("/api/graphql"). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON() + res := Request(e, uID.String(), requestBody) styleId := res.Path("$.data.addStyle.style.id").String().Raw() return requestBody, res, styleId @@ -80,12 +73,7 @@ func updateStyleName(e *httpexpect.Expect, styleId, newName string) (GraphQLRequ }, } - res := e.POST("/api/graphql"). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON() + res := Request(e, uID.String(), requestBody) return requestBody, res } @@ -103,12 +91,7 @@ func removeStyle(e *httpexpect.Expect, styleId string) (GraphQLRequest, *httpexp }, } - res := e.POST("/api/graphql"). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON() + res := Request(e, uID.String(), requestBody) return requestBody, res } @@ -130,12 +113,7 @@ func duplicateStyle(e *httpexpect.Expect, styleId string) (GraphQLRequest, *http }, } - res := e.POST("/api/graphql"). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody). - Expect(). - Status(http.StatusOK). - JSON() + res := Request(e, uID.String(), requestBody) return requestBody, res } @@ -162,25 +140,12 @@ func fetchSceneForStyles(e *httpexpect.Expect, sID string) (GraphQLRequest, *htt }, } - res := e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("X-Reearth-Debug-User", uID.String()). - WithHeader("Content-Type", "application/json"). - WithJSON(fetchSceneRequestBody). - Expect(). - Status(http.StatusOK). - JSON() - + res := Request(e, uID.String(), fetchSceneRequestBody) return fetchSceneRequestBody, res } func TestStyleCRUD(t *testing.T) { - e := StartServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeeder) + e := Server(t, baseSeeder) pId := createProject(e, "test") _, _, sId := createScene(e, pId) diff --git a/server/e2e/mock_test.go b/server/e2e/mock_test.go index 7b7eac540..648053663 100644 --- a/server/e2e/mock_test.go +++ b/server/e2e/mock_test.go @@ -41,14 +41,7 @@ func TestMockAuth(t *testing.T) { Query: "query GetMe { \n me { \n id \n name \n email\n } \n}", Variables: map[string]any{}, } - response2 := e.POST("/api/graphql"). - WithHeader("Origin", "https://example.com"). - WithHeader("X-Reearth-Debug-User", userId). - WithHeader("Content-Type", "application/json"). - WithJSON(requestBody2). - Expect(). - Status(http.StatusOK). - JSON(). + response2 := Request(e, userId, requestBody2). Object() response2.Value("data").Object().Value("me").Object().ContainsKey("id")