-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodels_test.go
117 lines (106 loc) · 2.47 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
package handler_test
import (
"encoding/json"
"reflect"
"strings"
"testing"
handler "github.com/telia-oss/concourse-github-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",
"repositories": [
{
"name": "repo1",
"owner": "telia-oss",
"readOnly": true
}
]
}
`),
expected: handler.Team{
Name: "team",
Repositories: []handler.Repository{
{
Name: "repo1",
Owner: "telia-oss",
ReadOnly: true,
},
},
},
},
}
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 got, want := output, tc.expected; !reflect.DeepEqual(got, want) {
t.Errorf("\ngot:\n%v\nwant:\n%v\n", got, want)
}
})
}
}
func TestTemplate(t *testing.T) {
tests := []struct {
description string
template string
team string
owner string
repository string
expected string
shouldError bool
}{
{
description: "template works as intended",
template: "/concourse/{{.Team}}/{{.Repository}}-deploy-key",
team: "TEAM",
owner: "OWNER",
repository: "REPOSITORY",
expected: "/concourse/TEAM/REPOSITORY-deploy-key",
shouldError: false,
},
{
description: "template supports owner",
template: "/concourse/{{.Team}}/{{.Owner}}-access-token",
team: "TEAM",
owner: "OWNER",
repository: "REPOSITORY",
expected: "/concourse/TEAM/OWNER-access-token",
shouldError: false,
},
{
description: "fails if the template expects more parameters",
template: "/concourse/{{.Team}}/{{.Repository}}/{{.Something}}",
team: "TEAM",
owner: "OWNER",
repository: "REPOSITORY",
expected: "",
shouldError: true,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
got, err := handler.NewTemplate(tc.team, tc.repository, tc.owner, 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)
}
})
}
}