Skip to content

Commit 9c0d81f

Browse files
authored
Merge branch 'master' into assets_dir
2 parents 23ce9fa + 0c77e07 commit 9c0d81f

File tree

17 files changed

+218
-16
lines changed

17 files changed

+218
-16
lines changed

integrations/api_helper_for_declarative_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,26 @@ func doAPICreatePullRequest(ctx APITestContext, owner, repo, baseBranch, headBra
239239
}
240240
}
241241

242+
func doAPIGetPullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) (api.PullRequest, error) {
243+
return func(t *testing.T) (api.PullRequest, error) {
244+
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d?token=%s",
245+
owner, repo, index, ctx.Token)
246+
req := NewRequest(t, http.MethodGet, urlStr)
247+
248+
expected := 200
249+
if ctx.ExpectedCode != 0 {
250+
expected = ctx.ExpectedCode
251+
}
252+
resp := ctx.Session.MakeRequest(t, req, expected)
253+
254+
json := jsoniter.ConfigCompatibleWithStandardLibrary
255+
decoder := json.NewDecoder(resp.Body)
256+
pr := api.PullRequest{}
257+
err := decoder.Decode(&pr)
258+
return pr, err
259+
}
260+
}
261+
242262
func doAPIMergePullRequest(ctx APITestContext, owner, repo string, index int64) func(*testing.T) {
243263
return func(t *testing.T) {
244264
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge?token=%s",

integrations/api_oauth2_apps_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ func testAPIDeleteOAuth2Application(t *testing.T) {
9292
session.MakeRequest(t, req, http.StatusNoContent)
9393

9494
models.AssertNotExistsBean(t, &models.OAuth2Application{UID: oldApp.UID, Name: oldApp.Name})
95+
96+
// Delete again will return not found
97+
req = NewRequest(t, "DELETE", urlStr)
98+
session.MakeRequest(t, req, http.StatusNotFound)
9599
}
96100

97101
func testAPIGetOAuth2Application(t *testing.T) {

integrations/api_user_email_test.go

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

integrations/git_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package integrations
66

77
import (
8+
"encoding/hex"
89
"fmt"
910
"io/ioutil"
1011
"math/rand"
@@ -452,26 +453,34 @@ func doMergeFork(ctx, baseCtx APITestContext, baseBranch, headBranch string) fun
452453

453454
// Then get the diff string
454455
var diffHash string
456+
var diffLength int
455457
t.Run("GetDiff", func(t *testing.T) {
456458
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(baseCtx.Username), url.PathEscape(baseCtx.Reponame), pr.Index))
457459
resp := ctx.Session.MakeRequestNilResponseHashSumRecorder(t, req, http.StatusOK)
458460
diffHash = string(resp.Hash.Sum(nil))
461+
diffLength = resp.Length
459462
})
460463

461464
// Now: Merge the PR & make sure that doesn't break the PR page or change its diff
462465
t.Run("MergePR", doAPIMergePullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index))
463466
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
464-
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash))
467+
t.Run("CheckPR", func(t *testing.T) {
468+
oldMergeBase := pr.MergeBase
469+
pr2, err := doAPIGetPullRequest(baseCtx, baseCtx.Username, baseCtx.Reponame, pr.Index)(t)
470+
assert.NoError(t, err)
471+
assert.Equal(t, oldMergeBase, pr2.MergeBase)
472+
})
473+
t.Run("EnsurDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash, diffLength))
465474

466475
// Then: Delete the head branch & make sure that doesn't break the PR page or change its diff
467476
t.Run("DeleteHeadBranch", doBranchDelete(baseCtx, baseCtx.Username, baseCtx.Reponame, headBranch))
468477
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
469-
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash))
478+
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash, diffLength))
470479

471480
// Delete the head repository & make sure that doesn't break the PR page or change its diff
472481
t.Run("DeleteHeadRepository", doAPIDeleteRepository(ctx))
473482
t.Run("EnsureCanSeePull", doEnsureCanSeePull(baseCtx, pr))
474-
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash))
483+
t.Run("EnsureDiffNoChange", doEnsureDiffNoChange(baseCtx, pr, diffHash, diffLength))
475484
}
476485
}
477486

@@ -515,14 +524,15 @@ func doEnsureCanSeePull(ctx APITestContext, pr api.PullRequest) func(t *testing.
515524
}
516525
}
517526

518-
func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffHash string) func(t *testing.T) {
527+
func doEnsureDiffNoChange(ctx APITestContext, pr api.PullRequest, diffHash string, diffLength int) func(t *testing.T) {
519528
return func(t *testing.T) {
520529
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/pulls/%d.diff", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame), pr.Index))
521530
resp := ctx.Session.MakeRequestNilResponseHashSumRecorder(t, req, http.StatusOK)
522531
actual := string(resp.Hash.Sum(nil))
532+
actualLength := resp.Length
523533

524534
equal := diffHash == actual
525-
assert.True(t, equal, "Unexpected change in the diff string: expected hash: %s but was actually: %s", diffHash, actual)
535+
assert.True(t, equal, "Unexpected change in the diff string: expected hash: %s size: %d but was actually: %s size: %d", hex.EncodeToString([]byte(diffHash)), diffLength, hex.EncodeToString([]byte(actual)), actualLength)
526536
}
527537
}
528538

models/error.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ func (err ErrEmailInvalid) Error() string {
222222
return fmt.Sprintf("e-mail invalid [email: %s]", err.Email)
223223
}
224224

225+
// ErrEmailAddressNotExist email address not exist
226+
type ErrEmailAddressNotExist struct {
227+
Email string
228+
}
229+
230+
// IsErrEmailAddressNotExist checks if an error is an ErrEmailAddressNotExist
231+
func IsErrEmailAddressNotExist(err error) bool {
232+
_, ok := err.(ErrEmailAddressNotExist)
233+
return ok
234+
}
235+
236+
func (err ErrEmailAddressNotExist) Error() string {
237+
return fmt.Sprintf("Email address does not exist [email: %s]", err.Email)
238+
}
239+
225240
// ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error.
226241
type ErrOpenIDAlreadyUsed struct {
227242
OpenID string

models/migrate.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func InsertMilestones(ms ...*Milestone) (err error) {
3939
// InsertIssues insert issues to database
4040
func InsertIssues(issues ...*Issue) error {
4141
sess := x.NewSession()
42+
defer sess.Close()
4243
if err := sess.Begin(); err != nil {
4344
return err
4445
}
@@ -194,6 +195,7 @@ func InsertPullRequests(prs ...*PullRequest) error {
194195
// InsertReleases migrates release
195196
func InsertReleases(rels ...*Release) error {
196197
sess := x.NewSession()
198+
defer sess.Close()
197199
if err := sess.Begin(); err != nil {
198200
return err
199201
}

models/oauth2_application.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error {
235235
if deleted, err := sess.Delete(&OAuth2Application{ID: id, UID: userid}); err != nil {
236236
return err
237237
} else if deleted == 0 {
238-
return fmt.Errorf("cannot find oauth2 application")
238+
return ErrOAuthApplicationNotFound{ID: id}
239239
}
240240
codes := make([]*OAuth2AuthorizationCode, 0)
241241
// delete correlating auth codes
@@ -261,6 +261,7 @@ func deleteOAuth2Application(sess *xorm.Session, id, userid int64) error {
261261
// DeleteOAuth2Application deletes the application with the given id and the grants and auth codes related to it. It checks if the userid was the creator of the app.
262262
func DeleteOAuth2Application(id, userid int64) error {
263263
sess := x.NewSession()
264+
defer sess.Close()
264265
if err := sess.Begin(); err != nil {
265266
return err
266267
}

models/pull.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ func (pr *PullRequest) SetMerged() (bool, error) {
406406
return false, fmt.Errorf("Issue.changeStatus: %v", err)
407407
}
408408

409-
if _, err := sess.Where("id = ?", pr.ID).Cols("has_merged, status, merged_commit_id, merger_id, merged_unix").Update(pr); err != nil {
409+
// We need to save all of the data used to compute this merge as it may have already been changed by TestPatch. FIXME: need to set some state to prevent TestPatch from running whilst we are merging.
410+
if _, err := sess.Where("id = ?", pr.ID).Cols("has_merged, status, merge_base, merged_commit_id, merger_id, merged_unix").Update(pr); err != nil {
410411
return false, fmt.Errorf("Failed to update pr[%d]: %v", pr.ID, err)
411412
}
412413

models/user_mail.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package models
77

88
import (
9-
"errors"
109
"fmt"
1110
"net/mail"
1211
"strings"
@@ -18,9 +17,6 @@ import (
1817
"xorm.io/builder"
1918
)
2019

21-
// ErrEmailAddressNotExist email address not exist
22-
var ErrEmailAddressNotExist = errors.New("Email address does not exist")
23-
2420
// EmailAddress is the list of all email addresses of a user. Can contain the
2521
// primary email address, but is not obligatory.
2622
type EmailAddress struct {
@@ -243,7 +239,7 @@ func DeleteEmailAddress(email *EmailAddress) (err error) {
243239
if err != nil {
244240
return err
245241
} else if deleted != 1 {
246-
return ErrEmailAddressNotExist
242+
return ErrEmailAddressNotExist{Email: email.Email}
247243
}
248244
return nil
249245
}

modules/markup/markdown/goldmark.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa
9898
}
9999
prefix = strings.Replace(prefix, "/src/", "/media/", 1)
100100

101-
lnk := string(link)
101+
lnk := strings.TrimLeft(string(link), "/")
102+
102103
lnk = giteautil.URLJoin(prefix, lnk)
103104
link = []byte(lnk)
104105
}

modules/markup/markdown/markdown_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ func TestRender_Images(t *testing.T) {
9393
test(
9494
"[!["+title+"]("+url+")]("+href+")",
9595
`<p><a href="`+href+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
96+
97+
url = "/../../.images/src/02/train.jpg"
98+
test(
99+
"!["+title+"]("+url+")",
100+
`<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
101+
102+
test(
103+
"[["+title+"|"+url+"]]",
104+
`<p><a href="`+result+`" rel="nofollow"><img src="`+result+`" title="`+title+`" alt="`+title+`"/></a></p>`)
105+
test(
106+
"[!["+title+"]("+url+")]("+href+")",
107+
`<p><a href="`+href+`" rel="nofollow"><img src="`+result+`" alt="`+title+`"/></a></p>`)
108+
96109
}
97110

98111
func testAnswers(baseURLContent, baseURLImages string) []string {

modules/queue/manager.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func (m *Manager) FlushAll(baseCtx context.Context, timeout time.Duration) error
174174
default:
175175
}
176176
mqs := m.ManagedQueues()
177+
log.Debug("Found %d Managed Queues", len(mqs))
177178
wg := sync.WaitGroup{}
178179
wg.Add(len(mqs))
179180
allEmpty := true
@@ -184,6 +185,7 @@ func (m *Manager) FlushAll(baseCtx context.Context, timeout time.Duration) error
184185
}
185186
allEmpty = false
186187
if flushable, ok := mq.Managed.(Flushable); ok {
188+
log.Debug("Flushing (flushable) queue: %s", mq.Name)
187189
go func(q *ManagedQueue) {
188190
localCtx, localCancel := context.WithCancel(ctx)
189191
pid := q.RegisterWorkers(1, start, hasTimeout, end, localCancel, true)
@@ -196,7 +198,11 @@ func (m *Manager) FlushAll(baseCtx context.Context, timeout time.Duration) error
196198
wg.Done()
197199
}(mq)
198200
} else {
199-
wg.Done()
201+
log.Debug("Queue: %s is non-empty but is not flushable - adding 100 millisecond wait", mq.Name)
202+
go func() {
203+
<-time.After(100 * time.Millisecond)
204+
wg.Done()
205+
}()
200206
}
201207

202208
}

0 commit comments

Comments
 (0)