-
Notifications
You must be signed in to change notification settings - Fork 84
/
notifications.go
280 lines (241 loc) · 12.6 KB
/
notifications.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package asserters
import (
"context"
"fmt"
"io"
"net/http"
"testing"
"github.com/kyma-incubator/compass/tests/pkg/certs"
"github.com/kyma-incubator/compass/tests/pkg/fixtures"
"github.com/machinebox/graphql"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/tidwall/sjson"
context_keys "github.com/kyma-incubator/compass/tests/pkg/notifications/context-keys"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)
const (
unassignOperation = "unassign"
assignOperation = "assign"
)
type NotificationsAsserter struct {
expectedNotificationsCount int
useItemsStruct bool
assertTrustDetails bool
expectedSubjects []string
op string
state *string
targetObjectID string
sourceObjectID string
localTenantID string
appNamespace string
region string
tenant string
tenantParentCustomer string
config *string
formationName string // used when the test operates with formation different from the one provided in pre setup
externalServicesMockMtlsSecuredURL string
certSecuredGraphQLClient *graphql.Client
client *http.Client
}
func NewNotificationsAsserter(expectedNotificationsCount int, op string, targetObjectID, sourceObjectID string, localTenantID string, appNamespace string, region string, tenant string, tenantParentCustomer string, config *string, externalServicesMockMtlsSecuredURL string, client *http.Client) *NotificationsAsserter {
return &NotificationsAsserter{
expectedNotificationsCount: expectedNotificationsCount,
op: op,
targetObjectID: targetObjectID,
sourceObjectID: sourceObjectID,
localTenantID: localTenantID,
appNamespace: appNamespace,
region: region,
tenant: tenant,
tenantParentCustomer: tenantParentCustomer,
config: config,
externalServicesMockMtlsSecuredURL: externalServicesMockMtlsSecuredURL,
client: client,
}
}
func (a *NotificationsAsserter) WithState(state string) *NotificationsAsserter {
a.state = &state
return a
}
func (a *NotificationsAsserter) WithFormationName(formationName string) *NotificationsAsserter {
a.formationName = formationName
return a
}
func (a *NotificationsAsserter) WithGQLClient(gqlClient *graphql.Client) *NotificationsAsserter {
a.certSecuredGraphQLClient = gqlClient
return a
}
func (a *NotificationsAsserter) WithUseItemsStruct(useItemsStruct bool) *NotificationsAsserter {
a.useItemsStruct = useItemsStruct
return a
}
func (a *NotificationsAsserter) WithAssertTrustDetails(assertTrustDetails bool) *NotificationsAsserter {
a.assertTrustDetails = assertTrustDetails
return a
}
func (a *NotificationsAsserter) WithExpectedSubjects(expectedSubjects []string) *NotificationsAsserter {
a.expectedSubjects = expectedSubjects
return a
}
func (a *NotificationsAsserter) AssertExpectations(t *testing.T, ctx context.Context) {
var formationID string
if a.formationName != "" {
formation := fixtures.GetFormationByName(t, ctx, a.certSecuredGraphQLClient, a.formationName, a.tenant)
formationID = formation.ID
} else {
formationID = ctx.Value(context_keys.FormationIDKey).(string)
}
body := getNotificationsFromExternalSvcMock(t, a.client, a.externalServicesMockMtlsSecuredURL)
assertNotificationsCount(t, body, a.targetObjectID, a.expectedNotificationsCount)
if a.expectedNotificationsCount == 0 {
return
}
notificationsForTarget := gjson.GetBytes(body, a.targetObjectID)
assignNotificationAboutSource := notificationsForTarget.Array()[0]
if a.useItemsStruct {
assertFormationAssignmentsNotificationWithConfigContainingItemsStructure(t, assignNotificationAboutSource, assignOperation, formationID, a.sourceObjectID, a.localTenantID, a.appNamespace, a.region, a.tenant, a.tenantParentCustomer, a.config)
if a.assertTrustDetails {
require.NotEmpty(t, a.expectedSubjects)
assertTrustDetailsForTargetAndNoTrustDetailsForSource(t, assignNotificationAboutSource, a.expectedSubjects)
}
} else {
err := verifyFormationAssignmentNotification(t, assignNotificationAboutSource, assignOperation, formationID, a.sourceObjectID, a.localTenantID, a.appNamespace, a.region, a.config, a.tenant, a.tenantParentCustomer, false, a.state)
require.NoError(t, err)
}
}
func getNotificationsFromExternalSvcMock(t *testing.T, client *http.Client, ExternalServicesMockMtlsSecuredURL string) []byte {
t.Logf("Getting formation notifications recieved in external services mock")
resp, err := client.Get(ExternalServicesMockMtlsSecuredURL + "/formation-callback")
defer func() {
if err := resp.Body.Close(); err != nil {
t.Logf("Could not close response body %s", err)
}
}()
require.NoError(t, err)
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode, fmt.Sprintf("actual status code %d is different from the expected one: %d. Reason: %v", resp.StatusCode, http.StatusOK, string(body)))
return body
}
func assertNotificationsCount(t *testing.T, body []byte, objectID string, count int) {
notifications := gjson.GetBytes(body, objectID)
if count > 0 {
require.True(t, notifications.Exists())
require.Len(t, notifications.Array(), count)
} else {
require.False(t, notifications.Exists())
}
}
// will be used once the tests that depend on the items structure are adapted to the new test format
func assertFormationAssignmentsNotificationWithConfigContainingItemsStructure(t *testing.T, notification gjson.Result, op, formationID, expectedAppID, expectedLocalTenantID, expectedAppNamespace, expectedAppRegion, expectedTenant, expectedCustomerID string, expectedConfig *string) {
require.Equal(t, op, notification.Get("Operation").String())
if op == unassignOperation {
require.Equal(t, expectedAppID, notification.Get("ApplicationID").String())
}
require.Equal(t, formationID, notification.Get("RequestBody.ucl-formation-id").String())
require.Equal(t, expectedTenant, notification.Get("RequestBody.globalAccountId").String())
require.Equal(t, expectedCustomerID, notification.Get("RequestBody.crmId").String())
notificationItems := notification.Get("RequestBody.items")
require.True(t, notificationItems.Exists())
require.Len(t, notificationItems.Array(), 1)
app1FromNotification := notificationItems.Array()[0]
require.Equal(t, expectedAppID, app1FromNotification.Get("ucl-system-tenant-id").String())
require.Equal(t, expectedLocalTenantID, app1FromNotification.Get("tenant-id").String())
require.Equal(t, expectedAppNamespace, app1FromNotification.Get("application-namespace").String())
require.Equal(t, expectedAppRegion, app1FromNotification.Get("region").String())
if expectedConfig != nil {
require.Equal(t, *expectedConfig, notification.Get("RequestBody.config").String())
}
}
func assertTrustDetailsForTargetAndNoTrustDetailsForSource(t *testing.T, assignNotificationAboutApp2 gjson.Result, expectedSubjects []string) {
t.Logf("Assert trust details are send to the target")
notificationItems := assignNotificationAboutApp2.Get("RequestBody.items")
app1FromNotification := notificationItems.Array()[0]
targetTrustDetails := app1FromNotification.Get("target-trust-details")
certificateDetails := targetTrustDetails.Array()[0].String()
certificateDetailsSecond := targetTrustDetails.Array()[1].String()
sortedSubjects := make([]string, 0, len(expectedSubjects))
for _, subject := range expectedSubjects {
sortedSubjects = append(sortedSubjects, certs.SortSubject(subject))
}
require.ElementsMatch(t, sortedSubjects, []string{certificateDetails, certificateDetailsSecond})
t.Logf("Assert that there are no trust details for the source")
sourceTrustDetails := app1FromNotification.Get("source-trust-details")
require.Equal(t, 0, len(sourceTrustDetails.Array()))
}
func verifyFormationAssignmentNotification(t *testing.T, notification gjson.Result, op, formationID, expectedObjectID, expectedAppLocalTenantID, expectedObjectNamespace, expectedObjectRegion string, expectedConfiguration *string, expectedTenant, expectedCustomerID string, shouldRemoveDestinationCertificateData bool, expectedState *string) error {
actualOp := notification.Get("Operation").String()
if op != actualOp {
return errors.Errorf("Operation does not match - expected: %q, but got: %q", op, actualOp)
}
if op == unassignOperation {
if actualObjectIDExists := notification.Get("ApplicationID").Exists(); !actualObjectIDExists {
return errors.New("ObjectID does not exist")
}
actualObjectID := notification.Get("ApplicationID").String()
if expectedObjectID != actualObjectID {
return errors.Errorf("ObjectID does not match - expected: %q, but got: %q", expectedObjectID, actualObjectID)
}
}
actualFormationID := notification.Get("RequestBody.context.uclFormationId").String()
if formationID != actualFormationID {
return errors.Errorf("RequestBody.context.uclFormationId does not match - expected: %q, but got: %q", formationID, actualFormationID)
}
actualTenantID := notification.Get("RequestBody.context.globalAccountId").String()
if expectedTenant != actualTenantID {
return errors.Errorf("RequestBody.context.globalAccountId does not match - expected: %q, but got: %q", expectedTenant, actualTenantID)
}
actualCustomerID := notification.Get("RequestBody.context.crmId").String()
if expectedCustomerID != actualCustomerID {
return errors.Errorf("RequestBody.context.crmId does not match - expected: %q, but got: %q", expectedCustomerID, actualCustomerID)
}
actualAppTenantID := notification.Get("RequestBody.receiverTenant.applicationTenantId").String()
if expectedAppLocalTenantID != actualAppTenantID {
return errors.Errorf("RequestBody.receiverTenant.applicationTenantId does not match - expected: %q, but got: %q", expectedAppLocalTenantID, actualAppTenantID)
}
actualObjectRegion := notification.Get("RequestBody.receiverTenant.deploymentRegion").String()
if expectedObjectRegion != actualObjectRegion {
return errors.Errorf("RequestBody.receiverTenant.deploymentRegion does not match - expected: %q, but got: %q", expectedObjectRegion, actualObjectRegion)
}
actualObjectNamespace := notification.Get("RequestBody.receiverTenant.applicationNamespace").String()
if expectedObjectNamespace != actualObjectNamespace {
return errors.Errorf("RequestBody.receiverTenant.applicationNamespace does not match - expected: %q, but got: %q", expectedObjectNamespace, actualObjectNamespace)
}
if expectedState != nil {
notificationReceiverState := notification.Get("RequestBody.receiverTenant.state").String()
if notificationReceiverState != *expectedState {
return errors.Errorf("RequestBody.receiverTenant.state does not match - expeted: %q, but got: %q", *expectedState, notificationReceiverState)
}
}
if shouldRemoveDestinationCertificateData {
notificationReceiverCfg := notification.Get("RequestBody.receiverTenant.configuration").String()
notificationReceiverState := notification.Get("RequestBody.receiverTenant.state").String()
if notificationReceiverCfg == "" && notificationReceiverState == "INITIAL" {
return nil
}
modifiedNotification, err := sjson.Delete(notification.String(), "RequestBody.receiverTenant.configuration.credentials.inboundCommunication.samlAssertion.certificate")
if err != nil {
return err
}
modifiedNotification, err = sjson.Delete(modifiedNotification, "RequestBody.receiverTenant.configuration.credentials.inboundCommunication.clientCertificateAuthentication.certificate")
if err != nil {
return err
}
modifiedNotification, err = sjson.Delete(modifiedNotification, "RequestBody.receiverTenant.configuration.credentials.inboundCommunication.samlAssertion.assertionIssuer")
if err != nil {
return err
}
modifiedConfig := gjson.Get(modifiedNotification, "RequestBody.receiverTenant.configuration").String()
if expectedConfiguration != nil {
assert.JSONEq(t, *expectedConfiguration, modifiedConfig, "RequestBody.receiverTenant.configuration does not match")
}
} else {
actualConfiguration := notification.Get("RequestBody.receiverTenant.configuration").String()
if expectedConfiguration != nil && *expectedConfiguration != "" && *expectedConfiguration != actualConfiguration {
return errors.Errorf("RequestBody.receiverTenant.configuration does not match - expected: %q, but got: %q", *expectedConfiguration, actualConfiguration)
}
}
return nil
}