Commit 18bc6810 by Emil Hessman Committed by GitHub

Chore: Rewrite ldap login test to standard library (#29998)

* Chore: Rewrite ldap login test to standard library

* Preserve original ldap enabled setting after test
parent 504e4f23
...@@ -8,17 +8,16 @@ import ( ...@@ -8,17 +8,16 @@ import (
"github.com/grafana/grafana/pkg/services/ldap" "github.com/grafana/grafana/pkg/services/ldap"
"github.com/grafana/grafana/pkg/services/multildap" "github.com/grafana/grafana/pkg/services/multildap"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
. "github.com/smartystreets/goconvey/convey" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
var errTest = errors.New("test error") var errTest = errors.New("test error")
func TestLDAPLogin(t *testing.T) { func TestLoginUsingLDAP(t *testing.T) {
Convey("Login using ldap", t, func() { LDAPLoginScenario(t, "When LDAP enabled and no server configured", func(sc *LDAPLoginScenarioContext) {
Convey("Given ldap enabled and no server configured", func() {
setting.LDAPEnabled = true setting.LDAPEnabled = true
LDAPLoginScenario("When login", func(sc *LDAPLoginScenarioContext) {
sc.withLoginResult(false) sc.withLoginResult(false)
getLDAPConfig = func(*setting.Cfg) (*ldap.Config, error) { getLDAPConfig = func(*setting.Cfg) (*ldap.Config, error) {
config := &ldap.Config{ config := &ldap.Config{
...@@ -29,44 +28,21 @@ func TestLDAPLogin(t *testing.T) { ...@@ -29,44 +28,21 @@ func TestLDAPLogin(t *testing.T) {
} }
enabled, err := loginUsingLDAP(sc.loginUserQuery) enabled, err := loginUsingLDAP(sc.loginUserQuery)
require.EqualError(t, err, errTest.Error())
Convey("it should return true", func() { assert.True(t, enabled)
So(enabled, ShouldBeTrue) assert.True(t, sc.LDAPAuthenticatorMock.loginCalled)
}) })
Convey("it should return no LDAP servers error", func() { LDAPLoginScenario(t, "When LDAP disabled", func(sc *LDAPLoginScenarioContext) {
So(err, ShouldEqual, errTest)
})
Convey("it should not call ldap login", func() {
So(sc.LDAPAuthenticatorMock.loginCalled, ShouldBeTrue)
})
})
})
Convey("Given ldap disabled", func() {
setting.LDAPEnabled = false setting.LDAPEnabled = false
LDAPLoginScenario("When login", func(sc *LDAPLoginScenarioContext) {
sc.withLoginResult(false) sc.withLoginResult(false)
enabled, err := loginUsingLDAP(&models.LoginUserQuery{ enabled, err := loginUsingLDAP(sc.loginUserQuery)
Username: "user", require.NoError(t, err)
Password: "pwd",
})
Convey("it should return false", func() {
So(enabled, ShouldBeFalse)
})
Convey("it should not return error", func() {
So(err, ShouldBeNil)
})
Convey("it should not call ldap login", func() { assert.False(t, enabled)
So(sc.LDAPAuthenticatorMock.loginCalled, ShouldBeFalse) assert.False(t, sc.LDAPAuthenticatorMock.loginCalled)
})
})
})
}) })
} }
...@@ -137,8 +113,10 @@ type LDAPLoginScenarioContext struct { ...@@ -137,8 +113,10 @@ type LDAPLoginScenarioContext struct {
type LDAPLoginScenarioFunc func(c *LDAPLoginScenarioContext) type LDAPLoginScenarioFunc func(c *LDAPLoginScenarioContext)
func LDAPLoginScenario(desc string, fn LDAPLoginScenarioFunc) { func LDAPLoginScenario(t *testing.T, desc string, fn LDAPLoginScenarioFunc) {
Convey(desc, func() { t.Helper()
t.Run(desc, func(t *testing.T) {
mock := &mockAuth{} mock := &mockAuth{}
sc := &LDAPLoginScenarioContext{ sc := &LDAPLoginScenarioContext{
...@@ -152,10 +130,12 @@ func LDAPLoginScenario(desc string, fn LDAPLoginScenarioFunc) { ...@@ -152,10 +130,12 @@ func LDAPLoginScenario(desc string, fn LDAPLoginScenarioFunc) {
origNewLDAP := newLDAP origNewLDAP := newLDAP
origGetLDAPConfig := getLDAPConfig origGetLDAPConfig := getLDAPConfig
defer func() { origLDAPEnabled := setting.LDAPEnabled
t.Cleanup(func() {
newLDAP = origNewLDAP newLDAP = origNewLDAP
getLDAPConfig = origGetLDAPConfig getLDAPConfig = origGetLDAPConfig
}() setting.LDAPEnabled = origLDAPEnabled
})
getLDAPConfig = func(*setting.Cfg) (*ldap.Config, error) { getLDAPConfig = func(*setting.Cfg) (*ldap.Config, error) {
config := &ldap.Config{ config := &ldap.Config{
......
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