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.
118 lines
4.3 KiB
118 lines
4.3 KiB
8 years ago
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
2 years ago
|
package integration
|
||
8 years ago
|
|
||
|
import (
|
||
|
"net/http"
|
||
5 years ago
|
"net/http/httptest"
|
||
8 years ago
|
"path"
|
||
|
"testing"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/modules/json"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/setting"
|
||
6 years ago
|
api "code.gitea.io/gitea/modules/structs"
|
||
2 years ago
|
"code.gitea.io/gitea/tests"
|
||
8 years ago
|
|
||
8 years ago
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestRepoCommits(t *testing.T) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
8 years ago
|
|
||
8 years ago
|
session := loginUser(t, "user2")
|
||
8 years ago
|
|
||
|
// Request repository commits page
|
||
7 years ago
|
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
|
||
7 years ago
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||
8 years ago
|
|
||
8 years ago
|
doc := NewHTMLParser(t, resp.Body)
|
||
8 years ago
|
commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
|
||
|
assert.True(t, exists)
|
||
|
assert.NotEmpty(t, commitURL)
|
||
|
}
|
||
|
|
||
|
func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) {
|
||
2 years ago
|
defer tests.PrepareTestEnv(t)()
|
||
8 years ago
|
|
||
8 years ago
|
session := loginUser(t, "user2")
|
||
8 years ago
|
|
||
|
// Request repository commits page
|
||
7 years ago
|
req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
|
||
7 years ago
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||
8 years ago
|
|
||
8 years ago
|
doc := NewHTMLParser(t, resp.Body)
|
||
8 years ago
|
// Get first commit URL
|
||
|
commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href")
|
||
|
assert.True(t, exists)
|
||
|
assert.NotEmpty(t, commitURL)
|
||
|
|
||
|
// Call API to add status for commit
|
||
3 years ago
|
t.Run("CreateStatus", doAPICreateCommitStatus(NewAPITestContext(t, "user2", "repo1"), path.Base(commitURL), api.CommitStatusState(state)))
|
||
8 years ago
|
|
||
7 years ago
|
req = NewRequest(t, "GET", "/user2/repo1/commits/branch/master")
|
||
7 years ago
|
resp = session.MakeRequest(t, req, http.StatusOK)
|
||
8 years ago
|
|
||
8 years ago
|
doc = NewHTMLParser(t, resp.Body)
|
||
8 years ago
|
// Check if commit status is displayed in message column
|
||
2 years ago
|
sel := doc.doc.Find("#commits-table tbody tr td.message a.commit-statuses-trigger .commit-status")
|
||
4 years ago
|
assert.Equal(t, 1, sel.Length())
|
||
8 years ago
|
for _, class := range classes {
|
||
|
assert.True(t, sel.HasClass(class))
|
||
|
}
|
||
5 years ago
|
|
||
3 years ago
|
// By SHA
|
||
5 years ago
|
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/statuses")
|
||
3 years ago
|
reqOne := NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/"+path.Base(commitURL)+"/status")
|
||
|
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
|
||
|
|
||
3 years ago
|
// By Ref
|
||
5 years ago
|
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/statuses")
|
||
3 years ago
|
reqOne = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/master/status")
|
||
|
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
|
||
5 years ago
|
req = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/v1.1/statuses")
|
||
3 years ago
|
reqOne = NewRequest(t, "GET", "/api/v1/repos/user2/repo1/commits/v1.1/status")
|
||
|
testRepoCommitsWithStatus(t, session.MakeRequest(t, req, http.StatusOK), session.MakeRequest(t, reqOne, http.StatusOK), state)
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
func testRepoCommitsWithStatus(t *testing.T, resp, respOne *httptest.ResponseRecorder, state string) {
|
||
|
var statuses []*api.CommitStatus
|
||
|
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &statuses))
|
||
|
var status api.CombinedStatus
|
||
|
assert.NoError(t, json.Unmarshal(respOne.Body.Bytes(), &status))
|
||
|
assert.NotNil(t, status)
|
||
|
|
||
|
if assert.Len(t, statuses, 1) {
|
||
|
assert.Equal(t, api.CommitStatusState(state), statuses[0].State)
|
||
|
assert.Equal(t, setting.AppURL+"api/v1/repos/user2/repo1/statuses/65f1bf27bc3bf70f64657658635e66094edbcb4d", statuses[0].URL)
|
||
|
assert.Equal(t, "http://test.ci/", statuses[0].TargetURL)
|
||
|
assert.Equal(t, "", statuses[0].Description)
|
||
|
assert.Equal(t, "testci", statuses[0].Context)
|
||
|
|
||
|
assert.Len(t, status.Statuses, 1)
|
||
|
assert.Equal(t, statuses[0], status.Statuses[0])
|
||
|
assert.Equal(t, "65f1bf27bc3bf70f64657658635e66094edbcb4d", status.SHA)
|
||
5 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestRepoCommitsWithStatusPending(t *testing.T) {
|
||
2 years ago
|
doTestRepoCommitWithStatus(t, "pending", "octicon-dot-fill", "yellow")
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestRepoCommitsWithStatusSuccess(t *testing.T) {
|
||
2 years ago
|
doTestRepoCommitWithStatus(t, "success", "octicon-check", "green")
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestRepoCommitsWithStatusError(t *testing.T) {
|
||
2 years ago
|
doTestRepoCommitWithStatus(t, "error", "gitea-exclamation", "red")
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestRepoCommitsWithStatusFailure(t *testing.T) {
|
||
2 years ago
|
doTestRepoCommitWithStatus(t, "failure", "octicon-x", "red")
|
||
8 years ago
|
}
|
||
|
|
||
|
func TestRepoCommitsWithStatusWarning(t *testing.T) {
|
||
2 years ago
|
doTestRepoCommitWithStatus(t, "warning", "gitea-exclamation", "yellow")
|
||
8 years ago
|
}
|