htpasswd-file_test.go 846 B

123456789101112131415161718192021222324252627
  1. package test
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/ncarlier/webhookd/pkg/assert"
  6. "github.com/ncarlier/webhookd/pkg/auth"
  7. )
  8. func TestValidateCredentials(t *testing.T) {
  9. htpasswdFile, err := auth.NewHtpasswdFromFile("test.htpasswd")
  10. assert.Nil(t, err, ".htpasswd file should be loaded")
  11. assert.NotNil(t, htpasswdFile, ".htpasswd file should be loaded")
  12. req, err := http.NewRequest("POST", "http://localhost:8080", http.NoBody)
  13. assert.Nil(t, err, "")
  14. req.SetBasicAuth("foo", "bar")
  15. ok, username := htpasswdFile.Validate(req)
  16. assert.Equal(t, true, ok, "credentials should be valid")
  17. assert.Equal(t, "foo", username, "invalid username")
  18. req.SetBasicAuth("foo", "bad")
  19. ok, username = htpasswdFile.Validate(req)
  20. assert.Equal(t, false, ok, "credentials should be invalid")
  21. assert.Equal(t, "foo", username, "invalid username")
  22. }