account_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 users", func() {
  11. ac1cmd := m.CreateUserCommand{Login: "ac1", Email: "ac1@test.com", Name: "ac1 name"}
  12. ac2cmd := m.CreateUserCommand{Login: "ac2", Email: "ac2@test.com", Name: "ac2 name", IsAdmin: true}
  13. err := CreateUser(&ac1cmd)
  14. err = CreateUser(&ac2cmd)
  15. So(err, ShouldBeNil)
  16. ac1 := ac1cmd.Result
  17. ac2 := ac2cmd.Result
  18. Convey("Should be able to read user info projection", func() {
  19. query := m.GetUserInfoQuery{UserId: ac1.Id}
  20. err = GetUserInfo(&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 users", func() {
  26. query := m.SearchUsersQuery{Query: ""}
  27. err := SearchUsers(&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 account user", func() {
  33. cmd := m.AddAccountUserCommand{
  34. AccountId: ac1.AccountId,
  35. UserId: ac2.Id,
  36. Role: m.ROLE_VIEWER,
  37. }
  38. err := AddAccountUser(&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{UserId: ac2.Id}
  44. err := GetSignedInUser(&query)
  45. So(err, ShouldBeNil)
  46. So(query.Result.Email, ShouldEqual, "ac2@test.com")
  47. So(query.Result.AccountId, ShouldEqual, ac2.AccountId)
  48. So(query.Result.Name, ShouldEqual, "ac2 name")
  49. So(query.Result.Login, ShouldEqual, "ac2")
  50. So(query.Result.AccountRole, ShouldEqual, "Admin")
  51. So(query.Result.AccountName, ShouldEqual, "ac2@test.com")
  52. So(query.Result.IsGrafanaAdmin, ShouldBeTrue)
  53. })
  54. Convey("Can get user accounts", func() {
  55. query := m.GetUserAccountsQuery{UserId: ac2.Id}
  56. err := GetUserAccounts(&query)
  57. So(err, ShouldBeNil)
  58. So(len(query.Result), ShouldEqual, 2)
  59. })
  60. Convey("Can get account users", func() {
  61. query := m.GetAccountUsersQuery{AccountId: ac1.AccountId}
  62. err := GetAccountUsers(&query)
  63. So(err, ShouldBeNil)
  64. So(len(query.Result), ShouldEqual, 2)
  65. So(query.Result[0].Role, ShouldEqual, "Admin")
  66. })
  67. Convey("Can set using account", func() {
  68. cmd := m.SetUsingAccountCommand{UserId: ac2.Id, AccountId: ac1.Id}
  69. err := SetUsingAccount(&cmd)
  70. So(err, ShouldBeNil)
  71. Convey("SignedInUserQuery with a different account", func() {
  72. query := m.GetSignedInUserQuery{UserId: ac2.Id}
  73. err := GetSignedInUser(&query)
  74. So(err, ShouldBeNil)
  75. So(query.Result.AccountId, ShouldEqual, ac1.Id)
  76. So(query.Result.Email, ShouldEqual, "ac2@test.com")
  77. So(query.Result.Name, ShouldEqual, "ac2 name")
  78. So(query.Result.Login, ShouldEqual, "ac2")
  79. So(query.Result.AccountName, ShouldEqual, "ac1@test.com")
  80. So(query.Result.AccountRole, ShouldEqual, "Viewer")
  81. })
  82. })
  83. Convey("Cannot delete last admin account user", func() {
  84. cmd := m.RemoveAccountUserCommand{AccountId: ac1.AccountId, UserId: ac1.Id}
  85. err := RemoveAccountUser(&cmd)
  86. So(err, ShouldEqual, m.ErrLastAccountAdmin)
  87. })
  88. })
  89. })
  90. })
  91. }