-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboards): support dashboards service in SDK (#311)
* init commit * fix tests * typos
- Loading branch information
Showing
6 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package vela | ||
|
||
import ( | ||
"fmt" | ||
|
||
api "github.com/go-vela/server/api/types" | ||
) | ||
|
||
// DashboardService handles retrieving Dashboards from | ||
// the server methods of the Vela API. | ||
type DashboardService service | ||
|
||
// Get returns the provided Dashboard. | ||
func (svc *DashboardService) Get(dashboard string) (*api.DashCard, *Response, error) { | ||
// set the API endpoint path we send the request to | ||
u := fmt.Sprintf("/api/v1/dashboards/%s", dashboard) | ||
|
||
// API Dashboard type we want to return | ||
v := new(api.DashCard) | ||
|
||
// send request using client | ||
resp, err := svc.client.Call("GET", u, nil, v) | ||
|
||
return v, resp, err | ||
} | ||
|
||
// GetAllUser returns a list of all dashboards for the authenticated user. | ||
func (svc *DashboardService) GetAllUser() (*[]api.DashCard, *Response, error) { | ||
// set the API endpoint path we send the request to | ||
u := "/api/v1/user/dashboards" | ||
|
||
// slice library Dashboard type we want to return | ||
v := new([]api.DashCard) | ||
|
||
// send request using client | ||
resp, err := svc.client.Call("GET", u, nil, v) | ||
|
||
return v, resp, err | ||
} | ||
|
||
// Add constructs a Dashboard with the provided details. | ||
func (svc *DashboardService) Add(d *api.Dashboard) (*api.Dashboard, *Response, error) { | ||
// set the API endpoint path we send the request to | ||
u := "/api/v1/dashboards" | ||
|
||
// api dashboard type we want to return | ||
v := new(api.Dashboard) | ||
|
||
// send request using client | ||
resp, err := svc.client.Call("POST", u, d, v) | ||
|
||
return v, resp, err | ||
} | ||
|
||
// Update modifies a dashboard with the provided details. | ||
func (svc *DashboardService) Update(d *api.Dashboard) (*api.Dashboard, *Response, error) { | ||
// set the API endpoint path we send the request to | ||
u := fmt.Sprintf("/api/v1/dashboards/%s", d.GetID()) | ||
|
||
// API dashboard type we want to return | ||
v := new(api.Dashboard) | ||
|
||
// send request using client | ||
resp, err := svc.client.Call("PUT", u, d, v) | ||
|
||
return v, resp, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package vela | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
|
||
api "github.com/go-vela/server/api/types" | ||
"github.com/go-vela/server/mock/server" | ||
) | ||
|
||
func TestDashboard_Get_200(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
data := []byte(server.DashCardResp) | ||
|
||
var want api.DashCard | ||
err := json.Unmarshal(data, &want) | ||
if err != nil { | ||
t.Errorf("unable to unmarshal data: %v", err) | ||
} | ||
|
||
// run test | ||
got, resp, err := c.Dashboard.Get("c976470d-34c1-49b2-9a98-1035871c576b") | ||
|
||
if err != nil { | ||
t.Errorf("New returned err: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("Get returned %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("Get is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestDashboard_Get_404(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
want := api.DashCard{} | ||
|
||
// run test | ||
got, resp, err := c.Dashboard.Get("0") | ||
|
||
if err == nil { | ||
t.Errorf("Get returned err: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusNotFound { | ||
t.Errorf("Get returned %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("Get is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestDashboard_GetAllUser_200(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
data := []byte(server.DashCardsResp) | ||
|
||
var want []api.DashCard | ||
_ = json.Unmarshal(data, &want) | ||
|
||
// run test | ||
got, resp, err := c.Dashboard.GetAllUser() | ||
|
||
if err != nil { | ||
t.Errorf("GetAllUser returned err: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("GetAllUser returned %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("GetAllUser is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestDashboard_Add_201(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
data := []byte(server.DashboardResp) | ||
|
||
var want api.Dashboard | ||
_ = json.Unmarshal(data, &want) | ||
|
||
// run test | ||
got, resp, err := c.Dashboard.Add(&want) | ||
|
||
if err != nil { | ||
t.Errorf("Add returned err: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusCreated { | ||
t.Errorf("Add returned %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("Add is %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestDashboard_Update_200(t *testing.T) { | ||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
s := httptest.NewServer(server.FakeHandler()) | ||
c, _ := NewClient(s.URL, "", nil) | ||
|
||
data := []byte(server.DashboardResp) | ||
|
||
var want api.Dashboard | ||
_ = json.Unmarshal(data, &want) | ||
|
||
// run test | ||
got, resp, err := c.Dashboard.Update(&want) | ||
|
||
if err != nil { | ||
t.Errorf("Update returned err: %v", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
t.Errorf("Update returned %v, want %v", resp.StatusCode, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, &want) { | ||
t.Errorf("Update is %v, want %v", got, want) | ||
} | ||
} |