Skip to content

Commit

Permalink
Add test case for GetRestConfigAsUser
Browse files Browse the repository at this point in the history
  • Loading branch information
Tof1973 committed Dec 18, 2023
1 parent 2e3644c commit 102b6df
Showing 1 changed file with 59 additions and 16 deletions.
75 changes: 59 additions & 16 deletions cmd/ocm-backplane/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/openshift/backplane-cli/pkg/backplaneapi"
backplaneapiMock "github.com/openshift/backplane-cli/pkg/backplaneapi/mocks"
"github.com/openshift/backplane-cli/pkg/cli/config"
"github.com/openshift/backplane-cli/pkg/client/mocks"
"github.com/openshift/backplane-cli/pkg/login"
"github.com/openshift/backplane-cli/pkg/ocm"
Expand All @@ -33,22 +34,24 @@ func MakeIoReader(s string) io.ReadCloser {
var _ = Describe("Login command", func() {

var (
mockCtrl *gomock.Controller
mockClient *mocks.MockClientInterface
mockClientWithResp *mocks.MockClientWithResponsesInterface
mockOcmInterface *ocmMock.MockOCMInterface
mockClientUtil *backplaneapiMock.MockClientUtils

testClusterID string
testToken string
trueClusterID string
managingClusterID string
backplaneAPIURI string
serviceClusterID string
serviceClusterName string
fakeResp *http.Response
ocmEnv *cmv1.Environment
kubeConfigPath string
mockCtrl *gomock.Controller
mockClient *mocks.MockClientInterface
mockClientWithResp *mocks.MockClientWithResponsesInterface
mockOcmInterface *ocmMock.MockOCMInterface
mockClientUtil *backplaneapiMock.MockClientUtils

testClusterID string
testToken string
trueClusterID string
managingClusterID string
backplaneAPIURI string
serviceClusterID string
serviceClusterName string
fakeResp *http.Response
ocmEnv *cmv1.Environment
kubeConfigPath string
mockCluster *cmv1.Cluster
backplaneConfiguration config.BackplaneConfiguration
)

BeforeEach(func() {
Expand Down Expand Up @@ -87,6 +90,10 @@ var _ = Describe("Login command", func() {
globalOpts.BackplaneURL = backplaneAPIURI

ocmEnv, _ = cmv1.NewEnvironment().BackplaneURL("https://dummy.api").Build()

mockCluster = &cmv1.Cluster{}

backplaneConfiguration = config.BackplaneConfiguration{URL: backplaneAPIURI}
})

AfterEach(func() {
Expand Down Expand Up @@ -387,4 +394,40 @@ var _ = Describe("Login command", func() {
})

})

Context("check GetRestConfigAsUser", func() {

It("should fail if generated config do not contains provided username without elevationReasons",func () {
mockOcmInterface.EXPECT().GetClusterInfoByID(testClusterID).Return(mockCluster, nil)
mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil)
mockClientUtil.EXPECT().MakeRawBackplaneAPIClientWithAccessToken(backplaneAPIURI, testToken).Return(mockClient, nil)
mockClient.EXPECT().LoginCluster(gomock.Any(), gomock.Eq(testClusterID)).Return(fakeResp, nil)

username := "test-user"

config, err := GetRestConfigAsUser(backplaneConfiguration, testClusterID, username)
Expect(err).To(BeNil())
Expect(config.Impersonate.UserName).To(Equal(username))
Expect(len(config.Impersonate.Extra["reason"])).To(Equal(0))

})

It("should fail if generated config do not contains provided username/elevationReasons",func () {
mockOcmInterface.EXPECT().GetClusterInfoByID(testClusterID).Return(mockCluster, nil)
mockOcmInterface.EXPECT().GetOCMAccessToken().Return(&testToken, nil)
mockClientUtil.EXPECT().MakeRawBackplaneAPIClientWithAccessToken(backplaneAPIURI, testToken).Return(mockClient, nil)
mockClient.EXPECT().LoginCluster(gomock.Any(), gomock.Eq(testClusterID)).Return(fakeResp, nil)

username := "test-user"
elevationReasons := []string{"reason1", "reason2"}

config, err := GetRestConfigAsUser(backplaneConfiguration, testClusterID, username, elevationReasons...)
Expect(err).To(BeNil())
Expect(config.Impersonate.UserName).To(Equal(username))
Expect(config.Impersonate.Extra["reason"][0]).To(Equal(elevationReasons[0]))
Expect(config.Impersonate.Extra["reason"][1]).To(Equal(elevationReasons[1]))

})

})
})

0 comments on commit 102b6df

Please sign in to comment.