-
Notifications
You must be signed in to change notification settings - Fork 11
/
models_test.go
120 lines (109 loc) · 2.39 KB
/
models_test.go
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
108
109
110
111
112
113
114
115
116
117
118
119
120
package handler_test
import (
"encoding/json"
"reflect"
"strings"
"testing"
handler "github.com/telia-oss/concourse-sts-lambda"
)
func TestConfig(t *testing.T) {
tests := []struct {
description string
input string
expected handler.Team
}{
{
description: "Unmarshal works as intended",
input: strings.TrimSpace(`
{
"name": "team",
"accounts": [{
"name": "account",
"roleArn": "role"
},
{
"name": "account2",
"roleArn": "role2",
"duration": 4000
}]
}
`),
expected: handler.Team{
Name: "team",
Accounts: []*handler.Account{
{
Name: "account",
RoleArn: "role",
Duration: 0,
},
{
Name: "account2",
RoleArn: "role2",
Duration: 4000,
},
},
},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
var output handler.Team
err := json.Unmarshal([]byte(tc.input), &output)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(output, tc.expected) {
got, err := json.Marshal(output)
if err != nil {
t.Fatalf("failed to marshal output: %s", err)
}
want, err := json.Marshal(tc.expected)
if err != nil {
t.Fatalf("failed to marshal expected: %s", err)
}
t.Errorf("\ngot:\n%s\nwant:\n%s\n", got, want)
}
})
}
}
func TestSecretPath(t *testing.T) {
tests := []struct {
description string
template string
team string
account string
expected string
shouldError bool
}{
{
description: "template works as intended",
template: "/concourse/{{.Team}}/{{.Account}}",
team: "TEAM",
account: "ACCOUNT",
expected: "/concourse/TEAM/ACCOUNT",
shouldError: false,
},
{
description: "fails if the template expects more parameters",
template: "/concourse/{{.Team}}/{{.Account}}/{{.Something}}",
team: "TEAM",
account: "ACCOUNT",
expected: "",
shouldError: true,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
got, err := handler.NewSecretPath(tc.team, tc.account, tc.template).String()
if tc.shouldError && err == nil {
t.Fatal("expected an error to occur")
}
if !tc.shouldError && err != nil {
t.Fatalf("unexpected error: %s", err)
}
if want := tc.expected; got != want {
t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want)
}
})
}
}