mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-02 13:44:16 +09:00
test: consolidate Go tests (#39962)
Consolidate all Go tests. Coverage is identical to before. Go tests are around 5% faster while being 7% smaller. No non-test code is touched.
This commit is contained in:
1 parent
ce7364e215
commit
8abda62312
388 files changed
+18410
-29561
No files matched your search
@@ -15,54 +15,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// TestAPIManageEmailsFeatureDisabled ensures the email management API honors the
|
||||
// manage_credentials feature restriction, matching the web UI.
|
||||
func TestAPIManageEmailsFeatureDisabled(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
session := loginUser(t, "user2")
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageCredentials)
|
||||
|
||||
addReq := NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &api.CreateEmailOption{
|
||||
Emails: []string{"user2-3@example.com"},
|
||||
}).AddTokenAuth(token)
|
||||
MakeRequest(t, addReq, http.StatusNotFound)
|
||||
|
||||
delReq := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails", &api.DeleteEmailOption{
|
||||
Emails: []string{"user2-2@example.com"},
|
||||
}).AddTokenAuth(token)
|
||||
MakeRequest(t, delReq, http.StatusNotFound)
|
||||
}
|
||||
|
||||
func TestAPIListEmails(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
normalUsername := "user2"
|
||||
session := loginUser(t, normalUsername)
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
|
||||
|
||||
req := NewRequest(t, "GET", "/api/v1/user/emails").
|
||||
AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
emails := DecodeJSON(t, resp, []*api.Email{})
|
||||
|
||||
assert.Equal(t, []*api.Email{
|
||||
{
|
||||
Email: "user2@example.com",
|
||||
Verified: true,
|
||||
Primary: true,
|
||||
},
|
||||
{
|
||||
Email: "user2-2@example.com",
|
||||
Verified: false,
|
||||
Primary: false,
|
||||
},
|
||||
}, emails)
|
||||
}
|
||||
|
||||
func TestAPIAddEmail(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
@@ -70,46 +22,85 @@ func TestAPIAddEmail(t *testing.T) {
|
||||
session := loginUser(t, normalUsername)
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
opts := api.CreateEmailOption{
|
||||
Emails: []string{"user101@example.com"},
|
||||
}
|
||||
t.Run("ManageCredentialsDisabled", func(t *testing.T) {
|
||||
WithDisabledFeatures(t, setting.UserFeatureManageCredentials)
|
||||
|
||||
req := NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &opts).
|
||||
AddTokenAuth(token)
|
||||
MakeRequest(t, req, http.StatusConflict)
|
||||
addReq := NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &api.CreateEmailOption{
|
||||
Emails: []string{"user2-3@example.com"},
|
||||
}).AddTokenAuth(token)
|
||||
MakeRequest(t, addReq, http.StatusNotFound)
|
||||
|
||||
opts = api.CreateEmailOption{
|
||||
Emails: []string{"user2-3@example.com"},
|
||||
}
|
||||
req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &opts).
|
||||
AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusCreated)
|
||||
delReq := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails", &api.DeleteEmailOption{
|
||||
Emails: []string{"user2-2@example.com"},
|
||||
}).AddTokenAuth(token)
|
||||
MakeRequest(t, delReq, http.StatusNotFound)
|
||||
})
|
||||
|
||||
emails := DecodeJSON(t, resp, []*api.Email{})
|
||||
assert.Equal(t, []*api.Email{
|
||||
{
|
||||
Email: "user2@example.com",
|
||||
Verified: true,
|
||||
Primary: true,
|
||||
},
|
||||
{
|
||||
Email: "user2-2@example.com",
|
||||
Verified: false,
|
||||
Primary: false,
|
||||
},
|
||||
{
|
||||
Email: "user2-3@example.com",
|
||||
Verified: true,
|
||||
Primary: false,
|
||||
},
|
||||
}, emails)
|
||||
t.Run("List", func(t *testing.T) {
|
||||
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
|
||||
|
||||
opts = api.CreateEmailOption{
|
||||
Emails: []string{"notAEmail"},
|
||||
}
|
||||
req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &opts).
|
||||
AddTokenAuth(token)
|
||||
MakeRequest(t, req, http.StatusBadRequest)
|
||||
req := NewRequest(t, "GET", "/api/v1/user/emails").
|
||||
AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
emails := DecodeJSON(t, resp, []*api.Email{})
|
||||
|
||||
assert.Equal(t, []*api.Email{
|
||||
{
|
||||
Email: "user2@example.com",
|
||||
Verified: true,
|
||||
Primary: true,
|
||||
},
|
||||
{
|
||||
Email: "user2-2@example.com",
|
||||
Verified: false,
|
||||
Primary: false,
|
||||
},
|
||||
}, emails)
|
||||
})
|
||||
|
||||
t.Run("Add", func(t *testing.T) {
|
||||
opts := api.CreateEmailOption{
|
||||
Emails: []string{"user101@example.com"},
|
||||
}
|
||||
|
||||
req := NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &opts).
|
||||
AddTokenAuth(token)
|
||||
MakeRequest(t, req, http.StatusConflict)
|
||||
|
||||
opts = api.CreateEmailOption{
|
||||
Emails: []string{"user2-3@example.com"},
|
||||
}
|
||||
req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &opts).
|
||||
AddTokenAuth(token)
|
||||
resp := MakeRequest(t, req, http.StatusCreated)
|
||||
|
||||
emails := DecodeJSON(t, resp, []*api.Email{})
|
||||
assert.Equal(t, []*api.Email{
|
||||
{
|
||||
Email: "user2@example.com",
|
||||
Verified: true,
|
||||
Primary: true,
|
||||
},
|
||||
{
|
||||
Email: "user2-2@example.com",
|
||||
Verified: false,
|
||||
Primary: false,
|
||||
},
|
||||
{
|
||||
Email: "user2-3@example.com",
|
||||
Verified: true,
|
||||
Primary: false,
|
||||
},
|
||||
}, emails)
|
||||
|
||||
opts = api.CreateEmailOption{
|
||||
Emails: []string{"notAEmail"},
|
||||
}
|
||||
req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails", &opts).
|
||||
AddTokenAuth(token)
|
||||
MakeRequest(t, req, http.StatusBadRequest)
|
||||
})
|
||||
}
|
||||
|
||||
func TestAPIDeleteEmail(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user