user_auth_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package sqlstore
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "time"
  7. . "github.com/smartystreets/goconvey/convey"
  8. "golang.org/x/oauth2"
  9. m "github.com/grafana/grafana/pkg/models"
  10. )
  11. func TestUserAuth(t *testing.T) {
  12. InitTestDB(t)
  13. Convey("Given 5 users", t, func() {
  14. var err error
  15. var cmd *m.CreateUserCommand
  16. for i := 0; i < 5; i++ {
  17. cmd = &m.CreateUserCommand{
  18. Email: fmt.Sprint("user", i, "@test.com"),
  19. Name: fmt.Sprint("user", i),
  20. Login: fmt.Sprint("loginuser", i),
  21. }
  22. err = CreateUser(context.Background(), cmd)
  23. So(err, ShouldBeNil)
  24. }
  25. Reset(func() {
  26. _, err := x.Exec("DELETE FROM org_user WHERE 1=1")
  27. So(err, ShouldBeNil)
  28. _, err = x.Exec("DELETE FROM org WHERE 1=1")
  29. So(err, ShouldBeNil)
  30. _, err = x.Exec("DELETE FROM " + dialect.Quote("user") + " WHERE 1=1")
  31. So(err, ShouldBeNil)
  32. _, err = x.Exec("DELETE FROM user_auth WHERE 1=1")
  33. So(err, ShouldBeNil)
  34. })
  35. Convey("Can find existing user", func() {
  36. // By Login
  37. login := "loginuser0"
  38. query := &m.GetUserByAuthInfoQuery{Login: login}
  39. err = GetUserByAuthInfo(query)
  40. So(err, ShouldBeNil)
  41. So(query.Result.Login, ShouldEqual, login)
  42. // By ID
  43. id := query.Result.Id
  44. query = &m.GetUserByAuthInfoQuery{UserId: id}
  45. err = GetUserByAuthInfo(query)
  46. So(err, ShouldBeNil)
  47. So(query.Result.Id, ShouldEqual, id)
  48. // By Email
  49. email := "user1@test.com"
  50. query = &m.GetUserByAuthInfoQuery{Email: email}
  51. err = GetUserByAuthInfo(query)
  52. So(err, ShouldBeNil)
  53. So(query.Result.Email, ShouldEqual, email)
  54. // Don't find nonexistent user
  55. email = "nonexistent@test.com"
  56. query = &m.GetUserByAuthInfoQuery{Email: email}
  57. err = GetUserByAuthInfo(query)
  58. So(err, ShouldEqual, m.ErrUserNotFound)
  59. So(query.Result, ShouldBeNil)
  60. })
  61. Convey("Can set & locate by AuthModule and AuthId", func() {
  62. // get nonexistent user_auth entry
  63. query := &m.GetUserByAuthInfoQuery{AuthModule: "test", AuthId: "test"}
  64. err = GetUserByAuthInfo(query)
  65. So(err, ShouldEqual, m.ErrUserNotFound)
  66. So(query.Result, ShouldBeNil)
  67. // create user_auth entry
  68. login := "loginuser0"
  69. query.Login = login
  70. err = GetUserByAuthInfo(query)
  71. So(err, ShouldBeNil)
  72. So(query.Result.Login, ShouldEqual, login)
  73. // get via user_auth
  74. query = &m.GetUserByAuthInfoQuery{AuthModule: "test", AuthId: "test"}
  75. err = GetUserByAuthInfo(query)
  76. So(err, ShouldBeNil)
  77. So(query.Result.Login, ShouldEqual, login)
  78. // get with non-matching id
  79. id := query.Result.Id
  80. query.UserId = id + 1
  81. err = GetUserByAuthInfo(query)
  82. So(err, ShouldBeNil)
  83. So(query.Result.Login, ShouldEqual, "loginuser1")
  84. // get via user_auth
  85. query = &m.GetUserByAuthInfoQuery{AuthModule: "test", AuthId: "test"}
  86. err = GetUserByAuthInfo(query)
  87. So(err, ShouldBeNil)
  88. So(query.Result.Login, ShouldEqual, "loginuser1")
  89. // remove user
  90. _, err = x.Exec("DELETE FROM "+dialect.Quote("user")+" WHERE id=?", query.Result.Id)
  91. So(err, ShouldBeNil)
  92. // get via user_auth for deleted user
  93. query = &m.GetUserByAuthInfoQuery{AuthModule: "test", AuthId: "test"}
  94. err = GetUserByAuthInfo(query)
  95. So(err, ShouldEqual, m.ErrUserNotFound)
  96. So(query.Result, ShouldBeNil)
  97. })
  98. Convey("Can set & retrieve oauth token information", func() {
  99. token := &oauth2.Token{
  100. AccessToken: "testaccess",
  101. RefreshToken: "testrefresh",
  102. Expiry: time.Now(),
  103. TokenType: "Bearer",
  104. }
  105. // Find a user to set tokens on
  106. login := "loginuser0"
  107. // Calling GetUserByAuthInfoQuery on an existing user will populate an entry in the user_auth table
  108. query := &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test", AuthId: "test"}
  109. err = GetUserByAuthInfo(query)
  110. So(err, ShouldBeNil)
  111. So(query.Result.Login, ShouldEqual, login)
  112. cmd := &m.UpdateAuthInfoCommand{
  113. UserId: query.Result.Id,
  114. AuthId: query.AuthId,
  115. AuthModule: query.AuthModule,
  116. OAuthToken: token,
  117. }
  118. err = UpdateAuthInfo(cmd)
  119. So(err, ShouldBeNil)
  120. getAuthQuery := &m.GetAuthInfoQuery{
  121. UserId: query.Result.Id,
  122. }
  123. err = GetAuthInfo(getAuthQuery)
  124. So(err, ShouldBeNil)
  125. So(getAuthQuery.Result.OAuthAccessToken, ShouldEqual, token.AccessToken)
  126. So(getAuthQuery.Result.OAuthRefreshToken, ShouldEqual, token.RefreshToken)
  127. So(getAuthQuery.Result.OAuthTokenType, ShouldEqual, token.TokenType)
  128. })
  129. Convey("Always return the most recently used auth_module", func() {
  130. // Find a user to set tokens on
  131. login := "loginuser0"
  132. // Calling GetUserByAuthInfoQuery on an existing user will populate an entry in the user_auth table
  133. // Make the first log-in during the past
  134. getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
  135. query := &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test1", AuthId: "test1"}
  136. err = GetUserByAuthInfo(query)
  137. getTime = time.Now
  138. So(err, ShouldBeNil)
  139. So(query.Result.Login, ShouldEqual, login)
  140. // Add a second auth module for this user
  141. // Have this module's last log-in be more recent
  142. getTime = func() time.Time { return time.Now().AddDate(0, 0, -1) }
  143. query = &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test2", AuthId: "test2"}
  144. err = GetUserByAuthInfo(query)
  145. getTime = time.Now
  146. So(err, ShouldBeNil)
  147. So(query.Result.Login, ShouldEqual, login)
  148. // Get the latest entry by not supply an authmodule or authid
  149. getAuthQuery := &m.GetAuthInfoQuery{
  150. UserId: query.Result.Id,
  151. }
  152. err = GetAuthInfo(getAuthQuery)
  153. So(err, ShouldBeNil)
  154. So(getAuthQuery.Result.AuthModule, ShouldEqual, "test2")
  155. // "log in" again with the first auth module
  156. updateAuthCmd := &m.UpdateAuthInfoCommand{UserId: query.Result.Id, AuthModule: "test1", AuthId: "test1"}
  157. err = UpdateAuthInfo(updateAuthCmd)
  158. So(err, ShouldBeNil)
  159. // Get the latest entry by not supply an authmodule or authid
  160. getAuthQuery = &m.GetAuthInfoQuery{
  161. UserId: query.Result.Id,
  162. }
  163. err = GetAuthInfo(getAuthQuery)
  164. So(err, ShouldBeNil)
  165. So(getAuthQuery.Result.AuthModule, ShouldEqual, "test1")
  166. })
  167. })
  168. }