您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

api_user_email_test.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2021 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 integrations
  5. import (
  6. "net/http"
  7. "testing"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestAPIListEmails(t *testing.T) {
  12. defer prepareTestEnv(t)()
  13. normalUsername := "user2"
  14. session := loginUser(t, normalUsername)
  15. token := getTokenForLoggedInUser(t, session)
  16. req := NewRequest(t, "GET", "/api/v1/user/emails?token="+token)
  17. resp := session.MakeRequest(t, req, http.StatusOK)
  18. var emails []*api.Email
  19. DecodeJSON(t, resp, &emails)
  20. assert.EqualValues(t, []*api.Email{
  21. {
  22. Email: "user2@example.com",
  23. Verified: true,
  24. Primary: true,
  25. },
  26. {
  27. Email: "user21@example.com",
  28. Verified: false,
  29. Primary: false,
  30. },
  31. }, emails)
  32. }
  33. func TestAPIAddEmail(t *testing.T) {
  34. defer prepareTestEnv(t)()
  35. normalUsername := "user2"
  36. session := loginUser(t, normalUsername)
  37. token := getTokenForLoggedInUser(t, session)
  38. opts := api.CreateEmailOption{
  39. Emails: []string{"user101@example.com"},
  40. }
  41. req := NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
  42. session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  43. opts = api.CreateEmailOption{
  44. Emails: []string{"user22@example.com"},
  45. }
  46. req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
  47. resp := session.MakeRequest(t, req, http.StatusCreated)
  48. var emails []*api.Email
  49. DecodeJSON(t, resp, &emails)
  50. assert.EqualValues(t, []*api.Email{
  51. {
  52. Email: "user22@example.com",
  53. Verified: true,
  54. Primary: false,
  55. },
  56. }, emails)
  57. }
  58. func TestAPIDeleteEmail(t *testing.T) {
  59. defer prepareTestEnv(t)()
  60. normalUsername := "user2"
  61. session := loginUser(t, normalUsername)
  62. token := getTokenForLoggedInUser(t, session)
  63. opts := api.DeleteEmailOption{
  64. Emails: []string{"user22@example.com"},
  65. }
  66. req := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
  67. session.MakeRequest(t, req, http.StatusNotFound)
  68. opts = api.DeleteEmailOption{
  69. Emails: []string{"user21@example.com"},
  70. }
  71. req = NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
  72. session.MakeRequest(t, req, http.StatusNoContent)
  73. req = NewRequest(t, "GET", "/api/v1/user/emails?token="+token)
  74. resp := session.MakeRequest(t, req, http.StatusOK)
  75. var emails []*api.Email
  76. DecodeJSON(t, resp, &emails)
  77. assert.EqualValues(t, []*api.Email{
  78. {
  79. Email: "user2@example.com",
  80. Verified: true,
  81. Primary: true,
  82. },
  83. }, emails)
  84. }