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

feat(server): Add coreSupport to Asset #1246

Merged
merged 6 commits into from
Nov 22, 2024
Merged
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
166 changes: 166 additions & 0 deletions server/e2e/gql_asset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package e2e

import (
"net/http"
"os"
"testing"

"github.com/gavv/httpexpect/v2"
"github.com/reearth/reearth/server/internal/app/config"
)

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

hexaforce marked this conversation as resolved.
Show resolved Hide resolved
teamId := wID.String()

res := createAsset(t, e, "test.png", true, teamId)
res.Object().Value("data").Object().Value("createAsset").Object().Value("asset").Object().
ValueEqual("teamId", teamId).
ValueEqual("name", "test.png").
ValueEqual("coreSupport", true)

res = createAsset(t, e, "test.png", false, teamId)
res.Object().Value("data").Object().Value("createAsset").Object().Value("asset").Object().
ValueEqual("teamId", teamId).
ValueEqual("name", "test.png").
ValueEqual("coreSupport", false)

res = createAsset(t, e, "test.csv", true, teamId)
res.Object().Value("data").Object().Value("createAsset").Object().Value("asset").Object().
ValueEqual("teamId", teamId).
ValueEqual("name", "test.csv").
ValueEqual("coreSupport", true)

res = createAsset(t, e, "test.csv", false, teamId)
res.Object().Value("data").Object().Value("createAsset").Object().Value("asset").Object().
ValueEqual("teamId", teamId).
ValueEqual("name", "test.csv").
ValueEqual("coreSupport", false)

res = getAssets(e, teamId)
assets := res.Object().Value("data").Object().Value("assets").Object().Value("nodes").Array().Iter()
for _, a := range assets {
a.Object().ValueEqual("coreSupport", true) // coreSupport true only
}
hexaforce marked this conversation as resolved.
Show resolved Hide resolved

}

const CreateAssetMutation = `mutation CreateAsset($teamId: ID!, $file: Upload!, $coreSupport: Boolean!) {
createAsset(input: {teamId: $teamId, file: $file, coreSupport: $coreSupport}) {
asset {
id
teamId
name
size
url
contentType
coreSupport
__typename
}
__typename
}
}`
hexaforce marked this conversation as resolved.
Show resolved Hide resolved

func createAsset(t *testing.T, e *httpexpect.Expect, filePath string, coreSupport bool, teamId string) *httpexpect.Value {
file, err := os.Open(filePath)
if err != nil {
t.Fatalf("failed to open file: %v", err)
}
defer func() {
if cerr := file.Close(); cerr != nil && err == nil {
err = cerr
}
}()
requestBody := map[string]interface{}{
"operationName": "CreateAsset",
"variables": map[string]interface{}{
"teamId": teamId,
"coreSupport": coreSupport,
"file": nil,
},
"query": CreateAssetMutation,
}
return e.POST("/api/graphql").
WithHeader("Origin", "https://example.com").
WithHeader("authorization", "Bearer test").
WithHeader("X-Reearth-Debug-User", uID.String()).
WithMultipart().
WithFormField("operations", toJSONString(requestBody)).
WithFormField("map", `{"0": ["variables.file"]}`).
WithFile("0", filePath).
Expect().
Status(http.StatusOK).
JSON()
}
hexaforce marked this conversation as resolved.
Show resolved Hide resolved

const GetAssetsQuery = `query GetAssets($teamId: ID!, $pagination: Pagination, $keyword: String, $sort: AssetSort) {
assets(teamId: $teamId, pagination: $pagination, keyword: $keyword, sort: $sort) {
edges {
cursor
node {
id
teamId
name
size
url
createdAt
contentType
coreSupport
__typename
}
__typename
}
nodes {
id
teamId
name
size
url
createdAt
contentType
coreSupport
__typename
}
pageInfo {
endCursor
hasNextPage
hasPreviousPage
startCursor
__typename
}
totalCount
__typename
}
}`

func getAssets(e *httpexpect.Expect, teamId string) *httpexpect.Value {
requestBody := GraphQLRequest{
OperationName: "GetAssets",
Query: GetAssetsQuery,
Variables: map[string]any{
"teamId": teamId,
"pagination": map[string]any{
"first": 20,
},
"sort": map[string]string{
"direction": "DESC",
"field": "DATE",
},
},
}
return e.POST("/api/graphql").
WithHeader("Origin", "https://example.com").
WithHeader("X-Reearth-Debug-User", uID.String()).
WithHeader("Content-Type", "application/json").
WithJSON(requestBody).
Expect().
Status(http.StatusOK).
JSON()
}
hexaforce marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions server/e2e/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title,lat,lng,size,sizex10,height,color,show,data,url
龍安寺,35.03449434,135.7182635,10,100,1000,#3d86fc,TRUE,Oct.1,https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/RyoanJi-Dry_garden.jpg/800px-RyoanJi-Dry_garden.jpg
Binary file added server/e2e/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 10 additions & 3 deletions server/gql/asset.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Asset implements Node {
url: String!
contentType: String!
team: Team
coreSupport: Boolean!
}

enum AssetSortField {
Expand All @@ -19,6 +20,7 @@ enum AssetSortField {

input CreateAssetInput {
teamId: ID!
coreSupport: Boolean!
file: Upload!
}

Expand Down Expand Up @@ -55,11 +57,16 @@ type AssetEdge {
node: Asset
}

extend type Query{
assets(teamId: ID!, pagination: Pagination, keyword: String, sort: AssetSort): AssetConnection!
extend type Query {
assets(
teamId: ID!
pagination: Pagination
keyword: String
sort: AssetSort
): AssetConnection!
hexaforce marked this conversation as resolved.
Show resolved Hide resolved
}

extend type Mutation {
createAsset(input: CreateAssetInput!): CreateAssetPayload
removeAsset(input: RemoveAssetInput!): RemoveAssetPayload
}
}
86 changes: 82 additions & 4 deletions server/internal/adapter/gql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/internal/adapter/gql/gqlmodel/convert_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func ToAsset(a *asset.Asset) *Asset {
Size: a.Size(),
URL: a.URL(),
ContentType: a.ContentType(),
CoreSupport: a.CoreSupport(),
}
}

Expand Down
Loading
Loading