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 id project and workspace [VIZ-BE-574] #1426

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ help:
@echo " run-app Run the application"
@echo " run-db Run the MongoDB database using Docker Compose"
@echo " run-reset Initialize the MongoDB data"
@echo " run-clear-cache Clear the golang cache"
@echo " up-gcs Run the fake-gcs-server using Docker Compose"
@echo " down-gcs Stop the fake-gcs-server using Docker Compose"
@echo " gql Generate GraphQL code include dataloader"
Expand Down Expand Up @@ -89,6 +90,9 @@ run-reset:
make run-db
make mockuser

run-clear-cache:
go clean -cache -modcache -testcache -fuzzcache

generate: dev-install
go generate ./...

Expand All @@ -109,4 +113,4 @@ schematyper:
go run $(SCHEMATYPER) -o $(MANIFEST_DIR)/schema_translation.go --package manifest --prefix Translation ./schemas/plugin_manifest_translation.json
go run $(SCHEMATYPER) -o $(MANIFEST_DIR)/schema_gen.go --package manifest ./schemas/plugin_manifest.json

.PHONY: lint test failcheck e2e build dev-install dev run-app run-db run-reset gql up-gcs down-gcs mockuser schematyper
.PHONY: lint test failcheck e2e build dev-install dev run-app run-db run-reset run-clear-cache gql up-gcs down-gcs mockuser schematyper
142 changes: 68 additions & 74 deletions server/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net"
"net/http"
"reflect"
"regexp"
"strings"
"testing"
Expand All @@ -31,13 +32,23 @@ import (
"golang.org/x/text/language"
)

type Seeder func(ctx context.Context, r *repo.Container) error
var (
fr *gateway.File
disabledAuthConfig = &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
}
)

type Seeder func(ctx context.Context, r *repo.Container, f gateway.File) error

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

func initRepos(t *testing.T, useMongo bool, seeder Seeder) (repos *repo.Container) {
func initRepos(t *testing.T, useMongo bool, seeder Seeder) (repos *repo.Container, file gateway.File) {
ctx := context.Background()

if useMongo {
Expand All @@ -48,18 +59,25 @@ func initRepos(t *testing.T, useMongo bool, seeder Seeder) (repos *repo.Containe
repos = memory.New()
}

file = lo.Must(fs.NewFile(afero.NewMemMapFs(), "https://example.com/"))
fr = &file
if seeder != nil {
if err := seeder(ctx, repos); err != nil {
if err := seeder(ctx, repos, file); err != nil {
t.Fatalf("failed to seed the db: %s", err)
}
}

return repos
return repos, file
}

func initGateway() *gateway.Container {
if fr == nil {
return &gateway.Container{
File: lo.Must(fs.NewFile(afero.NewMemMapFs(), "https://example.com/")),
}
}
return &gateway.Container{
File: lo.Must(fs.NewFile(afero.NewMemMapFs(), "https://example.com")),
File: *fr,
}
}

Expand Down Expand Up @@ -117,96 +135,51 @@ func StartGQLServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Conta
}

func StartGQLServerAndRepos(t *testing.T, seeder Seeder) (*httpexpect.Expect, *accountrepo.Container) {
cfg := &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
}
repos := initRepos(t, true, seeder)
e, _, _ := StartGQLServerWithRepos(t, cfg, repos)
repos, _ := initRepos(t, true, seeder)
e, _, _ := StartGQLServerWithRepos(t, disabledAuthConfig, repos)
return e, repos.AccountRepos()
}

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 startServer(t *testing.T, cfg *config.Config, useMongo bool, seeder Seeder) (*httpexpect.Expect, *repo.Container, *gateway.Container) {
repos, _ := initRepos(t, useMongo, seeder)
e, gateways, _ := StartGQLServerWithRepos(t, cfg, repos)
return e, repos, gateways
}

func StartServerWithRepos(t *testing.T, cfg *config.Config, repos *repo.Container) (*httpexpect.Expect, *gateway.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, gateways := initServer(cfg, repos, ctx)

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.Default(t, "http://"+l.Addr().String()), gateways
func ServerAndRepos(t *testing.T, seeder Seeder) (*httpexpect.Expect, *repo.Container, *gateway.Container) {
return startServer(t, disabledAuthConfig, true, seeder)
}

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 Server(t *testing.T, seeder Seeder) *httpexpect.Expect {
e, _, _ := startServer(t, disabledAuthConfig, true, seeder)
return e
}

func StartServer(t *testing.T, cfg *config.Config, useMongo bool, seeder Seeder) *httpexpect.Expect {
e, _, _ := StartServerAndRepos(t, cfg, useMongo, seeder)
func ServerPingTest(t *testing.T) *httpexpect.Expect {
e, _, _ := startServer(t, disabledAuthConfig, false, nil)
return e
}

func Server(t *testing.T, seeder Seeder) *httpexpect.Expect {
func ServerMockTest(t *testing.T) *httpexpect.Expect {
c := &config.Config{
Origins: []string{"https://example.com"},
Dev: true,
MockAuth: true,
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
}
return StartServer(t, c, true, seeder)
e, _, _ := startServer(t, c, true, nil)
return e
}

func ServerLanguage(t *testing.T, lang language.Tag) *httpexpect.Expect {
c := &config.Config{
Origins: []string{"https://example.com"},
AuthSrv: config.AuthSrvConfig{
Disabled: true,
},
}
return StartServer(t, c, true,
func(ctx context.Context, r *repo.Container) error {
return baseSeederWithLang(ctx, r, lang)
e, _, _ := startServer(t, disabledAuthConfig, true,
func(ctx context.Context, r *repo.Container, f gateway.File) error {
return baseSeederWithLang(ctx, r, f, lang)
},
)
return e
}

type GraphQLRequest struct {
Expand Down Expand Up @@ -275,6 +248,12 @@ func RegexpJSONEReadCloser(t *testing.T, actual io.ReadCloser, expected string)
actualBuf := new(bytes.Buffer)
_, err := actualBuf.ReadFrom(actual)
assert.NoError(t, err)
var data map[string]interface{}
err = json.Unmarshal(actualBuf.Bytes(), &data)
assert.NoError(t, err)
if text, err := json.MarshalIndent(data, "", " "); err == nil {
fmt.Println(string(text))
}
return JSONEqRegexp(t, actualBuf.String(), expected)
}

Expand All @@ -284,6 +263,13 @@ func JSONEqRegexpInterface(t *testing.T, actual interface{}, expected string) bo
return JSONEqRegexp(t, string(actualBytes), expected)
}

func JSONEqRegexpValue(t *testing.T, actual *httpexpect.Value, expected string) bool {
if actualData, ok := actual.Raw().(map[string]interface{}); ok {
return JSONEqRegexpInterface(t, actualData, expected)
}
return false
}

func aligningJSON(t *testing.T, str string) string {
// Unmarshal and Marshal to make the JSON format consistent
var obj interface{}
Expand All @@ -295,9 +281,17 @@ func aligningJSON(t *testing.T, str string) string {
}

func ValueDump(val *httpexpect.Value) {
if data, ok := val.Raw().(map[string]interface{}); ok {
raw := val.Raw()
switch data := raw.(type) {
case map[string]interface{}:
if text, err := json.MarshalIndent(data, "", " "); err == nil {
fmt.Println(string(text))
}
case []interface{}:
if text, err := json.MarshalIndent(data, "", " "); err == nil {
fmt.Println(string(text))
}
default:
fmt.Println("Unsupported type:", reflect.TypeOf(raw))
}
}
Loading
Loading