user_auth_test.go 6.0 KB

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