...

Source file src/github.com/okta/okta-sdk-golang/v2/tests/unit/socialAuthToken_test.go

Documentation: github.com/okta/okta-sdk-golang/v2/tests/unit

     1  /*
     2   * Copyright 2018 - Present Okta, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package unit
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"github.com/okta/okta-sdk-golang/v2/okta"
    27  )
    28  
    29  func TestSocialAuthTokenMarshaling(t *testing.T) {
    30  	type errorTestCases struct {
    31  		name              string
    32  		_json             []byte
    33  		expectedScopes    []string
    34  		expectedExpiredAt string
    35  	}
    36  
    37  	for _, scenario := range []errorTestCases{
    38  		{
    39  			name: "social auth base case",
    40  			_json: []byte(`
    41  				{
    42  					"id": "<unique token identifier>",
    43  					"token": "JBTWGV22G4ZGKV3N",
    44  					"tokenType" : "urn:ietf:params:oauth:token-type:access_token",
    45  					"tokenAuthScheme": "Bearer",
    46  					"expiresAt" : "2014-08-06T16:56:31.000Z",
    47  					"scopes" : [
    48  						"email",
    49  						"openid",
    50  						"profile"
    51  					]
    52  			  	}
    53  			`),
    54  			expectedScopes:    []string{"email", "openid", "profile"},
    55  			expectedExpiredAt: "2014-08-06T16:56:31.000Z",
    56  		},
    57  		{
    58  			name: "social auth token with blank exiresAt",
    59  			_json: []byte(`
    60  				{
    61  					"id": "<unique token identifier>",
    62  					"token": "JBTWGV22G4ZGKV3N",
    63  					"tokenType" : "urn:ietf:params:oauth:token-type:access_token",
    64  					"tokenAuthScheme": null,
    65  					"expiresAt" : "",
    66  					"scopes" : []
    67  			  	}
    68  			`),
    69  			expectedScopes: []string{},
    70  		},
    71  	} {
    72  		t.Run(scenario.name, func(t *testing.T) {
    73  			var token okta.SocialAuthToken
    74  			err := json.Unmarshal(scenario._json, &token)
    75  			require.NoError(t, err)
    76  
    77  			if scenario.expectedExpiredAt == "" {
    78  				require.Nil(t, token.ExpiresAt)
    79  			} else {
    80  				expectedExpiredAt, err := time.Parse(time.RFC3339, scenario.expectedExpiredAt)
    81  				require.NoError(t, err)
    82  				require.Equal(t, &expectedExpiredAt, token.ExpiresAt)
    83  			}
    84  
    85  			require.Equal(t, scenario.expectedScopes, token.Scopes)
    86  		})
    87  	}
    88  }
    89  

View as plain text