|
| 1 | +// Copyright 2021 The Serverless Workflow Specification Authors |
| 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 | +// http://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 model |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + |
| 22 | + val "github.com/serverlessworkflow/sdk-go/v2/validator" |
| 23 | +) |
| 24 | + |
| 25 | +func TestAuthDefinitionsStructLevelValidation(t *testing.T) { |
| 26 | + type testCase struct { |
| 27 | + desp string |
| 28 | + authDefs AuthDefinitions |
| 29 | + err string |
| 30 | + } |
| 31 | + testCases := []testCase{ |
| 32 | + { |
| 33 | + desp: "nil defs", |
| 34 | + authDefs: AuthDefinitions{ |
| 35 | + Defs: nil, |
| 36 | + }, |
| 37 | + err: ``, |
| 38 | + }, |
| 39 | + { |
| 40 | + desp: "zero length defs", |
| 41 | + authDefs: AuthDefinitions{ |
| 42 | + Defs: []Auth{}, |
| 43 | + }, |
| 44 | + err: ``, |
| 45 | + }, |
| 46 | + { |
| 47 | + desp: "multi unique defs", |
| 48 | + authDefs: AuthDefinitions{ |
| 49 | + Defs: []Auth{ |
| 50 | + { |
| 51 | + Name: "1", |
| 52 | + }, |
| 53 | + { |
| 54 | + Name: "2", |
| 55 | + }, |
| 56 | + { |
| 57 | + Name: "3", |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + err: ``, |
| 62 | + }, |
| 63 | + { |
| 64 | + desp: "multi non-unique defs", |
| 65 | + authDefs: AuthDefinitions{ |
| 66 | + Defs: []Auth{ |
| 67 | + { |
| 68 | + Name: "1", |
| 69 | + }, |
| 70 | + { |
| 71 | + Name: "2", |
| 72 | + }, |
| 73 | + { |
| 74 | + Name: "1", |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + err: `Key: 'AuthDefinitions.Name' Error:Field validation for 'Name' failed on the 'reqnameunique' tag`, |
| 79 | + }, |
| 80 | + } |
| 81 | + for _, tc := range testCases { |
| 82 | + t.Run(tc.desp, func(t *testing.T) { |
| 83 | + err := val.GetValidator().Struct(tc.authDefs) |
| 84 | + |
| 85 | + if tc.err != "" { |
| 86 | + assert.Error(t, err) |
| 87 | + assert.Regexp(t, tc.err, err) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + assert.NoError(t, err) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
0 commit comments