-
Notifications
You must be signed in to change notification settings - Fork 67
/
check_request_parameter_pattern_added_or_changed_test.go
102 lines (92 loc) · 4.95 KB
/
check_request_parameter_pattern_added_or_changed_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
package checker_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/tufin/oasdiff/checker"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
)
// CL: changing pattern of request parameters
func TestRequestParameterPatternChanged(t *testing.T) {
s1, err := open("../data/checker/request_parameter_pattern_added_or_changed_base.yaml")
require.NoError(t, err)
s2, err := open("../data/checker/request_parameter_pattern_added_or_changed_base.yaml")
require.NoError(t, err)
s2.Spec.Paths.Value("/test").Post.Parameters[0].Value.Schema.Value.Pattern = "^[\\w\\s]+$"
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestParameterPatternAddedOrChangedCheck), d, osm, checker.WARN)
require.Len(t, errs, 1)
require.Equal(t, checker.ApiChange{
Id: checker.RequestParameterPatternChangedId,
Args: []any{"query", "category", "^\\w+$", "^[\\w\\s]+$"},
Comment: checker.PatternChangedCommentId,
Level: checker.WARN,
Operation: "POST",
Path: "/test",
Source: load.NewSource("../data/checker/request_parameter_pattern_added_or_changed_base.yaml"),
}, errs[0])
require.Equal(t, "changed the pattern of the 'query' request parameter 'category' from '^\\w+$' to '^[\\w\\s]+$'", errs[0].GetUncolorizedText(checker.NewDefaultLocalizer()))
require.Equal(t, "This is a warning because it is difficult to automatically analyze if the new pattern is a superset of the previous pattern (e.g. changed from '[0-9]+' to '[0-9]*')", errs[0].GetComment(checker.NewDefaultLocalizer()))
}
// CL: generalizing pattern of request parameters
func TestRequestParameterPatternGeneralized(t *testing.T) {
s1, err := open("../data/checker/request_parameter_pattern_added_or_changed_base.yaml")
require.NoError(t, err)
s2, err := open("../data/checker/request_parameter_pattern_added_or_changed_base.yaml")
require.NoError(t, err)
s2.Spec.Paths.Value("/test").Post.Parameters[0].Value.Schema.Value.Pattern = ".*"
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestParameterPatternAddedOrChangedCheck), d, osm, checker.INFO)
require.Len(t, errs, 1)
require.Equal(t, checker.ApiChange{
Id: checker.RequestParameterPatternGeneralizedId,
Args: []any{"query", "category", "^\\w+$", ".*"},
Level: checker.INFO,
Operation: "POST",
Path: "/test",
Source: load.NewSource("../data/checker/request_parameter_pattern_added_or_changed_base.yaml"),
}, errs[0])
require.Equal(t, "changed the pattern of the 'query' request parameter 'category' from '^\\w+$' to a more general pattern '.*'", errs[0].GetUncolorizedText(checker.NewDefaultLocalizer()))
}
// CL: adding pattern to request parameters
func TestRequestParameterPatternAdded(t *testing.T) {
s1, err := open("../data/checker/request_parameter_pattern_added_or_changed_revision.yaml")
require.NoError(t, err)
s2, err := open("../data/checker/request_parameter_pattern_added_or_changed_base.yaml")
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestParameterPatternAddedOrChangedCheck), d, osm, checker.WARN)
require.Len(t, errs, 1)
require.Equal(t, checker.ApiChange{
Id: checker.RequestParameterPatternAddedId,
Args: []any{"^\\w+$", "query", "category"},
Comment: checker.PatternChangedCommentId,
Level: checker.WARN,
Operation: "POST",
Path: "/test",
Source: load.NewSource("../data/checker/request_parameter_pattern_added_or_changed_base.yaml"),
}, errs[0])
require.Equal(t, "This is a warning because it is difficult to automatically analyze if the new pattern is a superset of the previous pattern (e.g. changed from '[0-9]+' to '[0-9]*')", errs[0].GetComment(checker.NewDefaultLocalizer()))
}
// CL: removing pattern from request parameters
func TestRequestParameterPatternRemoved(t *testing.T) {
s1, err := open("../data/checker/request_parameter_pattern_added_or_changed_base.yaml")
require.NoError(t, err)
s2, err := open("../data/checker/request_parameter_pattern_added_or_changed_revision.yaml")
require.NoError(t, err)
d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2)
require.NoError(t, err)
errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestParameterPatternAddedOrChangedCheck), d, osm, checker.INFO)
require.Len(t, errs, 1)
require.Equal(t, checker.ApiChange{
Id: checker.RequestParameterPatternRemovedId,
Args: []any{"^\\w+$", "query", "category"},
Level: checker.INFO,
Operation: "POST",
Path: "/test",
Source: load.NewSource("../data/checker/request_parameter_pattern_added_or_changed_revision.yaml"),
}, errs[0])
}