2014-12-19 02:43:16 -06:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
|
|
|
m "github.com/torkelo/grafana-pro/pkg/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccountDataAccess(t *testing.T) {
|
|
|
|
|
|
|
|
Convey("Testing Account DB Access", t, func() {
|
|
|
|
InitTestDB(t)
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
Convey("Given two saved users", func() {
|
|
|
|
ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
|
|
|
ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
|
2014-12-19 04:53:27 -06:00
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
err := CreateUser(&ac1cmd)
|
|
|
|
err = CreateUser(&ac2cmd)
|
2014-12-19 03:45:22 -06:00
|
|
|
So(err, ShouldBeNil)
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2014-12-19 04:53:27 -06:00
|
|
|
ac1 := ac1cmd.Result
|
|
|
|
ac2 := ac2cmd.Result
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
Convey("Should be able to read user info projection", func() {
|
|
|
|
query := m.GetUserInfoQuery{UserId: ac1.Id}
|
|
|
|
err = GetUserInfo(&query)
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2014-12-19 03:45:22 -06:00
|
|
|
So(err, ShouldBeNil)
|
2014-12-19 04:53:27 -06:00
|
|
|
So(query.Result.Email, ShouldEqual, "ac1@test.com")
|
2015-01-16 00:45:37 -06:00
|
|
|
So(query.Result.Login, ShouldEqual, "ac1")
|
|
|
|
})
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
Convey("Can search users", func() {
|
|
|
|
query := m.SearchUsersQuery{Query: ""}
|
|
|
|
err := SearchUsers(&query)
|
2015-01-16 00:45:37 -06:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
|
|
|
|
So(query.Result[1].Email, ShouldEqual, "ac2@test.com")
|
2014-12-19 03:45:22 -06:00
|
|
|
})
|
2014-12-19 02:43:16 -06:00
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
Convey("Given an added account user", func() {
|
|
|
|
cmd := m.AddAccountUserCommand{
|
|
|
|
AccountId: ac1.AccountId,
|
|
|
|
UserId: ac2.Id,
|
|
|
|
Role: m.ROLE_VIEWER,
|
2014-12-19 03:45:22 -06:00
|
|
|
}
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
err := AddAccountUser(&cmd)
|
2015-01-16 07:32:18 -06:00
|
|
|
Convey("Should have been saved without error", func() {
|
2014-12-19 03:45:22 -06:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
2015-01-16 07:32:18 -06:00
|
|
|
Convey("Can get logged in user projection", func() {
|
2015-01-19 11:01:04 -06:00
|
|
|
query := m.GetSignedInUserQuery{UserId: ac2.Id}
|
2015-01-16 07:32:18 -06:00
|
|
|
err := GetSignedInUser(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2015-01-19 11:01:04 -06:00
|
|
|
So(query.Result.AccountId, ShouldEqual, ac2.AccountId)
|
|
|
|
So(query.Result.Email, ShouldEqual, "ac2@test.com")
|
|
|
|
So(query.Result.Name, ShouldEqual, "ac2 name")
|
|
|
|
So(query.Result.Login, ShouldEqual, "ac2")
|
|
|
|
So(query.Result.AccountRole, ShouldEqual, "Admin")
|
|
|
|
So(query.Result.AccountName, ShouldEqual, "ac2@test.com")
|
2015-01-16 07:32:18 -06:00
|
|
|
So(query.Result.IsGrafanaAdmin, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
|
2015-01-19 11:01:04 -06:00
|
|
|
Convey("Can get user accounts", func() {
|
|
|
|
query := m.GetUserAccountsQuery{UserId: ac2.Id}
|
|
|
|
err := GetUserAccounts(&query)
|
2014-12-19 04:08:49 -06:00
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2015-01-19 11:01:04 -06:00
|
|
|
So(len(query.Result), ShouldEqual, 2)
|
2014-12-19 04:08:49 -06:00
|
|
|
})
|
2014-12-19 06:12:47 -06:00
|
|
|
|
|
|
|
Convey("Can set using account", func() {
|
2015-01-19 11:01:04 -06:00
|
|
|
cmd := m.SetUsingAccountCommand{UserId: ac2.Id, AccountId: ac1.Id}
|
2014-12-19 06:12:47 -06:00
|
|
|
err := SetUsingAccount(&cmd)
|
|
|
|
So(err, ShouldBeNil)
|
2015-01-16 07:32:18 -06:00
|
|
|
|
|
|
|
Convey("Logged in user query should return correct using account info", func() {
|
2015-01-19 11:01:04 -06:00
|
|
|
query := m.GetSignedInUserQuery{UserId: ac2.Id}
|
2015-01-16 07:32:18 -06:00
|
|
|
err := GetSignedInUser(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
2015-01-19 11:01:04 -06:00
|
|
|
So(query.Result.AccountId, ShouldEqual, ac1.Id)
|
|
|
|
So(query.Result.Email, ShouldEqual, "ac2@test.com")
|
|
|
|
So(query.Result.Name, ShouldEqual, "ac2 name")
|
|
|
|
So(query.Result.Login, ShouldEqual, "ac2")
|
|
|
|
So(query.Result.AccountName, ShouldEqual, "ac1@test.com")
|
|
|
|
So(query.Result.AccountRole, ShouldEqual, "Viewer")
|
2015-01-16 07:32:18 -06:00
|
|
|
})
|
2014-12-19 06:12:47 -06:00
|
|
|
})
|
2014-12-19 03:45:22 -06:00
|
|
|
})
|
2014-12-19 02:43:16 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|