github.com/openshift-online/ocm-sdk-go@v0.1.473/authentication/context_test.go (about)

     1  /*
     2  Copyright (c) 2019 Red Hat, 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  // This file contains tests for the functions that extract authentication and authorization
    18  // information from contexts.
    19  
    20  package authentication
    21  
    22  import (
    23  	"context"
    24  
    25  	. "github.com/onsi/ginkgo/v2/dsl/core"             // nolint
    26  	. "github.com/onsi/gomega"                         // nolint
    27  	. "github.com/openshift-online/ocm-sdk-go/testing" // nolint
    28  )
    29  
    30  var _ = Describe("Add token to context", func() {
    31  	It("Adds the token", func() {
    32  		token := MakeTokenObject(nil)
    33  		ctx := ContextWithToken(context.TODO(), token)
    34  		extracted := ctx.Value(tokenKeyValue)
    35  		Expect(extracted).To(BeIdenticalTo(token))
    36  	})
    37  })
    38  
    39  var _ = Describe("Get token from context", func() {
    40  	It("Succeeds if there is a token", func() {
    41  		token := MakeTokenObject(nil)
    42  		ctx := context.WithValue(context.TODO(), tokenKeyValue, token)
    43  		extracted, err := TokenFromContext(ctx)
    44  		Expect(err).ToNot(HaveOccurred())
    45  		Expect(extracted).ToNot(BeNil())
    46  		Expect(extracted.Raw).To(Equal(token.Raw))
    47  	})
    48  
    49  	It("Succeeds if there is no token", func() {
    50  		ctx := context.TODO()
    51  		extracted, err := TokenFromContext(ctx)
    52  		Expect(err).ToNot(HaveOccurred())
    53  		Expect(extracted).To(BeNil())
    54  	})
    55  })
    56  
    57  var _ = Describe("Get bearer from context", func() {
    58  	It("Succeeds if there is a token", func() {
    59  		token := MakeTokenObject(nil)
    60  		ctx := context.WithValue(context.TODO(), tokenKeyValue, token)
    61  		extracted, err := BearerFromContext(ctx)
    62  		Expect(err).ToNot(HaveOccurred())
    63  		Expect(extracted).To(Equal(token.Raw))
    64  	})
    65  
    66  	It("Succeeds if there is no token", func() {
    67  		ctx := context.TODO()
    68  		extracted, err := BearerFromContext(ctx)
    69  		Expect(err).ToNot(HaveOccurred())
    70  		Expect(extracted).To(BeEmpty())
    71  	})
    72  })