mirror of
https://github.com/grafana/grafana.git
synced 2024-12-26 08:51:33 -06:00
c3ab2fdeb7
* remove macaron.Request, use http.Request instead * remove com dependency from bindings module * fix another c.Req.Request
42 lines
1.3 KiB
Go
42 lines
1.3 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestBasicAuthenticatedRequest(t *testing.T) {
|
|
const expectedUser = "prometheus"
|
|
const expectedPass = "password"
|
|
|
|
t.Run("Given a valid set of basic auth credentials", func(t *testing.T) {
|
|
req, err := http.NewRequest("GET", "http://localhost:3000/metrics", nil)
|
|
require.NoError(t, err)
|
|
encodedCreds := encodeBasicAuthCredentials(expectedUser, expectedPass)
|
|
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", encodedCreds))
|
|
authenticated := BasicAuthenticatedRequest(req, expectedUser, expectedPass)
|
|
|
|
assert.True(t, authenticated)
|
|
})
|
|
|
|
t.Run("Given an invalid set of basic auth credentials", func(t *testing.T) {
|
|
req, err := http.NewRequest("GET", "http://localhost:3000/metrics", nil)
|
|
require.NoError(t, err)
|
|
encodedCreds := encodeBasicAuthCredentials("invaliduser", "invalidpass")
|
|
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", encodedCreds))
|
|
authenticated := BasicAuthenticatedRequest(req, expectedUser, expectedPass)
|
|
|
|
assert.False(t, authenticated)
|
|
})
|
|
}
|
|
|
|
func encodeBasicAuthCredentials(user, pass string) string {
|
|
creds := fmt.Sprintf("%s:%s", user, pass)
|
|
return base64.StdEncoding.EncodeToString([]byte(creds))
|
|
}
|