Skip to content

Commit

Permalink
APIGOV-23624 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor Johnson committed Sep 13, 2022
1 parent d63a000 commit 7fd5fb3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pkg/anypoint/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion pkg/discovery/servicehandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
12 changes: 9 additions & 3 deletions pkg/subscription/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
85 changes: 83 additions & 2 deletions pkg/subscription/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 7fd5fb3

Please sign in to comment.