users_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package db
  5. import (
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "gogs.io/gogs/internal/auth"
  10. "gogs.io/gogs/internal/errutil"
  11. )
  12. func Test_users(t *testing.T) {
  13. if testing.Short() {
  14. t.Skip()
  15. }
  16. t.Parallel()
  17. tables := []interface{}{new(User), new(EmailAddress)}
  18. db := &users{
  19. DB: initTestDB(t, "users", tables...),
  20. }
  21. for _, tc := range []struct {
  22. name string
  23. test func(*testing.T, *users)
  24. }{
  25. {"Authenticate", test_users_Authenticate},
  26. {"Create", test_users_Create},
  27. {"GetByEmail", test_users_GetByEmail},
  28. {"GetByID", test_users_GetByID},
  29. {"GetByUsername", test_users_GetByUsername},
  30. } {
  31. t.Run(tc.name, func(t *testing.T) {
  32. t.Cleanup(func() {
  33. err := clearTables(t, db.DB, tables...)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. })
  38. tc.test(t, db)
  39. })
  40. if t.Failed() {
  41. break
  42. }
  43. }
  44. }
  45. // TODO: Only local account is tested, tests for external account will be added
  46. // along with addressing https://github.com/gogs/gogs/issues/6115.
  47. func test_users_Authenticate(t *testing.T, db *users) {
  48. password := "pa$$word"
  49. alice, err := db.Create("alice", "[email protected]", CreateUserOpts{
  50. Password: password,
  51. })
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. t.Run("user not found", func(t *testing.T) {
  56. _, err := db.Authenticate("bob", password, -1)
  57. expErr := auth.ErrBadCredentials{Args: map[string]interface{}{"login": "bob"}}
  58. assert.Equal(t, expErr, err)
  59. })
  60. t.Run("invalid password", func(t *testing.T) {
  61. _, err := db.Authenticate(alice.Name, "bad_password", -1)
  62. expErr := auth.ErrBadCredentials{Args: map[string]interface{}{"login": alice.Name, "userID": alice.ID}}
  63. assert.Equal(t, expErr, err)
  64. })
  65. t.Run("via email and password", func(t *testing.T) {
  66. user, err := db.Authenticate(alice.Email, password, -1)
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. assert.Equal(t, alice.Name, user.Name)
  71. })
  72. t.Run("via username and password", func(t *testing.T) {
  73. user, err := db.Authenticate(alice.Name, password, -1)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. assert.Equal(t, alice.Name, user.Name)
  78. })
  79. }
  80. func test_users_Create(t *testing.T, db *users) {
  81. alice, err := db.Create("alice", "[email protected]", CreateUserOpts{
  82. Activated: true,
  83. })
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. t.Run("name not allowed", func(t *testing.T) {
  88. _, err := db.Create("-", "", CreateUserOpts{})
  89. expErr := ErrNameNotAllowed{args: errutil.Args{"reason": "reserved", "name": "-"}}
  90. assert.Equal(t, expErr, err)
  91. })
  92. t.Run("name already exists", func(t *testing.T) {
  93. _, err := db.Create(alice.Name, "", CreateUserOpts{})
  94. expErr := ErrUserAlreadyExist{args: errutil.Args{"name": alice.Name}}
  95. assert.Equal(t, expErr, err)
  96. })
  97. t.Run("email already exists", func(t *testing.T) {
  98. _, err := db.Create("bob", alice.Email, CreateUserOpts{})
  99. expErr := ErrEmailAlreadyUsed{args: errutil.Args{"email": alice.Email}}
  100. assert.Equal(t, expErr, err)
  101. })
  102. user, err := db.GetByUsername(alice.Name)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. assert.Equal(t, db.NowFunc().Format(time.RFC3339), user.Created.UTC().Format(time.RFC3339))
  107. assert.Equal(t, db.NowFunc().Format(time.RFC3339), user.Updated.UTC().Format(time.RFC3339))
  108. }
  109. func test_users_GetByEmail(t *testing.T, db *users) {
  110. t.Run("empty email", func(t *testing.T) {
  111. _, err := db.GetByEmail("")
  112. expErr := ErrUserNotExist{args: errutil.Args{"email": ""}}
  113. assert.Equal(t, expErr, err)
  114. })
  115. t.Run("ignore organization", func(t *testing.T) {
  116. // TODO: Use Orgs.Create to replace SQL hack when the method is available.
  117. org, err := db.Create("gogs", "[email protected]", CreateUserOpts{})
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. err = db.Model(&User{}).Where("id", org.ID).UpdateColumn("type", UserOrganization).Error
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. _, err = db.GetByEmail(org.Email)
  126. expErr := ErrUserNotExist{args: errutil.Args{"email": org.Email}}
  127. assert.Equal(t, expErr, err)
  128. })
  129. t.Run("by primary email", func(t *testing.T) {
  130. alice, err := db.Create("alice", "[email protected]", CreateUserOpts{})
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. _, err = db.GetByEmail(alice.Email)
  135. expErr := ErrUserNotExist{args: errutil.Args{"email": alice.Email}}
  136. assert.Equal(t, expErr, err)
  137. // Mark user as activated
  138. // TODO: Use UserEmails.Verify to replace SQL hack when the method is available.
  139. err = db.Model(&User{}).Where("id", alice.ID).UpdateColumn("is_active", true).Error
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. user, err := db.GetByEmail(alice.Email)
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. assert.Equal(t, alice.Name, user.Name)
  148. })
  149. t.Run("by secondary email", func(t *testing.T) {
  150. bob, err := db.Create("bob", "[email protected]", CreateUserOpts{})
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. // TODO: Use UserEmails.Create to replace SQL hack when the method is available.
  155. email2 := "[email protected]"
  156. err = db.Exec(`INSERT INTO email_address (uid, email) VALUES (?, ?)`, bob.ID, email2).Error
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. _, err = db.GetByEmail(email2)
  161. expErr := ErrUserNotExist{args: errutil.Args{"email": email2}}
  162. assert.Equal(t, expErr, err)
  163. // TODO: Use UserEmails.Verify to replace SQL hack when the method is available.
  164. err = db.Exec(`UPDATE email_address SET is_activated = ? WHERE email = ?`, true, email2).Error
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. user, err := db.GetByEmail(email2)
  169. if err != nil {
  170. t.Fatal(err)
  171. }
  172. assert.Equal(t, bob.Name, user.Name)
  173. })
  174. }
  175. func test_users_GetByID(t *testing.T, db *users) {
  176. alice, err := db.Create("alice", "[email protected]", CreateUserOpts{})
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. user, err := db.GetByID(alice.ID)
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. assert.Equal(t, alice.Name, user.Name)
  185. _, err = db.GetByID(404)
  186. expErr := ErrUserNotExist{args: errutil.Args{"userID": int64(404)}}
  187. assert.Equal(t, expErr, err)
  188. }
  189. func test_users_GetByUsername(t *testing.T, db *users) {
  190. alice, err := db.Create("alice", "[email protected]", CreateUserOpts{})
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. user, err := db.GetByUsername(alice.Name)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. assert.Equal(t, alice.Name, user.Name)
  199. _, err = db.GetByUsername("bad_username")
  200. expErr := ErrUserNotExist{args: errutil.Args{"name": "bad_username"}}
  201. assert.Equal(t, expErr, err)
  202. }