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.

api_user_email_test.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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: "user2-2@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{"user2-3@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: "user2-3@example.com",
  53. Verified: true,
  54. Primary: false,
  55. },
  56. }, emails)
  57. opts = api.CreateEmailOption{
  58. Emails: []string{"notAEmail"},
  59. }
  60. req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
  61. session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  62. }
  63. func TestAPIDeleteEmail(t *testing.T) {
  64. defer prepareTestEnv(t)()
  65. normalUsername := "user2"
  66. session := loginUser(t, normalUsername)
  67. token := getTokenForLoggedInUser(t, session)
  68. opts := api.DeleteEmailOption{
  69. Emails: []string{"user2-3@example.com"},
  70. }
  71. req := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
  72. session.MakeRequest(t, req, http.StatusNotFound)
  73. opts = api.DeleteEmailOption{
  74. Emails: []string{"user2-2@example.com"},
  75. }
  76. req = NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
  77. session.MakeRequest(t, req, http.StatusNoContent)
  78. req = NewRequest(t, "GET", "/api/v1/user/emails?token="+token)
  79. resp := session.MakeRequest(t, req, http.StatusOK)
  80. var emails []*api.Email
  81. DecodeJSON(t, resp, &emails)
  82. assert.EqualValues(t, []*api.Email{
  83. {
  84. Email: "user2@example.com",
  85. Verified: true,
  86. Primary: true,
  87. },
  88. }, emails)
  89. }