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

enhance(mock): add dashboard responses from server in mock #1118

Merged
merged 6 commits into from
May 6, 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
252 changes: 252 additions & 0 deletions mock/server/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
// SPDX-License-Identifier: Apache-2.0

package server

import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/gin-gonic/gin"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/types"
)

const (
// DashboardResp represents a JSON return for a dashboard.
DashboardResp = `{
"id": "c976470d-34c1-49b2-9a98-1035871c576b",
"name": "my-dashboard",
"created_at": 1714573212,
"created_by": "Octocat",
"updated_at": 1714573212,
"updated_by": "Octocat",
"admins": [
{
"id": 1,
"name": "Octocat",
"active": true
}
],
"repos": [
{
"id": 1,
"name": "Octocat/vela-repo",
"branches": [
"main"
],
"events": [
"push"
]
}
]
}`

// DashCardResp represents a JSON return for a DashCard.
DashCardResp = `{
"dashboard": {
"id": "6e9f84c3-d853-4afb-b56e-99ff200264c0",
"name": "dashboard-1",
"created_at": 1714677999,
"created_by": "Octocat",
"updated_at": 1714678173,
"updated_by": "Octocat",
"admins": [
{
"id": 1,
"name": "Octocat",
"active": true
}
],
"repos": [
{
"id": 2,
"name": "Octocat/test-repo"
},
{
"id": 1,
"name": "Octocat/test-repo-2"
}
]
},
"repos": [
{
"org": "Octocat",
"name": "test-repo",
"counter": 1,
"builds": [
{
"number": 1,
"started": 1714678666,
"finished": 1714678672,
"sender": "Octocat",
"status": "failure",
"event": "deployment",
"branch": "refs/heads/main",
"link": "http://vela/Octocat/test-repo/1"
}
]
},
{
"org": "Octocat",
"name": "test-repo-2"
}
]
}`

// DashCardsResp represents a JSON return for multiple DashCards.
DashCardsResp = `[
{
"dashboard": {
"id": "6e9f84c3-d853-4afb-b56e-99ff200264c0",
"name": "dashboard-1",
"created_at": 1714677999,
"created_by": "Octocat",
"updated_at": 1714678173,
"updated_by": "Octocat",
"admins": [
{
"id": 1,
"name": "Octocat",
"active": true
}
],
"repos": [
{
"id": 2,
"name": "Octocat/test-repo"
},
{
"id": 1,
"name": "Octocat/test-repo-2"
}
]
},
"repos": [
{
"org": "Octocat",
"name": "test-repo",
"counter": 1,
"builds": [
{
"number": 1,
"started": 1714678666,
"finished": 1714678672,
"sender": "Octocat",
"status": "failure",
"event": "deployment",
"branch": "refs/heads/main",
"link": "http://vela/Octocat/test-repo/1"
}
]
},
{
"org": "Octocat",
"name": "test-repo-2"
}
]
},
{
"dashboard": {
"id": "6e9f84c3-d853-4afb-b56e-99ff200264c1",
"name": "dashboard-2",
"created_at": 1714677999,
"created_by": "Octocat",
"updated_at": 1714678173,
"updated_by": "Octocat",
"admins": [
{
"id": 1,
"name": "Octocat",
"active": true
}
],
"repos": [
{
"id": 2,
"name": "Octocat/test-repo"
},
{
"id": 1,
"name": "Octocat/test-repo-2"
}
]
},
"repos": [
{
"org": "Octocat",
"name": "test-repo",
"counter": 1,
"builds": [
{
"number": 1,
"started": 1714678666,
"finished": 1714678672,
"sender": "Octocat",
"status": "failure",
"event": "deployment",
"branch": "refs/heads/main",
"link": "http://vela/Octocat/test-repo/1"
}
]
},
{
"org": "Octocat",
"name": "test-repo-2"
}
]
}
]`
)

// getDashboards returns mock JSON for a http GET.
func getDashboards(c *gin.Context) {
data := []byte(DashCardsResp)

var body []api.Dashboard
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}

// getDashboard has a param :dashboard returns mock JSON for a http GET.
func getDashboard(c *gin.Context) {
d := c.Param("dashboard")

if strings.EqualFold(d, "0") {
msg := fmt.Sprintf("Dashboard %s does not exist", d)

c.AbortWithStatusJSON(http.StatusNotFound, types.Error{Message: &msg})

return
}

data := []byte(DashCardResp)

var body api.Dashboard
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}

// addDashboard returns mock JSON for a http POST.
func addDashboard(c *gin.Context) {
data := []byte(DashboardResp)

var body api.Dashboard
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusCreated, body)
}

// updateDashboard returns mock JSON for a http PUT.
func updateDashboard(c *gin.Context) {
data := []byte(DashboardResp)

var body api.Dashboard
_ = json.Unmarshal(data, &body)

c.JSON(http.StatusOK, body)
}
59 changes: 59 additions & 0 deletions mock/server/dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: Apache-2.0

package server

import (
"encoding/json"
"reflect"
"testing"

api "github.com/go-vela/server/api/types"
)

func TestDashboard_ActiveDashboardResp(t *testing.T) {
testDashboard := api.Dashboard{}

err := json.Unmarshal([]byte(DashboardResp), &testDashboard)
if err != nil {
t.Errorf("error unmarshaling dashboard: %v", err)
}

tDashboard := reflect.TypeOf(testDashboard)

for i := 0; i < tDashboard.NumField(); i++ {
if reflect.ValueOf(testDashboard).Field(i).IsNil() {
t.Errorf("DashboardResp missing field %s", tDashboard.Field(i).Name)
}
}

testDashCard := api.DashCard{}

err = json.Unmarshal([]byte(DashCardResp), &testDashCard)
if err != nil {
t.Errorf("error unmarshaling dash card: %v", err)
}

tDashCard := reflect.TypeOf(testDashCard)

for i := 0; i < tDashCard.NumField(); i++ {
if reflect.ValueOf(testDashCard).Field(i).IsNil() {
t.Errorf("DashCardResp missing field %s", tDashCard.Field(i).Name)
}
}

testDashCards := []api.DashCard{}
err = json.Unmarshal([]byte(DashCardsResp), &testDashCards)
if err != nil {
t.Errorf("error unmarshaling dash cards: %v", err)
}

for _, testDashCard := range testDashCards {
tDashCard := reflect.TypeOf(testDashCard)

for i := 0; i < tDashCard.NumField(); i++ {
if reflect.ValueOf(testDashCard).Field(i).IsNil() {
t.Errorf("DashboardsResp missing field %s", tDashboard.Field(i).Name)
}
}
}
}
6 changes: 6 additions & 0 deletions mock/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func FakeHandler() http.Handler {
e.GET("/api/v1/repos/:org/:repo/builds/:build/token", buildToken)
e.GET("/api/v1/repos/:org/:repo/builds/:build/executable", buildExecutable)

// mock endpoints for dashboard calls
e.GET("/api/v1/dashboards/:dashboard", getDashboard)
e.GET("/api/v1/user/dashboards", getDashboards)
e.POST("/api/v1/dashboards", addDashboard)
e.PUT("/api/v1/dashboards/:dashboard", updateDashboard)

// mock endpoints for deployment calls
e.GET("/api/v1/deployments/:org/:repo", getDeployments)
e.POST("/api/v1/deployments/:org/:repo", addDeployment)
Expand Down
Loading