Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ NOTE: As semantic versioning states all 0.y.z releases can contain breaking chan
- [#264](https://github.com/kobsio/kobs/pull/264): [core] Add tests for core packages.
- [#266](https://github.com/kobsio/kobs/pull/266): [dashboards] Add tests for dashboards plugin.
- [#267](https://github.com/kobsio/kobs/pull/267): [users] Add tests for users plugin.
- [#268](https://github.com/kobsio/kobs/pull/268): [teams] Add tests for teams plugin.

### Fixed

Expand Down
4 changes: 3 additions & 1 deletion plugins/teams/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const Route = "/teams"
// Config is the structure of the configuration for the teams plugin.
type Config struct{}

// Router implements the router for the resources plugin, which can be registered in the router for our rest api.
// Router implements the router for the teams plugin, which can be registered in the router for our rest api. The router
// contains all api endpoints for the plugin, the cluster client to get the teams from the Kubernetes api server and the
// user defined configuration.
type Router struct {
*chi.Mux
clustersClient clusters.Client
Expand Down
132 changes: 132 additions & 0 deletions plugins/teams/teams_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package teams

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

team "github.com/kobsio/kobs/pkg/api/apis/team/v1beta1"
"github.com/kobsio/kobs/pkg/api/clusters"
"github.com/kobsio/kobs/pkg/api/clusters/cluster"
"github.com/kobsio/kobs/pkg/api/plugins/plugin"

"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func TestGetUsers(t *testing.T) {
for _, tt := range []struct {
name string
expectedStatusCode int
expectedBody string
prepare func(mockClusterClient *cluster.MockClient)
}{
{
name: "get teams error",
expectedStatusCode: http.StatusBadRequest,
expectedBody: "{\"error\":\"Could not get teams: could not get teams\"}\n",
prepare: func(mockClusterClient *cluster.MockClient) {
mockClusterClient.On("GetTeams", mock.Anything, "").Return(nil, fmt.Errorf("could not get teams"))
},
},
{
name: "get teams",
expectedStatusCode: http.StatusOK,
expectedBody: "[{\"cluster\":\"cluster1\",\"namespace\":\"namespace1\",\"name\":\"name1\",\"id\":\"id1\",\"permissions\":{\"plugins\":null,\"resources\":null}}]\n",
prepare: func(mockClusterClient *cluster.MockClient) {
mockClusterClient.On("GetTeams", mock.Anything, "").Return([]team.TeamSpec{{Cluster: "cluster1", Namespace: "namespace1", Name: "name1", ID: "id1"}}, nil)
},
},
} {
t.Run(tt.name, func(t *testing.T) {
mockClusterClient := &cluster.MockClient{}
mockClusterClient.AssertExpectations(t)

mockClustersClient := &clusters.MockClient{}
mockClustersClient.AssertExpectations(t)
mockClustersClient.On("GetClusters").Return([]cluster.Client{mockClusterClient})

tt.prepare(mockClusterClient)

router := Router{chi.NewRouter(), mockClustersClient, Config{}}
router.Get("/teams", router.getTeams)

req, _ := http.NewRequest(http.MethodGet, "/teams", nil)
w := httptest.NewRecorder()

router.getTeams(w, req)

require.Equal(t, tt.expectedStatusCode, w.Code)
require.Equal(t, tt.expectedBody, string(w.Body.Bytes()))
})
}
}

func TestGetUser(t *testing.T) {
for _, tt := range []struct {
name string
url string
expectedStatusCode int
expectedBody string
}{
{
name: "invalid cluster name",
url: "/team",
expectedStatusCode: http.StatusBadRequest,
expectedBody: "{\"error\":\"Invalid cluster name\"}\n",
},
{
name: "get team error",
url: "/team?cluster=cluster1&namespace=namespace1&name=team2",
expectedStatusCode: http.StatusBadRequest,
expectedBody: "{\"error\":\"Could not get team: could not get team\"}\n",
},
{
name: "get team",
url: "/team?cluster=cluster1&namespace=namespace1&name=team1",
expectedStatusCode: http.StatusOK,
expectedBody: "{\"cluster\":\"cluster1\",\"namespace\":\"namespace1\",\"name\":\"name1\",\"id\":\"id1\",\"permissions\":{\"plugins\":null,\"resources\":null}}\n",
},
} {
t.Run(tt.name, func(t *testing.T) {
mockClusterClient := &cluster.MockClient{}
mockClusterClient.AssertExpectations(t)
mockClusterClient.On("GetTeam", mock.Anything, "namespace1", "team1").Return(&team.TeamSpec{Cluster: "cluster1", Namespace: "namespace1", Name: "name1", ID: "id1"}, nil)
mockClusterClient.On("GetTeam", mock.Anything, "namespace1", "team2").Return(nil, fmt.Errorf("could not get team"))

mockClustersClient := &clusters.MockClient{}
mockClustersClient.AssertExpectations(t)
mockClustersClient.On("GetCluster", "").Return(nil)
mockClustersClient.On("GetCluster", "cluster1").Return(mockClusterClient)

router := Router{chi.NewRouter(), mockClustersClient, Config{}}
router.Get("/team", router.getTeam)

req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
w := httptest.NewRecorder()

router.getTeam(w, req)

require.Equal(t, tt.expectedStatusCode, w.Code)
require.Equal(t, tt.expectedBody, string(w.Body.Bytes()))
})
}
}

func TestRegister(t *testing.T) {
plugins := &plugin.Plugins{}
router := Register(nil, plugins, Config{})

require.NotEmpty(t, router)
require.Equal(t, &plugin.Plugins{
plugin.Plugin{
Name: "teams",
DisplayName: "Teams",
Description: "Define an ownership for your Kubernetes resources.",
Home: true,
Type: "teams",
},
}, plugins)
}