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
a00a42d9
Commit
a00a42d9
authored
Jan 30, 2018
by
Daniel Lee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
api: extract api test code to common_test.go
parent
0fb05bcf
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
109 additions
and
157 deletions
+109
-157
pkg/api/annotations_test.go
+2
-31
pkg/api/common_test.go
+105
-0
pkg/api/dashboard.go
+0
-1
pkg/api/dashboard_acl_test.go
+1
-16
pkg/api/dashboard_test.go
+1
-18
pkg/api/datasources_test.go
+0
-91
No files found.
pkg/api/annotations_test.go
View file @
a00a42d9
package
api
import
(
"path/filepath"
"testing"
"github.com/go-macaron/session"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
m
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/annotations"
macaron
"gopkg.in/macaron.v1"
.
"github.com/smartystreets/goconvey/convey"
)
...
...
@@ -202,20 +199,7 @@ func postAnnotationScenario(desc string, url string, routePattern string, role m
Convey
(
desc
+
" "
+
url
,
func
()
{
defer
bus
.
ClearBusHandlers
()
sc
:=
&
scenarioContext
{
url
:
url
,
}
viewsPath
,
_
:=
filepath
.
Abs
(
"../../public/views"
)
sc
.
m
=
macaron
.
New
()
sc
.
m
.
Use
(
macaron
.
Renderer
(
macaron
.
RenderOptions
{
Directory
:
viewsPath
,
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
}))
sc
.
m
.
Use
(
middleware
.
GetContextHandler
())
sc
.
m
.
Use
(
middleware
.
Sessioner
(
&
session
.
Options
{}))
sc
:=
setupScenarioContext
(
url
)
sc
.
defaultHandler
=
wrap
(
func
(
c
*
middleware
.
Context
)
Response
{
sc
.
context
=
c
sc
.
context
.
UserId
=
TestUserID
...
...
@@ -238,20 +222,7 @@ func putAnnotationScenario(desc string, url string, routePattern string, role m.
Convey
(
desc
+
" "
+
url
,
func
()
{
defer
bus
.
ClearBusHandlers
()
sc
:=
&
scenarioContext
{
url
:
url
,
}
viewsPath
,
_
:=
filepath
.
Abs
(
"../../public/views"
)
sc
.
m
=
macaron
.
New
()
sc
.
m
.
Use
(
macaron
.
Renderer
(
macaron
.
RenderOptions
{
Directory
:
viewsPath
,
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
}))
sc
.
m
.
Use
(
middleware
.
GetContextHandler
())
sc
.
m
.
Use
(
middleware
.
Sessioner
(
&
session
.
Options
{}))
sc
:=
setupScenarioContext
(
url
)
sc
.
defaultHandler
=
wrap
(
func
(
c
*
middleware
.
Context
)
Response
{
sc
.
context
=
c
sc
.
context
.
UserId
=
TestUserID
...
...
pkg/api/common_test.go
0 → 100644
View file @
a00a42d9
package
api
import
(
"net/http"
"net/http/httptest"
"path/filepath"
"github.com/go-macaron/session"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
"github.com/grafana/grafana/pkg/models"
macaron
"gopkg.in/macaron.v1"
.
"github.com/smartystreets/goconvey/convey"
)
func
loggedInUserScenario
(
desc
string
,
url
string
,
fn
scenarioFunc
)
{
loggedInUserScenarioWithRole
(
desc
,
"GET"
,
url
,
url
,
models
.
ROLE_EDITOR
,
fn
)
}
func
loggedInUserScenarioWithRole
(
desc
string
,
method
string
,
url
string
,
routePattern
string
,
role
models
.
RoleType
,
fn
scenarioFunc
)
{
Convey
(
desc
+
" "
+
url
,
func
()
{
defer
bus
.
ClearBusHandlers
()
sc
:=
setupScenarioContext
(
url
)
sc
.
defaultHandler
=
wrap
(
func
(
c
*
middleware
.
Context
)
Response
{
sc
.
context
=
c
sc
.
context
.
UserId
=
TestUserID
sc
.
context
.
OrgId
=
TestOrgID
sc
.
context
.
OrgRole
=
role
if
sc
.
handlerFunc
!=
nil
{
return
sc
.
handlerFunc
(
sc
.
context
)
}
return
nil
})
switch
method
{
case
"GET"
:
sc
.
m
.
Get
(
routePattern
,
sc
.
defaultHandler
)
case
"DELETE"
:
sc
.
m
.
Delete
(
routePattern
,
sc
.
defaultHandler
)
}
fn
(
sc
)
})
}
func
(
sc
*
scenarioContext
)
fakeReq
(
method
,
url
string
)
*
scenarioContext
{
sc
.
resp
=
httptest
.
NewRecorder
()
req
,
err
:=
http
.
NewRequest
(
method
,
url
,
nil
)
So
(
err
,
ShouldBeNil
)
sc
.
req
=
req
return
sc
}
func
(
sc
*
scenarioContext
)
fakeReqWithParams
(
method
,
url
string
,
queryParams
map
[
string
]
string
)
*
scenarioContext
{
sc
.
resp
=
httptest
.
NewRecorder
()
req
,
err
:=
http
.
NewRequest
(
method
,
url
,
nil
)
q
:=
req
.
URL
.
Query
()
for
k
,
v
:=
range
queryParams
{
q
.
Add
(
k
,
v
)
}
req
.
URL
.
RawQuery
=
q
.
Encode
()
So
(
err
,
ShouldBeNil
)
sc
.
req
=
req
return
sc
}
type
scenarioContext
struct
{
m
*
macaron
.
Macaron
context
*
middleware
.
Context
resp
*
httptest
.
ResponseRecorder
handlerFunc
handlerFunc
defaultHandler
macaron
.
Handler
req
*
http
.
Request
url
string
}
func
(
sc
*
scenarioContext
)
exec
()
{
sc
.
m
.
ServeHTTP
(
sc
.
resp
,
sc
.
req
)
}
type
scenarioFunc
func
(
c
*
scenarioContext
)
type
handlerFunc
func
(
c
*
middleware
.
Context
)
Response
func
setupScenarioContext
(
url
string
)
*
scenarioContext
{
sc
:=
&
scenarioContext
{
url
:
url
,
}
viewsPath
,
_
:=
filepath
.
Abs
(
"../../public/views"
)
sc
.
m
=
macaron
.
New
()
sc
.
m
.
Use
(
macaron
.
Renderer
(
macaron
.
RenderOptions
{
Directory
:
viewsPath
,
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
}))
sc
.
m
.
Use
(
middleware
.
GetContextHandler
())
sc
.
m
.
Use
(
middleware
.
Sessioner
(
&
session
.
Options
{}))
return
sc
}
pkg/api/dashboard.go
View file @
a00a42d9
...
...
@@ -51,7 +51,6 @@ func GetDashboard(c *middleware.Context) Response {
guardian
:=
guardian
.
NewDashboardGuardian
(
dash
.
Id
,
c
.
OrgId
,
c
.
SignedInUser
)
if
canView
,
err
:=
guardian
.
CanView
();
err
!=
nil
||
!
canView
{
fmt
.
Printf
(
"%v"
,
err
)
return
dashboardGuardianResponse
(
err
)
}
...
...
pkg/api/dashboard_acl_test.go
View file @
a00a42d9
package
api
import
(
"path/filepath"
"testing"
"github.com/go-macaron/session"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/middleware"
m
"github.com/grafana/grafana/pkg/models"
macaron
"gopkg.in/macaron.v1"
.
"github.com/smartystreets/goconvey/convey"
)
...
...
@@ -236,19 +233,7 @@ func postAclScenario(desc string, url string, routePattern string, role m.RoleTy
Convey
(
desc
+
" "
+
url
,
func
()
{
defer
bus
.
ClearBusHandlers
()
sc
:=
&
scenarioContext
{
url
:
url
,
}
viewsPath
,
_
:=
filepath
.
Abs
(
"../../public/views"
)
sc
.
m
=
macaron
.
New
()
sc
.
m
.
Use
(
macaron
.
Renderer
(
macaron
.
RenderOptions
{
Directory
:
viewsPath
,
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
}))
sc
.
m
.
Use
(
middleware
.
GetContextHandler
())
sc
.
m
.
Use
(
middleware
.
Sessioner
(
&
session
.
Options
{}))
sc
:=
setupScenarioContext
(
url
)
sc
.
defaultHandler
=
wrap
(
func
(
c
*
middleware
.
Context
)
Response
{
sc
.
context
=
c
...
...
pkg/api/dashboard_test.go
View file @
a00a42d9
...
...
@@ -2,12 +2,8 @@ package api
import
(
"encoding/json"
"path/filepath"
"testing"
macaron
"gopkg.in/macaron.v1"
"github.com/go-macaron/session"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
...
...
@@ -488,20 +484,7 @@ func postDashboardScenario(desc string, url string, routePattern string, role m.
Convey
(
desc
+
" "
+
url
,
func
()
{
defer
bus
.
ClearBusHandlers
()
sc
:=
&
scenarioContext
{
url
:
url
,
}
viewsPath
,
_
:=
filepath
.
Abs
(
"../../public/views"
)
sc
.
m
=
macaron
.
New
()
sc
.
m
.
Use
(
macaron
.
Renderer
(
macaron
.
RenderOptions
{
Directory
:
viewsPath
,
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
}))
sc
.
m
.
Use
(
middleware
.
GetContextHandler
())
sc
.
m
.
Use
(
middleware
.
Sessioner
(
&
session
.
Options
{}))
sc
:=
setupScenarioContext
(
url
)
sc
.
defaultHandler
=
wrap
(
func
(
c
*
middleware
.
Context
)
Response
{
sc
.
context
=
c
sc
.
context
.
UserId
=
TestUserID
...
...
pkg/api/datasources_test.go
View file @
a00a42d9
...
...
@@ -2,17 +2,11 @@ package api
import
(
"encoding/json"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"github.com/grafana/grafana/pkg/models"
macaron
"gopkg.in/macaron.v1"
"github.com/go-macaron/session"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/middleware"
.
"github.com/smartystreets/goconvey/convey"
)
...
...
@@ -54,88 +48,3 @@ func TestDataSourcesProxy(t *testing.T) {
})
})
}
func
loggedInUserScenario
(
desc
string
,
url
string
,
fn
scenarioFunc
)
{
loggedInUserScenarioWithRole
(
desc
,
"GET"
,
url
,
url
,
models
.
ROLE_EDITOR
,
fn
)
}
func
loggedInUserScenarioWithRole
(
desc
string
,
method
string
,
url
string
,
routePattern
string
,
role
models
.
RoleType
,
fn
scenarioFunc
)
{
Convey
(
desc
+
" "
+
url
,
func
()
{
defer
bus
.
ClearBusHandlers
()
sc
:=
&
scenarioContext
{
url
:
url
,
}
viewsPath
,
_
:=
filepath
.
Abs
(
"../../public/views"
)
sc
.
m
=
macaron
.
New
()
sc
.
m
.
Use
(
macaron
.
Renderer
(
macaron
.
RenderOptions
{
Directory
:
viewsPath
,
Delims
:
macaron
.
Delims
{
Left
:
"[["
,
Right
:
"]]"
},
}))
sc
.
m
.
Use
(
middleware
.
GetContextHandler
())
sc
.
m
.
Use
(
middleware
.
Sessioner
(
&
session
.
Options
{}))
sc
.
defaultHandler
=
wrap
(
func
(
c
*
middleware
.
Context
)
Response
{
sc
.
context
=
c
sc
.
context
.
UserId
=
TestUserID
sc
.
context
.
OrgId
=
TestOrgID
sc
.
context
.
OrgRole
=
role
if
sc
.
handlerFunc
!=
nil
{
return
sc
.
handlerFunc
(
sc
.
context
)
}
return
nil
})
switch
method
{
case
"GET"
:
sc
.
m
.
Get
(
routePattern
,
sc
.
defaultHandler
)
case
"DELETE"
:
sc
.
m
.
Delete
(
routePattern
,
sc
.
defaultHandler
)
}
fn
(
sc
)
})
}
func
(
sc
*
scenarioContext
)
fakeReq
(
method
,
url
string
)
*
scenarioContext
{
sc
.
resp
=
httptest
.
NewRecorder
()
req
,
err
:=
http
.
NewRequest
(
method
,
url
,
nil
)
So
(
err
,
ShouldBeNil
)
sc
.
req
=
req
return
sc
}
func
(
sc
*
scenarioContext
)
fakeReqWithParams
(
method
,
url
string
,
queryParams
map
[
string
]
string
)
*
scenarioContext
{
sc
.
resp
=
httptest
.
NewRecorder
()
req
,
err
:=
http
.
NewRequest
(
method
,
url
,
nil
)
q
:=
req
.
URL
.
Query
()
for
k
,
v
:=
range
queryParams
{
q
.
Add
(
k
,
v
)
}
req
.
URL
.
RawQuery
=
q
.
Encode
()
So
(
err
,
ShouldBeNil
)
sc
.
req
=
req
return
sc
}
type
scenarioContext
struct
{
m
*
macaron
.
Macaron
context
*
middleware
.
Context
resp
*
httptest
.
ResponseRecorder
handlerFunc
handlerFunc
defaultHandler
macaron
.
Handler
req
*
http
.
Request
url
string
}
func
(
sc
*
scenarioContext
)
exec
()
{
sc
.
m
.
ServeHTTP
(
sc
.
resp
,
sc
.
req
)
}
type
scenarioFunc
func
(
c
*
scenarioContext
)
type
handlerFunc
func
(
c
*
middleware
.
Context
)
Response
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