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)
|
|
|
|
|
2014-12-19 03:45:22 -06:00
|
|
|
Convey("Given two saved accounts", func() {
|
2015-01-16 07:32:18 -06:00
|
|
|
ac1cmd := m.CreateAccountCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
|
|
|
|
ac2cmd := m.CreateAccountCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
|
2014-12-19 04:53:27 -06:00
|
|
|
|
|
|
|
err := CreateAccount(&ac1cmd)
|
|
|
|
err = CreateAccount(&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
|
|
|
|
|
2014-12-19 03:45:22 -06:00
|
|
|
Convey("Should be able to read account info projection", func() {
|
|
|
|
query := m.GetAccountInfoQuery{Id: ac1.Id}
|
|
|
|
err = GetAccountInfo(&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")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Can search accounts", func() {
|
|
|
|
query := m.SearchAccountsQuery{Query: ""}
|
|
|
|
err := SearchAccounts(&query)
|
|
|
|
|
|
|
|
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-16 07:32:18 -06:00
|
|
|
Convey("Given an added collaborator", func() {
|
2014-12-19 03:45:22 -06:00
|
|
|
cmd := m.AddCollaboratorCommand{
|
2015-01-15 03:52:22 -06:00
|
|
|
AccountId: ac1.Id,
|
|
|
|
CollaboratorId: ac2.Id,
|
2015-01-16 04:54:19 -06:00
|
|
|
Role: m.ROLE_VIEWER,
|
2014-12-19 03:45:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
err := AddCollaborator(&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() {
|
|
|
|
query := m.GetSignedInUserQuery{AccountId: ac2.Id}
|
|
|
|
err := GetSignedInUser(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(query.Result.AccountId, ShouldEqual, ac2.Id)
|
|
|
|
So(query.Result.UserEmail, ShouldEqual, "ac2@test.com")
|
|
|
|
So(query.Result.UserName, ShouldEqual, "ac2 name")
|
|
|
|
So(query.Result.UserLogin, ShouldEqual, "ac2")
|
|
|
|
So(query.Result.UserRole, ShouldEqual, "Owner")
|
|
|
|
So(query.Result.UsingAccountName, ShouldEqual, "ac2 name")
|
|
|
|
So(query.Result.UsingAccountId, ShouldEqual, ac2.Id)
|
|
|
|
So(query.Result.IsGrafanaAdmin, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
|
2014-12-19 04:08:49 -06:00
|
|
|
Convey("Can get other accounts", func() {
|
|
|
|
query := m.GetOtherAccountsQuery{AccountId: ac2.Id}
|
|
|
|
err := GetOtherAccounts(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
|
|
|
|
})
|
2014-12-19 06:12:47 -06:00
|
|
|
|
|
|
|
Convey("Can set using account", func() {
|
|
|
|
cmd := m.SetUsingAccountCommand{AccountId: ac2.Id, UsingAccountId: ac1.Id}
|
|
|
|
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() {
|
|
|
|
query := m.GetSignedInUserQuery{AccountId: ac2.Id}
|
|
|
|
err := GetSignedInUser(&query)
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(query.Result.AccountId, ShouldEqual, ac2.Id)
|
|
|
|
So(query.Result.UserEmail, ShouldEqual, "ac2@test.com")
|
|
|
|
So(query.Result.UserName, ShouldEqual, "ac2 name")
|
|
|
|
So(query.Result.UserLogin, ShouldEqual, "ac2")
|
|
|
|
So(query.Result.UsingAccountName, ShouldEqual, "ac1 name")
|
|
|
|
So(query.Result.UsingAccountId, ShouldEqual, ac1.Id)
|
|
|
|
So(query.Result.UserRole, ShouldEqual, "Viewer")
|
|
|
|
})
|
2014-12-19 06:12:47 -06:00
|
|
|
})
|
2014-12-19 03:45:22 -06:00
|
|
|
})
|
2014-12-19 02:43:16 -06:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|