accounts_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package sqlstore
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. m "github.com/torkelo/grafana-pro/pkg/models"
  6. )
  7. func TestAccountDataAccess(t *testing.T) {
  8. Convey("Testing Account DB Access", t, func() {
  9. InitTestDB(t)
  10. Convey("Given two saved accounts", func() {
  11. ac1cmd := m.CreateAccountCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
  12. ac2cmd := m.CreateAccountCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
  13. err := CreateAccount(&ac1cmd)
  14. err = CreateAccount(&ac2cmd)
  15. So(err, ShouldBeNil)
  16. ac1 := ac1cmd.Result
  17. ac2 := ac2cmd.Result
  18. Convey("Should be able to read account info projection", func() {
  19. query := m.GetAccountInfoQuery{Id: ac1.Id}
  20. err = GetAccountInfo(&query)
  21. So(err, ShouldBeNil)
  22. So(query.Result.Email, ShouldEqual, "ac1@test.com")
  23. So(query.Result.Login, ShouldEqual, "ac1")
  24. })
  25. Convey("Can search accounts", func() {
  26. query := m.SearchAccountsQuery{Query: ""}
  27. err := SearchAccounts(&query)
  28. So(err, ShouldBeNil)
  29. So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
  30. So(query.Result[1].Email, ShouldEqual, "ac2@test.com")
  31. })
  32. Convey("Given an added collaborator", func() {
  33. cmd := m.AddCollaboratorCommand{
  34. AccountId: ac1.Id,
  35. CollaboratorId: ac2.Id,
  36. Role: m.ROLE_VIEWER,
  37. }
  38. err := AddCollaborator(&cmd)
  39. Convey("Should have been saved without error", func() {
  40. So(err, ShouldBeNil)
  41. })
  42. Convey("Can get logged in user projection", func() {
  43. query := m.GetSignedInUserQuery{AccountId: ac2.Id}
  44. err := GetSignedInUser(&query)
  45. So(err, ShouldBeNil)
  46. So(query.Result.AccountId, ShouldEqual, ac2.Id)
  47. So(query.Result.UserEmail, ShouldEqual, "ac2@test.com")
  48. So(query.Result.UserName, ShouldEqual, "ac2 name")
  49. So(query.Result.UserLogin, ShouldEqual, "ac2")
  50. So(query.Result.UserRole, ShouldEqual, "Owner")
  51. So(query.Result.UsingAccountName, ShouldEqual, "ac2 name")
  52. So(query.Result.UsingAccountId, ShouldEqual, ac2.Id)
  53. So(query.Result.IsGrafanaAdmin, ShouldBeTrue)
  54. })
  55. Convey("Can get other accounts", func() {
  56. query := m.GetOtherAccountsQuery{AccountId: ac2.Id}
  57. err := GetOtherAccounts(&query)
  58. So(err, ShouldBeNil)
  59. So(query.Result[0].Email, ShouldEqual, "ac1@test.com")
  60. })
  61. Convey("Can set using account", func() {
  62. cmd := m.SetUsingAccountCommand{AccountId: ac2.Id, UsingAccountId: ac1.Id}
  63. err := SetUsingAccount(&cmd)
  64. So(err, ShouldBeNil)
  65. Convey("Logged in user query should return correct using account info", func() {
  66. query := m.GetSignedInUserQuery{AccountId: ac2.Id}
  67. err := GetSignedInUser(&query)
  68. So(err, ShouldBeNil)
  69. So(query.Result.AccountId, ShouldEqual, ac2.Id)
  70. So(query.Result.UserEmail, ShouldEqual, "ac2@test.com")
  71. So(query.Result.UserName, ShouldEqual, "ac2 name")
  72. So(query.Result.UserLogin, ShouldEqual, "ac2")
  73. So(query.Result.UsingAccountName, ShouldEqual, "ac1 name")
  74. So(query.Result.UsingAccountId, ShouldEqual, ac1.Id)
  75. So(query.Result.UserRole, ShouldEqual, "Viewer")
  76. })
  77. })
  78. })
  79. })
  80. })
  81. }