Files
Marwan Sulaiman dfb7887080 Allow Athens to Propagate Authentication to Mod Download (#1650)
* Allow Athens to Propagate Authentication to Mod Download

* update readme

* add pattern matching to auth propagation

* Propagate authentication to pre declared static host

* quote redis test

* fix flaky redis error message

* fix config tests

* fix config tests

* Update config.dev.toml

Co-authored-by: Ted Wexler <ted@stuckinacan.com>

* gofmt

Co-authored-by: Ted Wexler <ted@stuckinacan.com>
2020-07-30 17:06:53 -04:00

64 lines
1.3 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gomods/athens/pkg/auth"
)
func TestAuthMiddleware(t *testing.T) {
var tests = []struct {
name string
reqfunc func(r *http.Request)
wantok bool
wantauth auth.BasicAuth
}{
{
name: "no auth",
reqfunc: func(r *http.Request) {},
},
{
name: "with basic auth",
reqfunc: func(r *http.Request) {
r.SetBasicAuth("user", "pass")
},
wantok: true,
wantauth: auth.BasicAuth{User: "user", Password: "pass"},
},
{
name: "only user",
reqfunc: func(r *http.Request) {
r.SetBasicAuth("justuser", "")
},
wantok: true,
wantauth: auth.BasicAuth{User: "justuser"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var (
givenok bool
givenauth auth.BasicAuth
)
h := WithAuth(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
givenauth.User, givenauth.Password, givenok = r.BasicAuth()
}))
r := httptest.NewRequest("GET", "/", nil)
tc.reqfunc(r)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if givenok != tc.wantok {
t.Fatalf("expected basic auth existence to be %t but got %t", tc.wantok, givenok)
}
if givenauth != tc.wantauth {
t.Fatalf("expected basic auth to be %+v but got %+v", tc.wantauth, givenauth)
}
})
}
}