-
Notifications
You must be signed in to change notification settings - Fork 29
/
source_type_handlers_test.go
164 lines (133 loc) · 3.3 KB
/
source_type_handlers_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"encoding/json"
"net/http"
"testing"
"github.com/RedHatInsights/sources-api-go/internal/testutils"
"github.com/RedHatInsights/sources-api-go/internal/testutils/fixtures"
"github.com/RedHatInsights/sources-api-go/internal/testutils/request"
"github.com/RedHatInsights/sources-api-go/internal/testutils/templates"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
)
func TestSourceTypeList(t *testing.T) {
c, rec := request.CreateTestContext(
http.MethodGet,
"/api/sources/v3.1/source_types",
nil,
map[string]interface{}{
"limit": 100,
"offset": 0,
"filters": []util.Filter{},
},
)
err := SourceTypeList(c)
if err != nil {
t.Error(err)
}
if rec.Code != 200 {
t.Error("Did not return 200")
}
var out util.Collection
err = json.Unmarshal(rec.Body.Bytes(), &out)
if err != nil {
t.Error("Failed unmarshaling output")
}
if out.Meta.Limit != 100 {
t.Error("limit not set correctly")
}
if out.Meta.Offset != 0 {
t.Error("offset not set correctly")
}
if len(out.Data) != len(fixtures.TestSourceTypeData) {
t.Error("not enough objects passed back from DB")
}
for _, sourceType := range out.Data {
s, ok := sourceType.(map[string]interface{})
if !ok {
t.Error("model did not deserialize as a application type response")
}
if s["id"] == 1 && s["name"] != "amazon" {
t.Error("ghosts infected the return")
}
}
testutils.AssertLinks(t, c.Request().RequestURI, out.Links, 100, 0)
}
func TestSourceTypeListBadRequestInvalidFilter(t *testing.T) {
testutils.SkipIfNotRunningIntegrationTests(t)
c, rec := request.CreateTestContext(
http.MethodGet,
"/api/sources/v3.1/source_types",
nil,
map[string]interface{}{
"limit": 100,
"offset": 0,
"filters": []util.Filter{
{Name: "wrongName", Value: []string{"wrongValue"}},
},
},
)
badRequestSourceTypeList := ErrorHandlingContext(SourceTypeList)
err := badRequestSourceTypeList(c)
if err != nil {
t.Error(err)
}
templates.BadRequestTest(t, rec)
}
func TestSourceTypeGet(t *testing.T) {
c, rec := request.CreateTestContext(
http.MethodGet,
"/api/sources/v3.1/source_types/1",
nil,
nil,
)
c.SetParamNames("id")
c.SetParamValues("1")
err := SourceTypeGet(c)
if err != nil {
t.Error(err)
}
if rec.Code != 200 {
t.Error("Did not return 200")
}
var outSrc m.SourceResponse
err = json.Unmarshal(rec.Body.Bytes(), &outSrc)
if err != nil {
t.Error("Failed unmarshaling output")
}
if *outSrc.Name != "amazon" {
t.Error("ghosts infected the return")
}
}
func TestSourceTypeGetNotFound(t *testing.T) {
c, rec := request.CreateTestContext(
http.MethodGet,
"/api/sources/v3.1/source_types/3098539345",
nil,
nil,
)
c.SetParamNames("id")
c.SetParamValues("3098539345")
notFoundSourceTypeGet := ErrorHandlingContext(SourceTypeGet)
err := notFoundSourceTypeGet(c)
if err != nil {
t.Error(err)
}
templates.NotFoundTest(t, rec)
}
func TestSourceTypeGetBadRequest(t *testing.T) {
c, rec := request.CreateTestContext(
http.MethodGet,
"/api/sources/v3.1/source_types/xxx",
nil,
nil,
)
c.SetParamNames("id")
c.SetParamValues("xxx")
notFoundSourceTypeGet := ErrorHandlingContext(SourceTypeGet)
err := notFoundSourceTypeGet(c)
if err != nil {
t.Error(err)
}
templates.BadRequestTest(t, rec)
}