Commit 504e4f23 by Emil Hessman Committed by GitHub

Chore: Rewrite grafana login test to standard library (#29997)

parent de563bfa
......@@ -3,83 +3,51 @@ package login
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGrafanaLogin(t *testing.T) {
Convey("Login using Grafana DB", t, func() {
grafanaLoginScenario("When login with non-existing user", func(sc *grafanaLoginScenarioContext) {
sc.withNonExistingUser()
err := loginUsingGrafanaDB(sc.loginUserQuery)
Convey("it should result in user not found error", func() {
So(err, ShouldEqual, models.ErrUserNotFound)
})
Convey("it should not call password validation", func() {
So(sc.validatePasswordCalled, ShouldBeFalse)
})
Convey("it should not populate user object", func() {
So(sc.loginUserQuery.User, ShouldBeNil)
})
})
grafanaLoginScenario("When login with invalid credentials", func(sc *grafanaLoginScenarioContext) {
sc.withInvalidPassword()
err := loginUsingGrafanaDB(sc.loginUserQuery)
Convey("it should result in invalid credentials error", func() {
So(err, ShouldEqual, ErrInvalidCredentials)
})
Convey("it should call password validation", func() {
So(sc.validatePasswordCalled, ShouldBeTrue)
})
func TestLoginUsingGrafanaDB(t *testing.T) {
grafanaLoginScenario(t, "When login with non-existing user", func(sc *grafanaLoginScenarioContext) {
sc.withNonExistingUser()
err := loginUsingGrafanaDB(sc.loginUserQuery)
require.EqualError(t, err, models.ErrUserNotFound.Error())
Convey("it should not populate user object", func() {
So(sc.loginUserQuery.User, ShouldBeNil)
})
})
assert.False(t, sc.validatePasswordCalled)
assert.Nil(t, sc.loginUserQuery.User)
})
grafanaLoginScenario("When login with valid credentials", func(sc *grafanaLoginScenarioContext) {
sc.withValidCredentials()
err := loginUsingGrafanaDB(sc.loginUserQuery)
grafanaLoginScenario(t, "When login with invalid credentials", func(sc *grafanaLoginScenarioContext) {
sc.withInvalidPassword()
err := loginUsingGrafanaDB(sc.loginUserQuery)
Convey("it should not result in error", func() {
So(err, ShouldBeNil)
})
require.EqualError(t, err, ErrInvalidCredentials.Error())
Convey("it should call password validation", func() {
So(sc.validatePasswordCalled, ShouldBeTrue)
})
assert.True(t, sc.validatePasswordCalled)
assert.Nil(t, sc.loginUserQuery.User)
})
Convey("it should populate user object", func() {
So(sc.loginUserQuery.User, ShouldNotBeNil)
So(sc.loginUserQuery.User.Login, ShouldEqual, sc.loginUserQuery.Username)
So(sc.loginUserQuery.User.Password, ShouldEqual, sc.loginUserQuery.Password)
})
})
grafanaLoginScenario(t, "When login with valid credentials", func(sc *grafanaLoginScenarioContext) {
sc.withValidCredentials()
err := loginUsingGrafanaDB(sc.loginUserQuery)
require.NoError(t, err)
grafanaLoginScenario("When login with disabled user", func(sc *grafanaLoginScenarioContext) {
sc.withDisabledUser()
err := loginUsingGrafanaDB(sc.loginUserQuery)
assert.True(t, sc.validatePasswordCalled)
Convey("it should return user is disabled error", func() {
So(err, ShouldEqual, ErrUserDisabled)
})
require.NotNil(t, sc.loginUserQuery.User)
assert.Equal(t, sc.loginUserQuery.Username, sc.loginUserQuery.User.Login)
assert.Equal(t, sc.loginUserQuery.Password, sc.loginUserQuery.User.Password)
})
Convey("it should not call password validation", func() {
So(sc.validatePasswordCalled, ShouldBeFalse)
})
grafanaLoginScenario(t, "When login with disabled user", func(sc *grafanaLoginScenarioContext) {
sc.withDisabledUser()
err := loginUsingGrafanaDB(sc.loginUserQuery)
require.EqualError(t, err, ErrUserDisabled.Error())
Convey("it should not populate user object", func() {
So(sc.loginUserQuery.User, ShouldBeNil)
})
})
assert.False(t, sc.validatePasswordCalled)
assert.Nil(t, sc.loginUserQuery.User)
})
}
......@@ -90,8 +58,10 @@ type grafanaLoginScenarioContext struct {
type grafanaLoginScenarioFunc func(c *grafanaLoginScenarioContext)
func grafanaLoginScenario(desc string, fn grafanaLoginScenarioFunc) {
Convey(desc, func() {
func grafanaLoginScenario(t *testing.T, desc string, fn grafanaLoginScenarioFunc) {
t.Helper()
t.Run(desc, func(t *testing.T) {
origValidatePassword := validatePassword
sc := &grafanaLoginScenarioContext{
......@@ -103,9 +73,9 @@ func grafanaLoginScenario(desc string, fn grafanaLoginScenarioFunc) {
validatePasswordCalled: false,
}
defer func() {
t.Cleanup(func() {
validatePassword = origValidatePassword
}()
})
fn(sc)
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment