|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/gavv/httpexpect/v2" |
| 9 | + "github.com/reearth/reearth/server/internal/app/config" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGetAssets(t *testing.T) { |
| 13 | + c := &config.Config{ |
| 14 | + Origins: []string{"https://example.com"}, |
| 15 | + AuthSrv: config.AuthSrvConfig{ |
| 16 | + Disabled: true, |
| 17 | + }, |
| 18 | + } |
| 19 | + e := StartServer(t, c, true, baseSeeder) |
| 20 | + |
| 21 | + teamId := wID.String() |
| 22 | + |
| 23 | + res := createAsset(t, e, "test.png", true, teamId) |
| 24 | + res.Object().Value("data").Object().Value("createAsset").Object().Value("asset").Object(). |
| 25 | + ValueEqual("teamId", teamId). |
| 26 | + ValueEqual("name", "test.png"). |
| 27 | + ValueEqual("coreSupport", true) |
| 28 | + |
| 29 | + res = createAsset(t, e, "test.png", false, teamId) |
| 30 | + res.Object().Value("data").Object().Value("createAsset").Object().Value("asset").Object(). |
| 31 | + ValueEqual("teamId", teamId). |
| 32 | + ValueEqual("name", "test.png"). |
| 33 | + ValueEqual("coreSupport", false) |
| 34 | + |
| 35 | + res = createAsset(t, e, "test.csv", true, teamId) |
| 36 | + res.Object().Value("data").Object().Value("createAsset").Object().Value("asset").Object(). |
| 37 | + ValueEqual("teamId", teamId). |
| 38 | + ValueEqual("name", "test.csv"). |
| 39 | + ValueEqual("coreSupport", true) |
| 40 | + |
| 41 | + res = createAsset(t, e, "test.csv", false, teamId) |
| 42 | + res.Object().Value("data").Object().Value("createAsset").Object().Value("asset").Object(). |
| 43 | + ValueEqual("teamId", teamId). |
| 44 | + ValueEqual("name", "test.csv"). |
| 45 | + ValueEqual("coreSupport", false) |
| 46 | + |
| 47 | + res = getAssets(e, teamId) |
| 48 | + assets := res.Object().Value("data").Object().Value("assets").Object().Value("nodes").Array().Iter() |
| 49 | + for _, a := range assets { |
| 50 | + a.Object().ValueEqual("coreSupport", true) // coreSupport true only |
| 51 | + } |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +const CreateAssetMutation = `mutation CreateAsset($teamId: ID!, $file: Upload!, $coreSupport: Boolean!) { |
| 56 | + createAsset(input: {teamId: $teamId, file: $file, coreSupport: $coreSupport}) { |
| 57 | + asset { |
| 58 | + id |
| 59 | + teamId |
| 60 | + name |
| 61 | + size |
| 62 | + url |
| 63 | + contentType |
| 64 | + coreSupport |
| 65 | + __typename |
| 66 | + } |
| 67 | + __typename |
| 68 | + } |
| 69 | +}` |
| 70 | + |
| 71 | +func createAsset(t *testing.T, e *httpexpect.Expect, filePath string, coreSupport bool, teamId string) *httpexpect.Value { |
| 72 | + file, err := os.Open(filePath) |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("failed to open file: %v", err) |
| 75 | + } |
| 76 | + defer func() { |
| 77 | + if cerr := file.Close(); cerr != nil && err == nil { |
| 78 | + err = cerr |
| 79 | + } |
| 80 | + }() |
| 81 | + requestBody := map[string]interface{}{ |
| 82 | + "operationName": "CreateAsset", |
| 83 | + "variables": map[string]interface{}{ |
| 84 | + "teamId": teamId, |
| 85 | + "coreSupport": coreSupport, |
| 86 | + "file": nil, |
| 87 | + }, |
| 88 | + "query": CreateAssetMutation, |
| 89 | + } |
| 90 | + return e.POST("/api/graphql"). |
| 91 | + WithHeader("Origin", "https://example.com"). |
| 92 | + WithHeader("authorization", "Bearer test"). |
| 93 | + WithHeader("X-Reearth-Debug-User", uID.String()). |
| 94 | + WithMultipart(). |
| 95 | + WithFormField("operations", toJSONString(requestBody)). |
| 96 | + WithFormField("map", `{"0": ["variables.file"]}`). |
| 97 | + WithFile("0", filePath). |
| 98 | + Expect(). |
| 99 | + Status(http.StatusOK). |
| 100 | + JSON() |
| 101 | +} |
| 102 | + |
| 103 | +const GetAssetsQuery = `query GetAssets($teamId: ID!, $pagination: Pagination, $keyword: String, $sort: AssetSort) { |
| 104 | + assets(teamId: $teamId, pagination: $pagination, keyword: $keyword, sort: $sort) { |
| 105 | + edges { |
| 106 | + cursor |
| 107 | + node { |
| 108 | + id |
| 109 | + teamId |
| 110 | + name |
| 111 | + size |
| 112 | + url |
| 113 | + createdAt |
| 114 | + contentType |
| 115 | + coreSupport |
| 116 | + __typename |
| 117 | + } |
| 118 | + __typename |
| 119 | + } |
| 120 | + nodes { |
| 121 | + id |
| 122 | + teamId |
| 123 | + name |
| 124 | + size |
| 125 | + url |
| 126 | + createdAt |
| 127 | + contentType |
| 128 | + coreSupport |
| 129 | + __typename |
| 130 | + } |
| 131 | + pageInfo { |
| 132 | + endCursor |
| 133 | + hasNextPage |
| 134 | + hasPreviousPage |
| 135 | + startCursor |
| 136 | + __typename |
| 137 | + } |
| 138 | + totalCount |
| 139 | + __typename |
| 140 | + } |
| 141 | + }` |
| 142 | + |
| 143 | +func getAssets(e *httpexpect.Expect, teamId string) *httpexpect.Value { |
| 144 | + requestBody := GraphQLRequest{ |
| 145 | + OperationName: "GetAssets", |
| 146 | + Query: GetAssetsQuery, |
| 147 | + Variables: map[string]any{ |
| 148 | + "teamId": teamId, |
| 149 | + "pagination": map[string]any{ |
| 150 | + "first": 20, |
| 151 | + }, |
| 152 | + "sort": map[string]string{ |
| 153 | + "direction": "DESC", |
| 154 | + "field": "DATE", |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | + return e.POST("/api/graphql"). |
| 159 | + WithHeader("Origin", "https://example.com"). |
| 160 | + WithHeader("X-Reearth-Debug-User", uID.String()). |
| 161 | + WithHeader("Content-Type", "application/json"). |
| 162 | + WithJSON(requestBody). |
| 163 | + Expect(). |
| 164 | + Status(http.StatusOK). |
| 165 | + JSON() |
| 166 | +} |
0 commit comments