From f22314d554ae428889c72961c6ec692cb6eb4066 Mon Sep 17 00:00:00 2001 From: Reuven Harrison Date: Mon, 25 Nov 2024 21:52:06 +0200 Subject: [PATCH] add unit tests --- checker/api_change_test.go | 2 +- ...equest_body_required_value_updated_test.go | 41 ++++++++++--- .../request_body_became_optional_deleted.yaml | 59 ++++++++++++++++++ ...request_body_became_optional_revision.yaml | 60 +++++++++++++++++++ docs/BREAKING-CHANGES-EXAMPLES.md | 1 + 5 files changed, 153 insertions(+), 10 deletions(-) create mode 100644 data/checker/request_body_became_optional_deleted.yaml create mode 100644 data/checker/request_body_became_optional_revision.yaml diff --git a/checker/api_change_test.go b/checker/api_change_test.go index 5a4380c6..49078a05 100644 --- a/checker/api_change_test.go +++ b/checker/api_change_test.go @@ -85,7 +85,7 @@ func TestApiChange_SourceUrl(t *testing.T) { require.Equal(t, "", apiChangeSourceFile.GetSourceFile()) } -func TestApiChange_WithOrigin(t *testing.T) { +func TestApiChange_WithLocation(t *testing.T) { apiChangeSourceFile := apiChange.WithLocation(&openapi3.Origin{ Fields: map[string]openapi3.Location{"field": { Line: 1, diff --git a/checker/check_request_body_required_value_updated_test.go b/checker/check_request_body_required_value_updated_test.go index 2f5d8805..a09b1ed8 100644 --- a/checker/check_request_body_required_value_updated_test.go +++ b/checker/check_request_body_required_value_updated_test.go @@ -34,23 +34,46 @@ func TestRequestBodyBecameRequired(t *testing.T) { // CL: changing request's body to optional func TestRequestBodyBecameOptional(t *testing.T) { - s1, err := open("../data/checker/request_body_became_optional_base.yaml") + s1, err := openWithLocation("../data/checker/request_body_became_optional_base.yaml") require.NoError(t, err) - s2, err := open("../data/checker/request_body_became_optional_base.yaml") + s2, err := openWithLocation("../data/checker/request_body_became_optional_revision.yaml") require.NoError(t, err) - s2.Spec.Paths.Value("/api/v1.0/groups").Post.RequestBody.Value.Required = false + d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2) + require.NoError(t, err) + errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestBodyRequiredUpdatedCheck), d, osm, checker.INFO) + require.Len(t, errs, 1) + require.Equal(t, checker.ApiChange{ + Id: checker.RequestBodyBecameOptionalId, + Level: checker.INFO, + Operation: "POST", + Path: "/api/v1.0/groups", + Source: load.NewSource("../data/checker/request_body_became_optional_revision.yaml"), + OperationId: "createOneGroup", + SourceLine: 19, + SourceColumn: 9, + }, errs[0]) +} + +// CL: changing request's body to optional by deletion +func TestRequestBodyBecameOptionalDeleted(t *testing.T) { + s1, err := openWithLocation("../data/checker/request_body_became_optional_base.yaml") + require.NoError(t, err) + s2, err := openWithLocation("../data/checker/request_body_became_optional_deleted.yaml") + require.NoError(t, err) d, osm, err := diff.GetWithOperationsSourcesMap(diff.NewConfig(), s1, s2) require.NoError(t, err) errs := checker.CheckBackwardCompatibilityUntilLevel(singleCheckConfig(checker.RequestBodyRequiredUpdatedCheck), d, osm, checker.INFO) require.Len(t, errs, 1) require.Equal(t, checker.ApiChange{ - Id: checker.RequestBodyBecameOptionalId, - Level: checker.INFO, - Operation: "POST", - Path: "/api/v1.0/groups", - Source: load.NewSource("../data/checker/request_body_became_optional_base.yaml"), - OperationId: "createOneGroup", + Id: checker.RequestBodyBecameOptionalId, + Level: checker.INFO, + Operation: "POST", + Path: "/api/v1.0/groups", + Source: load.NewSource("../data/checker/request_body_became_optional_deleted.yaml"), + OperationId: "createOneGroup", + SourceLine: 9, + SourceColumn: 5, }, errs[0]) } diff --git a/data/checker/request_body_became_optional_deleted.yaml b/data/checker/request_body_became_optional_deleted.yaml new file mode 100644 index 00000000..42044a0b --- /dev/null +++ b/data/checker/request_body_became_optional_deleted.yaml @@ -0,0 +1,59 @@ +openapi: 3.0.1 +info: + title: Tufin + version: "2.0" +servers: +- url: https://localhost:9080 +paths: + /api/v1.0/groups: + post: + tags: + - Group + operationId: createOneGroup + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/GroupView' + description: Creates one project. + responses: + "200": + content: + application/json: + schema: + $ref: '#/components/schemas/GroupView' + description: OK + "409": + content: + application/json: + schema: + $ref: '#/components/schemas/GroupView' + description: Conflict + summary: Create One Project +components: + parameters: + groupId: + in: path + name: groupId + required: true + schema: + type: string + schemas: + GroupView: + type: object + properties: + data: + type: object + properties: + created: + type: string + format: date-time + readOnly: true + pattern: "^[a-z]+$" + id: + type: string + readOnly: true + name: + type: string + required: + - name \ No newline at end of file diff --git a/data/checker/request_body_became_optional_revision.yaml b/data/checker/request_body_became_optional_revision.yaml new file mode 100644 index 00000000..5dddd5ed --- /dev/null +++ b/data/checker/request_body_became_optional_revision.yaml @@ -0,0 +1,60 @@ +openapi: 3.0.1 +info: + title: Tufin + version: "2.0" +servers: +- url: https://localhost:9080 +paths: + /api/v1.0/groups: + post: + tags: + - Group + operationId: createOneGroup + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/GroupView' + description: Creates one project. + required: false + responses: + "200": + content: + application/json: + schema: + $ref: '#/components/schemas/GroupView' + description: OK + "409": + content: + application/json: + schema: + $ref: '#/components/schemas/GroupView' + description: Conflict + summary: Create One Project +components: + parameters: + groupId: + in: path + name: groupId + required: true + schema: + type: string + schemas: + GroupView: + type: object + properties: + data: + type: object + properties: + created: + type: string + format: date-time + readOnly: true + pattern: "^[a-z]+$" + id: + type: string + readOnly: true + name: + type: string + required: + - name \ No newline at end of file diff --git a/docs/BREAKING-CHANGES-EXAMPLES.md b/docs/BREAKING-CHANGES-EXAMPLES.md index bf0be386..5afe33f2 100644 --- a/docs/BREAKING-CHANGES-EXAMPLES.md +++ b/docs/BREAKING-CHANGES-EXAMPLES.md @@ -251,6 +251,7 @@ These examples are automatically generated from unit tests. [changing request property type](../checker/check_request_property_type_changed_test.go?plain=1#L64) [changing request query parameter format](../checker/check_request_parameters_type_changed_test.go?plain=1#L110) [changing request query parameter type](../checker/check_request_parameters_type_changed_test.go?plain=1#L38) +[changing request's body to optional by deletion](../checker/check_request_body_required_value_updated_test.go?plain=1#L58) [changing request's body to optional](../checker/check_request_body_required_value_updated_test.go?plain=1#L35) [changing request's body to required is breaking](../checker/check_request_body_required_value_updated_test.go?plain=1#L12) [changing required request property to not read-only](../checker/check_request_property_write_only_read_only_test.go?plain=1#L187)