Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic auth test #88

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions middleware/basicauth/basicauth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package basicauth_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/go-fuego/fuego/middleware/basicauth"
"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
t.Run("cannot create middleware without username", func(t *testing.T) {
require.Panics(t, func() {
basicauth.New(basicauth.Config{
Password: "pass",
})
})
})

t.Run("cannot create middleware without password", func(t *testing.T) {
require.Panics(t, func() {
basicauth.New(basicauth.Config{
Username: "user",
})
})
})

t.Run("usual creation", func(t *testing.T) {
basicAuth := basicauth.New(basicauth.Config{
Username: "user",
Password: "pass",
})
require.NotNil(t, basicAuth)

handler := basicAuth(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}))

t.Run("without auth", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)

// Test without auth
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
require.Equal(t, http.StatusUnauthorized, w.Code)
require.Equal(t, `Basic realm="Restricted"`, w.Header().Get("WWW-Authenticate"))
})

t.Run("with wrong auth", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
req.SetBasicAuth("user", "wrongpass")

// Test with wrong auth
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
require.Equal(t, http.StatusUnauthorized, w.Code)
require.Equal(t, `Basic realm="Restricted"`, w.Header().Get("WWW-Authenticate"))
})

t.Run("with correct auth", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
req.SetBasicAuth("user", "pass")

// Test with correct auth
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, "OK", w.Body.String())
})
})

t.Run("allow get requests without auth", func(t *testing.T) {
basicAuth := basicauth.New(basicauth.Config{
Username: "user",
Password: "pass",
AllowGet: true,
})
require.NotNil(t, basicAuth)

handler := basicAuth(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}))

t.Run("get authorized without auth", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)

// Test without auth
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, "OK", w.Body.String())
})

t.Run("get authorized with wrong auth", func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
req.SetBasicAuth("user", "wrongpass")

// Test with wrong auth
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
require.Equal(t, "OK", w.Body.String())
})

t.Run("post unauthorized without auth", func(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, "/", nil)
require.NoError(t, err)

// Test without auth
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
require.Equal(t, http.StatusUnauthorized, w.Code)
require.Equal(t, `Basic realm="Restricted"`, w.Header().Get("WWW-Authenticate"))
})
})
}
2 changes: 1 addition & 1 deletion serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func SendJSONError(w http.ResponseWriter, err error) {
}

w.WriteHeader(status)
w.Header().Set("Content-Type", "application/problems+json")
w.Header().Set("Content-Type", "application/problem+json")
SendJSON(w, errorStatus)
}

Expand Down
Loading