users_test.go 6.4 KB

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