Skip to content

Commit

Permalink
Adding tests for SaveCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
sledigabel committed Aug 4, 2023
1 parent f322307 commit ce74b2c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
4 changes: 4 additions & 0 deletions helper/credentials/saml.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package credentials

import (
"errors"
"path"

"github.com/versent/saml2aws/v2/pkg/creds"
Expand Down Expand Up @@ -67,6 +68,9 @@ func SaveCredentials(idpName, url, username, password string) error {
Secret: password,
}

if idpName == "" {
return errors.New("idpName is empty")
}
return CurrentHelper.Add(creds)
}

Expand Down
59 changes: 58 additions & 1 deletion helper/credentials/saml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type MockHelper struct {
DeleteFailError error
}

func NewMockHelper() *MockHelper {
return &MockHelper{
Credentials: make(map[string]*Credentials),
}
}

func (m *MockHelper) Add(c *Credentials) error {
if m.AddFailError != nil {
return m.AddFailError
Expand Down Expand Up @@ -230,7 +236,7 @@ func TestLookupCredentials(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.CaseName, func(t *testing.T) {
t.Log(testCase.CaseName)
m := &MockHelper{}
m := NewMockHelper()
CurrentHelper = m
m.Credentials = testCase.initialCredentials
t.Log(testCase.initialCredentials)
Expand All @@ -251,3 +257,54 @@ func TestLookupCredentials(t *testing.T) {
// restoring the old Helper
CurrentHelper = oldHelper
}

func TestSaveCredentials(t *testing.T) {
oldHelper := CurrentHelper

testCases := []struct {
CaseName string
IdpName string
URL string
Username string
Password string
expectedCredentialKeyName string
expectedError bool
}{
{
CaseName: "SaveCredentials",
IdpName: "test",
URL: "http://test.com/",
Username: "user1",
Password: "password1",
expectedCredentialKeyName: "saml2aws_credentials_test",
},
{
CaseName: "EmptyIdpNameRaisesError",
IdpName: "",
URL: "http://test.com/",
Username: "user2",
Password: "password2",
expectedError: true,
},
}

for _, testCase := range testCases {
t.Run(testCase.CaseName, func(t *testing.T) {
m := NewMockHelper()
CurrentHelper = m
err := SaveCredentials(testCase.IdpName, testCase.URL, testCase.Username, testCase.Password)
if testCase.expectedError {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
_, ok := m.Credentials[testCase.expectedCredentialKeyName]
assert.True(t, ok)
assert.EqualValues(t, testCase.Username, m.Credentials[testCase.expectedCredentialKeyName].Username)
assert.EqualValues(t, testCase.Password, m.Credentials[testCase.expectedCredentialKeyName].Secret)
}
})
}

// restoring the old Helper
CurrentHelper = oldHelper
}

0 comments on commit ce74b2c

Please sign in to comment.