本站源代码
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.

172 lines
3.9KB

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