mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 02:40:26 -06:00
2324597d8d
* AuthN: Create password client wrapper and use that on in basic auth client * AuthN: fix basic auth client test * AuthN: Add tests for form authentication * API: Inject authn service * Login: If authnService feature flag is enabled use authn login * Login: Handle token creation errors
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package clients
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/grafana/grafana/pkg/services/authn"
|
|
"github.com/grafana/grafana/pkg/services/authn/authntest"
|
|
)
|
|
|
|
func TestForm_Authenticate(t *testing.T) {
|
|
type testCase struct {
|
|
desc string
|
|
req *authn.Request
|
|
expectedErr error
|
|
}
|
|
|
|
tests := []testCase{
|
|
{
|
|
desc: "should success on valid request",
|
|
req: &authn.Request{HTTPRequest: &http.Request{
|
|
Header: map[string][]string{"Content-Type": {"application/json"}},
|
|
Body: io.NopCloser(strings.NewReader(`{"user": "test", "password": "test"}`)),
|
|
}},
|
|
},
|
|
{
|
|
desc: "should return error for bad request",
|
|
req: &authn.Request{HTTPRequest: &http.Request{
|
|
Header: map[string][]string{"Content-Type": {"application/json"}},
|
|
Body: io.NopCloser(strings.NewReader(`{}`)),
|
|
}},
|
|
expectedErr: errBadForm,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.desc, func(t *testing.T) {
|
|
c := ProvideForm(&authntest.FakePasswordClient{})
|
|
_, err := c.Authenticate(context.Background(), tt.req)
|
|
assert.ErrorIs(t, err, tt.expectedErr)
|
|
})
|
|
}
|
|
}
|