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
16 changes: 10 additions & 6 deletions src/jetstream/api/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ type AuthProvider struct {
UserInfo GetUserInfoFromToken
}

type LogCacheLink struct {
Href string `json:"href"`
}

type ApiRootLinks struct {
LogCache LogCacheLink `json:"log_cache"`
}

type ApiRoot struct {
Links struct {
LogCache struct {
Href string `json:"href"`
} `json:"log_cache"`
} `json:"links"`
Links ApiRootLinks
}

// V2Info is the response for the Cloud Foundry /v2/info API
Expand All @@ -47,7 +51,7 @@ type V2Info struct {

type EndpointInfo struct {
ApiRoot ApiRoot
V2Info V2Info
V2Info V2Info
}

type InfoFunc func(apiEndpoint string, skipSSLValidation bool, caCert string) (CNSIRecord, interface{}, error)
Expand Down
74 changes: 23 additions & 51 deletions src/jetstream/cnsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ import (
func TestRegisterCFCluster(t *testing.T) {
t.Parallel()

mockV2Info := setupMockServer(t,
msRoute("/v2/info"),
msMethod("GET"),
msStatus(http.StatusOK),
msBody(jsonMust(mockV2InfoResponse)))
mockV2Info := setupMockEndpointServer(t)

defer mockV2Info.Close()

Expand Down Expand Up @@ -64,11 +60,7 @@ func TestRegisterCFCluster(t *testing.T) {
func TestRegisterCFClusterWithMissingName(t *testing.T) {
t.Parallel()

mockV2Info := setupMockServer(t,
msRoute("/v2/info"),
msMethod("GET"),
msStatus(http.StatusOK),
msBody(jsonMust(mockV2InfoResponse)))
mockV2Info := setupMockEndpointServer(t)

defer mockV2Info.Close()

Expand Down Expand Up @@ -104,11 +96,7 @@ func getCFPlugin(p *portalProxy, endpointType string) api.EndpointPlugin {
func TestRegisterCFClusterWithMissingAPIEndpoint(t *testing.T) {
t.Parallel()

mockV2Info := setupMockServer(t,
msRoute("/v2/info"),
msMethod("GET"),
msStatus(http.StatusOK),
msBody(jsonMust(mockV2InfoResponse)))
mockV2Info := setupMockEndpointServer(t)

defer mockV2Info.Close()

Expand All @@ -128,11 +116,7 @@ func TestRegisterCFClusterWithMissingAPIEndpoint(t *testing.T) {
func TestRegisterCFClusterWithInvalidAPIEndpoint(t *testing.T) {
t.Parallel()

mockV2Info := setupMockServer(t,
msRoute("/v2/info"),
msMethod("GET"),
msStatus(http.StatusOK),
msBody(jsonMust(mockV2InfoResponse)))
mockV2Info := setupMockEndpointServer(t)

defer mockV2Info.Close()

Expand Down Expand Up @@ -180,11 +164,7 @@ func TestRegisterCFClusterWithBadV2Request(t *testing.T) {
func TestRegisterCFClusterButCantSaveCNSIRecord(t *testing.T) {
t.Parallel()

mockV2Info := setupMockServer(t,
msRoute("/v2/info"),
msMethod("GET"),
msStatus(http.StatusOK),
msBody(jsonMust(mockV2InfoResponse)))
mockV2Info := setupMockEndpointServer(t)

defer mockV2Info.Close()

Expand Down Expand Up @@ -287,16 +267,8 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
defer db.Close()

// mock individual APIEndpoints
mockV2Info := []*httptest.Server{}
for i := 0; i < 1; i++ {
server := setupMockServer(t,
msRoute("/v2/info"),
msMethod("GET"),
msStatus(http.StatusOK),
msBody(jsonMust(mockV2InfoResponse)))
defer server.Close()
mockV2Info = append(mockV2Info, server)
}
mockV2Info := setupMockEndpointServer(t)
defer mockV2Info.Close()

// mock different users
mockAdmin := setupMockUser(mockAdminGUID, true, []string{})
Expand All @@ -310,7 +282,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
Convey("as admin", func() {
Convey("with createSystemEndpoint enabled", func() {
// setup
adminEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info[0], "CF Cluster 1", true, true)
adminEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info, "CF Cluster 1", true, true)

if errSession := pp.setSessionValues(adminEndpoint.EchoContext, mockAdmin.SessionValues); errSession != nil {
t.Error(errors.New("unable to mock/stub user in session object"))
Expand All @@ -324,7 +296,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {

// return no already saved endpoints
rows := testutils.GetEmptyCNSIRows()
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info.URL).WillReturnRows(rows)

mock.ExpectExec(insertIntoCNSIs).
WithArgs(adminEndpoint.InsertArgs...).
Expand All @@ -344,7 +316,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
})
Convey("create system endpoint over existing user endpoints", func() {
// setup
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1 User", false, false)
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info, "CF Cluster 1 User", false, false)

// mock executions
mockStratosAuth.
Expand All @@ -354,7 +326,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {

// return a user endpoint with same apiurl
rows := testutils.GetEmptyCNSIRows().AddRow(userEndpoint.QueryArgs...)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info.URL).WillReturnRows(rows)

// save cnsi
mock.ExpectExec(insertIntoCNSIs).
Expand Down Expand Up @@ -382,7 +354,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {

// return a admin endpoint with same apiurl
rows := testutils.GetEmptyCNSIRows().AddRow(adminEndpoint.QueryArgs...)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info.URL).WillReturnRows(rows)

// test
err := pp.RegisterEndpoint(adminEndpoint.EchoContext, getCFPlugin(pp, "cf").Info)
Expand All @@ -404,8 +376,8 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
Convey("with createSystemEndpoint disabled", func() {

// setup
adminEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info[0], "CF Cluster 1", false, false)
systemEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info[0], "CF Cluster 1", false, true)
adminEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info, "CF Cluster 1", false, false)
systemEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info, "CF Cluster 1", false, true)

if errSession := pp.setSessionValues(adminEndpoint.EchoContext, mockAdmin.SessionValues); errSession != nil {
t.Error(errors.New("unable to mock/stub user in session object"))
Expand All @@ -420,7 +392,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {

// return a admin endpoint with same apiurl
rows := testutils.GetEmptyCNSIRows().AddRow(systemEndpoint.QueryArgs...)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info.URL).WillReturnRows(rows)

// save cnsi
mock.ExpectExec(insertIntoCNSIs).
Expand Down Expand Up @@ -448,7 +420,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {

// return a user endpoint with same apiurl
rows := testutils.GetEmptyCNSIRows().AddRow(adminEndpoint.QueryArgs...)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info.URL).WillReturnRows(rows)

// test
err := pp.RegisterEndpoint(adminEndpoint.EchoContext, getCFPlugin(pp, "cf").Info)
Expand All @@ -472,7 +444,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
Convey("as user", func() {
Convey("with createSystemEndpoint enabled", func() {
// setup
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1", true, false)
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info, "CF Cluster 1", true, false)

if errSession := pp.setSessionValues(userEndpoint.EchoContext, mockUser1.SessionValues); errSession != nil {
t.Error(errors.New("unable to mock/stub user in session object"))
Expand All @@ -486,7 +458,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
Return(mockUser1.ConnectedUser, nil)

rows := testutils.GetEmptyCNSIRows()
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info.URL).WillReturnRows(rows)

mock.ExpectExec(insertIntoCNSIs).
WithArgs(userEndpoint.InsertArgs...).
Expand All @@ -504,7 +476,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
})
})
Convey("register existing endpoint from different user", func() {
userEndpoint2 := setupMockEndpointRegisterRequest(t, mockUser2.ConnectedUser, mockV2Info[0], "CF Cluster 2", false, false)
userEndpoint2 := setupMockEndpointRegisterRequest(t, mockUser2.ConnectedUser, mockV2Info, "CF Cluster 2", false, false)

// mock executions
mockStratosAuth.
Expand All @@ -513,7 +485,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
Return(mockUser1.ConnectedUser, nil)

rows := testutils.GetEmptyCNSIRows().AddRow(userEndpoint2.QueryArgs...)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info.URL).WillReturnRows(rows)

mock.ExpectExec(insertIntoCNSIs).
WithArgs(userEndpoint.InsertArgs...).
Expand All @@ -538,7 +510,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
Return(mockUser1.ConnectedUser, nil)

rows := testutils.GetEmptyCNSIRows().AddRow(userEndpoint.QueryArgs...)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info.URL).WillReturnRows(rows)

err := pp.RegisterEndpoint(userEndpoint.EchoContext, getCFPlugin(pp, "cf").Info)
dberr := mock.ExpectationsWereMet()
Expand All @@ -557,7 +529,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
})
})
Convey("with createSystemEndpoint disabled", func() {
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1", false, false)
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info, "CF Cluster 1", false, false)

if errSession := pp.setSessionValues(userEndpoint.EchoContext, mockUser1.SessionValues); errSession != nil {
t.Error(errors.New("unable to mock/stub user in session object"))
Expand All @@ -570,7 +542,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
Return(mockUser1.ConnectedUser, nil)

rows := testutils.GetEmptyCNSIRows().AddRow(userEndpoint.QueryArgs...)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info.URL).WillReturnRows(rows)

err := pp.RegisterEndpoint(userEndpoint.EchoContext, getCFPlugin(pp, "cf").Info)
dberr := mock.ExpectationsWereMet()
Expand Down
40 changes: 40 additions & 0 deletions src/jetstream/mock_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"database/sql/driver"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -319,6 +320,36 @@ func msBody(body string) mockServerFunc {
}
}

func setupMockEndpointServer(t *testing.T) *httptest.Server {

server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var responseBody []byte
var err error

if r.URL.Path == "/" {
responseBody, err = json.Marshal(mockApiRootResponse)

} else if r.URL.Path == "/v2/info" {
responseBody, err = json.Marshal(mockV2InfoResponse)

} else {
t.Errorf("No API Setup path / or /v1/info, got path '%s'", r.URL.Path)
}

if err != nil {
t.Errorf("Could not Marshal mock response '%s'", err)
}

if r.Method != http.MethodGet {
t.Errorf("Wanted method 'GET', got method '%s'", r.Method)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(responseBody))
}))

return server
}

func setupMockServer(t *testing.T, modifiers ...mockServerFunc) *httptest.Server {
mServer := &mockServer{}
for _, mod := range modifiers {
Expand Down Expand Up @@ -364,6 +395,7 @@ const (
mockClientId = "stratos_clientid"
mockClientSecret = "big_secret"
mockProxyVersion = 20161117141922
mockLogCache = "https://log-cache.127.0.0.1"

stringCFType = "cf"

Expand Down Expand Up @@ -395,6 +427,14 @@ var mockV2InfoResponse = api.V2Info{
DopplerLoggingEndpoint: mockDopplerEndpoint,
}

var mockApiRootResponse = api.ApiRoot{
Links: api.ApiRootLinks{
LogCache: api.LogCacheLink{
Href: mockLogCache,
},
},
}

var mockInfoResponse = api.V2Info{
AuthorizationEndpoint: mockAuthEndpoint,
TokenEndpoint: mockTokenEndpoint,
Expand Down