github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/plugin/oauth2/middleware_test.go (about)

     1  package oauth2
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"testing"
     8  
     9  	"github.com/hellofresh/janus/pkg/test"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  type mockManager struct {
    14  	authorized bool
    15  }
    16  
    17  func (m *mockManager) IsKeyAuthorized(ctx context.Context, accessToken string) bool {
    18  	return m.authorized
    19  }
    20  
    21  func TestContextKey(t *testing.T) {
    22  	key := ContextKey("test")
    23  	assert.Equal(t, "janus.test", key.String())
    24  }
    25  
    26  func TestValidKeyStorage(t *testing.T) {
    27  	manager := &mockManager{true}
    28  	mw := NewKeyExistsMiddleware(manager)
    29  
    30  	w, err := test.Record(
    31  		"GET",
    32  		"/",
    33  		map[string]string{
    34  			"Content-Type":  "application/json",
    35  			"Authorization": fmt.Sprintf("Bearer %s", "123"),
    36  		},
    37  		mw(http.HandlerFunc(test.Ping)),
    38  	)
    39  	assert.NoError(t, err)
    40  
    41  	assert.Equal(t, http.StatusOK, w.Code)
    42  	assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
    43  }
    44  
    45  func TestWrongAuthHeader(t *testing.T) {
    46  	manager := &mockManager{false}
    47  	mw := NewKeyExistsMiddleware(manager)
    48  
    49  	w, err := test.Record(
    50  		"GET",
    51  		"/",
    52  		map[string]string{
    53  			"Content-Type":  "application/json",
    54  			"Authorization": fmt.Sprintf("Wrong %s", "123"),
    55  		},
    56  		mw(http.HandlerFunc(test.Ping)),
    57  	)
    58  	assert.NoError(t, err)
    59  
    60  	assert.Equal(t, http.StatusBadRequest, w.Code)
    61  	assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
    62  }
    63  
    64  func TestMissingAuthHeader(t *testing.T) {
    65  	manager := &mockManager{false}
    66  	mw := NewKeyExistsMiddleware(manager)
    67  
    68  	w, err := test.Record(
    69  		"GET",
    70  		"/",
    71  		map[string]string{
    72  			"Content-Type": "application/json",
    73  		},
    74  		mw(http.HandlerFunc(test.Ping)),
    75  	)
    76  	assert.NoError(t, err)
    77  
    78  	assert.Equal(t, http.StatusBadRequest, w.Code)
    79  	assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
    80  }
    81  
    82  func TestMissingKeyStorage(t *testing.T) {
    83  	manager := &mockManager{false}
    84  	mw := NewKeyExistsMiddleware(manager)
    85  
    86  	w, err := test.Record(
    87  		"GET",
    88  		"/",
    89  		map[string]string{
    90  			"Content-Type":  "application/json",
    91  			"Authorization": fmt.Sprintf("Bearer %s", "1234"),
    92  		},
    93  		mw(http.HandlerFunc(test.Ping)),
    94  	)
    95  	assert.NoError(t, err)
    96  
    97  	assert.Equal(t, http.StatusUnauthorized, w.Code)
    98  	assert.Equal(t, "application/json", w.Header().Get("Content-Type"))
    99  }