github.com/googleapis/api-linter@v1.65.2/rules/aip0135/request_required_fields_test.go (about)

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package aip0135
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/googleapis/api-linter/rules/internal/testutils"
    21  	"github.com/jhump/protoreflect/desc"
    22  )
    23  
    24  func TestRequiredFieldTests(t *testing.T) {
    25  	for _, test := range []struct {
    26  		name                 string
    27  		Fields               string
    28  		problematicFieldName string
    29  		problems             testutils.Problems
    30  	}{
    31  		{
    32  			"ValidNoExtraFields",
    33  			"",
    34  			"",
    35  			nil,
    36  		},
    37  		{
    38  			"ValidOptionalAllowMissing",
    39  			"bool allow_missing = 2 [(google.api.field_behavior) = OPTIONAL];",
    40  			"allow_missing",
    41  			nil,
    42  		},
    43  		{
    44  			"InvalidRequiredAllowMissing",
    45  			"bool allow_missing = 2 [(google.api.field_behavior) = REQUIRED];",
    46  			"allow_missing",
    47  			testutils.Problems{
    48  				{Message: `Delete RPCs must only require fields explicitly described in AIPs, not "allow_missing"`},
    49  			},
    50  		},
    51  		{
    52  			"InvalidRequiredUnknownField",
    53  			"bool create_iam = 2 [(google.api.field_behavior) = REQUIRED];",
    54  			"create_iam",
    55  			testutils.Problems{
    56  				{Message: `Delete RPCs must only require fields explicitly described in AIPs, not "create_iam"`},
    57  			},
    58  		},
    59  	} {
    60  		t.Run(test.name, func(t *testing.T) {
    61  			f := testutils.ParseProto3Tmpl(t, `
    62  				import "google/api/annotations.proto";
    63  				import "google/api/field_behavior.proto";
    64  				import "google/api/resource.proto";
    65  			    import "google/longrunning/operations.proto";
    66  
    67  				service Library {
    68  					rpc DeleteBook(DeleteBookRequest) returns (google.longrunning.Operation) {
    69  						option (google.api.http) = {
    70  						    delete: "/v1/{name=publishers/*/books/*}"
    71  						};
    72  						option (google.longrunning.operation_info) = {
    73  							response_type: "google.protobuf.Empty"
    74  							metadata_type: "OperationMetadata"
    75  						};
    76  					}
    77  				}
    78  
    79  				message DeleteBookRequest {
    80  					string name = 1 [
    81  						(google.api.field_behavior) = REQUIRED
    82  					];
    83  					{{.Fields}}
    84  				}
    85  			`, test)
    86  			var dbr desc.Descriptor = f.FindMessage("DeleteBookRequest")
    87  			if test.problematicFieldName != "" {
    88  				dbr = f.FindMessage("DeleteBookRequest").FindFieldByName(test.problematicFieldName)
    89  			}
    90  			if diff := test.problems.SetDescriptor(dbr).Diff(requestRequiredFields.Lint(f)); diff != "" {
    91  				t.Errorf(diff)
    92  			}
    93  		})
    94  	}
    95  }