Skip to content

Commit

Permalink
feat(server): support field management in integration api (#1181)
Browse files Browse the repository at this point in the history
imp

Co-authored-by: mimoham24 <69579255+mimoham24@users.noreply.github.com>
  • Loading branch information
yk-eukarya and mimoham24 authored Jun 21, 2024
1 parent a61e3bb commit c5c4b7f
Show file tree
Hide file tree
Showing 11 changed files with 3,165 additions and 87 deletions.
307 changes: 284 additions & 23 deletions server/e2e/integration_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,150 @@ import (
"github.com/reearth/reearth-cms/server/pkg/id"
)

// GET /models/{modelId}
func TestIntegrationModelGetAPI(t *testing.T) {
e := StartServer(t, &app.Config{}, true, baseSeeder)

e.GET("/api/models/{modelId}", id.NewModelID()).
Expect().
Status(http.StatusUnauthorized)

e.GET("/api/models/{modelId}", id.NewModelID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.GET("/api/models/{modelId}", id.NewModelID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.GET("/api/models/{modelId}", id.NewModelID()).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)

// key cannot be used
e.GET("/api/models/{modelId}", ikey1).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusBadRequest)

obj := e.GET("/api/models/{modelId}", mId1).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusOK).
JSON().
Object().
HasValue("id", mId1.String()).
HasValue("name", "m1").
HasValue("description", "m1 desc").
HasValue("public", true).
HasValue("key", ikey1.String()).
HasValue("projectId", pid).
HasValue("schemaId", sid1)

obj.Value("createdAt").NotNull()
obj.Value("updatedAt").NotNull()
obj.Value("lastModified").NotNull()
}

// PATCH /models/{modelId}
func TestIntegrationModelUpdateAPI(t *testing.T) {
endpoint := "/api/models/{modelId}"
e := StartServer(t, &app.Config{}, true, baseSeeder)

e.PATCH(endpoint, id.NewProjectID()).
Expect().
Status(http.StatusUnauthorized)

e.PATCH(endpoint, id.NewProjectID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.PATCH(endpoint, id.NewProjectID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

obj := e.PATCH(endpoint, mId1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"name": "newM1 updated",
"description": "newM1 desc updated",
"key": "newM1KeyUpdated",
}).
Expect().
Status(http.StatusOK).
JSON().
Object()
obj.
ContainsKey("id").
ContainsKey("schemaId").
HasValue("projectId", pid).
HasValue("name", "newM1 updated").
HasValue("description", "newM1 desc updated").
HasValue("key", "newM1KeyUpdated")

obj = e.GET("/api/models/{modelId}", mId1).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusOK).
JSON().
Object().
HasValue("id", mId1).
HasValue("name", "newM1 updated").
HasValue("description", "newM1 desc updated").
HasValue("public", true).
HasValue("key", "newM1KeyUpdated").
HasValue("projectId", pid)

obj.Value("createdAt").NotNull()
obj.Value("updatedAt").NotNull()
//obj.Value("lastModified").NotNull()
obj.Value("schemaId").NotNull()
}

// DELETE /models/{modelId}
func TestIntegrationModelDeleteAPI(t *testing.T) {
endpoint := "/api/models/{modelId}"
e := StartServer(t, &app.Config{}, true, baseSeeder)

e.DELETE(endpoint, id.NewProjectID()).
Expect().
Status(http.StatusUnauthorized)

e.DELETE(endpoint, id.NewProjectID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.DELETE(endpoint, id.NewProjectID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.DELETE(endpoint, mId1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"name": "newM1 updated",
"description": "newM1 desc updated",
"key": "newM1KeyUpdated",
}).
Expect().
Status(http.StatusOK).
JSON().
Object().
HasValue("id", mId1)

e.GET("/api/models/{modelId}", mId1).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)

}

// GET /projects/{projectIdOrKey}/models
func TestIntegrationModelFilterAPI(t *testing.T) {
endpoint := "/api/projects/{projectIdOrKey}/models"
Expand Down Expand Up @@ -90,54 +234,62 @@ func TestIntegrationModelFilterAPI(t *testing.T) {
Expect())
}

// GET /models/{modelId}
func TestIntegrationModelGetAPI(t *testing.T) {
// POST /projects/{projectIdOrKey}/models
func TestIntegrationModelCreateAPI(t *testing.T) {
endpoint := "/api/projects/{projectIdOrKey}/models"
e := StartServer(t, &app.Config{}, true, baseSeeder)

e.GET("/api/models/{modelId}", id.NewModelID()).
e.POST(endpoint, id.NewProjectID()).
Expect().
Status(http.StatusUnauthorized)

e.GET("/api/models/{modelId}", id.NewModelID()).
e.POST(endpoint, id.NewProjectID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.GET("/api/models/{modelId}", id.NewModelID()).
e.POST(endpoint, id.NewProjectID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.GET("/api/models/{modelId}", id.NewModelID()).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)

// key cannot be used
e.GET("/api/models/{modelId}", ikey1).
obj := e.POST(endpoint, pid.String()).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"name": "newM1",
"description": "newM1 desc",
"key": "newM1Key",
}).
Expect().
Status(http.StatusBadRequest)
Status(http.StatusOK).
JSON().
Object()
obj.
ContainsKey("id").
ContainsKey("schemaId").
HasValue("projectId", pid).
HasValue("name", "newM1").
HasValue("description", "newM1 desc").
HasValue("key", "newM1Key")

obj := e.GET("/api/models/{modelId}", mId1).
mId := obj.Value("id").String().Raw()
obj = e.GET("/api/models/{modelId}", mId).
WithHeader("authorization", "Bearer "+secret).
WithQuery("page", 1).
WithQuery("perPage", 5).
Expect().
Status(http.StatusOK).
JSON().
Object().
HasValue("id", mId1.String()).
HasValue("name", "m1").
HasValue("description", "m1 desc").
HasValue("id", mId).
HasValue("name", "newM1").
HasValue("description", "newM1 desc").
HasValue("public", true).
HasValue("key", ikey1.String()).
HasValue("projectId", pid).
HasValue("schemaId", sid1)
HasValue("key", "newM1Key").
HasValue("projectId", pid)

obj.Value("createdAt").NotNull()
obj.Value("updatedAt").NotNull()
obj.Value("lastModified").NotNull()
//obj.Value("lastModified").NotNull()
obj.Value("schemaId").NotNull()
}

// GET /projects/{projectIdOrAlias}/models/{modelIdOrKey}
Expand Down Expand Up @@ -205,3 +357,112 @@ func TestIntegrationModelGetWithProjectAPI(t *testing.T) {
obj.Value("updatedAt").NotNull()
obj.Value("lastModified").NotNull()
}

// PATCH /projects/{projectIdOrAlias}/models/{modelIdOrKey}
func TestIntegrationModelUpdateWithProjectAPI(t *testing.T) {
endpoint := "/api/projects/{projectIdOrAlias}/models/{modelIdOrKey}"
e := StartServer(t, &app.Config{}, true, baseSeeder)

e.PATCH(endpoint, palias, id.NewModelID()).
Expect().
Status(http.StatusUnauthorized)

e.PATCH(endpoint, palias, id.NewModelID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.PATCH(endpoint, palias, id.NewModelID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.PATCH(endpoint, palias, id.NewModelID()).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)

obj := e.PATCH(endpoint, palias, mId1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"name": "newM1 updated",
"description": "newM1 desc updated",
"key": "newM1KeyUpdated",
}).
Expect().
Status(http.StatusOK).
JSON().
Object().
HasValue("id", mId1.String()).
HasValue("name", "newM1 updated").
HasValue("description", "newM1 desc updated").
HasValue("public", true).
HasValue("key", "newM1KeyUpdated").
HasValue("projectId", pid).
HasValue("schemaId", sid1)

obj.Value("createdAt").NotNull()
obj.Value("updatedAt").NotNull()
obj.Value("lastModified").NotNull()

obj = e.GET("/api/projects/{projectIdOrAlias}/models/{modelIdOrKey}", palias, "newM1KeyUpdated").
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusOK).
JSON().
Object().
HasValue("id", mId1.String()).
HasValue("name", "newM1 updated").
HasValue("description", "newM1 desc updated").
HasValue("public", true).
HasValue("key", "newM1KeyUpdated").
HasValue("projectId", pid).
HasValue("schemaId", sid1)

obj.Value("createdAt").NotNull()
obj.Value("updatedAt").NotNull()
obj.Value("lastModified").NotNull()
}

// DELETE /projects/{projectIdOrAlias}/models/{modelIdOrKey}
func TestIntegrationModelDeleteWithProjectAPI(t *testing.T) {
endpoint := "/api/projects/{projectIdOrAlias}/models/{modelIdOrKey}"
e := StartServer(t, &app.Config{}, true, baseSeeder)

e.DELETE(endpoint, palias, id.NewModelID()).
Expect().
Status(http.StatusUnauthorized)

e.DELETE(endpoint, palias, id.NewModelID()).
WithHeader("authorization", "secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.DELETE(endpoint, palias, id.NewModelID()).
WithHeader("authorization", "Bearer secret_abc").
Expect().
Status(http.StatusUnauthorized)

e.DELETE(endpoint, palias, id.NewModelID()).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)

e.DELETE(endpoint, palias, mId1).
WithHeader("authorization", "Bearer "+secret).
WithJSON(map[string]interface{}{
"name": "newM1 updated",
"description": "newM1 desc updated",
"key": "newM1KeyUpdated",
}).
Expect().
Status(http.StatusOK).
JSON().
Object().
HasValue("id", mId1.String())

e.GET("/api/projects/{projectIdOrAlias}/models/{modelIdOrKey}", palias, mId1).
WithHeader("authorization", "Bearer "+secret).
Expect().
Status(http.StatusNotFound)
}
Loading

0 comments on commit c5c4b7f

Please sign in to comment.