github.com/pelicanplatform/pelican@v1.0.5/client/bearer_auth_test.go (about)

     1  /***************************************************************
     2   *
     3   * Copyright (C) 2023, University of Nebraska-Lincoln
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License"); you
     6   * may not use this file except in compliance with the License.  You may
     7   * obtain a copy of the License at
     8   *
     9   *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   ***************************************************************/
    18  package client
    19  
    20  import (
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestBearerAuthenticator_Authorize(t *testing.T) {
    29  	// Set up a test HTTP server
    30  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    31  		// Verify that the Authorization header is correct
    32  		authHeader := r.Header.Get("Authorization")
    33  		assert.Equal(t, "Bearer some_token_1234_abc", authHeader)
    34  
    35  		w.WriteHeader(http.StatusOK)
    36  	}))
    37  	defer server.Close()
    38  
    39  	authenticator := &bearerAuthenticator{token: "some_token_1234_abc"}
    40  	client := &http.Client{}
    41  
    42  	// Create a HTTP request to be authorized
    43  	request, err := http.NewRequest("GET", server.URL, nil)
    44  	assert.NoError(t, err)
    45  	err = authenticator.Authorize(client, request, "/test/path")
    46  	assert.NoError(t, err)
    47  
    48  	// Send the request and verify
    49  	response, err := client.Do(request)
    50  	assert.NoError(t, err)
    51  	assert.Equal(t, http.StatusOK, response.StatusCode)
    52  }
    53  
    54  func TestBearerAuthenticator_Verify(t *testing.T) {
    55  	authenticator := &bearerAuthenticator{token: "some_token_1234_abc"}
    56  	client := &http.Client{}
    57  
    58  	// Create a dummy HTTP response with a 401 status
    59  	response := &http.Response{
    60  		StatusCode: http.StatusUnauthorized,
    61  	}
    62  
    63  	// Verify the authentication
    64  	redo, err := authenticator.Verify(client, response, "/test/path")
    65  	assert.Error(t, err)
    66  	assert.True(t, redo, "Expected Verify to return true for 401 Unauthorized")
    67  
    68  	// Create a dummy HTTP response with a 200 OK status
    69  	responseOK := &http.Response{
    70  		StatusCode: http.StatusOK,
    71  	}
    72  
    73  	// Verify the authentication
    74  	redo, err = authenticator.Verify(client, responseOK, "/test/path")
    75  	assert.NoError(t, err)
    76  	assert.False(t, redo, "Expected Verify to return false for 200 OK")
    77  }