Skip to content

Commit

Permalink
feat(server): geoJson/json import via integration api (#1231)
Browse files Browse the repository at this point in the history
* imp

* imp

* wip: add e2e tests to geojson import

* refactor e2e

* fix geoJson handling

* fix

* fix lint

* add i18n

---------

Co-authored-by: nourbalaha <[email protected]>
Co-authored-by: mimoham24 <[email protected]>
  • Loading branch information
3 people committed Oct 1, 2024
1 parent 7a89ce6 commit a694312
Show file tree
Hide file tree
Showing 30 changed files with 1,257 additions and 290 deletions.
89 changes: 8 additions & 81 deletions server/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,7 @@ func init() {
mongotest.Env = "REEARTH_CMS_DB"
}

func StartServer(t *testing.T, cfg *app.Config, useMongo bool, seeder Seeder) *httpexpect.Expect {
e, _ := StartServerAndRepos(t, cfg, useMongo, seeder)
return e
}

func StartServerAndRepos(t *testing.T, cfg *app.Config, useMongo bool, seeder Seeder) (*httpexpect.Expect, *repo.Container) {
ctx := context.Background()

var repos *repo.Container
var accountRepos *accountrepo.Container
if useMongo {
db := mongotest.Connect(t)(t)
log.Infof("test: new db created with name: %v", db.Name())
accountRepos = lo.Must(accountmongo.New(ctx, db.Client(), db.Name(), false, false, nil))
repos = lo.Must(mongo.NewWithDB(ctx, db, false, accountRepos))
} else {
repos = memory.New()
accountRepos = accountmemory.New()
}

if seeder != nil {
if err := seeder(ctx, repos); err != nil {
t.Fatalf("failed to seed the db: %s", err)
}
}

return StartServerWithRepos(t, cfg, repos, accountRepos), repos
}
func StartServerWithRepos(t *testing.T, cfg *app.Config, repos *repo.Container, accountrepos *accountrepo.Container) *httpexpect.Expect {
func startServer(t *testing.T, cfg *app.Config, repos *repo.Container, accountrepos *accountrepo.Container) *httpexpect.Expect {
t.Helper()

if testing.Short() {
Expand All @@ -83,6 +55,7 @@ func StartServerWithRepos(t *testing.T, cfg *app.Config, repos *repo.Container,
AcGateways: &accountgateway.Container{
Mailer: mailer.New(ctx, &mailer.Config{}),
},
Debug: true,
})

ch := make(chan error)
Expand All @@ -106,22 +79,16 @@ func StartServerWithRepos(t *testing.T, cfg *app.Config, repos *repo.Container,
return httpexpect.Default(t, "http://"+l.Addr().String())
}

func StartGQLServer(t *testing.T, cfg *app.Config, useMongo bool, seeder Seeder) (*httpexpect.Expect, *accountrepo.Container) {
e, r := StartGQLServerAndRepos(t, cfg, useMongo, seeder)
return e, r
}

func StartGQLServerAndRepos(t *testing.T, cfg *app.Config, useMongo bool, seeder Seeder) (*httpexpect.Expect, *accountrepo.Container) {
func StartServerWithRepos(t *testing.T, cfg *app.Config, useMongo bool, seeder Seeder) (*httpexpect.Expect, *repo.Container, *accountrepo.Container) {
ctx := context.Background()

var repos *repo.Container
var accountRepos *accountrepo.Container

if useMongo {
db := mongotest.Connect(t)(t)
log.Infof("test: new db created with name: %v", db.Name())
accountRepos = lo.Must(accountmongo.New(ctx, db.Client(), db.Name(), false, false, nil))
repos = lo.Must(mongo.New(ctx, db.Client(), db.Name(), false, accountRepos))
repos = lo.Must(mongo.NewWithDB(ctx, db, false, accountRepos))
} else {
repos = memory.New()
accountRepos = accountmemory.New()
Expand All @@ -133,52 +100,12 @@ func StartGQLServerAndRepos(t *testing.T, cfg *app.Config, useMongo bool, seeder
}
}

return StartGQLServerWithRepos(t, cfg, repos, accountRepos), accountRepos
return startServer(t, cfg, repos, accountRepos), repos, accountRepos
}

func StartGQLServerWithRepos(t *testing.T, cfg *app.Config, repos *repo.Container, accountrepos *accountrepo.Container) *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)
}

srv := app.NewServer(ctx, &app.ServerConfig{
Config: cfg,
Repos: repos,
AcRepos: accountrepos,
Gateways: &gateway.Container{
File: lo.Must(fs.NewFile(afero.NewMemMapFs(), "https://example.com")),
},
AcGateways: &accountgateway.Container{
Mailer: mailer.New(ctx, &mailer.Config{}),
},
Debug: true,
})

ch := make(chan error)
go func() {
if err := srv.Serve(l); !errors.Is(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())
func StartServer(t *testing.T, cfg *app.Config, useMongo bool, seeder Seeder) *httpexpect.Expect {
e, _, _ := StartServerWithRepos(t, cfg, useMongo, seeder)
return e
}

type GraphQLRequest struct {
Expand Down
4 changes: 2 additions & 2 deletions server/e2e/gql_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func createMetaFieldOfEachType(t *testing.T, e *httpexpect.Expect, mId string) m
}

func TestCreateField(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-1")

Expand Down Expand Up @@ -437,7 +437,7 @@ func TestCreateField(t *testing.T) {
}

func TestClearFieldDefaultValue(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-1")

Expand Down
6 changes: 3 additions & 3 deletions server/e2e/gql_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func createGroup(e *httpexpect.Expect, pID, name, desc, key string) (string, *ht
}

func TestCreateGroup(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-1")

Expand Down Expand Up @@ -92,7 +92,7 @@ func updateGroupsOrder(e *httpexpect.Expect, ids []string) *httpexpect.Value {
}

func TestUpdateGroupsOrder(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-2")

Expand All @@ -103,4 +103,4 @@ func TestUpdateGroupsOrder(t *testing.T) {
res := updateGroupsOrder(e, []string{mId4, mId1, mId2, mId3})
res.Path("$.data.updateGroupsOrder.groups[:].id").Array().IsEqual([]string{mId4, mId1, mId2, mId3})
res.Path("$.data.updateGroupsOrder.groups[:].order").Array().IsEqual([]int{0, 1, 2, 3})
}
}
10 changes: 5 additions & 5 deletions server/e2e/gql_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func deleteItem(e *httpexpect.Expect, iID string) (string, *httpexpect.Value) {
}

func TestCreateItem(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-1")

Expand All @@ -381,7 +381,7 @@ func TestCreateItem(t *testing.T) {
}

func TestClearItemValues(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-1")

Expand Down Expand Up @@ -458,7 +458,7 @@ func TestClearItemValues(t *testing.T) {
}

func TestOneWayReferenceFields(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-1")

Expand Down Expand Up @@ -511,7 +511,7 @@ func TestOneWayReferenceFields(t *testing.T) {
}

func TestTwoWayReferenceFields(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-1")

Expand Down Expand Up @@ -643,7 +643,7 @@ func TestTwoWayReferenceFields(t *testing.T) {
}

func TestSearchItem(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

// region init
pId, _ := createProject(e, wId.String(), "test", "test", "test-1")
Expand Down
6 changes: 3 additions & 3 deletions server/e2e/gql_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func getModel(e *httpexpect.Expect, mID string) (string, string, *httpexpect.Val
}

func TestCreateModel(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-1")

Expand All @@ -331,7 +331,7 @@ func TestCreateModel(t *testing.T) {

}
func TestUpdateModel(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-2")

Expand All @@ -347,7 +347,7 @@ func TestUpdateModel(t *testing.T) {
}

func TestUpdateModelsOrder(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-2")

Expand Down
4 changes: 2 additions & 2 deletions server/e2e/gql_model_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func getViews(e *httpexpect.Expect, mID string) *httpexpect.Value {
}

func TestViewCRUD(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

pId, _ := createProject(e, wId.String(), "test", "test", "test-1")

Expand Down Expand Up @@ -347,7 +347,7 @@ func updateViewsOrder(e *httpexpect.Expect, ids []string) *httpexpect.Value {
}

func TestUpdateViewsOrder(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)
pId, _ := createProject(e, wId.String(), "test", "test", "test-2")
mId, _ := createModel(e, pId, "test", "test", "test-2")
vId1, _ := createView(e, pId, mId, "test1", nil, nil, nil)
Expand Down
2 changes: 1 addition & 1 deletion server/e2e/gql_porject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func createProject(e *httpexpect.Expect, wID, name, desc, alias string) (string,
}

func TestCreateProject(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)

_, res := createProject(e, wId.String(), "test", "test", "test-1")

Expand Down
22 changes: 11 additions & 11 deletions server/e2e/gql_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func baseSeederUser(ctx context.Context, r *repo.Container) error {
}

func TestUpdateMe(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)
query := `mutation { updateMe(input: {name: "updated",email:"[email protected]",lang: "ja",theme: DEFAULT,password: "Ajsownndww1",passwordConfirmation: "Ajsownndww1"}){ me{ id name email lang theme } }}`
request := GraphQLRequest{
Query: query,
Expand All @@ -109,9 +109,9 @@ func TestUpdateMe(t *testing.T) {
}

func TestRemoveMyAuth(t *testing.T) {
e, r := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e, _, ar := StartServerWithRepos(t, &app.Config{}, true, baseSeederUser)

u, err := r.User.FindByID(context.Background(), uId1)
u, err := ar.User.FindByID(context.Background(), uId1)
assert.Nil(t, err)
assert.Equal(t, &user.Auth{Provider: "reearth", Sub: "reearth|" + uId1.String()}, u.Auths().GetByProvider("reearth"))

Expand All @@ -129,15 +129,15 @@ func TestRemoveMyAuth(t *testing.T) {
WithHeader("X-Reearth-Debug-User", uId1.String()).
WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object()

u, err = r.User.FindByID(context.Background(), uId1)
u, err = ar.User.FindByID(context.Background(), uId1)
assert.Nil(t, err)
assert.Nil(t, u.Auths().Get("sub"))
}

func TestDeleteMe(t *testing.T) {
e, r := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e, _, ar := StartServerWithRepos(t, &app.Config{}, true, baseSeederUser)

u, err := r.User.FindByID(context.Background(), uId1)
u, err := ar.User.FindByID(context.Background(), uId1)
assert.Nil(t, err)
assert.NotNil(t, u)

Expand All @@ -155,12 +155,12 @@ func TestDeleteMe(t *testing.T) {
WithHeader("X-Reearth-Debug-User", uId1.String()).
WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object()

_, err = r.User.FindByID(context.Background(), uId1)
_, err = ar.User.FindByID(context.Background(), uId1)
assert.Equal(t, rerror.ErrNotFound, err)
}

func TestMe(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)
query := ` { me{ id name email lang theme myWorkspaceId } }`
request := GraphQLRequest{
Query: query,
Expand Down Expand Up @@ -195,7 +195,7 @@ func TestMe(t *testing.T) {
}

func TestSearchUser(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)
query := fmt.Sprintf(` { searchUser(nameOrEmail: "%s"){ id name email } }`, "e2e")
request := GraphQLRequest{
Query: query,
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestSearchUser(t *testing.T) {
}

func TestNode(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)
query := fmt.Sprintf(` { node(id: "%s", type: USER){ id } }`, uId1.String())
request := GraphQLRequest{
Query: query,
Expand All @@ -248,7 +248,7 @@ func TestNode(t *testing.T) {
}

func TestNodes(t *testing.T) {
e, _ := StartGQLServer(t, &app.Config{}, true, baseSeederUser)
e := StartServer(t, &app.Config{}, true, baseSeederUser)
query := fmt.Sprintf(` { nodes(id: "%s", type: USER){ id } }`, uId1.String())
request := GraphQLRequest{
Query: query,
Expand Down
Loading

0 comments on commit a694312

Please sign in to comment.