Skip to content

Commit 75e7dd5

Browse files
authored
Merge pull request #505 from synfinatic/80-percent
Increase test coverage to 80%
2 parents 8f1fa0d + 1b6cc2d commit 75e7dd5

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

sso/cache_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ func (suite *CacheTestSuite) TestExpired() {
335335
assert.Error(t, suite.cache.Expired(&s))
336336
s.settings.CacheRefresh = 0
337337
assert.NoError(t, suite.cache.Expired(&s))
338+
339+
// invalid version
340+
c := &Cache{
341+
Version: 1, // invalid
342+
}
343+
err := c.Expired(&s)
344+
assert.Error(t, err)
338345
}
339346

340347
func (suite *CacheTestSuite) TestGetRole() {
@@ -560,3 +567,11 @@ func (suite *CacheTestSuite) TestPruneSSO() {
560567
assert.Contains(t, c.SSO, "Primary")
561568
assert.NotContains(t, c.SSO, "Invalid")
562569
}
570+
571+
func TestOpenCacheFailure(t *testing.T) {
572+
s := &Settings{}
573+
c, err := OpenCache("/dev/null", s)
574+
assert.Error(t, err)
575+
assert.Equal(t, int64(0), c.ConfigCreatedAt)
576+
assert.Equal(t, int64(1), c.Version)
577+
}

sso/config_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package sso
2+
3+
/*
4+
* AWS SSO CLI
5+
* Copyright (c) 2021-2022 Aaron Turner <synfinatic at gmail dot com>
6+
*
7+
* This program is free software: you can redistribute it
8+
* and/or modify it under the terms of the GNU General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or with the authors permission any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
import (
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestGetRoleMatches(t *testing.T) {
28+
accounts := map[string]*SSOAccount{
29+
"123456789012": {
30+
Tags: map[string]string{
31+
"Foo": "foo",
32+
"Bar": "bar",
33+
},
34+
Name: "MyAccount",
35+
},
36+
}
37+
roles := map[string]*SSORole{
38+
"FirstRole": {
39+
account: accounts["123456789012"],
40+
ARN: "arn:aws:iam::123456789012:role/FirstRole",
41+
Tags: map[string]string{
42+
"Hello": "There",
43+
},
44+
},
45+
"SecondRole": {
46+
account: accounts["123456789012"],
47+
ARN: "arn:aws:iam::123456789012:role/SecondRole",
48+
Tags: map[string]string{
49+
"Yes": "Please",
50+
},
51+
},
52+
}
53+
accounts["123456789012"].Roles = roles
54+
s := &SSOConfig{
55+
Accounts: accounts,
56+
}
57+
58+
none := map[string]string{
59+
"No": "Hits",
60+
}
61+
empty := s.GetRoleMatches(none)
62+
assert.Empty(t, empty)
63+
64+
twohits := map[string]string{
65+
"Foo": "foo",
66+
"Bar": "bar",
67+
}
68+
two := s.GetRoleMatches(twohits)
69+
assert.Equal(t, 2, len(two))
70+
71+
onehit := map[string]string{
72+
"Hello": "There",
73+
}
74+
one := s.GetRoleMatches(onehit)
75+
assert.Equal(t, 1, len(one))
76+
77+
yes := accounts["123456789012"].HasRole("arn:aws:iam::123456789012:role/FirstRole")
78+
assert.True(t, yes)
79+
80+
no := accounts["123456789012"].HasRole("arn:aws:iam::123456789012:role/MissingRole")
81+
assert.False(t, no)
82+
}

sso/options_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package sso
2+
3+
/*
4+
* AWS SSO CLI
5+
* Copyright (c) 2021-2022 Aaron Turner <synfinatic at gmail dot com>
6+
*
7+
* This program is free software: you can redistribute it
8+
* and/or modify it under the terms of the GNU General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or with the authors permission any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
import (
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/suite"
26+
)
27+
28+
func eChecker(in string, breakline bool) bool {
29+
return true
30+
}
31+
32+
type OptionsTestSuite struct {
33+
suite.Suite
34+
settings *Settings
35+
}
36+
37+
func TestOptionsTestSuite(t *testing.T) {
38+
over := OverrideSettings{}
39+
defaults := map[string]interface{}{}
40+
// defined in settings_test.go
41+
settings, err := LoadSettings(TEST_SETTINGS_FILE, TEST_CACHE_FILE, defaults, over)
42+
assert.Nil(t, err)
43+
44+
s := &OptionsTestSuite{
45+
settings: settings,
46+
}
47+
suite.Run(t, s)
48+
}
49+
50+
func (suite *OptionsTestSuite) TestDefaultOptions() {
51+
t := suite.T()
52+
s := suite.settings
53+
opts := s.DefaultOptions(eChecker)
54+
assert.Equal(t, 4, len(opts))
55+
}
56+
57+
func (suite *OptionsTestSuite) TestGetColorOptions() {
58+
t := suite.T()
59+
s := suite.settings
60+
opts := s.GetColorOptions()
61+
assert.Equal(t, 16, len(opts))
62+
}

sso/settings_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,10 @@ func TestApplyDeprecations(t *testing.T) {
356356
// AccountIdStr .AccountId => .AccountIdPad
357357
assert.Equal(t, "{{ .AccountIdPad }}:{{ .RoleName }}", s.ProfileFormat)
358358
}
359+
360+
func TestGetExecutable(t *testing.T) {
361+
path, err := getExecutable()
362+
assert.NoError(t, err)
363+
// can't test the NIX path really can we??
364+
assert.Contains(t, path, "sso.test")
365+
}

0 commit comments

Comments
 (0)