github.com/blend/go-sdk@v1.20220411.3/tracing/oauthtrace/tracer_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package oauthtrace
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  
    14  	"github.com/opentracing/opentracing-go/mocktracer"
    15  	"golang.org/x/oauth2"
    16  
    17  	"github.com/blend/go-sdk/assert"
    18  	"github.com/blend/go-sdk/oauth"
    19  	"github.com/blend/go-sdk/tracing"
    20  )
    21  
    22  func TestStart(t *testing.T) {
    23  	assert := assert.New(t)
    24  	mockTracer := mocktracer.New()
    25  	oauthTracer := Tracer(mockTracer)
    26  
    27  	finisher := oauthTracer.Start(context.Background(), &oauth2.Config{RedirectURL: "/admin"})
    28  	span := finisher.(oauthTraceFinisher).span
    29  	mockSpan := span.(*mocktracer.MockSpan)
    30  	assert.Equal(tracing.OperationHTTPRequest, mockSpan.OperationName)
    31  
    32  	assert.Len(mockSpan.Tags(), 2)
    33  	assert.Equal(tracing.SpanTypeHTTP, mockSpan.Tags()[tracing.TagKeySpanType])
    34  	assert.True(mockSpan.FinishTime.IsZero())
    35  }
    36  
    37  func TestFinish(t *testing.T) {
    38  	assert := assert.New(t)
    39  	mockTracer := mocktracer.New()
    40  	oauthTracer := Tracer(mockTracer)
    41  
    42  	finisher := oauthTracer.Start(context.Background(), &oauth2.Config{RedirectURL: "/admin"})
    43  	finisher.Finish(context.Background(), &oauth2.Config{RedirectURL: "/admin"}, &oauth.Result{Profile: oauth.Profile{Email: "example-string@blend.com"}}, nil)
    44  
    45  	span := finisher.(oauthTraceFinisher).span
    46  	mockSpan := span.(*mocktracer.MockSpan)
    47  	assert.Equal("example-string@blend.com", mockSpan.Tags()[tracing.TagKeyOAuthUsername])
    48  	assert.False(mockSpan.FinishTime.IsZero())
    49  }