-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathinternal_handlers_test.go
214 lines (175 loc) · 5.34 KB
/
internal_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"encoding/json"
"net/http"
"strings"
"testing"
"github.com/RedHatInsights/sources-api-go/dao"
"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"
h "github.com/RedHatInsights/sources-api-go/middleware/headers"
m "github.com/RedHatInsights/sources-api-go/model"
"github.com/RedHatInsights/sources-api-go/util"
)
func TestSourceListInternal(t *testing.T) {
testutils.SkipIfNotRunningIntegrationTests(t)
c, rec := request.CreateTestContext(
http.MethodGet,
"/internal/v2.0/sources",
nil,
map[string]interface{}{
"limit": 100,
"offset": 0,
"filters": []util.Filter{},
})
err := InternalSourceList(c)
if err != nil {
t.Error(err)
}
if rec.Code != http.StatusOK {
t.Error("Did not return 200")
}
var out util.Collection
err = json.Unmarshal(rec.Body.Bytes(), &out)
if err != nil {
t.Error("Failed unmarshalling 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.TestSourceData) {
t.Errorf("not enough objects passed back from DB, want %d, got %d", len(fixtures.TestSourceData), len(out.Data))
}
for _, src := range out.Data {
s, ok := src.(map[string]interface{})
if !ok {
t.Error("model did not deserialize as a source")
}
// Parse the source
responseSourceId, err := util.InterfaceToInt64(s["id"])
if err != nil {
t.Errorf("could not parse id from response: %s", err)
}
responseExternalTenant := s["tenant"].(string)
responseOrgId := s["org_id"].(string)
responseAvailabilityStatus := s["availability_status"].(string)
// Check that the expected source data and the received data are the same
wantTenant := fixtures.TestTenantData[0]
if wantTenant.ExternalTenant != responseExternalTenant {
t.Errorf("Tenants don't match. Want %#v, got %#v", wantTenant.ExternalTenant, responseExternalTenant)
}
if wantTenant.OrgID != responseOrgId {
t.Errorf("Org Ids don't match. Want %#v, got %#v", wantTenant.OrgID, responseOrgId)
}
sourceInFixtures := false
for _, source := range fixtures.TestSourceData {
if source.ID == responseSourceId {
if source.AvailabilityStatus != responseAvailabilityStatus {
t.Errorf("Availability statuses don't match. Want %s, got %s", source.AvailabilityStatus, responseAvailabilityStatus)
}
sourceInFixtures = true
break
}
}
if !sourceInFixtures {
t.Errorf("Source ID %d not found in fixtures", responseSourceId)
}
}
testutils.AssertLinks(t, c.Request().RequestURI, out.Links, 100, 0)
}
func TestSourceListInternalBadRequestInvalidFilter(t *testing.T) {
testutils.SkipIfNotRunningIntegrationTests(t)
c, rec := request.CreateTestContext(
http.MethodGet,
"/internal/v2.0/sources",
nil,
map[string]interface{}{
"limit": 100,
"offset": 0,
"filters": []util.Filter{
{Name: "wrongName", Value: []string{"wrongValue"}},
},
},
)
badRequestInternalSourceList := ErrorHandlingContext(InternalSourceList)
err := badRequestInternalSourceList(c)
if err != nil {
t.Error(err)
}
templates.BadRequestTest(t, rec)
}
func TestInternalSecretGet(t *testing.T) {
testutils.SkipIfNotRunningIntegrationTests(t)
// Set the encryption key
util.OverrideEncryptionKey(strings.Repeat("test", 8))
tenantIDForSecret := int64(1)
testUserId := "testUser"
userDao := dao.GetUserDao(&tenantIDForSecret)
user, err := userDao.FindOrCreate(testUserId)
if err != nil {
t.Error(err)
}
encryptedPassword, err := util.Encrypt("password")
if err != nil {
t.Error(err)
}
var userID *int64
for _, userScoped := range []bool{false, true} {
if userScoped {
userID = &user.Id
}
secret, err := dao.CreateSecretByName("Secret 1", &tenantIDForSecret, userID)
if err != nil {
t.Error(err)
}
secretID, err := util.InterfaceToString(secret.DbID)
if err != nil {
t.Error(err)
}
c, rec := request.CreateTestContext(
http.MethodGet,
"/api/internal/v2.0/secrets/"+secretID,
nil,
map[string]interface{}{
"tenantID": tenantIDForSecret,
},
)
c.SetParamNames("id")
c.SetParamValues(secretID)
if userID != nil {
c.Set(h.UserID, user.Id)
}
err = InternalSecretGet(c)
if err != nil {
t.Error(err)
}
if rec.Code != http.StatusOK {
t.Error("Did not return 200")
}
var outSecret m.SecretResponse
err = json.Unmarshal(rec.Body.Bytes(), &outSecret)
if err != nil {
t.Error("Failed unmarshaling output")
}
if outSecret.ID != secretID {
t.Errorf(`wrong secret fetched. Want "%s", got "%s"`, secretID, outSecret.ID)
}
secretDao := dao.GetSecretDao(&dao.RequestParams{TenantID: &tenantIDForSecret, UserID: userID})
secret, err = secretDao.GetById(&secret.DbID)
if err != nil {
t.Error("secret not found")
}
if *secret.Password != encryptedPassword {
t.Errorf("expected password %v but got %v", encryptedPassword, *secret.Password)
}
if userScoped && secret.UserID == nil || !userScoped && secret.UserID != nil {
t.Error("user id has to be nil as user ownership was not requested for secret")
}
cleanSecretByID(t, secretID, &dao.RequestParams{TenantID: &tenantIDForSecret, UserID: userID})
}
}