Skip to content
This repository has been archived by the owner on Feb 28, 2019. It is now read-only.

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Skelcy committed Nov 28, 2017
1 parent 7a77315 commit 5fe3566
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion auth/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (a simpleAuth) NewAuthHandler(next http.Handler, errHandler errorResponseHa
userID := r.Header.Get(a.authentication.userIDHeader)
err := a.authentication.authenticate(userID)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
errHandler(w, http.StatusUnauthorized, err.Error())
return
}

Expand Down
34 changes: 34 additions & 0 deletions auth/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package auth

import (
"bytes"
"context"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -134,6 +135,39 @@ func TestHealthCheck(t *testing.T) {
wrappedCall.ServeHTTP(httptest.NewRecorder(), &http.Request{})
}

func TestAuthenticateFailure(t *testing.T) {
a := testConfig.NewSimpleAuth()
f := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v, err := a.GetUser(r.Context())
require.NoError(t, err)
require.Equal(t, "testHeader", v)
})
recorder := httptest.NewRecorder()

wrappedCall := a.NewAuthHandler(f, writeAPIResponse)
wrappedCall.ServeHTTP(recorder, &http.Request{})
require.Equal(t, http.StatusUnauthorized, recorder.Code)
require.Equal(t, "application/json", recorder.HeaderMap["Content-Type"][0])
}

func TestAuthorizeFailure(t *testing.T) {
a := testConfig.NewSimpleAuth()
f := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v, err := a.GetUser(r.Context())
require.NoError(t, err)
require.Equal(t, "testHeader", v)
})
recorder := httptest.NewRecorder()
req, err := http.NewRequest("Get", "/create", bytes.NewBuffer(nil))
require.NoError(t, err)
req.Header.Add("testHeader", "validUserID")

wrappedCall := a.NewAuthHandler(f, writeAPIResponse)
wrappedCall.ServeHTTP(recorder, req)
require.Equal(t, http.StatusForbidden, recorder.Code)
require.Equal(t, "application/json", recorder.HeaderMap["Content-Type"][0])
}

func writeAPIResponse(w http.ResponseWriter, code int, msg string) error {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
Expand Down

0 comments on commit 5fe3566

Please sign in to comment.