2017-02-08 07:20:07 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-01-13 10:10:19 -06:00
|
|
|
"fmt"
|
2019-09-11 07:43:05 -05:00
|
|
|
"net/http"
|
2017-02-08 07:20:07 -06:00
|
|
|
"testing"
|
2019-09-11 07:43:05 -05:00
|
|
|
"time"
|
2017-02-08 07:20:07 -06:00
|
|
|
|
2020-01-13 10:10:19 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2017-02-08 07:20:07 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-09-11 07:43:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2017-02-08 07:20:07 -06:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2019-09-11 07:43:05 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-02-08 07:20:07 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUserApiEndpoint(t *testing.T) {
|
|
|
|
Convey("Given a user is logged in", t, func() {
|
|
|
|
mockResult := models.SearchUserQueryResult{
|
|
|
|
Users: []*models.UserSearchHitDTO{
|
|
|
|
{Name: "user1"},
|
|
|
|
{Name: "user2"},
|
|
|
|
},
|
|
|
|
TotalCount: 2,
|
|
|
|
}
|
|
|
|
|
2019-09-11 07:43:05 -05:00
|
|
|
loggedInUserScenario("When calling GET on", "api/users/:id", func(sc *scenarioContext) {
|
|
|
|
fakeNow := time.Date(2019, 2, 11, 17, 30, 40, 0, time.UTC)
|
|
|
|
bus.AddHandler("test", func(query *models.GetUserProfileQuery) error {
|
|
|
|
query.Result = models.UserProfileDTO{
|
|
|
|
Id: int64(1),
|
|
|
|
Email: "daniel@grafana.com",
|
|
|
|
Name: "Daniel",
|
|
|
|
Login: "danlee",
|
|
|
|
OrgId: int64(2),
|
|
|
|
IsGrafanaAdmin: true,
|
|
|
|
IsDisabled: false,
|
|
|
|
IsExternal: false,
|
|
|
|
UpdatedAt: fakeNow,
|
2019-09-28 06:12:33 -05:00
|
|
|
CreatedAt: fakeNow,
|
2019-09-11 07:43:05 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
bus.AddHandler("test", func(query *models.GetAuthInfoQuery) error {
|
|
|
|
query.Result = &models.UserAuth{
|
|
|
|
AuthModule: models.AuthModuleLDAP,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.handlerFunc = GetUserByID
|
2020-01-13 10:10:19 -06:00
|
|
|
avatarUrl := dtos.GetGravatarUrl("daniel@grafana.com")
|
2019-09-11 07:43:05 -05:00
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
|
|
|
|
2020-01-13 10:10:19 -06:00
|
|
|
expected := fmt.Sprintf(`
|
2019-09-11 07:43:05 -05:00
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"email": "daniel@grafana.com",
|
|
|
|
"name": "Daniel",
|
|
|
|
"login": "danlee",
|
|
|
|
"theme": "",
|
|
|
|
"orgId": 2,
|
|
|
|
"isGrafanaAdmin": true,
|
|
|
|
"isDisabled": false,
|
|
|
|
"isExternal": true,
|
|
|
|
"authLabels": [
|
|
|
|
"LDAP"
|
|
|
|
],
|
2020-01-13 10:10:19 -06:00
|
|
|
"avatarUrl": "%s",
|
2019-09-28 06:12:33 -05:00
|
|
|
"updatedAt": "2019-02-11T17:30:40Z",
|
|
|
|
"createdAt": "2019-02-11T17:30:40Z"
|
2019-09-11 07:43:05 -05:00
|
|
|
}
|
2020-01-13 10:10:19 -06:00
|
|
|
`, avatarUrl)
|
2019-09-11 07:43:05 -05:00
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, sc.resp.Code)
|
|
|
|
require.JSONEq(t, expected, sc.resp.Body.String())
|
|
|
|
})
|
|
|
|
|
2019-09-30 14:54:09 -05:00
|
|
|
loggedInUserScenario("When calling GET on", "/api/users/lookup", func(sc *scenarioContext) {
|
|
|
|
fakeNow := time.Date(2019, 2, 11, 17, 30, 40, 0, time.UTC)
|
|
|
|
bus.AddHandler("test", func(query *models.GetUserByLoginQuery) error {
|
|
|
|
require.Equal(t, "danlee", query.LoginOrEmail)
|
|
|
|
|
|
|
|
query.Result = &models.User{
|
|
|
|
Id: int64(1),
|
|
|
|
Email: "daniel@grafana.com",
|
|
|
|
Name: "Daniel",
|
|
|
|
Login: "danlee",
|
|
|
|
Theme: "light",
|
|
|
|
IsAdmin: true,
|
|
|
|
OrgId: int64(2),
|
|
|
|
IsDisabled: false,
|
|
|
|
Updated: fakeNow,
|
|
|
|
Created: fakeNow,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.handlerFunc = GetUserByLoginOrEmail
|
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{"loginOrEmail": "danlee"}).exec()
|
|
|
|
|
|
|
|
expected := `
|
|
|
|
{
|
|
|
|
"id": 1,
|
|
|
|
"email": "daniel@grafana.com",
|
|
|
|
"name": "Daniel",
|
|
|
|
"login": "danlee",
|
|
|
|
"theme": "light",
|
|
|
|
"orgId": 2,
|
|
|
|
"isGrafanaAdmin": true,
|
|
|
|
"isDisabled": false,
|
|
|
|
"authLabels": null,
|
|
|
|
"isExternal": false,
|
2020-01-13 10:10:19 -06:00
|
|
|
"avatarUrl": "",
|
2019-09-30 14:54:09 -05:00
|
|
|
"updatedAt": "2019-02-11T17:30:40Z",
|
|
|
|
"createdAt": "2019-02-11T17:30:40Z"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, sc.resp.Code)
|
|
|
|
require.JSONEq(t, expected, sc.resp.Body.String())
|
|
|
|
})
|
|
|
|
|
2017-02-08 07:20:07 -06:00
|
|
|
loggedInUserScenario("When calling GET on", "/api/users", func(sc *scenarioContext) {
|
|
|
|
var sentLimit int
|
|
|
|
var sendPage int
|
|
|
|
bus.AddHandler("test", func(query *models.SearchUsersQuery) error {
|
|
|
|
query.Result = mockResult
|
|
|
|
|
|
|
|
sentLimit = query.Limit
|
|
|
|
sendPage = query.Page
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.handlerFunc = SearchUsers
|
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
|
|
|
|
|
|
|
So(sentLimit, ShouldEqual, 1000)
|
|
|
|
So(sendPage, ShouldEqual, 1)
|
|
|
|
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(len(respJSON.MustArray()), ShouldEqual, 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
loggedInUserScenario("When calling GET with page and limit querystring parameters on", "/api/users", func(sc *scenarioContext) {
|
|
|
|
var sentLimit int
|
|
|
|
var sendPage int
|
|
|
|
bus.AddHandler("test", func(query *models.SearchUsersQuery) error {
|
|
|
|
query.Result = mockResult
|
|
|
|
|
|
|
|
sentLimit = query.Limit
|
|
|
|
sendPage = query.Page
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.handlerFunc = SearchUsers
|
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{"perpage": "10", "page": "2"}).exec()
|
|
|
|
|
|
|
|
So(sentLimit, ShouldEqual, 10)
|
|
|
|
So(sendPage, ShouldEqual, 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
loggedInUserScenario("When calling GET on", "/api/users/search", func(sc *scenarioContext) {
|
|
|
|
var sentLimit int
|
|
|
|
var sendPage int
|
|
|
|
bus.AddHandler("test", func(query *models.SearchUsersQuery) error {
|
|
|
|
query.Result = mockResult
|
|
|
|
|
|
|
|
sentLimit = query.Limit
|
|
|
|
sendPage = query.Page
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.handlerFunc = SearchUsersWithPaging
|
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{}).exec()
|
|
|
|
|
|
|
|
So(sentLimit, ShouldEqual, 1000)
|
|
|
|
So(sendPage, ShouldEqual, 1)
|
|
|
|
|
|
|
|
respJSON, err := simplejson.NewJson(sc.resp.Body.Bytes())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
So(respJSON.Get("totalCount").MustInt(), ShouldEqual, 2)
|
|
|
|
So(len(respJSON.Get("users").MustArray()), ShouldEqual, 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
loggedInUserScenario("When calling GET with page and perpage querystring parameters on", "/api/users/search", func(sc *scenarioContext) {
|
|
|
|
var sentLimit int
|
|
|
|
var sendPage int
|
|
|
|
bus.AddHandler("test", func(query *models.SearchUsersQuery) error {
|
|
|
|
query.Result = mockResult
|
|
|
|
|
|
|
|
sentLimit = query.Limit
|
|
|
|
sendPage = query.Page
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
sc.handlerFunc = SearchUsersWithPaging
|
|
|
|
sc.fakeReqWithParams("GET", sc.url, map[string]string{"perpage": "10", "page": "2"}).exec()
|
|
|
|
|
|
|
|
So(sentLimit, ShouldEqual, 10)
|
|
|
|
So(sendPage, ShouldEqual, 2)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|