github.com/googleapis/api-linter@v1.65.2/rules/aip0133/request_unknown_fields_test.go (about) 1 // Copyright 2019 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 aip0133 16 17 import ( 18 "testing" 19 20 "github.com/googleapis/api-linter/rules/internal/testutils" 21 "github.com/jhump/protoreflect/desc" 22 "github.com/jhump/protoreflect/desc/builder" 23 ) 24 25 type field struct { 26 fieldName string 27 fieldType *builder.FieldType 28 } 29 30 func TestUnknownFields(t *testing.T) { 31 // Set up the testing permutations. 32 tests := []struct { 33 testName string 34 messageName string 35 messageFields []field 36 problems testutils.Problems 37 problemDesc func(m *desc.MessageDescriptor) desc.Descriptor 38 }{ 39 { 40 "Parent", 41 "CreateBookRequest", 42 []field{{"parent", builder.FieldTypeString()}}, 43 testutils.Problems{}, 44 nil, 45 }, 46 { 47 "ValidateOnly", 48 "CreateBookRequest", 49 []field{{"parent", builder.FieldTypeString()}, {"validate_only", builder.FieldTypeBool()}}, 50 testutils.Problems{}, 51 nil, 52 }, 53 { 54 "ResourceRelatedField", 55 "CreateBookRequest", 56 []field{ 57 {"book", builder.FieldTypeMessage(builder.NewMessage("Book"))}, 58 {"book_id", builder.FieldTypeString()}, 59 }, 60 testutils.Problems{}, 61 nil, 62 }, 63 { 64 "ResourceRelatedField", 65 "CreateBookStoreRequest", 66 []field{{"book_store_id", builder.FieldTypeString()}}, 67 testutils.Problems{}, 68 nil, 69 }, 70 { 71 "RequestIdField", 72 "CreateBookRequest", 73 []field{{"request_id", builder.FieldTypeString()}}, 74 testutils.Problems{}, 75 nil, 76 }, 77 { 78 "Invalid", 79 "CreateBookRequest", 80 []field{{"name", builder.FieldTypeString()}}, 81 testutils.Problems{{Message: "Create RPCs must only contain fields explicitly described in AIPs, not \"name\"."}}, 82 func(m *desc.MessageDescriptor) desc.Descriptor { 83 return m.FindFieldByName("name") 84 }, 85 }, 86 { 87 "InvalidResourceRelatedField", 88 "CreateBookStoreRequest", 89 []field{{"book_id", builder.FieldTypeString()}}, 90 testutils.Problems{{Message: "Create RPCs must only contain fields explicitly described in AIPs, not \"book_id\"."}}, 91 func(m *desc.MessageDescriptor) desc.Descriptor { 92 return m.FindFieldByName("book_id") 93 }, 94 }, 95 { 96 "Irrelevant", 97 "GetBookRequest", 98 []field{{"name", builder.FieldTypeString()}}, 99 testutils.Problems{}, 100 nil, 101 }, 102 } 103 104 // Run each test individually. 105 for _, test := range tests { 106 t.Run(test.testName, func(t *testing.T) { 107 // Create an appropriate message descriptor. 108 msgBuilder := builder.NewMessage(test.messageName) 109 for _, messageField := range test.messageFields { 110 msgBuilder.AddField( 111 builder.NewField(messageField.fieldName, messageField.fieldType), 112 ) 113 } 114 message, err := msgBuilder.Build() 115 if err != nil { 116 t.Fatalf("Could not build GetBookRequest message.") 117 } 118 119 // Determine the descriptor that a failing test will attach to. 120 var problemDesc desc.Descriptor = message 121 if test.problemDesc != nil { 122 problemDesc = test.problemDesc(message) 123 } 124 125 // Run the lint rule, and establish that it returns the correct problems. 126 problems := unknownFields.Lint(message.GetFile()) 127 if diff := test.problems.SetDescriptor(problemDesc).Diff(problems); diff != "" { 128 t.Errorf(diff) 129 } 130 }) 131 } 132 }