|
| 1 | +// Copyright 2019 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 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "path" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/models" |
| 13 | + api "code.gitea.io/sdk/gitea" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func TestPullCreate_CommitStatus(t *testing.T) { |
| 19 | + prepareTestEnv(t) |
| 20 | + session := loginUser(t, "user1") |
| 21 | + testRepoFork(t, session, "user2", "repo1", "user1", "repo1") |
| 22 | + testEditFileToNewBranch(t, session, "user1", "repo1", "master", "status1", "README.md", "status1") |
| 23 | + |
| 24 | + url := path.Join("user1", "repo1", "compare", "master...status1") |
| 25 | + req := NewRequestWithValues(t, "POST", url, |
| 26 | + map[string]string{ |
| 27 | + "_csrf": GetCSRF(t, session, url), |
| 28 | + "title": "pull request from status1", |
| 29 | + }, |
| 30 | + ) |
| 31 | + session.MakeRequest(t, req, http.StatusFound) |
| 32 | + |
| 33 | + req = NewRequest(t, "GET", "/user1/repo1/pulls") |
| 34 | + resp := session.MakeRequest(t, req, http.StatusOK) |
| 35 | + doc := NewHTMLParser(t, resp.Body) |
| 36 | + |
| 37 | + // Request repository commits page |
| 38 | + req = NewRequest(t, "GET", "/user1/repo1/pulls/1/commits") |
| 39 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 40 | + doc = NewHTMLParser(t, resp.Body) |
| 41 | + |
| 42 | + // Get first commit URL |
| 43 | + commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href") |
| 44 | + assert.True(t, exists) |
| 45 | + assert.NotEmpty(t, commitURL) |
| 46 | + |
| 47 | + commitID := path.Base(commitURL) |
| 48 | + |
| 49 | + statusList := []models.CommitStatusState{ |
| 50 | + models.CommitStatusPending, |
| 51 | + models.CommitStatusError, |
| 52 | + models.CommitStatusFailure, |
| 53 | + models.CommitStatusWarning, |
| 54 | + models.CommitStatusSuccess, |
| 55 | + } |
| 56 | + |
| 57 | + statesIcons := map[models.CommitStatusState]string{ |
| 58 | + models.CommitStatusPending: "circle icon yellow", |
| 59 | + models.CommitStatusSuccess: "check icon green", |
| 60 | + models.CommitStatusError: "warning icon red", |
| 61 | + models.CommitStatusFailure: "remove icon red", |
| 62 | + models.CommitStatusWarning: "warning sign icon yellow", |
| 63 | + } |
| 64 | + |
| 65 | + // Update commit status, and check if icon is updated as well |
| 66 | + for _, status := range statusList { |
| 67 | + |
| 68 | + // Call API to add status for commit |
| 69 | + token := getTokenForLoggedInUser(t, session) |
| 70 | + req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/user1/repo1/statuses/%s?token=%s", commitID, token), |
| 71 | + api.CreateStatusOption{ |
| 72 | + State: api.StatusState(status), |
| 73 | + TargetURL: "http://test.ci/", |
| 74 | + Description: "", |
| 75 | + Context: "testci", |
| 76 | + }, |
| 77 | + ) |
| 78 | + session.MakeRequest(t, req, http.StatusCreated) |
| 79 | + |
| 80 | + req = NewRequestf(t, "GET", "/user1/repo1/pulls/1/commits") |
| 81 | + resp = session.MakeRequest(t, req, http.StatusOK) |
| 82 | + doc = NewHTMLParser(t, resp.Body) |
| 83 | + |
| 84 | + commitURL, exists = doc.doc.Find("#commits-table tbody tr td.sha a").Last().Attr("href") |
| 85 | + assert.True(t, exists) |
| 86 | + assert.NotEmpty(t, commitURL) |
| 87 | + assert.EqualValues(t, commitID, path.Base(commitURL)) |
| 88 | + |
| 89 | + cls, ok := doc.doc.Find("#commits-table tbody tr td.message i.commit-status").Last().Attr("class") |
| 90 | + assert.True(t, ok) |
| 91 | + assert.EqualValues(t, "commit-status "+statesIcons[status], cls) |
| 92 | + } |
| 93 | +} |
0 commit comments