From 0f72fb5e8f988ee117c8eec92a96e2c88cbd9966 Mon Sep 17 00:00:00 2001 From: tantaka tomokazu Date: Mon, 6 Jan 2025 15:25:44 +0900 Subject: [PATCH] feat(server): modify query response (#1327) Co-authored-by: Tomokazu Tantaka --- server/e2e/common.go | 15 ++- server/e2e/gql_user_test.go | 143 +++----------------- server/e2e/gql_workspace_test.go | 149 +++------------------ server/internal/adapter/gql/loader_user.go | 3 +- 4 files changed, 42 insertions(+), 268 deletions(-) diff --git a/server/e2e/common.go b/server/e2e/common.go index 9d79f8b5c1..bcf2e893f7 100644 --- a/server/e2e/common.go +++ b/server/e2e/common.go @@ -120,13 +120,14 @@ type GraphQLRequest struct { Variables map[string]any `json:"variables"` } -func StartGQLServer(t *testing.T, cfg *config.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 *config.Config, useMongo bool, seeder Seeder) (*httpexpect.Expect, *accountrepo.Container) { - repos := initRepos(t, useMongo, seeder) +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) acRepos := repos.AccountRepos() return StartGQLServerWithRepos(t, cfg, repos, acRepos), acRepos } diff --git a/server/e2e/gql_user_test.go b/server/e2e/gql_user_test.go index 3644cf96aa..aa025ae320 100644 --- a/server/e2e/gql_user_test.go +++ b/server/e2e/gql_user_test.go @@ -2,12 +2,9 @@ package e2e import ( "context" - "encoding/json" "fmt" - "net/http" "testing" - "github.com/reearth/reearth/server/internal/app/config" "github.com/reearth/reearth/server/internal/usecase/repo" "github.com/reearth/reearthx/account/accountdomain" "github.com/reearth/reearthx/account/accountdomain/user" @@ -95,50 +92,13 @@ func baseSeederUser(ctx context.Context, r *repo.Container) error { return nil } -// func TestSignUp(t *testing.T) { -// e, _ := StartGQLServer(t, &config.Config{ -// Origins: []string{"https://example.com"}, -// AuthSrv: config.AuthSrvConfig{ -// Disabled: true, -// }, -// }, true, baseSeederUser) -// query := `mutation { signup(input: {lang: "ja",theme: DEFAULT,secret: "Ajsownndww1"}){ user{ id name email } }}` -// request := GraphQLRequest{ -// Query: query, -// } -// jsonData, err := json.Marshal(request) -// if err != nil { -// assert.NoError(t, err) -// } -// o := e.POST("/api/graphql"). -// WithHeader("authorization", "Bearer test"). -// WithHeader("Content-Type", "application/json"). -// WithHeader("X-Reearth-Debug-User", uId1.String()). -// WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object().Value("data").Object().Value("signup").Object().Value("user").Object() -// o.Value("name").String().Equal("updated") -// o.Value("email").String().Equal("hoge@test.com") -// } - func TestUpdateMe(t *testing.T) { - e, _ := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, _ := StartGQLServerAndRepos(t, baseSeederUser) query := `mutation { updateMe(input: {name: "updated",email:"hoge@test.com",lang: "ja",theme: DEFAULT,password: "Ajsownndww1",passwordConfirmation: "Ajsownndww1"}){ me{ id name email lang theme } }}` request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.NoError(t, err) - } - o := e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object().Value("data").Object().Value("updateMe").Object().Value("me").Object() + o := Request(e, uId1.String(), request).Object().Value("data").Object().Value("updateMe").Object().Value("me").Object() o.Value("name").String().Equal("updated") o.Value("email").String().Equal("hoge@test.com") o.Value("lang").String().Equal("ja") @@ -146,12 +106,7 @@ func TestUpdateMe(t *testing.T) { } func TestRemoveMyAuth(t *testing.T) { - e, r := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, r := StartGQLServerAndRepos(t, baseSeederUser) u, err := r.User.FindByID(context.Background(), uId1) assert.Nil(t, err) assert.Equal(t, &user.Auth{Provider: "reearth", Sub: "reearth|" + uId1.String()}, u.Auths().GetByProvider("reearth")) @@ -160,15 +115,7 @@ func TestRemoveMyAuth(t *testing.T) { request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.NoError(t, err) - } - e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object() + Request(e, uId1.String(), request).Object() u, err = r.User.FindByID(context.Background(), uId1) assert.Nil(t, err) @@ -176,12 +123,7 @@ func TestRemoveMyAuth(t *testing.T) { } func TestDeleteMe(t *testing.T) { - e, r := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, r := StartGQLServerAndRepos(t, baseSeederUser) u, err := r.User.FindByID(context.Background(), uId1) assert.Nil(t, err) assert.NotNil(t, u) @@ -190,40 +132,19 @@ func TestDeleteMe(t *testing.T) { request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.NoError(t, err) - } - e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object() + Request(e, uId1.String(), request).Object() _, err = r.User.FindByID(context.Background(), uId1) assert.Equal(t, rerror.ErrNotFound, err) } func TestSearchUser(t *testing.T) { - e, _ := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, _ := StartGQLServerAndRepos(t, baseSeederUser) query := fmt.Sprintf(` { searchUser(nameOrEmail: "%s"){ id name email } }`, "e2e") request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.NoError(t, err) - } - o := e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object().Value("data").Object().Value("searchUser").Object() + o := Request(e, uId1.String(), request).Object().Value("data").Object().Value("searchUser").Object() o.Value("id").String().Equal(uId1.String()) o.Value("name").String().Equal("e2e") o.Value("email").String().Equal("e2e@e2e.com") @@ -232,60 +153,28 @@ func TestSearchUser(t *testing.T) { request = GraphQLRequest{ Query: query, } - jsonData, err = json.Marshal(request) - if err != nil { - assert.NoError(t, err) - } - e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object(). - Value("data").Object().Value("searchUser").Null() + resp := Request(e, uId1.String(), request).Object() + resp.Value("data").Object().Value("searchUser").Null() + + resp.NotContainsKey("errors") // not exist } func TestNode(t *testing.T) { - e, _ := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, _ := StartGQLServerAndRepos(t, baseSeederUser) query := fmt.Sprintf(` { node(id: "%s", type: USER){ id } }`, uId1.String()) request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.NoError(t, err) - } - o := e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object().Value("data").Object().Value("node").Object() + o := Request(e, uId1.String(), request).Object().Value("data").Object().Value("node").Object() o.Value("id").String().Equal(uId1.String()) } func TestNodes(t *testing.T) { - e, _ := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, _ := StartGQLServerAndRepos(t, baseSeederUser) query := fmt.Sprintf(` { nodes(id: "%s", type: USER){ id } }`, uId1.String()) request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.NoError(t, err) - } - o := e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object().Value("data").Object().Value("nodes") + o := Request(e, uId1.String(), request).Object().Value("data").Object().Value("nodes") o.Array().Contains(map[string]string{"id": uId1.String()}) } diff --git a/server/e2e/gql_workspace_test.go b/server/e2e/gql_workspace_test.go index ff16d3b887..4e860fd55c 100644 --- a/server/e2e/gql_workspace_test.go +++ b/server/e2e/gql_workspace_test.go @@ -2,12 +2,9 @@ package e2e import ( "context" - "encoding/json" "fmt" - "net/http" "testing" - "github.com/reearth/reearth/server/internal/app/config" "github.com/reearth/reearthx/account/accountdomain" "github.com/reearth/reearthx/account/accountdomain/workspace" "github.com/reearth/reearthx/rerror" @@ -15,49 +12,24 @@ import ( ) func TestCreateTeam(t *testing.T) { - e, _ := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, _ := StartGQLServerAndRepos(t, baseSeederUser) query := `mutation { createTeam(input: {name: "test"}){ team{ id name } }}` request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.NoError(t, err) - } - o := e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object() + o := Request(e, uId1.String(), request).Object() o.Value("data").Object().Value("createTeam").Object().Value("team").Object().Value("name").String().Equal("test") } func TestDeleteTeam(t *testing.T) { - e, r := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, r := StartGQLServerAndRepos(t, baseSeederUser) _, err := r.Workspace.FindByID(context.Background(), wId1) assert.Nil(t, err) query := fmt.Sprintf(`mutation { deleteTeam(input: {teamId: "%s"}){ teamId }}`, wId1) request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - assert.Nil(t, err) - - o := e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object() + o := Request(e, uId1.String(), request).Object() o.Value("data").Object().Value("deleteTeam").Object().Value("teamId").String().Equal(wId1.String()) _, err = r.Workspace.FindByID(context.Background(), wId1) @@ -67,25 +39,13 @@ func TestDeleteTeam(t *testing.T) { request = GraphQLRequest{ Query: query, } - jsonData, err = json.Marshal(request) - assert.Nil(t, err) - - o = e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object() + o = Request(e, uId1.String(), request).Object() o.Value("errors").Array().First().Object().Value("message").Equal("input: deleteTeam operation denied") } func TestUpdateTeam(t *testing.T) { - e, r := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, r := StartGQLServerAndRepos(t, baseSeederUser) w, err := r.Workspace.FindByID(context.Background(), wId1) assert.Nil(t, err) @@ -95,15 +55,7 @@ func TestUpdateTeam(t *testing.T) { request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.Nil(t, err) - } - o := e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object() + o := Request(e, uId1.String(), request).Object() o.Value("data").Object().Value("updateTeam").Object().Value("team").Object().Value("name").String().Equal("updated") w, err = r.Workspace.FindByID(context.Background(), wId1) @@ -114,25 +66,12 @@ func TestUpdateTeam(t *testing.T) { request = GraphQLRequest{ Query: query, } - jsonData, err = json.Marshal(request) - if err != nil { - assert.Nil(t, err) - } - o = e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object() + o = Request(e, uId1.String(), request).Object() o.Value("errors").Array().First().Object().Value("message").Equal("input: updateTeam not found") } func TestAddMemberToTeam(t *testing.T) { - e, r := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, r := StartGQLServerAndRepos(t, baseSeederUser) w, err := r.Workspace.FindByID(context.Background(), wId1) assert.Nil(t, err) @@ -142,15 +81,7 @@ func TestAddMemberToTeam(t *testing.T) { request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.Nil(t, err) - } - e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK) + Request(e, uId1.String(), request) w, err = r.Workspace.FindByID(context.Background(), wId1) assert.Nil(t, err) @@ -161,25 +92,12 @@ func TestAddMemberToTeam(t *testing.T) { request = GraphQLRequest{ Query: query, } - jsonData, err = json.Marshal(request) - if err != nil { - assert.Nil(t, err) - } - e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object(). + Request(e, uId1.String(), request).Object(). Value("errors").Array().First().Object().Value("message").Equal("input: addMemberToTeam user already joined") } func TestRemoveMemberFromTeam(t *testing.T) { - e, r := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, r := StartGQLServerAndRepos(t, baseSeederUser) w, err := r.Workspace.FindByID(context.Background(), wId2) assert.Nil(t, err) @@ -189,35 +107,18 @@ func TestRemoveMemberFromTeam(t *testing.T) { request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.Nil(t, err) - } - e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK) + Request(e, uId1.String(), request) w, err = r.Workspace.FindByID(context.Background(), wId1) assert.Nil(t, err) assert.False(t, w.Members().HasUser(uId3)) - o := e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object() + o := Request(e, uId1.String(), request).Object() o.Value("errors").Array().First().Object().Value("message").Equal("input: removeMemberFromTeam target user does not exist in the workspace") } func TestUpdateMemberOfTeam(t *testing.T) { - e, r := StartGQLServer(t, &config.Config{ - Origins: []string{"https://example.com"}, - AuthSrv: config.AuthSrvConfig{ - Disabled: true, - }, - }, true, baseSeederUser) + e, r := StartGQLServerAndRepos(t, baseSeederUser) w, err := r.Workspace.FindByID(context.Background(), wId2) assert.Nil(t, err) @@ -226,15 +127,7 @@ func TestUpdateMemberOfTeam(t *testing.T) { request := GraphQLRequest{ Query: query, } - jsonData, err := json.Marshal(request) - if err != nil { - assert.Nil(t, err) - } - e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK) + Request(e, uId1.String(), request) w, err = r.Workspace.FindByID(context.Background(), wId2) assert.Nil(t, err) @@ -244,14 +137,6 @@ func TestUpdateMemberOfTeam(t *testing.T) { request = GraphQLRequest{ Query: query, } - jsonData, err = json.Marshal(request) - if err != nil { - assert.Nil(t, err) - } - o := e.POST("/api/graphql"). - WithHeader("authorization", "Bearer test"). - WithHeader("Content-Type", "application/json"). - WithHeader("X-Reearth-Debug-User", uId1.String()). - WithBytes(jsonData).Expect().Status(http.StatusOK).JSON().Object() + o := Request(e, uId1.String(), request).Object() o.Value("errors").Array().First().Object().Value("message").Equal("input: updateMemberOfTeam operation denied") } diff --git a/server/internal/adapter/gql/loader_user.go b/server/internal/adapter/gql/loader_user.go index f770c175c6..4dea0df810 100644 --- a/server/internal/adapter/gql/loader_user.go +++ b/server/internal/adapter/gql/loader_user.go @@ -2,7 +2,6 @@ package gql import ( "context" - "errors" "github.com/reearth/reearth/server/internal/adapter/gql/gqldataloader" "github.com/reearth/reearth/server/internal/adapter/gql/gqlmodel" @@ -50,7 +49,7 @@ func (c *UserLoader) SearchUser(ctx context.Context, nameOrEmail string) (*gqlmo } } - return nil, errors.New("user not found.") + return nil, nil } // data loader