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
59d0c19b
Unverified
Commit
59d0c19b
authored
Jan 22, 2019
by
Marcus Efraimsson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
passing middleware tests
parent
4096449a
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
92 additions
and
58 deletions
+92
-58
pkg/api/common_test.go
+32
-8
pkg/middleware/middleware_test.go
+42
-28
pkg/middleware/org_redirect_test.go
+11
-13
pkg/middleware/quota_test.go
+5
-8
pkg/middleware/recovery_test.go
+2
-1
No files found.
pkg/api/common_test.go
View file @
59d0c19b
...
@@ -95,13 +95,14 @@ func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map
...
@@ -95,13 +95,14 @@ func (sc *scenarioContext) fakeReqWithParams(method, url string, queryParams map
}
}
type
scenarioContext
struct
{
type
scenarioContext
struct
{
m
*
macaron
.
Macaron
m
*
macaron
.
Macaron
context
*
m
.
ReqContext
context
*
m
.
ReqContext
resp
*
httptest
.
ResponseRecorder
resp
*
httptest
.
ResponseRecorder
handlerFunc
handlerFunc
handlerFunc
handlerFunc
defaultHandler
macaron
.
Handler
defaultHandler
macaron
.
Handler
req
*
http
.
Request
req
*
http
.
Request
url
string
url
string
userAuthTokenService
*
fakeUserAuthTokenService
}
}
func
(
sc
*
scenarioContext
)
exec
()
{
func
(
sc
*
scenarioContext
)
exec
()
{
...
@@ -123,8 +124,31 @@ func setupScenarioContext(url string) *scenarioContext {
...
@@ -123,8 +124,31 @@ func setupScenarioContext(url string) *scenarioContext {
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
}))
}))
sc
.
m
.
Use
(
middleware
.
GetContextHandler
(
nil
))
sc
.
userAuthTokenService
=
newFakeUserAuthTokenService
()
sc
.
m
.
Use
(
middleware
.
GetContextHandler
(
sc
.
userAuthTokenService
))
sc
.
m
.
Use
(
middleware
.
Sessioner
(
&
session
.
Options
{},
0
))
sc
.
m
.
Use
(
middleware
.
Sessioner
(
&
session
.
Options
{},
0
))
return
sc
return
sc
}
}
type
fakeUserAuthTokenService
struct
{
initContextWithTokenProvider
func
(
ctx
*
m
.
ReqContext
,
orgID
int64
)
bool
}
func
newFakeUserAuthTokenService
()
*
fakeUserAuthTokenService
{
return
&
fakeUserAuthTokenService
{
initContextWithTokenProvider
:
func
(
ctx
*
m
.
ReqContext
,
orgID
int64
)
bool
{
return
false
},
}
}
func
(
s
*
fakeUserAuthTokenService
)
InitContextWithToken
(
ctx
*
m
.
ReqContext
,
orgID
int64
)
bool
{
return
s
.
initContextWithTokenProvider
(
ctx
,
orgID
)
}
func
(
s
*
fakeUserAuthTokenService
)
UserAuthenticatedHook
(
user
*
m
.
User
,
c
*
m
.
ReqContext
)
error
{
return
nil
}
func
(
s
*
fakeUserAuthTokenService
)
UserSignedOutHook
(
c
*
m
.
ReqContext
)
{}
pkg/middleware/middleware_test.go
View file @
59d0c19b
...
@@ -43,11 +43,6 @@ func TestMiddlewareContext(t *testing.T) {
...
@@ -43,11 +43,6 @@ func TestMiddlewareContext(t *testing.T) {
So
(
sc
.
resp
.
Header
()
.
Get
(
"Cache-Control"
),
ShouldBeEmpty
)
So
(
sc
.
resp
.
Header
()
.
Get
(
"Cache-Control"
),
ShouldBeEmpty
)
})
})
middlewareScenario
(
"Non api request should init session"
,
func
(
sc
*
scenarioContext
)
{
sc
.
fakeReq
(
"GET"
,
"/"
)
.
exec
()
So
(
sc
.
resp
.
Header
()
.
Get
(
"Set-Cookie"
),
ShouldContainSubstring
,
"grafana_sess"
)
})
middlewareScenario
(
"Invalid api key"
,
func
(
sc
*
scenarioContext
)
{
middlewareScenario
(
"Invalid api key"
,
func
(
sc
*
scenarioContext
)
{
sc
.
apiKey
=
"invalid_key_test"
sc
.
apiKey
=
"invalid_key_test"
sc
.
fakeReq
(
"GET"
,
"/"
)
.
exec
()
sc
.
fakeReq
(
"GET"
,
"/"
)
.
exec
()
...
@@ -151,22 +146,17 @@ func TestMiddlewareContext(t *testing.T) {
...
@@ -151,22 +146,17 @@ func TestMiddlewareContext(t *testing.T) {
})
})
})
})
middlewareScenario
(
"UserId in session"
,
func
(
sc
*
scenarioContext
)
{
middlewareScenario
(
"Auth token service"
,
func
(
sc
*
scenarioContext
)
{
var
wasCalled
bool
sc
.
fakeReq
(
"GET"
,
"/"
)
.
handler
(
func
(
c
*
m
.
ReqContext
)
{
sc
.
userAuthTokenService
.
initContextWithTokenProvider
=
func
(
ctx
*
m
.
ReqContext
,
orgId
int64
)
bool
{
c
.
Session
.
Set
(
session
.
SESS_KEY_USERID
,
int64
(
12
))
wasCalled
=
true
})
.
exec
()
return
false
}
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
GetSignedInUserQuery
)
error
{
query
.
Result
=
&
m
.
SignedInUser
{
OrgId
:
2
,
UserId
:
12
}
return
nil
})
sc
.
fakeReq
(
"GET"
,
"/"
)
.
exec
()
sc
.
fakeReq
(
"GET"
,
"/"
)
.
exec
()
Convey
(
"should init context with user info"
,
func
()
{
Convey
(
"should call middleware"
,
func
()
{
So
(
sc
.
context
.
IsSignedIn
,
ShouldBeTrue
)
So
(
wasCalled
,
ShouldBeTrue
)
So
(
sc
.
context
.
UserId
,
ShouldEqual
,
12
)
})
})
})
})
...
@@ -487,7 +477,8 @@ func middlewareScenario(desc string, fn scenarioFunc) {
...
@@ -487,7 +477,8 @@ func middlewareScenario(desc string, fn scenarioFunc) {
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
}))
}))
sc
.
m
.
Use
(
GetContextHandler
(
nil
))
sc
.
userAuthTokenService
=
newFakeUserAuthTokenService
()
sc
.
m
.
Use
(
GetContextHandler
(
sc
.
userAuthTokenService
))
// mock out gc goroutine
// mock out gc goroutine
session
.
StartSessionGC
=
func
()
{}
session
.
StartSessionGC
=
func
()
{}
sc
.
m
.
Use
(
Sessioner
(
&
ms
.
Options
{},
0
))
sc
.
m
.
Use
(
Sessioner
(
&
ms
.
Options
{},
0
))
...
@@ -508,15 +499,16 @@ func middlewareScenario(desc string, fn scenarioFunc) {
...
@@ -508,15 +499,16 @@ func middlewareScenario(desc string, fn scenarioFunc) {
}
}
type
scenarioContext
struct
{
type
scenarioContext
struct
{
m
*
macaron
.
Macaron
m
*
macaron
.
Macaron
context
*
m
.
ReqContext
context
*
m
.
ReqContext
resp
*
httptest
.
ResponseRecorder
resp
*
httptest
.
ResponseRecorder
apiKey
string
apiKey
string
authHeader
string
authHeader
string
respJson
map
[
string
]
interface
{}
respJson
map
[
string
]
interface
{}
handlerFunc
handlerFunc
handlerFunc
handlerFunc
defaultHandler
macaron
.
Handler
defaultHandler
macaron
.
Handler
url
string
url
string
userAuthTokenService
*
fakeUserAuthTokenService
req
*
http
.
Request
req
*
http
.
Request
}
}
...
@@ -585,3 +577,25 @@ func (sc *scenarioContext) exec() {
...
@@ -585,3 +577,25 @@ func (sc *scenarioContext) exec() {
type
scenarioFunc
func
(
c
*
scenarioContext
)
type
scenarioFunc
func
(
c
*
scenarioContext
)
type
handlerFunc
func
(
c
*
m
.
ReqContext
)
type
handlerFunc
func
(
c
*
m
.
ReqContext
)
type
fakeUserAuthTokenService
struct
{
initContextWithTokenProvider
func
(
ctx
*
m
.
ReqContext
,
orgID
int64
)
bool
}
func
newFakeUserAuthTokenService
()
*
fakeUserAuthTokenService
{
return
&
fakeUserAuthTokenService
{
initContextWithTokenProvider
:
func
(
ctx
*
m
.
ReqContext
,
orgID
int64
)
bool
{
return
false
},
}
}
func
(
s
*
fakeUserAuthTokenService
)
InitContextWithToken
(
ctx
*
m
.
ReqContext
,
orgID
int64
)
bool
{
return
s
.
initContextWithTokenProvider
(
ctx
,
orgID
)
}
func
(
s
*
fakeUserAuthTokenService
)
UserAuthenticatedHook
(
user
*
m
.
User
,
c
*
m
.
ReqContext
)
error
{
return
nil
}
func
(
s
*
fakeUserAuthTokenService
)
UserSignedOutHook
(
c
*
m
.
ReqContext
)
{}
pkg/middleware/org_redirect_test.go
View file @
59d0c19b
...
@@ -7,7 +7,6 @@ import (
...
@@ -7,7 +7,6 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/bus"
m
"github.com/grafana/grafana/pkg/models"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/session"
.
"github.com/smartystreets/goconvey/convey"
.
"github.com/smartystreets/goconvey/convey"
)
)
...
@@ -15,18 +14,15 @@ func TestOrgRedirectMiddleware(t *testing.T) {
...
@@ -15,18 +14,15 @@ func TestOrgRedirectMiddleware(t *testing.T) {
Convey
(
"Can redirect to correct org"
,
t
,
func
()
{
Convey
(
"Can redirect to correct org"
,
t
,
func
()
{
middlewareScenario
(
"when setting a correct org for the user"
,
func
(
sc
*
scenarioContext
)
{
middlewareScenario
(
"when setting a correct org for the user"
,
func
(
sc
*
scenarioContext
)
{
sc
.
fakeReq
(
"GET"
,
"/"
)
.
handler
(
func
(
c
*
m
.
ReqContext
)
{
c
.
Session
.
Set
(
session
.
SESS_KEY_USERID
,
int64
(
12
))
})
.
exec
()
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
SetUsingOrgCommand
)
error
{
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
SetUsingOrgCommand
)
error
{
return
nil
return
nil
})
})
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
GetSignedInUserQuery
)
error
{
sc
.
userAuthTokenService
.
initContextWithTokenProvider
=
func
(
ctx
*
m
.
ReqContext
,
orgId
int64
)
bool
{
query
.
Result
=
&
m
.
SignedInUser
{
OrgId
:
1
,
UserId
:
12
}
ctx
.
SignedInUser
=
&
m
.
SignedInUser
{
OrgId
:
1
,
UserId
:
12
}
return
nil
ctx
.
IsSignedIn
=
true
})
return
true
}
sc
.
m
.
Get
(
"/"
,
sc
.
defaultHandler
)
sc
.
m
.
Get
(
"/"
,
sc
.
defaultHandler
)
sc
.
fakeReq
(
"GET"
,
"/?orgId=3"
)
.
exec
()
sc
.
fakeReq
(
"GET"
,
"/?orgId=3"
)
.
exec
()
...
@@ -37,14 +33,16 @@ func TestOrgRedirectMiddleware(t *testing.T) {
...
@@ -37,14 +33,16 @@ func TestOrgRedirectMiddleware(t *testing.T) {
})
})
middlewareScenario
(
"when setting an invalid org for user"
,
func
(
sc
*
scenarioContext
)
{
middlewareScenario
(
"when setting an invalid org for user"
,
func
(
sc
*
scenarioContext
)
{
sc
.
fakeReq
(
"GET"
,
"/"
)
.
handler
(
func
(
c
*
m
.
ReqContext
)
{
c
.
Session
.
Set
(
session
.
SESS_KEY_USERID
,
int64
(
12
))
})
.
exec
()
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
SetUsingOrgCommand
)
error
{
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
SetUsingOrgCommand
)
error
{
return
fmt
.
Errorf
(
""
)
return
fmt
.
Errorf
(
""
)
})
})
sc
.
userAuthTokenService
.
initContextWithTokenProvider
=
func
(
ctx
*
m
.
ReqContext
,
orgId
int64
)
bool
{
ctx
.
SignedInUser
=
&
m
.
SignedInUser
{
OrgId
:
1
,
UserId
:
12
}
ctx
.
IsSignedIn
=
true
return
true
}
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
GetSignedInUserQuery
)
error
{
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
GetSignedInUserQuery
)
error
{
query
.
Result
=
&
m
.
SignedInUser
{
OrgId
:
1
,
UserId
:
12
}
query
.
Result
=
&
m
.
SignedInUser
{
OrgId
:
1
,
UserId
:
12
}
return
nil
return
nil
...
...
pkg/middleware/quota_test.go
View file @
59d0c19b
...
@@ -74,15 +74,12 @@ func TestMiddlewareQuota(t *testing.T) {
...
@@ -74,15 +74,12 @@ func TestMiddlewareQuota(t *testing.T) {
})
})
middlewareScenario
(
"with user logged in"
,
func
(
sc
*
scenarioContext
)
{
middlewareScenario
(
"with user logged in"
,
func
(
sc
*
scenarioContext
)
{
// log us in, so we have a user_id and org_id in the context
sc
.
userAuthTokenService
.
initContextWithTokenProvider
=
func
(
ctx
*
m
.
ReqContext
,
orgId
int64
)
bool
{
sc
.
fakeReq
(
"GET"
,
"/"
)
.
handler
(
func
(
c
*
m
.
ReqContext
)
{
ctx
.
SignedInUser
=
&
m
.
SignedInUser
{
OrgId
:
2
,
UserId
:
12
}
c
.
Session
.
Set
(
session
.
SESS_KEY_USERID
,
int64
(
12
))
ctx
.
IsSignedIn
=
true
})
.
exec
()
return
true
}
bus
.
AddHandler
(
"test"
,
func
(
query
*
m
.
GetSignedInUserQuery
)
error
{
query
.
Result
=
&
m
.
SignedInUser
{
OrgId
:
2
,
UserId
:
12
}
return
nil
})
bus
.
AddHandler
(
"globalQuota"
,
func
(
query
*
m
.
GetGlobalQuotaByTargetQuery
)
error
{
bus
.
AddHandler
(
"globalQuota"
,
func
(
query
*
m
.
GetGlobalQuotaByTargetQuery
)
error
{
query
.
Result
=
&
m
.
GlobalQuotaDTO
{
query
.
Result
=
&
m
.
GlobalQuotaDTO
{
Target
:
query
.
Target
,
Target
:
query
.
Target
,
...
...
pkg/middleware/recovery_test.go
View file @
59d0c19b
...
@@ -64,7 +64,8 @@ func recoveryScenario(desc string, url string, fn scenarioFunc) {
...
@@ -64,7 +64,8 @@ func recoveryScenario(desc string, url string, fn scenarioFunc) {
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
}))
}))
sc
.
m
.
Use
(
GetContextHandler
(
nil
))
sc
.
userAuthTokenService
=
newFakeUserAuthTokenService
()
sc
.
m
.
Use
(
GetContextHandler
(
sc
.
userAuthTokenService
))
// mock out gc goroutine
// mock out gc goroutine
session
.
StartSessionGC
=
func
()
{}
session
.
StartSessionGC
=
func
()
{}
sc
.
m
.
Use
(
Sessioner
(
&
ms
.
Options
{},
0
))
sc
.
m
.
Use
(
Sessioner
(
&
ms
.
Options
{},
0
))
...
...
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