github.com/greenpau/go-authcrunch@v1.0.50/pkg/kms/parsers_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 kms
    16  
    17  import (
    18  	"encoding/base64"
    19  	"encoding/json"
    20  	"fmt"
    21  	"github.com/greenpau/go-authcrunch/internal/tests"
    22  	"testing"
    23  )
    24  
    25  func TestParsePayloadFromToken(t *testing.T) {
    26  	var testcases = []struct {
    27  		name      string
    28  		input     map[string]interface{}
    29  		want      map[string]interface{}
    30  		shouldErr bool
    31  		err       error
    32  	}{
    33  		{
    34  			name: "valid token payload",
    35  			input: map[string]interface{}{
    36  				"email_verified":   true,
    37  				"cognito:username": "jsmith",
    38  				"custom:roles":     "authp/admin authp/user",
    39  				"custom:timezone":  "America/New_York",
    40  			},
    41  			want: map[string]interface{}{
    42  				"email_verified":   true,
    43  				"cognito:username": "jsmith",
    44  				"custom:roles":     "authp/admin authp/user",
    45  				"custom:timezone":  "America/New_York",
    46  			},
    47  		},
    48  	}
    49  	for _, tc := range testcases {
    50  		t.Run(tc.name, func(t *testing.T) {
    51  			msgs := []string{fmt.Sprintf("test name: %s", tc.name)}
    52  
    53  			input, err := json.Marshal(tc.input)
    54  			if err != nil {
    55  				t.Fatal(err)
    56  			}
    57  
    58  			got, err := ParsePayloadFromToken("foo." + base64.StdEncoding.EncodeToString(input) + ".bar")
    59  			if tests.EvalErrWithLog(t, err, "TestParsePayloadFromToken", tc.shouldErr, tc.err, msgs) {
    60  				return
    61  			}
    62  			tests.EvalObjects(t, "TestParsePayloadFromToken", tc.want, got)
    63  		})
    64  	}
    65  }