From 7fd5fb369248f66d8e3d3cd5178b77d1b883106c Mon Sep 17 00:00:00 2001 From: Trevor Johnson Date: Tue, 13 Sep 2022 15:01:29 -0700 Subject: [PATCH] APIGOV-23624 tests --- pkg/anypoint/mocks.go | 4 ++ pkg/discovery/servicehandler_test.go | 2 +- pkg/subscription/mock.go | 12 +++- pkg/subscription/provision_test.go | 85 +++++++++++++++++++++++++++- 4 files changed, 97 insertions(+), 6 deletions(-) diff --git a/pkg/anypoint/mocks.go b/pkg/anypoint/mocks.go index cfab8da..f714163 100644 --- a/pkg/anypoint/mocks.go +++ b/pkg/anypoint/mocks.go @@ -147,3 +147,7 @@ func (m *MockAnypointClient) RevokeContract(apiID, contractID string) error { args := m.Called() return args.Error(0) } + +func (m *MockAnypointClient) ResetAppSecret(appID int64) (*Application, error) { + return nil, nil +} diff --git a/pkg/discovery/servicehandler_test.go b/pkg/discovery/servicehandler_test.go index d98fea1..847506e 100644 --- a/pkg/discovery/servicehandler_test.go +++ b/pkg/discovery/servicehandler_test.go @@ -437,7 +437,7 @@ func Test_getAuthPolicy(t *testing.T) { } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - policy, conf, _ := getAuthPolicy(tc.policies) + policy, conf, _ := getAuthPolicy(tc.policies, catalog) assert.Equal(t, policy, tc.expected) assert.NotNil(t, conf) }) diff --git a/pkg/subscription/mock.go b/pkg/subscription/mock.go index 1c0e19b..4ef7d79 100644 --- a/pkg/subscription/mock.go +++ b/pkg/subscription/mock.go @@ -5,9 +5,11 @@ import ( ) type MockMuleSubscriptionClient struct { - app *anypoint.Application - err error - contract *anypoint.Contract + app *anypoint.Application + newApp *anypoint.Application + err error + rotateErr error + contract *anypoint.Contract } func (m *MockMuleSubscriptionClient) CreateApp(appName, apiID, description string) (*anypoint.Application, error) { @@ -29,3 +31,7 @@ func (m *MockMuleSubscriptionClient) DeleteContract(apiID, contractID string) er func (m *MockMuleSubscriptionClient) GetApp(id string) (*anypoint.Application, error) { return m.app, m.err } + +func (m *MockMuleSubscriptionClient) ResetAppSecret(appID int64) (*anypoint.Application, error) { + return m.newApp, m.rotateErr +} diff --git a/pkg/subscription/provision_test.go b/pkg/subscription/provision_test.go index b970a5a..de336d0 100644 --- a/pkg/subscription/provision_test.go +++ b/pkg/subscription/provision_test.go @@ -314,8 +314,89 @@ func TestCredentialProvision(t *testing.T) { assert.Equal(t, tc.status.String(), status.GetStatus().String()) if tc.status.String() == prov.Success.String() { assert.NotNil(t, cr) - assert.Contains(t, cr.GetData(), common.ClientSecret) - assert.Contains(t, cr.GetData(), common.ClientID) + assert.Contains(t, cr.GetData(), prov.OauthClientSecret) + assert.Contains(t, cr.GetData(), prov.OauthClientID) + } else { + assert.Nil(t, cr) + } + }) + } +} + +func TestCredentialUpdate(t *testing.T) { + tests := []struct { + name string + appName string + appID string + getAppErr error + rotateErr error + status prov.Status + action prov.CredentialAction + }{ + { + name: "should update credentials", + appName: "app1", + appID: "65432", + status: prov.Success, + action: prov.Rotate, + }, + { + name: "should fail to update credentials when the action is not rotate", + appName: "app1", + appID: "65432", + status: prov.Error, + action: prov.Suspend, + }, + { + name: "should fail to update credentials when making the api call", + appName: "app1", + appID: "65432", + rotateErr: fmt.Errorf("error"), + status: prov.Error, + action: prov.Rotate, + }, + { + name: "should return an error when app is not found", + appName: "app1", + appID: "65432", + status: prov.Error, + action: prov.Rotate, + getAppErr: fmt.Errorf("failed to get app"), + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + app := &anypoint.Application{ + ClientID: "12345", + ClientSecret: "lajksdf", + } + newApp := &anypoint.Application{ + ClientID: "12345", + ClientSecret: "uihgobfjd", + } + client := &MockMuleSubscriptionClient{ + err: tc.getAppErr, + rotateErr: tc.rotateErr, + app: app, + newApp: newApp, + } + prv := NewProvisioner(client, logrus.StandardLogger()) + req := mock.MockCredentialRequest{ + AppName: tc.appName, + AppDetails: map[string]string{ + common.AppID: tc.appID, + }, + Action: tc.action, + } + + status, cr := prv.CredentialUpdate(req) + assert.Equal(t, tc.status.String(), status.GetStatus().String()) + if tc.status.String() == prov.Success.String() { + assert.NotNil(t, cr) + assert.Contains(t, cr.GetData(), prov.OauthClientSecret) + assert.Contains(t, cr.GetData(), prov.OauthClientID) + assert.NotEqual(t, app.ClientSecret, cr.GetData()[prov.OauthClientSecret]) } else { assert.Nil(t, cr) }