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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "net/http"
  6. "testing"
  7. auth_model "code.gitea.io/gitea/models/auth"
  8. api "code.gitea.io/gitea/modules/structs"
  9. "code.gitea.io/gitea/tests"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestAPIListEmails(t *testing.T) {
  13. defer tests.PrepareTestEnv(t)()
  14. normalUsername := "user2"
  15. session := loginUser(t, normalUsername)
  16. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
  17. req := NewRequest(t, "GET", "/api/v1/user/emails?token="+token)
  18. resp := MakeRequest(t, req, http.StatusOK)
  19. var emails []*api.Email
  20. DecodeJSON(t, resp, &emails)
  21. assert.EqualValues(t, []*api.Email{
  22. {
  23. Email: "user2@example.com",
  24. Verified: true,
  25. Primary: true,
  26. },
  27. {
  28. Email: "user2-2@example.com",
  29. Verified: false,
  30. Primary: false,
  31. },
  32. }, emails)
  33. }
  34. func TestAPIAddEmail(t *testing.T) {
  35. defer tests.PrepareTestEnv(t)()
  36. normalUsername := "user2"
  37. session := loginUser(t, normalUsername)
  38. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser)
  39. opts := api.CreateEmailOption{
  40. Emails: []string{"user101@example.com"},
  41. }
  42. req := NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
  43. MakeRequest(t, req, http.StatusUnprocessableEntity)
  44. opts = api.CreateEmailOption{
  45. Emails: []string{"user2-3@example.com"},
  46. }
  47. req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
  48. resp := MakeRequest(t, req, http.StatusCreated)
  49. var emails []*api.Email
  50. DecodeJSON(t, resp, &emails)
  51. assert.EqualValues(t, []*api.Email{
  52. {
  53. Email: "user2-3@example.com",
  54. Verified: true,
  55. Primary: false,
  56. },
  57. }, emails)
  58. opts = api.CreateEmailOption{
  59. Emails: []string{"notAEmail"},
  60. }
  61. req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
  62. MakeRequest(t, req, http.StatusUnprocessableEntity)
  63. }
  64. func TestAPIDeleteEmail(t *testing.T) {
  65. defer tests.PrepareTestEnv(t)()
  66. normalUsername := "user2"
  67. session := loginUser(t, normalUsername)
  68. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser)
  69. opts := api.DeleteEmailOption{
  70. Emails: []string{"user2-3@example.com"},
  71. }
  72. req := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
  73. MakeRequest(t, req, http.StatusNotFound)
  74. opts = api.DeleteEmailOption{
  75. Emails: []string{"user2-2@example.com"},
  76. }
  77. req = NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
  78. MakeRequest(t, req, http.StatusNoContent)
  79. req = NewRequest(t, "GET", "/api/v1/user/emails?token="+token)
  80. resp := MakeRequest(t, req, http.StatusOK)
  81. var emails []*api.Email
  82. DecodeJSON(t, resp, &emails)
  83. assert.EqualValues(t, []*api.Email{
  84. {
  85. Email: "user2@example.com",
  86. Verified: true,
  87. Primary: true,
  88. },
  89. }, emails)
  90. }