Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
504e4f23
Unverified
Commit
504e4f23
authored
Dec 31, 2020
by
Emil Hessman
Committed by
GitHub
Dec 31, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Chore: Rewrite grafana login test to standard library (#29997)
parent
de563bfa
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
68 deletions
+38
-68
pkg/login/grafana_login_test.go
+38
-68
No files found.
pkg/login/grafana_login_test.go
View file @
504e4f23
...
...
@@ -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 in
valid 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
)
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment