github.com/greenpau/go-authcrunch@v1.1.4/pkg/identity/role_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 TestNewRole(t *testing.T) {
    26  	testcases := []struct {
    27  		name      string
    28  		input     string
    29  		want      map[string]interface{}
    30  		shouldErr bool
    31  		err       error
    32  	}{
    33  		{
    34  			name:  "test role without org",
    35  			input: "superadmin",
    36  			want: map[string]interface{}{
    37  				"name": "superadmin",
    38  				"org":  "",
    39  				"role": "superadmin",
    40  			},
    41  		},
    42  		{
    43  			name:  "test role with org",
    44  			input: "internal/superadmin",
    45  			want: map[string]interface{}{
    46  				"name": "superadmin",
    47  				"org":  "internal",
    48  				"role": "internal/superadmin",
    49  			},
    50  		},
    51  		{
    52  			name:      "test empty role",
    53  			input:     "",
    54  			shouldErr: true,
    55  			err:       errors.ErrRoleEmpty,
    56  		},
    57  	}
    58  	for _, tc := range testcases {
    59  		t.Run(tc.name, func(t *testing.T) {
    60  			msgs := []string{fmt.Sprintf("test name: %s", tc.name)}
    61  			entry, err := NewRole(tc.input)
    62  			if tests.EvalErrWithLog(t, err, "new role", tc.shouldErr, tc.err, msgs) {
    63  				return
    64  			}
    65  			got := make(map[string]interface{})
    66  			got["name"] = entry.Name
    67  			got["org"] = entry.Organization
    68  			got["role"] = entry.String()
    69  			tests.EvalObjectsWithLog(t, "eval", tc.want, got, msgs)
    70  		})
    71  	}
    72  }