github.com/greenpau/go-authcrunch@v1.1.4/pkg/credentials/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 credentials
    16  
    17  import (
    18  	// "fmt"
    19  	"github.com/google/go-cmp/cmp"
    20  	"github.com/greenpau/go-authcrunch/internal/tests"
    21  	// "github.com/greenpau/go-authcrunch/pkg/errors"
    22  	"testing"
    23  )
    24  
    25  func TestAddCredentials(t *testing.T) {
    26  	testcases := []struct {
    27  		name      string
    28  		entry     Credential
    29  		want      string
    30  		shouldErr bool
    31  		err       error
    32  	}{
    33  		{
    34  			name: "test valid generic credential",
    35  			entry: &Generic{
    36  				Name:     "default",
    37  				Username: "foo",
    38  				Password: "bar",
    39  			},
    40  			want: `{
    41                "generic": [
    42                  {
    43                    "name":     "default",
    44                    "username": "foo",
    45                    "password": "bar"
    46                  }
    47                ]
    48              }`,
    49  		},
    50  	}
    51  	for _, tc := range testcases {
    52  		t.Run(tc.name, func(t *testing.T) {
    53  			cfg := &Config{}
    54  			err := cfg.Add(tc.entry)
    55  			if err != nil {
    56  				if !tc.shouldErr {
    57  					t.Fatalf("expected success, got: %v", err)
    58  				}
    59  				if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" {
    60  					t.Fatalf("unexpected error: %v, want: %v", err, tc.err)
    61  				}
    62  				return
    63  			}
    64  			if tc.shouldErr {
    65  				t.Fatalf("unexpected success, want: %v", tc.err)
    66  			}
    67  			got := tests.Unpack(t, cfg)
    68  			want := tests.Unpack(t, tc.want)
    69  
    70  			if diff := cmp.Diff(want, got); diff != "" {
    71  				t.Errorf("Add() mismatch (-want +got):\n%s", diff)
    72  			}
    73  		})
    74  	}
    75  }