Skip to content

Commit

Permalink
feat(server): modify query response (#1327)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomokazu Tantaka <[email protected]>
  • Loading branch information
hexaforce and Tomokazu Tantaka authored Jan 6, 2025
1 parent 6edee20 commit 0f72fb5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 268 deletions.
15 changes: 8 additions & 7 deletions server/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
143 changes: 16 additions & 127 deletions server/e2e/gql_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -95,63 +92,21 @@ 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("[email protected]")
// }

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:"[email protected]",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("[email protected]")
o.Value("lang").String().Equal("ja")
o.Value("theme").String().Equal("default")
}

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"))
Expand All @@ -160,28 +115,15 @@ 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)
assert.Nil(t, u.Auths().Get("sub"))
}

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)
Expand All @@ -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("[email protected]")
Expand All @@ -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()})
}
Loading

0 comments on commit 0f72fb5

Please sign in to comment.