users_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. }
  41. }
  42. // TODO: Only local account is tested, tests for external account will be added
  43. // along with addressing https://github.com/gogs/gogs/issues/6115.
  44. func test_users_Authenticate(t *testing.T, db *users) {
  45. password := "pa$$word"
  46. alice, err := db.Create("alice", "alice@example.com", CreateUserOpts{
  47. Password: password,
  48. })
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. t.Run("user not found", func(t *testing.T) {
  53. _, err := db.Authenticate("bob", password, -1)
  54. expErr := auth.ErrBadCredentials{Args: map[string]interface{}{"login": "bob"}}
  55. assert.Equal(t, expErr, err)
  56. })
  57. t.Run("invalid password", func(t *testing.T) {
  58. _, err := db.Authenticate(alice.Name, "bad_password", -1)
  59. expErr := auth.ErrBadCredentials{Args: map[string]interface{}{"login": alice.Name, "userID": alice.ID}}
  60. assert.Equal(t, expErr, err)
  61. })
  62. t.Run("via email and password", func(t *testing.T) {
  63. user, err := db.Authenticate(alice.Email, password, -1)
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. assert.Equal(t, alice.Name, user.Name)
  68. })
  69. t.Run("via username and password", func(t *testing.T) {
  70. user, err := db.Authenticate(alice.Name, password, -1)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. assert.Equal(t, alice.Name, user.Name)
  75. })
  76. }
  77. func test_users_Create(t *testing.T, db *users) {
  78. alice, err := db.Create("alice", "alice@example.com", CreateUserOpts{
  79. Activated: true,
  80. })
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. t.Run("name not allowed", func(t *testing.T) {
  85. _, err := db.Create("-", "", CreateUserOpts{})
  86. expErr := ErrNameNotAllowed{args: errutil.Args{"reason": "reserved", "name": "-"}}
  87. assert.Equal(t, expErr, err)
  88. })
  89. t.Run("name already exists", func(t *testing.T) {
  90. _, err := db.Create(alice.Name, "", CreateUserOpts{})
  91. expErr := ErrUserAlreadyExist{args: errutil.Args{"name": alice.Name}}
  92. assert.Equal(t, expErr, err)
  93. })
  94. t.Run("email already exists", func(t *testing.T) {
  95. _, err := db.Create("bob", alice.Email, CreateUserOpts{})
  96. expErr := ErrEmailAlreadyUsed{args: errutil.Args{"email": alice.Email}}
  97. assert.Equal(t, expErr, err)
  98. })
  99. user, err := db.GetByUsername(alice.Name)
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. assert.Equal(t, db.NowFunc().Format(time.RFC3339), user.Created.UTC().Format(time.RFC3339))
  104. assert.Equal(t, db.NowFunc().Format(time.RFC3339), user.Updated.UTC().Format(time.RFC3339))
  105. }
  106. func test_users_GetByEmail(t *testing.T, db *users) {
  107. t.Run("empty email", func(t *testing.T) {
  108. _, err := db.GetByEmail("")
  109. expErr := ErrUserNotExist{args: errutil.Args{"email": ""}}
  110. assert.Equal(t, expErr, err)
  111. })
  112. t.Run("ignore organization", func(t *testing.T) {
  113. // TODO: Use Orgs.Create to replace SQL hack when the method is available.
  114. org, err := db.Create("gogs", "gogs@exmaple.com", CreateUserOpts{})
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. err = db.Exec(`UPDATE user SET type = ? WHERE id = ?`, UserOrganization, org.ID).Error
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. _, err = db.GetByEmail(org.Email)
  123. expErr := ErrUserNotExist{args: errutil.Args{"email": org.Email}}
  124. assert.Equal(t, expErr, err)
  125. })
  126. t.Run("by primary email", func(t *testing.T) {
  127. alice, err := db.Create("alice", "alice@exmaple.com", CreateUserOpts{})
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. _, err = db.GetByEmail(alice.Email)
  132. expErr := ErrUserNotExist{args: errutil.Args{"email": alice.Email}}
  133. assert.Equal(t, expErr, err)
  134. // Mark user as activated
  135. // TODO: Use UserEmails.Verify to replace SQL hack when the method is available.
  136. err = db.Exec(`UPDATE user SET is_active = ? WHERE id = ?`, true, alice.ID).Error
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. user, err := db.GetByEmail(alice.Email)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. assert.Equal(t, alice.Name, user.Name)
  145. })
  146. t.Run("by secondary email", func(t *testing.T) {
  147. bob, err := db.Create("bob", "bob@example.com", CreateUserOpts{})
  148. if err != nil {
  149. t.Fatal(err)
  150. }
  151. // TODO: Use UserEmails.Create to replace SQL hack when the method is available.
  152. email2 := "bob2@exmaple.com"
  153. err = db.Exec(`INSERT INTO email_address (uid, email) VALUES (?, ?)`, bob.ID, email2).Error
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. _, err = db.GetByEmail(email2)
  158. expErr := ErrUserNotExist{args: errutil.Args{"email": email2}}
  159. assert.Equal(t, expErr, err)
  160. // TODO: Use UserEmails.Verify to replace SQL hack when the method is available.
  161. err = db.Exec(`UPDATE email_address SET is_activated = ? WHERE email = ?`, true, email2).Error
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. user, err := db.GetByEmail(email2)
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. assert.Equal(t, bob.Name, user.Name)
  170. })
  171. }
  172. func test_users_GetByID(t *testing.T, db *users) {
  173. alice, err := db.Create("alice", "alice@exmaple.com", CreateUserOpts{})
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. user, err := db.GetByID(alice.ID)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. assert.Equal(t, alice.Name, user.Name)
  182. _, err = db.GetByID(404)
  183. expErr := ErrUserNotExist{args: errutil.Args{"userID": int64(404)}}
  184. assert.Equal(t, expErr, err)
  185. }
  186. func test_users_GetByUsername(t *testing.T, db *users) {
  187. alice, err := db.Create("alice", "alice@exmaple.com", CreateUserOpts{})
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. user, err := db.GetByUsername(alice.Name)
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. assert.Equal(t, alice.Name, user.Name)
  196. _, err = db.GetByUsername("bad_username")
  197. expErr := ErrUserNotExist{args: errutil.Args{"name": "bad_username"}}
  198. assert.Equal(t, expErr, err)
  199. }