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 ...@@ -3,83 +3,51 @@ package login
import ( import (
"testing" "testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestGrafanaLogin(t *testing.T) { func TestLoginUsingGrafanaDB(t *testing.T) {
Convey("Login using Grafana DB", t, func() { grafanaLoginScenario(t, "When login with non-existing user", func(sc *grafanaLoginScenarioContext) {
grafanaLoginScenario("When login with non-existing user", func(sc *grafanaLoginScenarioContext) {
sc.withNonExistingUser() sc.withNonExistingUser()
err := loginUsingGrafanaDB(sc.loginUserQuery) err := loginUsingGrafanaDB(sc.loginUserQuery)
require.EqualError(t, err, models.ErrUserNotFound.Error())
Convey("it should result in user not found error", func() { assert.False(t, sc.validatePasswordCalled)
So(err, ShouldEqual, models.ErrUserNotFound) assert.Nil(t, sc.loginUserQuery.User)
})
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) { grafanaLoginScenario(t, "When login with invalid credentials", func(sc *grafanaLoginScenarioContext) {
sc.withInvalidPassword() sc.withInvalidPassword()
err := loginUsingGrafanaDB(sc.loginUserQuery) err := loginUsingGrafanaDB(sc.loginUserQuery)
Convey("it should result in invalid credentials error", func() { require.EqualError(t, err, ErrInvalidCredentials.Error())
So(err, ShouldEqual, ErrInvalidCredentials)
})
Convey("it should call password validation", func() { assert.True(t, sc.validatePasswordCalled)
So(sc.validatePasswordCalled, ShouldBeTrue) assert.Nil(t, sc.loginUserQuery.User)
}) })
Convey("it should not populate user object", func() { grafanaLoginScenario(t, "When login with valid credentials", func(sc *grafanaLoginScenarioContext) {
So(sc.loginUserQuery.User, ShouldBeNil)
})
})
grafanaLoginScenario("When login with valid credentials", func(sc *grafanaLoginScenarioContext) {
sc.withValidCredentials() sc.withValidCredentials()
err := loginUsingGrafanaDB(sc.loginUserQuery) err := loginUsingGrafanaDB(sc.loginUserQuery)
require.NoError(t, err)
Convey("it should not result in error", func() { assert.True(t, sc.validatePasswordCalled)
So(err, ShouldBeNil)
})
Convey("it should call password validation", func() {
So(sc.validatePasswordCalled, ShouldBeTrue)
})
Convey("it should populate user object", func() { require.NotNil(t, sc.loginUserQuery.User)
So(sc.loginUserQuery.User, ShouldNotBeNil) assert.Equal(t, sc.loginUserQuery.Username, sc.loginUserQuery.User.Login)
So(sc.loginUserQuery.User.Login, ShouldEqual, sc.loginUserQuery.Username) assert.Equal(t, sc.loginUserQuery.Password, sc.loginUserQuery.User.Password)
So(sc.loginUserQuery.User.Password, ShouldEqual, sc.loginUserQuery.Password)
})
}) })
grafanaLoginScenario("When login with disabled user", func(sc *grafanaLoginScenarioContext) { grafanaLoginScenario(t, "When login with disabled user", func(sc *grafanaLoginScenarioContext) {
sc.withDisabledUser() sc.withDisabledUser()
err := loginUsingGrafanaDB(sc.loginUserQuery) err := loginUsingGrafanaDB(sc.loginUserQuery)
require.EqualError(t, err, ErrUserDisabled.Error())
Convey("it should return user is disabled error", func() { assert.False(t, sc.validatePasswordCalled)
So(err, ShouldEqual, ErrUserDisabled) assert.Nil(t, sc.loginUserQuery.User)
})
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)
})
})
}) })
} }
...@@ -90,8 +58,10 @@ type grafanaLoginScenarioContext struct { ...@@ -90,8 +58,10 @@ type grafanaLoginScenarioContext struct {
type grafanaLoginScenarioFunc func(c *grafanaLoginScenarioContext) type grafanaLoginScenarioFunc func(c *grafanaLoginScenarioContext)
func grafanaLoginScenario(desc string, fn grafanaLoginScenarioFunc) { func grafanaLoginScenario(t *testing.T, desc string, fn grafanaLoginScenarioFunc) {
Convey(desc, func() { t.Helper()
t.Run(desc, func(t *testing.T) {
origValidatePassword := validatePassword origValidatePassword := validatePassword
sc := &grafanaLoginScenarioContext{ sc := &grafanaLoginScenarioContext{
...@@ -103,9 +73,9 @@ func grafanaLoginScenario(desc string, fn grafanaLoginScenarioFunc) { ...@@ -103,9 +73,9 @@ func grafanaLoginScenario(desc string, fn grafanaLoginScenarioFunc) {
validatePasswordCalled: false, validatePasswordCalled: false,
} }
defer func() { t.Cleanup(func() {
validatePassword = origValidatePassword validatePassword = origValidatePassword
}() })
fn(sc) 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