code.gitea.io/gitea@v1.22.3/tests/integration/api_user_email_test.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "net/http" 8 "testing" 9 10 auth_model "code.gitea.io/gitea/models/auth" 11 api "code.gitea.io/gitea/modules/structs" 12 "code.gitea.io/gitea/tests" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestAPIListEmails(t *testing.T) { 18 defer tests.PrepareTestEnv(t)() 19 20 normalUsername := "user2" 21 session := loginUser(t, normalUsername) 22 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser) 23 24 req := NewRequest(t, "GET", "/api/v1/user/emails"). 25 AddTokenAuth(token) 26 resp := MakeRequest(t, req, http.StatusOK) 27 28 var emails []*api.Email 29 DecodeJSON(t, resp, &emails) 30 31 assert.EqualValues(t, []*api.Email{ 32 { 33 Email: "user2@example.com", 34 Verified: true, 35 Primary: true, 36 }, 37 { 38 Email: "user2-2@example.com", 39 Verified: false, 40 Primary: false, 41 }, 42 }, emails) 43 } 44 45 func TestAPIAddEmail(t *testing.T) { 46 defer tests.PrepareTestEnv(t)() 47 48 normalUsername := "user2" 49 session := loginUser(t, normalUsername) 50 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser) 51 52 opts := api.CreateEmailOption{ 53 Emails: []string{"user101@example.com"}, 54 } 55 56 req := NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &opts). 57 AddTokenAuth(token) 58 MakeRequest(t, req, http.StatusUnprocessableEntity) 59 60 opts = api.CreateEmailOption{ 61 Emails: []string{"user2-3@example.com"}, 62 } 63 req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &opts). 64 AddTokenAuth(token) 65 resp := MakeRequest(t, req, http.StatusCreated) 66 67 var emails []*api.Email 68 DecodeJSON(t, resp, &emails) 69 assert.EqualValues(t, []*api.Email{ 70 { 71 Email: "user2@example.com", 72 Verified: true, 73 Primary: true, 74 }, 75 { 76 Email: "user2-2@example.com", 77 Verified: false, 78 Primary: false, 79 }, 80 { 81 Email: "user2-3@example.com", 82 Verified: true, 83 Primary: false, 84 }, 85 }, emails) 86 87 opts = api.CreateEmailOption{ 88 Emails: []string{"notAEmail"}, 89 } 90 req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &opts). 91 AddTokenAuth(token) 92 MakeRequest(t, req, http.StatusUnprocessableEntity) 93 } 94 95 func TestAPIDeleteEmail(t *testing.T) { 96 defer tests.PrepareTestEnv(t)() 97 98 normalUsername := "user2" 99 session := loginUser(t, normalUsername) 100 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser) 101 102 opts := api.DeleteEmailOption{ 103 Emails: []string{"user2-3@example.com"}, 104 } 105 req := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails", &opts). 106 AddTokenAuth(token) 107 MakeRequest(t, req, http.StatusNotFound) 108 109 opts = api.DeleteEmailOption{ 110 Emails: []string{"user2-2@example.com"}, 111 } 112 req = NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails", &opts). 113 AddTokenAuth(token) 114 MakeRequest(t, req, http.StatusNoContent) 115 116 req = NewRequest(t, "GET", "/api/v1/user/emails"). 117 AddTokenAuth(token) 118 resp := MakeRequest(t, req, http.StatusOK) 119 120 var emails []*api.Email 121 DecodeJSON(t, resp, &emails) 122 assert.EqualValues(t, []*api.Email{ 123 { 124 Email: "user2@example.com", 125 Verified: true, 126 Primary: true, 127 }, 128 }, emails) 129 }