github.com/greenpau/go-authcrunch@v1.1.4/pkg/identity/name_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 identity
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/greenpau/go-authcrunch/internal/tests"
    22  	"github.com/greenpau/go-authcrunch/pkg/errors"
    23  )
    24  
    25  func TestNewName(t *testing.T) {
    26  	testcases := []struct {
    27  		name      string
    28  		fullName  string
    29  		want      map[string]interface{}
    30  		shouldErr bool
    31  		err       error
    32  	}{
    33  		{
    34  			name:     "test name",
    35  			fullName: "John Smith",
    36  			want: map[string]interface{}{
    37  				"claim":     "Smith, John",
    38  				"full_name": "Smith, John",
    39  			},
    40  		},
    41  		{
    42  			name:     "test alias",
    43  			fullName: "foobar",
    44  			want: map[string]interface{}{
    45  				"claim":     "foobar",
    46  				"full_name": "foobar",
    47  			},
    48  		},
    49  		{
    50  			name:      "test parse name error",
    51  			fullName:  "",
    52  			shouldErr: true,
    53  			err:       errors.ErrParseNameFailed.WithArgs(""),
    54  		},
    55  	}
    56  	for _, tc := range testcases {
    57  		t.Run(tc.name, func(t *testing.T) {
    58  			msgs := []string{fmt.Sprintf("test name: %s", tc.name)}
    59  			NewName()
    60  			entry, err := ParseName(tc.fullName)
    61  			if tests.EvalErrWithLog(t, err, "parse name", tc.shouldErr, tc.err, msgs) {
    62  				return
    63  			}
    64  			got := make(map[string]interface{})
    65  			got["claim"] = entry.GetNameClaim()
    66  			got["full_name"] = entry.GetFullName()
    67  			tests.EvalObjectsWithLog(t, "eval", tc.want, got, msgs)
    68  		})
    69  	}
    70  }