A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.1 KiB

  1. // Copyright 2017 The Gitea 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 models
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/modules/util"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestGetEmailAddresses(t *testing.T) {
  11. assert.NoError(t, PrepareTestDatabase())
  12. emails, _ := GetEmailAddresses(int64(1))
  13. if assert.Len(t, emails, 3) {
  14. assert.False(t, emails[0].IsPrimary)
  15. assert.True(t, emails[2].IsActivated)
  16. assert.True(t, emails[2].IsPrimary)
  17. }
  18. emails, _ = GetEmailAddresses(int64(2))
  19. if assert.Len(t, emails, 2) {
  20. assert.True(t, emails[0].IsPrimary)
  21. assert.True(t, emails[0].IsActivated)
  22. }
  23. }
  24. func TestIsEmailUsed(t *testing.T) {
  25. assert.NoError(t, PrepareTestDatabase())
  26. isExist, _ := IsEmailUsed("")
  27. assert.True(t, isExist)
  28. isExist, _ = IsEmailUsed("user11@example.com")
  29. assert.True(t, isExist)
  30. isExist, _ = IsEmailUsed("user1234567890@example.com")
  31. assert.False(t, isExist)
  32. }
  33. func TestAddEmailAddress(t *testing.T) {
  34. assert.NoError(t, PrepareTestDatabase())
  35. assert.NoError(t, AddEmailAddress(&EmailAddress{
  36. Email: "user1234567890@example.com",
  37. IsPrimary: true,
  38. IsActivated: true,
  39. }))
  40. // ErrEmailAlreadyUsed
  41. err := AddEmailAddress(&EmailAddress{
  42. Email: "user1234567890@example.com",
  43. })
  44. assert.Error(t, err)
  45. assert.True(t, IsErrEmailAlreadyUsed(err))
  46. }
  47. func TestAddEmailAddresses(t *testing.T) {
  48. assert.NoError(t, PrepareTestDatabase())
  49. // insert multiple email address
  50. emails := make([]*EmailAddress, 2)
  51. emails[0] = &EmailAddress{
  52. Email: "user1234@example.com",
  53. IsActivated: true,
  54. }
  55. emails[1] = &EmailAddress{
  56. Email: "user5678@example.com",
  57. IsActivated: true,
  58. }
  59. assert.NoError(t, AddEmailAddresses(emails))
  60. // ErrEmailAlreadyUsed
  61. err := AddEmailAddresses(emails)
  62. assert.Error(t, err)
  63. assert.True(t, IsErrEmailAlreadyUsed(err))
  64. }
  65. func TestDeleteEmailAddress(t *testing.T) {
  66. assert.NoError(t, PrepareTestDatabase())
  67. assert.NoError(t, DeleteEmailAddress(&EmailAddress{
  68. UID: int64(1),
  69. ID: int64(1),
  70. Email: "user11@example.com",
  71. }))
  72. assert.NoError(t, DeleteEmailAddress(&EmailAddress{
  73. UID: int64(1),
  74. Email: "user12@example.com",
  75. }))
  76. // Email address does not exist
  77. err := DeleteEmailAddress(&EmailAddress{
  78. UID: int64(1),
  79. Email: "user1234567890@example.com",
  80. })
  81. assert.Error(t, err)
  82. }
  83. func TestDeleteEmailAddresses(t *testing.T) {
  84. assert.NoError(t, PrepareTestDatabase())
  85. // delete multiple email address
  86. emails := make([]*EmailAddress, 2)
  87. emails[0] = &EmailAddress{
  88. UID: int64(2),
  89. ID: int64(3),
  90. Email: "user2@example.com",
  91. }
  92. emails[1] = &EmailAddress{
  93. UID: int64(2),
  94. Email: "user21@example.com",
  95. }
  96. assert.NoError(t, DeleteEmailAddresses(emails))
  97. // ErrEmailAlreadyUsed
  98. err := DeleteEmailAddresses(emails)
  99. assert.Error(t, err)
  100. }
  101. func TestMakeEmailPrimary(t *testing.T) {
  102. assert.NoError(t, PrepareTestDatabase())
  103. email := &EmailAddress{
  104. Email: "user567890@example.com",
  105. }
  106. err := MakeEmailPrimary(email)
  107. assert.Error(t, err)
  108. assert.Equal(t, ErrEmailNotExist.Error(), err.Error())
  109. email = &EmailAddress{
  110. Email: "user11@example.com",
  111. }
  112. err = MakeEmailPrimary(email)
  113. assert.Error(t, err)
  114. assert.Equal(t, ErrEmailNotActivated.Error(), err.Error())
  115. email = &EmailAddress{
  116. Email: "user9999999@example.com",
  117. }
  118. err = MakeEmailPrimary(email)
  119. assert.Error(t, err)
  120. assert.True(t, IsErrUserNotExist(err))
  121. email = &EmailAddress{
  122. Email: "user101@example.com",
  123. }
  124. err = MakeEmailPrimary(email)
  125. assert.NoError(t, err)
  126. user, _ := GetUserByID(int64(10))
  127. assert.Equal(t, "user101@example.com", user.Email)
  128. }
  129. func TestActivate(t *testing.T) {
  130. assert.NoError(t, PrepareTestDatabase())
  131. email := &EmailAddress{
  132. ID: int64(1),
  133. UID: int64(1),
  134. Email: "user11@example.com",
  135. }
  136. assert.NoError(t, email.Activate())
  137. emails, _ := GetEmailAddresses(int64(1))
  138. assert.Len(t, emails, 3)
  139. assert.True(t, emails[0].IsActivated)
  140. assert.True(t, emails[2].IsActivated)
  141. assert.True(t, emails[2].IsPrimary)
  142. }
  143. func TestListEmails(t *testing.T) {
  144. assert.NoError(t, PrepareTestDatabase())
  145. // Must find all users and their emails
  146. opts := &SearchEmailOptions{}
  147. emails, count, err := SearchEmails(opts)
  148. assert.NoError(t, err)
  149. assert.NotEqual(t, int64(0), count)
  150. assert.True(t, count > 5)
  151. contains := func(match func(s *SearchEmailResult) bool) bool {
  152. for _, v := range emails {
  153. if match(v) {
  154. return true
  155. }
  156. }
  157. return false
  158. }
  159. assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 18 }))
  160. // 'user3' is an organization
  161. assert.False(t, contains(func(s *SearchEmailResult) bool { return s.UID == 3 }))
  162. // Must find no records
  163. opts = &SearchEmailOptions{Keyword: "NOTFOUND"}
  164. emails, count, err = SearchEmails(opts)
  165. assert.NoError(t, err)
  166. assert.Equal(t, int64(0), count)
  167. // Must find users 'user2', 'user28', etc.
  168. opts = &SearchEmailOptions{Keyword: "user2"}
  169. emails, count, err = SearchEmails(opts)
  170. assert.NoError(t, err)
  171. assert.NotEqual(t, int64(0), count)
  172. assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 2 }))
  173. assert.True(t, contains(func(s *SearchEmailResult) bool { return s.UID == 27 }))
  174. // Must find only primary addresses (i.e. from the `user` table)
  175. opts = &SearchEmailOptions{IsPrimary: util.OptionalBoolTrue}
  176. emails, count, err = SearchEmails(opts)
  177. assert.NoError(t, err)
  178. assert.True(t, contains(func(s *SearchEmailResult) bool { return s.IsPrimary }))
  179. assert.False(t, contains(func(s *SearchEmailResult) bool { return !s.IsPrimary }))
  180. // Must find only inactive addresses (i.e. not validated)
  181. opts = &SearchEmailOptions{IsActivated: util.OptionalBoolFalse}
  182. emails, count, err = SearchEmails(opts)
  183. assert.NoError(t, err)
  184. assert.True(t, contains(func(s *SearchEmailResult) bool { return !s.IsActivated }))
  185. assert.False(t, contains(func(s *SearchEmailResult) bool { return s.IsActivated }))
  186. // Must find more than one page, but retrieve only one
  187. opts = &SearchEmailOptions{
  188. ListOptions: ListOptions{
  189. PageSize: 5,
  190. Page: 1,
  191. },
  192. }
  193. emails, count, err = SearchEmails(opts)
  194. assert.NoError(t, err)
  195. assert.Equal(t, 5, len(emails))
  196. assert.True(t, count > int64(len(emails)))
  197. }