webhookd/pkg/auth/test/htpasswd-file_test.go
2023-02-13 21:17:46 +00:00

28 lines
844 B
Go

package test
import (
"net/http"
"testing"
"github.com/ncarlier/webhookd/pkg/assert"
"github.com/ncarlier/webhookd/pkg/auth"
)
func TestValidateCredentials(t *testing.T) {
htpasswdFile, err := auth.NewHtpasswdFromFile("test.htpasswd")
assert.Nil(t, err, ".htpasswd file should be loaded")
assert.NotNil(t, htpasswdFile, ".htpasswd file should be loaded")
req, err := http.NewRequest("POST", "http://localhost:8080", nil)
assert.Nil(t, err, "")
req.SetBasicAuth("foo", "bar")
ok, username := htpasswdFile.Validate(req)
assert.Equal(t, true, ok, "credentials should be valid")
assert.Equal(t, "foo", *username, "invalid username")
req.SetBasicAuth("foo", "bad")
ok, username = htpasswdFile.Validate(req)
assert.Equal(t, false, ok, "credentials should be invalid")
assert.True(t, username == nil, "username should be nil")
}