-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julien Duchesne
committed
Aug 6, 2019
1 parent
1b21474
commit d5defe1
Showing
8 changed files
with
175 additions
and
57 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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package sync | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/coveooss/credentials-sync/credentials" | ||
) | ||
|
||
func TestSyncCredentials(t *testing.T) { | ||
cred1, cred2 := credentials.NewSecretText(), credentials.NewSecretText() | ||
cred1.ID = "test1" | ||
cred2.ID = "test2" | ||
|
||
config := &Configuration{StopOnError: true, TargetParallelism: 1} | ||
targetController, target := setTargetMock(t, config, "target", []string{"test1"}, false) | ||
sourceController, _ := setSourceMock(t, config, []credentials.Credentials{cred1, cred2}) | ||
defer targetController.Finish() | ||
defer sourceController.Finish() | ||
|
||
// Asserts that UpdateCredentials is called with `test1` and `test2` | ||
target.EXPECT().UpdateCredentials(cred1).Times(1) | ||
target.EXPECT().UpdateCredentials(cred2).Times(1) | ||
|
||
config.Sync() | ||
} | ||
|
||
func TestSyncCredentialsAndDeleteUnsynced(t *testing.T) { | ||
cred1, cred2 := credentials.NewSecretText(), credentials.NewSecretText() | ||
cred1.ID = "test1" | ||
cred2.ID = "test2" | ||
|
||
config := &Configuration{StopOnError: true, TargetParallelism: 1} | ||
targetController, target := setTargetMock(t, config, "target", []string{"test3"}, true) | ||
sourceController, _ := setSourceMock(t, config, []credentials.Credentials{cred1, cred2}) | ||
defer targetController.Finish() | ||
defer sourceController.Finish() | ||
|
||
// Asserts that UpdateCredentials is called with `test1` and `test2` | ||
target.EXPECT().UpdateCredentials(cred1).Times(1) | ||
target.EXPECT().UpdateCredentials(cred2).Times(1) | ||
|
||
// Asserts that DeleteCredentials is called with `unsynced` | ||
target.EXPECT().DeleteCredentials("test3").Times(1) | ||
|
||
config.Sync() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package sync | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/coveooss/credentials-sync/credentials" | ||
"github.com/coveooss/credentials-sync/targets" | ||
"github.com/golang/mock/gomock" | ||
) | ||
|
||
func setSourceMock(t *testing.T, config *Configuration, creds []credentials.Credentials) (*gomock.Controller, *credentials.MockSourceCollection) { | ||
ctrl := gomock.NewController(t) | ||
source := credentials.NewMockSource(ctrl) | ||
sourceCollection := credentials.NewMockSourceCollection(ctrl) | ||
sourceCollection.EXPECT().AllSources().Return([]credentials.Source{source}).AnyTimes() | ||
|
||
if creds != nil { | ||
sourceCollection.EXPECT().Credentials().Return(creds, nil).AnyTimes() | ||
} | ||
|
||
config.SetSources(sourceCollection) | ||
return ctrl, sourceCollection | ||
} | ||
|
||
func setMultipleTargetMock(t *testing.T, config *Configuration, name string, credentials []string, shouldDeleteUnsynced bool, targetNumber int) (*gomock.Controller, []*targets.MockTarget) { | ||
ctrl := gomock.NewController(t) | ||
targetCollection := targets.NewMockTargetCollection(ctrl) | ||
|
||
targetsToReturn := []*targets.MockTarget{} | ||
targetsToReturnInterface := []targets.Target{} | ||
for i := 1; i <= targetNumber; i++ { | ||
target := targets.NewMockTarget(ctrl) | ||
target.EXPECT().GetExistingCredentials().Return(credentials).AnyTimes() | ||
target.EXPECT().GetName().Return(name).AnyTimes() | ||
target.EXPECT().GetTags().Return(map[string]string{}).AnyTimes() | ||
target.EXPECT().ToString().Return(name).AnyTimes() | ||
target.EXPECT().ShouldDeleteUnsynced().Return(shouldDeleteUnsynced).AnyTimes() | ||
target.EXPECT().Initialize(gomock.Any()).AnyTimes() | ||
targetsToReturn = append(targetsToReturn, target) | ||
targetsToReturnInterface = append(targetsToReturnInterface, target) | ||
} | ||
targetCollection.EXPECT().AllTargets().Return(targetsToReturnInterface).AnyTimes() | ||
|
||
config.SetTargets(targetCollection) | ||
return ctrl, targetsToReturn | ||
} | ||
|
||
func setTargetMock(t *testing.T, config *Configuration, name string, credentials []string, shouldDeleteUnsynced bool) (*gomock.Controller, *targets.MockTarget) { | ||
ctrl, targetsToReturn := setMultipleTargetMock(t, config, name, credentials, shouldDeleteUnsynced, 1) | ||
return ctrl, targetsToReturn[0] | ||
} |