Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Jul 22, 2019
1 parent 3ce9bad commit bea083f
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ mockgen:
go install github.com/golang/mock/mockgen

mockgen -source targets/targets.go -destination targets/targets_mock.go -package targets -self_package github.com/coveooss/credentials-sync/targets
mockgen -source credentials/sources.go -destination credentials/sources_mock.go -package credentials -self_package github.com/coveooss/credentials-sync/credentials
mockgen -source credentials/sources.go -destination credentials/sources_mock.go -package credentials -self_package github.com/coveooss/credentials-sync/credentials
mockgen -source credentials/credentials.go -destination credentials/credentials_mock.go -package credentials -self_package github.com/coveooss/credentials-sync/credentials
103 changes: 103 additions & 0 deletions credentials/credentials_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 39 additions & 7 deletions sync/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ func setSourceMock(t *testing.T, config *Configuration) (*gomock.Controller, *cr
return ctrl, source
}

func setTargetMock(t *testing.T, config *Configuration) (*gomock.Controller, *targets.MockTarget) {
func setTargetMock(t *testing.T, config *Configuration, name string, credentials []string, shouldDeleteUnsynced bool) (*gomock.Controller, *targets.MockTarget) {
ctrl := gomock.NewController(t)
target := targets.NewMockTarget(ctrl)
targetCollection := targets.NewMockTargetCollection(ctrl)
targetCollection.EXPECT().AllTargets().Return([]targets.Target{target}).AnyTimes()

target.EXPECT().GetExistingCredentials().Return(credentials).AnyTimes()
target.EXPECT().GetName().Return(name).AnyTimes()
target.EXPECT().ShouldDeleteUnsynced().Return(shouldDeleteUnsynced).AnyTimes()

config.SetTargets(targetCollection)
return ctrl, target
}
Expand All @@ -32,17 +36,45 @@ func TestDeleteListOfCredentials(t *testing.T) {
config := &Configuration{
CredentialsToDelete: []string{"test1", "test-not-exist"},
}
setSourceMock(t, config)
targetController, target := setTargetMock(t, config)
targetController, target := setTargetMock(t, config, "", []string{"test1"}, false)
defer targetController.Finish()

// The existing creds is only `test1`
target.EXPECT().GetExistingCredentials().Return([]string{"test1"}).AnyTimes()
// Does not assert on GetName
target.EXPECT().GetName().Return("").AnyTimes()
// Asserts that DeleteCredentials is called with `test1`
target.EXPECT().DeleteCredentials("test1").Return(nil)

config.DeleteListOfCredentials(target)
}

func TestUpdateListOfCredentials(t *testing.T) {
config := &Configuration{}
targetController, target := setTargetMock(t, config, "", []string{"test1"}, false)
defer targetController.Finish()

cred1, cred2 := credentials.NewSecretText(), credentials.NewSecretText()
cred1.ID = "test1"
cred2.ID = "test2"

// Asserts that UpdateCredentials is called with `test1` and `test2`
target.EXPECT().UpdateCredentials(cred1).Times(1)
target.EXPECT().UpdateCredentials(cred2).Times(1)

config.UpdateListOfCredentials(target, []credentials.Credentials{cred1, cred2})
}

func TestDeleteUnsyncedCredentials(t *testing.T) {
config := &Configuration{}
targetController, target := setTargetMock(t, config, "", []string{"unsynced"}, true)
defer targetController.Finish()

cred1, cred2 := credentials.NewSecretText(), credentials.NewSecretText()
cred1.ID = "test1"
cred2.ID = "test2"

// Do not verify this call
target.EXPECT().UpdateCredentials(gomock.Any()).AnyTimes()

// Asserts that DeleteCredentials is called with `unsynced`
target.EXPECT().DeleteCredentials("unsynced").Times(1)

config.UpdateListOfCredentials(target, []credentials.Credentials{cred1, cred2})
}
69 changes: 69 additions & 0 deletions targets/jenkins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package targets
import (
"testing"

"github.com/bndr/gojenkins"

"github.com/coveooss/credentials-sync/credentials"
"github.com/stretchr/testify/assert"
)

Expand All @@ -14,3 +17,69 @@ func TestJenkinsToString(t *testing.T) {

assert.Equal(t, "targetName [Tags: my_tag=tag_value] (Jenkins) - test.com", jenkins.ToString())
}

func TestAwsCredentialsToJenkinsCred(t *testing.T) {
secret := credentials.NewAmazonWebServicesCredentials()
secret.ID = "test-id"
secret.Description = "a test description"
secret.AccessKey = "AKIASOMETHING"
secret.SecretKey = "test-secret"
secret.RoleARN = "my-role"

jenkinsSecretInterface := toJenkinsCredential(secret)

jenkinsSecret := jenkinsSecretInterface.(*gojenkins.AmazonWebServicesCredentials)
assert.Equal(t, secret.ID, jenkinsSecret.ID)
assert.Equal(t, secret.Description, jenkinsSecret.Description)
assert.Equal(t, secret.AccessKey, jenkinsSecret.AccessKey)
assert.Equal(t, secret.SecretKey, jenkinsSecret.SecretKey)
assert.Equal(t, secret.RoleARN, jenkinsSecret.IAMRoleARN)
}

func TestSecretToJenkinsCred(t *testing.T) {
secret := credentials.NewSecretText()
secret.ID = "test-id"
secret.Description = "a test description"
secret.Secret = "a-secret"

jenkinsSecretInterface := toJenkinsCredential(secret)

jenkinsSecret := jenkinsSecretInterface.(*gojenkins.StringCredentials)
assert.Equal(t, secret.ID, jenkinsSecret.ID)
assert.Equal(t, secret.Description, jenkinsSecret.Description)
assert.Equal(t, secret.Secret, jenkinsSecret.Secret)
}

func TestSSHKeyToJenkinsCred(t *testing.T) {
secret := credentials.NewSSHCredentials()
secret.ID = "test-id"
secret.Username = "a-user"
secret.Passphrase = "a-password"
secret.PrivateKey = "a-key"

jenkinsSecretInterface := toJenkinsCredential(secret)

jenkinsSecret := jenkinsSecretInterface.(*gojenkins.SSHCredentials)
assert.Equal(t, secret.ID, jenkinsSecret.ID)
assert.Equal(t, secret.ID, jenkinsSecret.Description) // No description, then use ID as description
assert.Equal(t, secret.Username, jenkinsSecret.Username)
assert.Equal(t, secret.Passphrase, jenkinsSecret.Passphrase)
privateKeySource := jenkinsSecret.PrivateKeySource.(*gojenkins.PrivateKey)
assert.Equal(t, secret.PrivateKey, privateKeySource.Value)
}

func TestUsernamePasswordToJenkinsCred(t *testing.T) {
secret := credentials.NewUsernamePassword()
secret.ID = "test-id"
secret.Description = "a test description"
secret.Username = "a-user"
secret.Password = "a-password"

jenkinsSecretInterface := toJenkinsCredential(secret)

jenkinsSecret := jenkinsSecretInterface.(*gojenkins.UsernameCredentials)
assert.Equal(t, secret.ID, jenkinsSecret.ID)
assert.Equal(t, secret.Description, jenkinsSecret.Description)
assert.Equal(t, secret.Username, jenkinsSecret.Username)
assert.Equal(t, secret.Password, jenkinsSecret.Password)
}

0 comments on commit bea083f

Please sign in to comment.