Skip to content

Commit

Permalink
[MM-1137]: Added testcases for setupAutolink function
Browse files Browse the repository at this point in the history
  • Loading branch information
Kshitij-Katiyar committed Nov 15, 2024
1 parent 6f11bf7 commit 796e0c1
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 12 deletions.
22 changes: 13 additions & 9 deletions server/kv_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

jira "github.com/andygrunwald/go-jira"
"github.com/pkg/errors"
"github.com/stretchr/testify/mock"

"github.com/mattermost/mattermost-plugin-jira/server/utils/types"
)
Expand Down Expand Up @@ -98,26 +99,29 @@ func (store mockUserStore) MapUsers(func(*User) error) error {
return nil
}

type mockInstanceStore struct{}
type mockInstanceStore struct {
mock.Mock
}

func (store mockInstanceStore) CreateInactiveCloudInstance(types.ID, string) error {
func (store *mockInstanceStore) CreateInactiveCloudInstance(types.ID, string) error {
return nil
}
func (store mockInstanceStore) DeleteInstance(types.ID) error {
func (store *mockInstanceStore) DeleteInstance(types.ID) error {
return nil
}
func (store mockInstanceStore) LoadInstance(types.ID) (Instance, error) {
return &testInstance{}, nil
func (store *mockInstanceStore) LoadInstance(id types.ID) (Instance, error) {
args := store.Called(id)
return args.Get(0).(Instance), args.Error(1)
}
func (store mockInstanceStore) LoadInstanceFullKey(string) (Instance, error) {
func (store *mockInstanceStore) LoadInstanceFullKey(string) (Instance, error) {
return &testInstance{}, nil
}
func (store mockInstanceStore) LoadInstances() (*Instances, error) {
func (store *mockInstanceStore) LoadInstances() (*Instances, error) {
return NewInstances(), nil
}
func (store mockInstanceStore) StoreInstance(instance Instance) error {
func (store *mockInstanceStore) StoreInstance(instance Instance) error {
return nil
}
func (store mockInstanceStore) StoreInstances(*Instances) error {
func (store *mockInstanceStore) StoreInstances(*Instances) error {
return nil
}
4 changes: 1 addition & 3 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ const (
WebhookMaxProcsPerServer = 20
WebhookBufferSize = 10000
PluginRepo = "https://github.com/mattermost/mattermost-plugin-jira"

apiTokenEncryptionKey = "token_encryption_key"
)

type externalConfig struct {
Expand Down Expand Up @@ -354,7 +352,7 @@ func (p *Plugin) SetupAutolink(instances *Instances) {
coi, coiOk := instance.(*cloudOAuthInstance)

if !ciOk && !coiOk {
p.client.Log.Info("only cloud and cloud-oauth instances supported for autolink", "err", err)
p.client.Log.Info("only cloud and cloud-oauth instances supported for autolink")
continue
}

Expand Down
136 changes: 136 additions & 0 deletions server/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ import (
"github.com/stretchr/testify/assert"
)

const (
MockInstanceID = "mockInstanceID"
MockAPIToken = "mockAPIToken"
MockAdminEmail = "[email protected]"
MockBaseURL = "mockBaseURL"
MockASCKey = "mockAtlassianSecurityContextKey"
MockASCClientKey = "mockAtlassianSecurityContextClientKey"
MockASCSharedSecret = "mockAtlassianSecurityContextSharedSecret" // #nosec G101: Potential hardcoded credentials - This is a mock for testing purposes
)

func validRequestBody() io.ReadCloser {
if f, err := os.Open("testdata/webhook-issue-created.json"); err != nil {
panic(err)
Expand Down Expand Up @@ -144,3 +154,129 @@ func TestPlugin(t *testing.T) {
})
}
}

func TestSetupAutolink(t *testing.T) {
mockAPI := &plugintest.API{}
dummyInstanceStore := new(mockInstanceStore)
mockPluginClient := pluginapi.NewClient(mockAPI, nil)
p := &Plugin{
client: mockPluginClient,
instanceStore: dummyInstanceStore,
}

tests := []struct {
name string
setup func()
InstanceType InstanceType
}{
{
name: "Missing API token or Admin email",
setup: func() {
mockAPI.On("LogInfo", "unable to setup autolink due to missing API Token or Admin Email").Return(nil).Times(1)
dummyInstanceStore.On("LoadInstance", mock.Anything).Return(&serverInstance{}, nil).Times(1)

p.updateConfig(func(c *config) {
c.AdminAPIToken = ""
c.AdminEmail = ""
})
},
InstanceType: ServerInstanceType,
},
{
name: "Unsupported instance type",
setup: func() {
mockAPI.On("LogInfo", "only cloud and cloud-oauth instances supported for autolink").Return(nil).Times(1)
dummyInstanceStore.On("LoadInstance", mock.Anything).Return(&serverInstance{}, nil).Times(1)

p.updateConfig(GetConfigSetterFunction())
},
InstanceType: ServerInstanceType,
},
{
name: "Autolink plugin unavailable API returned error",
setup: func() {
mockAPI.On("LogWarn", "OnActivate: Autolink plugin unavailable. API returned error", "error", mock.Anything).Return(nil).Times(1)
mockAPI.On("GetPluginStatus", autolinkPluginID).Return(nil, &model.AppError{Message: "error getting plugin status"}).Times(1)
dummyInstanceStore.On("LoadInstance", mock.Anything).Return(&cloudInstance{}, nil).Times(1)

p.updateConfig(GetConfigSetterFunction())
},
InstanceType: CloudInstanceType,
},
{
name: "Autolink plugin not running",
setup: func() {
mockAPI.On("LogWarn", "OnActivate: Autolink plugin unavailable. Plugin is not running", "status", &model.PluginStatus{State: model.PluginStateNotRunning}).Return(nil).Times(1)
mockAPI.On("GetPluginStatus", autolinkPluginID).Return(&model.PluginStatus{State: model.PluginStateNotRunning}, nil).Times(1)
dummyInstanceStore.On("LoadInstance", mock.Anything).Return(&cloudInstance{}, nil).Times(1)

p.updateConfig(GetConfigSetterFunction())
},
InstanceType: CloudInstanceType,
},
{
name: "Error installing autolinks for cloud instance",
setup: func() {
mockAPI.On("LogInfo", "could not install autolinks for cloud instance", "instance", "mockBaseURL", "err", mock.Anything).Return(nil).Times(1)
mockAPI.On("GetPluginStatus", autolinkPluginID).Return(&model.PluginStatus{State: model.PluginStateRunning}, nil).Times(1)
dummyInstanceStore.On("LoadInstance", mock.Anything).Return(
&cloudInstance{
InstanceCommon: &InstanceCommon{
Plugin: p,
},
AtlassianSecurityContext: &AtlassianSecurityContext{
BaseURL: MockBaseURL,
Key: MockASCKey,
ClientKey: MockASCClientKey,
SharedSecret: MockASCSharedSecret,
},
}, nil).Times(1)

p.updateConfig(GetConfigSetterFunction())
},
InstanceType: CloudInstanceType,
},
{
name: "Error installing autolinks for cloud-oauth instance",
setup: func() {
mockAPI.On("LogInfo", "could not install autolinks for cloud-oauth instance", "instance", "mockBaseURL", "err", mock.Anything).Return(nil).Times(1)
mockAPI.On("GetPluginStatus", autolinkPluginID).Return(&model.PluginStatus{State: model.PluginStateRunning}, nil).Times(1)
dummyInstanceStore.On("LoadInstance", mock.Anything).Return(
&cloudOAuthInstance{
InstanceCommon: &InstanceCommon{
Plugin: p,
},
JiraBaseURL: MockBaseURL,
}, nil).Times(1)

p.updateConfig(GetConfigSetterFunction())
},
InstanceType: CloudOAuthInstanceType,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setup()
instances := GetInstancesWithType(tt.InstanceType)

p.SetupAutolink(instances)

mockAPI.AssertExpectations(t)
dummyInstanceStore.AssertExpectations(t)
})
}
}

func GetConfigSetterFunction() func(*config) {
return func(c *config) {
c.AdminAPIToken = MockAPIToken
c.AdminEmail = MockAdminEmail
}
}

func GetInstancesWithType(instanceType InstanceType) *Instances {
return NewInstances(&InstanceCommon{
InstanceID: MockInstanceID,
Type: instanceType,
})
}

0 comments on commit 796e0c1

Please sign in to comment.