summaryrefslogtreecommitdiff
path: root/aws/sts_test.go
blob: 485d86a728d0db6efdac1f8ace2d1b9fc750e3da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package aws

import (
	"errors"
	"testing"

	"github.com/aws/aws-sdk-go/aws"

	"github.com/aws/aws-sdk-go/service/sts"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewSTS(t *testing.T) {
	s := NewSTS(ClientOptions{})
	cid := &DummyCallerIdentifier{
		account: "acct",
		userID:  "uid",
		arn:     "arn",
	}
	s.identifier = func() CallerIdentitifier {
		return cid
	}

	out, err := s.getCallerID()
	require.NoError(t, err)
	assert.Equal(t, &sts.GetCallerIdentityOutput{
		Account: aws.String("acct"),
		Arn:     aws.String("arn"),
		UserId:  aws.String("uid"),
	}, out)

	assert.Equal(t, "acct", must(s.Account()))
	assert.Equal(t, "arn", must(s.Arn()))
	assert.Equal(t, "uid", must(s.UserID()))

	s = NewSTS(ClientOptions{})
	cid = &DummyCallerIdentifier{
		account: "acct",
		userID:  "uid",
		arn:     "arn",
	}
	oldIDClient := identifierClient
	identifierClient = cid
	defer func() { identifierClient = oldIDClient }()

	out, err = s.getCallerID()
	require.NoError(t, err)
	assert.Equal(t, &sts.GetCallerIdentityOutput{
		Account: aws.String("acct"),
		Arn:     aws.String("arn"),
		UserId:  aws.String("uid"),
	}, out)

	assert.Equal(t, "acct", must(s.Account()))
	assert.Equal(t, "arn", must(s.Arn()))
	assert.Equal(t, "uid", must(s.UserID()))
}

func TestGetCallerIDErrors(t *testing.T) {
	s := NewSTS(ClientOptions{})
	cid := &DummyCallerIdentifier{
		account: "acct",
		userID:  "uid",
		arn:     "arn",
	}
	s.identifier = func() CallerIdentitifier {
		return cid
	}

	out, err := s.Account()
	require.NoError(t, err)
	assert.Equal(t, "acct", out)

	s = NewSTS(ClientOptions{})
	cid = &DummyCallerIdentifier{
		err: errors.New("ERRORED"),
	}
	s.identifier = func() CallerIdentitifier {
		return cid
	}

	_, err = s.Account()
	require.EqualError(t, err, "ERRORED")
	_, err = s.UserID()
	require.EqualError(t, err, "ERRORED")
	_, err = s.Arn()
	require.EqualError(t, err, "ERRORED")
}

type DummyCallerIdentifier struct {
	err                  error
	account, arn, userID string
}

func (c *DummyCallerIdentifier) GetCallerIdentity(*sts.GetCallerIdentityInput) (*sts.GetCallerIdentityOutput, error) {
	if c.err != nil {
		return nil, c.err
	}

	out := &sts.GetCallerIdentityOutput{
		Account: aws.String(c.account),
		Arn:     aws.String(c.arn),
		UserId:  aws.String(c.userID),
	}
	return out, nil
}