github.com/greenpau/go-authcrunch@v1.1.4/pkg/registry/user_config_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 registry
    16  
    17  import (
    18  	"github.com/google/go-cmp/cmp"
    19  	"github.com/greenpau/go-authcrunch/pkg/errors"
    20  	"testing"
    21  )
    22  
    23  func TestValidateUserRegistryConfig(t *testing.T) {
    24  	testcases := []struct {
    25  		name      string
    26  		config    *UserRegistryConfig
    27  		shouldErr bool
    28  		err       error
    29  	}{
    30  		{
    31  			name:      "test user registration config without name",
    32  			config:    &UserRegistryConfig{},
    33  			shouldErr: true,
    34  			err:       errors.ErrUserRegistrationConfig.WithArgs("", "name is not set"),
    35  		},
    36  		{
    37  			name: "test user registration config without dropbox",
    38  			config: &UserRegistryConfig{
    39  				Name: "default",
    40  			},
    41  			shouldErr: true,
    42  			err:       errors.ErrUserRegistrationConfig.WithArgs("default", "dropbox is not set"),
    43  		},
    44  		{
    45  			name: "test user registration config without email provider",
    46  			config: &UserRegistryConfig{
    47  				Name:    "default",
    48  				Dropbox: "foo",
    49  			},
    50  			shouldErr: true,
    51  			err:       errors.ErrUserRegistrationConfig.WithArgs("default", "email provider is not set"),
    52  		},
    53  		{
    54  			name: "test user registration config without admin email address",
    55  			config: &UserRegistryConfig{
    56  				Name:          "default",
    57  				Dropbox:       "foo",
    58  				EmailProvider: "bar",
    59  			},
    60  			shouldErr: true,
    61  			err:       errors.ErrUserRegistrationConfig.WithArgs("default", "admin email address is not set"),
    62  		},
    63  		{
    64  			name: "test valid user registration config without identity store",
    65  			config: &UserRegistryConfig{
    66  				Name:                "default",
    67  				Dropbox:             "foo",
    68  				EmailProvider:       "bar",
    69  				AdminEmails:         []string{"root@localhost"},
    70  				RequireAcceptTerms:  true,
    71  				TermsConditionsLink: "/terms.html",
    72  				PrivacyPolicyLink:   "/privacy.html",
    73  			},
    74  			shouldErr: true,
    75  			err:       errors.ErrUserRegistrationConfig.WithArgs("default", "identity store name is not set"),
    76  		},
    77  		{
    78  			name: "test valid user registration config",
    79  			config: &UserRegistryConfig{
    80  				Name:                "default",
    81  				Dropbox:             "foo",
    82  				EmailProvider:       "bar",
    83  				AdminEmails:         []string{"root@localhost"},
    84  				RequireAcceptTerms:  true,
    85  				TermsConditionsLink: "/terms.html",
    86  				PrivacyPolicyLink:   "/privacy.html",
    87  				IdentityStore:       "foo",
    88  			},
    89  		},
    90  	}
    91  	for _, tc := range testcases {
    92  		t.Run(tc.name, func(t *testing.T) {
    93  			err := tc.config.Validate()
    94  			if err != nil {
    95  				if !tc.shouldErr {
    96  					t.Fatalf("expected success, got: %v", err)
    97  				}
    98  				if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" {
    99  					t.Fatalf("unexpected error: %v, want: %v", err, tc.err)
   100  				}
   101  				return
   102  			}
   103  			if tc.shouldErr {
   104  				t.Fatalf("unexpected success, want: %v", tc.err)
   105  			}
   106  		})
   107  	}
   108  }