Skip to content
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

chore(server): refactoring e2e test code #1393

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 54 additions & 43 deletions server/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -68,33 +63,39 @@ 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() {
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,
Gateways: gateways,
Debug: true,
AccountRepos: repos.AccountRepos(),
})
srv, gateways, accountGateway := initServerWithAccountGateway(cfg, repos, ctx)

ch := make(chan error)
go func() {
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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 {
Expand All @@ -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").
Expand Down
7 changes: 1 addition & 6 deletions server/e2e/dataset_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
10 changes: 1 addition & 9 deletions server/e2e/gql_asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package e2e

import (
"context"
"net/http"
"os"
"testing"

Expand Down Expand Up @@ -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)
}
36 changes: 4 additions & 32 deletions server/e2e/gql_featureCollection_test.go
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
11 changes: 1 addition & 10 deletions server/e2e/gql_me_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package e2e

import (
"net/http"
"testing"

"github.com/reearth/reearth/server/internal/app/config"
Expand All @@ -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().
Expand Down
27 changes: 3 additions & 24 deletions server/e2e/gql_project_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

"github.com/gavv/httpexpect/v2"
"github.com/reearth/reearth/server/internal/app/config"
"github.com/stretchr/testify/assert"
)

Expand All @@ -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")

Expand Down Expand Up @@ -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)
}

Expand All @@ -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().
Expand Down
Loading