Skip to content

Commit

Permalink
test(server): add package for e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Oct 3, 2022
1 parent 76405b2 commit cf7ca5f
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 5 deletions.
7 changes: 5 additions & 2 deletions server/Makefile
Expand Up @@ -2,7 +2,10 @@ lint:
golangci-lint run --fix

test:
go test -race -v ./...
go test -race -short -v ./...

e2e:
go test -v ./e2e/...

build:
go build ./cmd/reearth
Expand All @@ -16,4 +19,4 @@ run-db:
gql:
go generate ./internal/adapter/gql

.PHONY: lint test build run-app run-db gql
.PHONY: lint test e2e build run-app run-db gql
70 changes: 70 additions & 0 deletions server/e2e/common_test.go
@@ -0,0 +1,70 @@
package e2e

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

"github.com/gavv/httpexpect/v2"
"github.com/reearth/reearth/server/internal/app"
"github.com/reearth/reearth/server/internal/infrastructure/memory"
"github.com/reearth/reearth/server/internal/infrastructure/mongo"
"github.com/reearth/reearth/server/internal/usecase/gateway"
"github.com/reearth/reearth/server/internal/usecase/repo"
"github.com/reearth/reearthx/mongox/mongotest"
"github.com/samber/lo"
)

func init() {
mongotest.Env = "REEARTH_DB"
}

func StartServer(t *testing.T, cfg *app.Config, useMongo bool) *httpexpect.Expect {
t.Helper()

if testing.Short() {
t.SkipNow()
}

ctx := context.Background()

l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatalf("server failed to listen: %v", err)
}

var repos *repo.Container
if useMongo {
db := mongotest.Connect(t)(t)
repos = lo.Must(mongo.New(ctx, db))
} else {
repos = memory.New()
}

srv := app.NewServer(ctx, &app.ServerConfig{
Config: cfg,
Repos: repos,
Gateways: &gateway.Container{},
})

ch := make(chan error)
go func() {
if err := srv.Serve(l); err != http.ErrServerClosed {
ch <- err
}
close(ch)
}()

t.Cleanup(func() {
if err := srv.Shutdown(context.Background()); err != nil {
t.Fatalf("server shutdown: %v", err)
}

if err := <-ch; err != nil {
t.Fatalf("server serve: %v", err)
}
})

return httpexpect.New(t, "http://"+l.Addr().String())
}
39 changes: 39 additions & 0 deletions server/e2e/ping_test.go
@@ -0,0 +1,39 @@
package e2e

import (
"net/http"
"testing"

"github.com/reearth/reearth/server/internal/app"
)

func TestPingAPI(t *testing.T) {
e := StartServer(t, &app.Config{
Origins: []string{"https://example.com"},
AuthSrv: app.AuthSrvConfig{
Disabled: true,
},
}, false)

e.OPTIONS("/api/ping").
WithHeader("Origin", "https://example.com").
Expect().
Status(http.StatusNoContent).
Header("Access-Control-Allow-Origin").
Equal("https://example.com")

r := e.GET("/api/ping").
WithHeader("Origin", "https://example.com").
Expect()

r.Header("Cache-Control").
Equal("private, no-store, no-cache, must-revalidate")

r.Header("Access-Control-Allow-Origin").
Equal("https://example.com")

r.Status(http.StatusOK).
JSON().
String().
Equal("pong")
}
15 changes: 15 additions & 0 deletions server/go.mod
Expand Up @@ -8,6 +8,7 @@ require (
github.com/auth0/go-jwt-middleware/v2 v2.0.1
github.com/avast/retry-go/v4 v4.0.4
github.com/blang/semver v3.5.1+incompatible
github.com/gavv/httpexpect/v2 v2.3.1
github.com/goccy/go-yaml v1.9.5
github.com/google/uuid v1.3.0
github.com/iancoleman/strcase v0.2.0
Expand Down Expand Up @@ -59,12 +60,15 @@ require (
cloud.google.com/go/trace v1.2.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.32.3 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20210927113745-59d0afb8317a // indirect
github.com/andybalholm/brotli v1.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/trifles v0.0.0-20200705224438-cafc02a1ee2b // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/fatih/structs v1.0.0 // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/gedex/inflector v0.0.0-20170307190818-16278e9db813 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand All @@ -75,6 +79,7 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/pprof v0.0.0-20220412212628-83db2b799d1f // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/googleapis/go-type-adapters v1.0.0 // indirect
Expand All @@ -86,6 +91,7 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/imkira/go-interpol v1.0.0 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/matryer/moq v0.2.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand All @@ -96,17 +102,25 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sendgrid/rest v2.6.6+incompatible // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/smartystreets/assertions v1.1.1 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/tidwall/pretty v1.0.1 // indirect
github.com/urfave/cli/v2 v2.8.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.27.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.1 // indirect
github.com/xdg-go/stringprep v1.0.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yalp/jsonpath v0.0.0-20180802001716-5cc68e5049a0 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
github.com/yudai/gojsondiff v1.0.0 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/zitadel/logging v0.3.3 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/contrib v1.7.0 // indirect
Expand All @@ -127,6 +141,7 @@ require (
gopkg.in/alecthomas/kingpin.v2 v2.2.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
moul.io/http2curl v1.0.1-0.20190925090545-5cd742060b0e // indirect
)

go 1.19

0 comments on commit cf7ca5f

Please sign in to comment.