-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MM-1137]: Added testcases for setupAutolink function
- Loading branch information
1 parent
6f11bf7
commit 796e0c1
Showing
3 changed files
with
150 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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, | ||
}) | ||
} |