mirror of
https://github.com/grafana/grafana.git
synced 2026-07-30 08:18:10 -05:00
Chore: Rewrite ldap login test to standard library (#29998)
* Chore: Rewrite ldap login test to standard library * Preserve original ldap enabled setting after test
This commit is contained in:
@@ -8,65 +8,41 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/ldap"
|
||||
"github.com/grafana/grafana/pkg/services/multildap"
|
||||
"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")
|
||||
|
||||
func TestLDAPLogin(t *testing.T) {
|
||||
Convey("Login using ldap", t, func() {
|
||||
Convey("Given ldap enabled and no server configured", func() {
|
||||
setting.LDAPEnabled = true
|
||||
func TestLoginUsingLDAP(t *testing.T) {
|
||||
LDAPLoginScenario(t, "When LDAP enabled and no server configured", func(sc *LDAPLoginScenarioContext) {
|
||||
setting.LDAPEnabled = true
|
||||
|
||||
LDAPLoginScenario("When login", func(sc *LDAPLoginScenarioContext) {
|
||||
sc.withLoginResult(false)
|
||||
getLDAPConfig = func(*setting.Cfg) (*ldap.Config, error) {
|
||||
config := &ldap.Config{
|
||||
Servers: []*ldap.ServerConfig{},
|
||||
}
|
||||
sc.withLoginResult(false)
|
||||
getLDAPConfig = func(*setting.Cfg) (*ldap.Config, error) {
|
||||
config := &ldap.Config{
|
||||
Servers: []*ldap.ServerConfig{},
|
||||
}
|
||||
|
||||
return config, nil
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
enabled, err := loginUsingLDAP(sc.loginUserQuery)
|
||||
enabled, err := loginUsingLDAP(sc.loginUserQuery)
|
||||
require.EqualError(t, err, errTest.Error())
|
||||
|
||||
Convey("it should return true", func() {
|
||||
So(enabled, ShouldBeTrue)
|
||||
})
|
||||
assert.True(t, enabled)
|
||||
assert.True(t, sc.LDAPAuthenticatorMock.loginCalled)
|
||||
})
|
||||
|
||||
Convey("it should return no LDAP servers error", func() {
|
||||
So(err, ShouldEqual, errTest)
|
||||
})
|
||||
LDAPLoginScenario(t, "When LDAP disabled", func(sc *LDAPLoginScenarioContext) {
|
||||
setting.LDAPEnabled = false
|
||||
|
||||
Convey("it should not call ldap login", func() {
|
||||
So(sc.LDAPAuthenticatorMock.loginCalled, ShouldBeTrue)
|
||||
})
|
||||
})
|
||||
})
|
||||
sc.withLoginResult(false)
|
||||
enabled, err := loginUsingLDAP(sc.loginUserQuery)
|
||||
require.NoError(t, err)
|
||||
|
||||
Convey("Given ldap disabled", func() {
|
||||
setting.LDAPEnabled = false
|
||||
|
||||
LDAPLoginScenario("When login", func(sc *LDAPLoginScenarioContext) {
|
||||
sc.withLoginResult(false)
|
||||
enabled, err := loginUsingLDAP(&models.LoginUserQuery{
|
||||
Username: "user",
|
||||
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() {
|
||||
So(sc.LDAPAuthenticatorMock.loginCalled, ShouldBeFalse)
|
||||
})
|
||||
})
|
||||
})
|
||||
assert.False(t, enabled)
|
||||
assert.False(t, sc.LDAPAuthenticatorMock.loginCalled)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -137,8 +113,10 @@ type LDAPLoginScenarioContext struct {
|
||||
|
||||
type LDAPLoginScenarioFunc func(c *LDAPLoginScenarioContext)
|
||||
|
||||
func LDAPLoginScenario(desc string, fn LDAPLoginScenarioFunc) {
|
||||
Convey(desc, func() {
|
||||
func LDAPLoginScenario(t *testing.T, desc string, fn LDAPLoginScenarioFunc) {
|
||||
t.Helper()
|
||||
|
||||
t.Run(desc, func(t *testing.T) {
|
||||
mock := &mockAuth{}
|
||||
|
||||
sc := &LDAPLoginScenarioContext{
|
||||
@@ -152,10 +130,12 @@ func LDAPLoginScenario(desc string, fn LDAPLoginScenarioFunc) {
|
||||
|
||||
origNewLDAP := newLDAP
|
||||
origGetLDAPConfig := getLDAPConfig
|
||||
defer func() {
|
||||
origLDAPEnabled := setting.LDAPEnabled
|
||||
t.Cleanup(func() {
|
||||
newLDAP = origNewLDAP
|
||||
getLDAPConfig = origGetLDAPConfig
|
||||
}()
|
||||
setting.LDAPEnabled = origLDAPEnabled
|
||||
})
|
||||
|
||||
getLDAPConfig = func(*setting.Cfg) (*ldap.Config, error) {
|
||||
config := &ldap.Config{
|
||||
|
||||
Reference in New Issue
Block a user