Skip to content

Commit ce74b2c

Browse files
committed
Adding tests for SaveCredentials
1 parent f322307 commit ce74b2c

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

helper/credentials/saml.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package credentials
22

33
import (
4+
"errors"
45
"path"
56

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

71+
if idpName == "" {
72+
return errors.New("idpName is empty")
73+
}
7074
return CurrentHelper.Add(creds)
7175
}
7276

helper/credentials/saml_test.go

+58-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ type MockHelper struct {
1515
DeleteFailError error
1616
}
1717

18+
func NewMockHelper() *MockHelper {
19+
return &MockHelper{
20+
Credentials: make(map[string]*Credentials),
21+
}
22+
}
23+
1824
func (m *MockHelper) Add(c *Credentials) error {
1925
if m.AddFailError != nil {
2026
return m.AddFailError
@@ -230,7 +236,7 @@ func TestLookupCredentials(t *testing.T) {
230236
for _, testCase := range testCases {
231237
t.Run(testCase.CaseName, func(t *testing.T) {
232238
t.Log(testCase.CaseName)
233-
m := &MockHelper{}
239+
m := NewMockHelper()
234240
CurrentHelper = m
235241
m.Credentials = testCase.initialCredentials
236242
t.Log(testCase.initialCredentials)
@@ -251,3 +257,54 @@ func TestLookupCredentials(t *testing.T) {
251257
// restoring the old Helper
252258
CurrentHelper = oldHelper
253259
}
260+
261+
func TestSaveCredentials(t *testing.T) {
262+
oldHelper := CurrentHelper
263+
264+
testCases := []struct {
265+
CaseName string
266+
IdpName string
267+
URL string
268+
Username string
269+
Password string
270+
expectedCredentialKeyName string
271+
expectedError bool
272+
}{
273+
{
274+
CaseName: "SaveCredentials",
275+
IdpName: "test",
276+
URL: "http://test.com/",
277+
Username: "user1",
278+
Password: "password1",
279+
expectedCredentialKeyName: "saml2aws_credentials_test",
280+
},
281+
{
282+
CaseName: "EmptyIdpNameRaisesError",
283+
IdpName: "",
284+
URL: "http://test.com/",
285+
Username: "user2",
286+
Password: "password2",
287+
expectedError: true,
288+
},
289+
}
290+
291+
for _, testCase := range testCases {
292+
t.Run(testCase.CaseName, func(t *testing.T) {
293+
m := NewMockHelper()
294+
CurrentHelper = m
295+
err := SaveCredentials(testCase.IdpName, testCase.URL, testCase.Username, testCase.Password)
296+
if testCase.expectedError {
297+
assert.NotNil(t, err)
298+
} else {
299+
assert.Nil(t, err)
300+
_, ok := m.Credentials[testCase.expectedCredentialKeyName]
301+
assert.True(t, ok)
302+
assert.EqualValues(t, testCase.Username, m.Credentials[testCase.expectedCredentialKeyName].Username)
303+
assert.EqualValues(t, testCase.Password, m.Credentials[testCase.expectedCredentialKeyName].Secret)
304+
}
305+
})
306+
}
307+
308+
// restoring the old Helper
309+
CurrentHelper = oldHelper
310+
}

0 commit comments

Comments
 (0)