-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcheck_not_breaking_test.go
300 lines (245 loc) · 11.2 KB
/
check_not_breaking_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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package checker_test
import (
"testing"
"time"
"cloud.google.com/go/civil"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
)
func verifyNonBreakingChangeIsChangelogEntry(t *testing.T, d *diff.Diff, osm *diff.OperationsSourcesMap, changeId string) {
t.Helper()
// Check no breaking change is detected
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
// Check changelog captures the change
errs = checker.CheckBackwardCompatibilityUntilLevel(allChecksConfig(), d, osm, checker.INFO)
require.Len(t, errs, 1)
require.Equal(t, checker.INFO, errs[0].GetLevel())
require.Equal(t, changeId, errs[0].GetId())
}
// BC: no change is not breaking
func TestBreaking_Same(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
}
// BC: adding an optional request body is not breaking
func TestBreaking_AddingOptionalRequestBody(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
s2.Spec.Paths.Value(installCommandPath).Get.RequestBody = &openapi3.RequestBodyRef{
Value: openapi3.NewRequestBody().WithRequired(false),
}
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
}
// CL: changing an existing request body from required to optional
func TestBreaking_RequestBodyRequiredDisabled(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
s1.Spec.Paths.Value(installCommandPath).Get.RequestBody = &openapi3.RequestBodyRef{
Value: openapi3.NewRequestBody().WithRequired(true),
}
s2.Spec.Paths.Value(installCommandPath).Get.RequestBody = &openapi3.RequestBodyRef{
Value: openapi3.NewRequestBody().WithRequired(false),
}
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
verifyNonBreakingChangeIsChangelogEntry(t, d, osm, checker.RequestBodyBecameOptionalId)
}
// BC: deleting a tag is not breaking
func TestBreaking_DeletedTag(t *testing.T) {
r := d(t, diff.NewConfig(), 1, 5)
require.Len(t, r, 5)
require.Equal(t, checker.ResponseSuccessStatusRemovedId, r[0].GetId())
require.Equal(t, checker.APIPathRemovedWithoutDeprecationId, r[1].GetId())
require.Equal(t, checker.APIPathRemovedWithoutDeprecationId, r[2].GetId())
require.Equal(t, checker.OptionalResponseHeaderRemovedId, r[3].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[4].GetId())
}
// BC: adding an enum value is not breaking
func TestBreaking_AddedEnum(t *testing.T) {
r := d(t, diff.NewConfig(), 1, 3)
require.Len(t, r, 6)
require.Equal(t, checker.ResponseSuccessStatusRemovedId, r[0].GetId())
require.Equal(t, checker.ResponseSuccessStatusRemovedId, r[1].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[2].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[3].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[4].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[5].GetId())
}
// BC: changing extensions is not breaking
func TestBreaking_ModifiedExtension(t *testing.T) {
r := d(t, diff.NewConfig(), 1, 3)
require.Len(t, r, 6)
require.Equal(t, checker.ResponseSuccessStatusRemovedId, r[0].GetId())
require.Equal(t, checker.ResponseSuccessStatusRemovedId, r[1].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[2].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[3].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[4].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[5].GetId())
}
// BC: changing comments is not breaking
func TestBreaking_Comments(t *testing.T) {
r := d(t, diff.NewConfig(), 1, 3)
require.Len(t, r, 6)
require.Equal(t, checker.ResponseSuccessStatusRemovedId, r[0].GetId())
require.Equal(t, checker.ResponseSuccessStatusRemovedId, r[1].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[2].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[3].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[4].GetId())
require.Equal(t, checker.RequestParameterRemovedId, r[5].GetId())
}
// BC: new optional header param is not breaking
func TestBreaking_NewOptionalHeaderParam(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
deleteParam(s1.Spec.Paths.Value(installCommandPath).Get, openapi3.ParameterInHeader, "network-policies")
s2.Spec.Paths.Value(installCommandPath).Get.Parameters.GetByInAndName(openapi3.ParameterInHeader, "network-policies").Required = false
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
}
// CL: changing an existing header param to optional
func TestBreaking_HeaderParamRequiredDisabled(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
s1.Spec.Paths.Value(installCommandPath).Get.Parameters.GetByInAndName(openapi3.ParameterInHeader, "network-policies").Required = true
s2.Spec.Paths.Value(installCommandPath).Get.Parameters.GetByInAndName(openapi3.ParameterInHeader, "network-policies").Required = false
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
changes := checker.CheckBackwardCompatibilityUntilLevel(allChecksConfig(), d, osm, checker.INFO)
require.NotEmpty(t, changes)
require.Equal(t, checker.RequestParameterBecomeOptionalId, changes[0].GetId())
require.Len(t, changes, 1)
}
func deleteResponseHeader(response *openapi3.Response, name string) {
delete(response.Headers, name)
}
// BC: new required response header param is not breaking
func TestBreaking_NewRequiredResponseHeader(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
deleteResponseHeader(s1.Spec.Paths.Value(installCommandPath).Get.Responses.Value("default").Value, "X-RateLimit-Limit")
s2.Spec.Paths.Value(installCommandPath).Get.Responses.Value("default").Value.Headers["X-RateLimit-Limit"].Value.Required = true
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
}
// BC: changing operation ID is not breaking
func TestBreaking_OperationID(t *testing.T) {
r := d(t, diff.NewConfig(), 3, 1)
require.Len(t, r, 3)
require.Equal(t, checker.RequestParameterMaxLengthDecreasedId, r[0].GetId())
require.Equal(t, checker.RequestParameterEnumValueRemovedId, r[1].GetId())
require.Equal(t, checker.RequestParameterPatternAddedId, r[2].GetId())
}
// BC: changing a link to operation ID is not breaking
func TestBreaking_LinkOperationID(t *testing.T) {
r := d(t, diff.NewConfig(), 3, 1)
require.Len(t, r, 3)
require.Equal(t, checker.RequestParameterMaxLengthDecreasedId, r[0].GetId())
require.Equal(t, checker.RequestParameterEnumValueRemovedId, r[1].GetId())
require.Equal(t, checker.RequestParameterPatternAddedId, r[2].GetId())
}
// BC: adding a media-type to response is not breaking
func TestBreaking_ResponseAddMediaType(t *testing.T) {
s1, err := open("../data/response-media-type-revision.yaml")
require.NoError(t, err)
s2, err := open("../data/response-media-type-base.yaml")
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
}
// CL: deprecating an operation with sunset greater than min
func TestBreaking_DeprecatedOperation(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
s2.Spec.Paths.Value(installCommandPath).Get.Deprecated = true
s2.Spec.Paths.Value(installCommandPath).Get.Extensions = map[string]interface{}{diff.SunsetExtension: toJson(t, civil.DateOf(time.Now()).AddDays(180).String())}
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(allChecksConfig(), d, osm, checker.INFO)
require.Len(t, errs, 1)
require.Equal(t, errs[0].GetLevel(), checker.INFO)
}
// BC: deprecating a parameter is not breaking
func TestBreaking_DeprecatedParameter(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
s2.Spec.Paths.Value(installCommandPath).Get.Parameters.GetByInAndName(openapi3.ParameterInHeader, "network-policies").Deprecated = true
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
}
// BC: deprecating a header is not breaking
func TestBreaking_DeprecatedHeader(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
s2.Spec.Paths.Value(installCommandPath).Get.Responses.Value("default").Value.Headers["X-RateLimit-Limit"].Value.Deprecated = true
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
}
// BC: deprecating a schema is not breaking
func TestBreaking_DeprecatedSchema(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
s2.Spec.Paths.Value(installCommandPath).Get.Parameters.GetByInAndName(openapi3.ParameterInHeader, "network-policies").Schema.Value.Deprecated = true
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
}
// BC: changing servers is not breaking
func TestBreaking_Servers(t *testing.T) {
s1, err := open("../data/servers/baseswagger.json")
require.NoError(t, err)
s2, err := open("../data/servers/revisionswagger.json")
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibility(allChecksConfig(), d, osm)
require.Empty(t, errs)
}
// BC: adding a tag is not breaking
func TestBreaking_TagAdded(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
s2.Spec.Paths.Value(securityScorePath).Get.Tags = append(s2.Spec.Paths.Value(securityScorePath).Get.Tags, "newTag")
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
verifyNonBreakingChangeIsChangelogEntry(t, d, osm, checker.APITagAddedId)
}
// BC: adding an operation ID is not breaking
func TestBreaking_OperationIdAdded(t *testing.T) {
s1 := l(t, 1)
s2 := l(t, 1)
s1.Spec.Paths.Value(securityScorePath).Get.OperationID = ""
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
verifyNonBreakingChangeIsChangelogEntry(t, d, osm, checker.APIOperationIdAddId)
}
// BC: adding a required property to response is not breaking
func TestBreaking_RequiredResponsePropertyAdded(t *testing.T) {
s1, err := open("../data/checker/response_required_property_added_base.yaml")
require.NoError(t, err)
s2, err := open("../data/checker/response_required_property_added_revision.yaml")
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
verifyNonBreakingChangeIsChangelogEntry(t, d, osm, "response-required-property-added")
}