github.com/greenpau/go-authcrunch@v1.1.4/pkg/messaging/file_test.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     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 messaging
    16  
    17  import (
    18  	"github.com/google/go-cmp/cmp"
    19  	"github.com/greenpau/go-authcrunch/pkg/errors"
    20  	"testing"
    21  )
    22  
    23  func TestValidateFileProvider(t *testing.T) {
    24  	testcases := []struct {
    25  		name      string
    26  		entry     *FileProvider
    27  		want      string
    28  		shouldErr bool
    29  		err       error
    30  	}{
    31  		{
    32  			name: "test valid file provider config",
    33  			entry: &FileProvider{
    34  				Name:    "default",
    35  				RootDir: "foobar",
    36  			},
    37  		},
    38  		{
    39  			name: "test file provider config without root directory",
    40  			entry: &FileProvider{
    41  				Name: "default",
    42  			},
    43  			shouldErr: true,
    44  			err:       errors.ErrMessagingProviderKeyValueEmpty.WithArgs("root_dir"),
    45  		},
    46  		{
    47  			name:      "test file provider config without name",
    48  			entry:     &FileProvider{},
    49  			shouldErr: true,
    50  			err:       errors.ErrMessagingProviderKeyValueEmpty.WithArgs("name"),
    51  		},
    52  		{
    53  			name: "test file provider config with invalid template",
    54  			entry: &FileProvider{
    55  				Name:    "default",
    56  				RootDir: "foobar",
    57  				Templates: map[string]string{
    58  					"foo": "bar",
    59  				},
    60  			},
    61  			shouldErr: true,
    62  			err:       errors.ErrMessagingProviderInvalidTemplate.WithArgs("foo"),
    63  		},
    64  	}
    65  	for _, tc := range testcases {
    66  		t.Run(tc.name, func(t *testing.T) {
    67  			err := tc.entry.Validate()
    68  			if err != nil {
    69  				if !tc.shouldErr {
    70  					t.Fatalf("expected success, got: %v", err)
    71  				}
    72  				if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" {
    73  					t.Fatalf("unexpected error: %v, want: %v", err, tc.err)
    74  				}
    75  				return
    76  			}
    77  			if tc.shouldErr {
    78  				t.Fatalf("unexpected success, want: %v", tc.err)
    79  			}
    80  		})
    81  	}
    82  }