Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

user_mail_test.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. "github.com/stretchr/testify/assert"
  8. )
  9. func TestGetEmailAddresses(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. emails, _ := GetEmailAddresses(int64(1))
  12. assert.Len(t, emails, 3)
  13. assert.False(t, emails[0].IsPrimary)
  14. assert.True(t, emails[2].IsActivated)
  15. assert.True(t, emails[2].IsPrimary)
  16. emails, _ = GetEmailAddresses(int64(2))
  17. assert.Len(t, emails, 2)
  18. assert.True(t, emails[0].IsPrimary)
  19. assert.True(t, emails[0].IsActivated)
  20. }
  21. func TestIsEmailUsed(t *testing.T) {
  22. assert.NoError(t, PrepareTestDatabase())
  23. isExist, _ := IsEmailUsed("")
  24. assert.True(t, isExist)
  25. isExist, _ = IsEmailUsed("user11@example.com")
  26. assert.True(t, isExist)
  27. isExist, _ = IsEmailUsed("user1234567890@example.com")
  28. assert.False(t, isExist)
  29. }
  30. func TestAddEmailAddress(t *testing.T) {
  31. assert.NoError(t, PrepareTestDatabase())
  32. assert.NoError(t, AddEmailAddress(&EmailAddress{
  33. Email: "user1234567890@example.com",
  34. IsPrimary: true,
  35. IsActivated: true,
  36. }))
  37. // ErrEmailAlreadyUsed
  38. err := AddEmailAddress(&EmailAddress{
  39. Email: "user1234567890@example.com",
  40. })
  41. assert.Error(t, err)
  42. assert.True(t, IsErrEmailAlreadyUsed(err))
  43. }
  44. func TestAddEmailAddresses(t *testing.T) {
  45. assert.NoError(t, PrepareTestDatabase())
  46. // insert multiple email address
  47. emails := make([]*EmailAddress, 2)
  48. emails[0] = &EmailAddress{
  49. Email: "user1234@example.com",
  50. IsActivated: true,
  51. }
  52. emails[1] = &EmailAddress{
  53. Email: "user5678@example.com",
  54. IsActivated: true,
  55. }
  56. assert.NoError(t, AddEmailAddresses(emails))
  57. // ErrEmailAlreadyUsed
  58. err := AddEmailAddresses(emails)
  59. assert.Error(t, err)
  60. assert.True(t, IsErrEmailAlreadyUsed(err))
  61. }
  62. func TestDeleteEmailAddress(t *testing.T) {
  63. assert.NoError(t, PrepareTestDatabase())
  64. assert.NoError(t, DeleteEmailAddress(&EmailAddress{
  65. UID: int64(1),
  66. ID: int64(1),
  67. Email: "user11@example.com",
  68. }))
  69. assert.NoError(t, DeleteEmailAddress(&EmailAddress{
  70. UID: int64(1),
  71. Email: "user12@example.com",
  72. }))
  73. // Email address does not exist
  74. err := DeleteEmailAddress(&EmailAddress{
  75. UID: int64(1),
  76. Email: "user1234567890@example.com",
  77. })
  78. assert.Error(t, err)
  79. }
  80. func TestDeleteEmailAddresses(t *testing.T) {
  81. assert.NoError(t, PrepareTestDatabase())
  82. // delete multiple email address
  83. emails := make([]*EmailAddress, 2)
  84. emails[0] = &EmailAddress{
  85. UID: int64(2),
  86. ID: int64(3),
  87. Email: "user2@example.com",
  88. }
  89. emails[1] = &EmailAddress{
  90. UID: int64(2),
  91. Email: "user21@example.com",
  92. }
  93. assert.NoError(t, DeleteEmailAddresses(emails))
  94. // ErrEmailAlreadyUsed
  95. err := DeleteEmailAddresses(emails)
  96. assert.Error(t, err)
  97. }
  98. func TestMakeEmailPrimary(t *testing.T) {
  99. assert.NoError(t, PrepareTestDatabase())
  100. email := &EmailAddress{
  101. Email: "user567890@example.com",
  102. }
  103. err := MakeEmailPrimary(email)
  104. assert.Error(t, err)
  105. assert.Equal(t, ErrEmailNotExist.Error(), err.Error())
  106. email = &EmailAddress{
  107. Email: "user11@example.com",
  108. }
  109. err = MakeEmailPrimary(email)
  110. assert.Error(t, err)
  111. assert.Equal(t, ErrEmailNotActivated.Error(), err.Error())
  112. email = &EmailAddress{
  113. Email: "user9999999@example.com",
  114. }
  115. err = MakeEmailPrimary(email)
  116. assert.Error(t, err)
  117. assert.True(t, IsErrUserNotExist(err))
  118. email = &EmailAddress{
  119. Email: "user101@example.com",
  120. }
  121. err = MakeEmailPrimary(email)
  122. assert.NoError(t, err)
  123. user, _ := GetUserByID(int64(10))
  124. assert.Equal(t, "user101@example.com", user.Email)
  125. }
  126. func TestActivate(t *testing.T) {
  127. assert.NoError(t, PrepareTestDatabase())
  128. email := &EmailAddress{
  129. ID: int64(1),
  130. UID: int64(1),
  131. Email: "user11@example.com",
  132. }
  133. assert.NoError(t, email.Activate())
  134. emails, _ := GetEmailAddresses(int64(1))
  135. assert.Len(t, emails, 3)
  136. assert.True(t, emails[0].IsActivated)
  137. assert.True(t, emails[2].IsActivated)
  138. assert.True(t, emails[2].IsPrimary)
  139. }