2017-01-09 08:38:36 +05:30
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2017-12-03 07:50:12 +05:30
|
|
|
"fmt"
|
2017-05-26 07:08:18 +05:30
|
|
|
"path"
|
2017-02-28 07:12:10 +05:30
|
|
|
"strings"
|
2017-01-09 08:38:36 +05:30
|
|
|
"testing"
|
|
|
|
|
2019-03-27 15:03:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2017-01-09 08:38:36 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-02-28 07:12:10 +05:30
|
|
|
|
2017-01-09 08:38:36 +05:30
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAction_GetRepoPath(t *testing.T) {
|
2017-05-26 07:08:18 +05:30
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
|
|
|
|
owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
|
|
|
|
action := &Action{RepoID: repo.ID}
|
|
|
|
assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestAction_GetRepoLink(t *testing.T) {
|
2017-05-26 07:08:18 +05:30
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
|
|
|
|
owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
|
|
|
|
action := &Action{RepoID: repo.ID}
|
2017-01-09 08:38:36 +05:30
|
|
|
setting.AppSubURL = "/suburl/"
|
2017-05-26 07:08:18 +05:30
|
|
|
expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
|
|
|
|
assert.Equal(t, expected, action.GetRepoLink())
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewRepoAction(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
2017-01-25 08:19:51 +05:30
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
|
2017-01-09 08:38:36 +05:30
|
|
|
repo.Owner = user
|
|
|
|
|
|
|
|
actionBean := &Action{
|
2017-05-26 07:08:18 +05:30
|
|
|
OpType: ActionCreateRepo,
|
|
|
|
ActUserID: user.ID,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
ActUser: user,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
AssertNotExistsBean(t, actionBean)
|
|
|
|
assert.NoError(t, NewRepoAction(user, repo))
|
|
|
|
AssertExistsAndLoadBean(t, actionBean)
|
2017-02-28 07:12:10 +05:30
|
|
|
CheckConsistencyFor(t, &Action{})
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameRepoAction(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
2017-01-25 08:19:51 +05:30
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
|
2017-01-09 08:38:36 +05:30
|
|
|
repo.Owner = user
|
|
|
|
|
|
|
|
oldRepoName := repo.Name
|
|
|
|
const newRepoName = "newRepoName"
|
|
|
|
repo.Name = newRepoName
|
2017-02-28 07:12:10 +05:30
|
|
|
repo.LowerName = strings.ToLower(newRepoName)
|
2017-01-09 08:38:36 +05:30
|
|
|
|
|
|
|
actionBean := &Action{
|
2017-05-26 07:08:18 +05:30
|
|
|
OpType: ActionRenameRepo,
|
|
|
|
ActUserID: user.ID,
|
|
|
|
ActUser: user,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
|
|
|
Content: oldRepoName,
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
AssertNotExistsBean(t, actionBean)
|
|
|
|
assert.NoError(t, RenameRepoAction(user, oldRepoName, repo))
|
|
|
|
AssertExistsAndLoadBean(t, actionBean)
|
2017-02-28 07:12:10 +05:30
|
|
|
|
2017-10-05 10:13:04 +05:30
|
|
|
_, err := x.ID(repo.ID).Cols("name", "lower_name").Update(repo)
|
2017-02-28 07:12:10 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
CheckConsistencyFor(t, &Action{})
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
|
|
|
|
pushCommits := NewPushCommits()
|
|
|
|
pushCommits.Commits = []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef1",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user4@example.com",
|
|
|
|
AuthorName: "User Four",
|
|
|
|
Message: "message1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Sha1: "abcdef2",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User Two",
|
|
|
|
Message: "message2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pushCommits.Len = len(pushCommits.Commits)
|
|
|
|
|
|
|
|
payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame")
|
2017-08-28 14:47:45 +05:30
|
|
|
if assert.Len(t, payloadCommits, 2) {
|
|
|
|
assert.Equal(t, "abcdef1", payloadCommits[0].ID)
|
|
|
|
assert.Equal(t, "message1", payloadCommits[0].Message)
|
|
|
|
assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
|
|
|
|
assert.Equal(t, "User Two", payloadCommits[0].Committer.Name)
|
|
|
|
assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
|
|
|
|
assert.Equal(t, "User Four", payloadCommits[0].Author.Name)
|
|
|
|
assert.Equal(t, "user4", payloadCommits[0].Author.UserName)
|
|
|
|
|
|
|
|
assert.Equal(t, "abcdef2", payloadCommits[1].ID)
|
|
|
|
assert.Equal(t, "message2", payloadCommits[1].Message)
|
|
|
|
assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL)
|
|
|
|
assert.Equal(t, "User Two", payloadCommits[1].Committer.Name)
|
|
|
|
assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
|
|
|
|
assert.Equal(t, "User Two", payloadCommits[1].Author.Name)
|
|
|
|
assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
|
|
|
|
}
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestPushCommits_AvatarLink(t *testing.T) {
|
|
|
|
pushCommits := NewPushCommits()
|
|
|
|
pushCommits.Commits = []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef1",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user4@example.com",
|
|
|
|
AuthorName: "User Four",
|
|
|
|
Message: "message1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Sha1: "abcdef2",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User Two",
|
|
|
|
Message: "message2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pushCommits.Len = len(pushCommits.Commits)
|
|
|
|
|
|
|
|
assert.Equal(t,
|
2017-10-23 14:20:07 +05:30
|
|
|
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
|
2017-01-09 08:38:36 +05:30
|
|
|
pushCommits.AvatarLink("user2@example.com"))
|
|
|
|
|
|
|
|
assert.Equal(t,
|
2017-10-23 14:20:07 +05:30
|
|
|
"https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154?d=identicon",
|
2017-01-09 08:38:36 +05:30
|
|
|
pushCommits.AvatarLink("nonexistent@example.com"))
|
|
|
|
}
|
|
|
|
|
2019-07-14 20:18:51 +05:30
|
|
|
func TestRegExp_issueReferenceKeywordsPat(t *testing.T) {
|
|
|
|
trueTestCases := []string{
|
|
|
|
"#2",
|
|
|
|
"[#2]",
|
|
|
|
"please see go-gitea/gitea#5",
|
|
|
|
}
|
|
|
|
falseTestCases := []string{
|
|
|
|
"kb#2",
|
|
|
|
"#2xy",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range trueTestCases {
|
|
|
|
assert.True(t, issueReferenceKeywordsPat.MatchString(testCase))
|
|
|
|
}
|
|
|
|
for _, testCase := range falseTestCases {
|
|
|
|
assert.False(t, issueReferenceKeywordsPat.MatchString(testCase))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-03 07:50:12 +05:30
|
|
|
func Test_getIssueFromRef(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
|
|
|
for _, test := range []struct {
|
|
|
|
Ref string
|
|
|
|
ExpectedIssueID int64
|
|
|
|
}{
|
|
|
|
{"#2", 2},
|
|
|
|
{"reopen #2", 2},
|
|
|
|
{"user2/repo2#1", 4},
|
|
|
|
{"fixes user2/repo2#1", 4},
|
2019-06-15 09:30:32 +05:30
|
|
|
{"fixes: user2/repo2#1", 4},
|
2017-12-03 07:50:12 +05:30
|
|
|
} {
|
|
|
|
issue, err := getIssueFromRef(repo, test.Ref)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NotNil(t, issue) {
|
|
|
|
assert.EqualValues(t, test.ExpectedIssueID, issue.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, badRef := range []string{
|
|
|
|
"doesnotexist/doesnotexist#1",
|
|
|
|
fmt.Sprintf("#%d", NonexistentID),
|
|
|
|
} {
|
|
|
|
issue, err := getIssueFromRef(repo, badRef)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, issue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 08:38:36 +05:30
|
|
|
func TestUpdateIssuesCommit(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
pushCommits := []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef1",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user4@example.com",
|
|
|
|
AuthorName: "User Four",
|
2017-07-13 09:05:47 +05:30
|
|
|
Message: "start working on #FST-1, #1",
|
2017-01-09 08:38:36 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
Sha1: "abcdef2",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User Two",
|
|
|
|
Message: "a plain message",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Sha1: "abcdef2",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User Two",
|
|
|
|
Message: "close #2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-01-25 08:19:51 +05:30
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
2017-01-09 08:38:36 +05:30
|
|
|
repo.Owner = user
|
|
|
|
|
|
|
|
commentBean := &Comment{
|
|
|
|
Type: CommentTypeCommitRef,
|
|
|
|
CommitSHA: "abcdef1",
|
|
|
|
PosterID: user.ID,
|
|
|
|
IssueID: 1,
|
|
|
|
}
|
|
|
|
issueBean := &Issue{RepoID: repo.ID, Index: 2}
|
|
|
|
|
|
|
|
AssertNotExistsBean(t, commentBean)
|
|
|
|
AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
|
2019-01-04 14:52:58 +05:30
|
|
|
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
|
2017-01-09 08:38:36 +05:30
|
|
|
AssertExistsAndLoadBean(t, commentBean)
|
|
|
|
AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
|
2017-02-28 07:12:10 +05:30
|
|
|
CheckConsistencyFor(t, &Action{})
|
2019-01-04 14:52:58 +05:30
|
|
|
|
|
|
|
// Test that push to a non-default branch closes no issue.
|
|
|
|
pushCommits = []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef1",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user4@example.com",
|
|
|
|
AuthorName: "User Four",
|
|
|
|
Message: "close #1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
repo = AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
|
|
|
|
commentBean = &Comment{
|
|
|
|
Type: CommentTypeCommitRef,
|
|
|
|
CommitSHA: "abcdef1",
|
|
|
|
PosterID: user.ID,
|
|
|
|
IssueID: 6,
|
|
|
|
}
|
|
|
|
issueBean = &Issue{RepoID: repo.ID, Index: 1}
|
|
|
|
|
|
|
|
AssertNotExistsBean(t, commentBean)
|
|
|
|
AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 1}, "is_closed=1")
|
|
|
|
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
|
|
|
|
AssertExistsAndLoadBean(t, commentBean)
|
|
|
|
AssertNotExistsBean(t, issueBean, "is_closed=1")
|
|
|
|
CheckConsistencyFor(t, &Action{})
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
|
2019-06-15 09:30:32 +05:30
|
|
|
func TestUpdateIssuesCommit_Colon(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
pushCommits := []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef2",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User Two",
|
|
|
|
Message: "close: #2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
|
|
|
repo.Owner = user
|
|
|
|
|
|
|
|
issueBean := &Issue{RepoID: repo.ID, Index: 2}
|
|
|
|
|
|
|
|
AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
|
|
|
|
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
|
|
|
|
AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
|
|
|
|
CheckConsistencyFor(t, &Action{})
|
|
|
|
}
|
|
|
|
|
2019-02-11 00:57:19 +05:30
|
|
|
func TestUpdateIssuesCommit_Issue5957(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
|
|
|
|
// Test that push to a non-default branch closes an issue.
|
|
|
|
pushCommits := []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef1",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user4@example.com",
|
|
|
|
AuthorName: "User Four",
|
|
|
|
Message: "close #2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
|
|
|
|
commentBean := &Comment{
|
|
|
|
Type: CommentTypeCommitRef,
|
|
|
|
CommitSHA: "abcdef1",
|
|
|
|
PosterID: user.ID,
|
|
|
|
IssueID: 7,
|
|
|
|
}
|
|
|
|
|
|
|
|
issueBean := &Issue{RepoID: repo.ID, Index: 2, ID: 7}
|
|
|
|
|
|
|
|
AssertNotExistsBean(t, commentBean)
|
|
|
|
AssertNotExistsBean(t, issueBean, "is_closed=1")
|
|
|
|
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
|
|
|
|
AssertExistsAndLoadBean(t, commentBean)
|
|
|
|
AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
|
|
|
|
CheckConsistencyFor(t, &Action{})
|
|
|
|
}
|
|
|
|
|
2019-05-01 21:51:05 +05:30
|
|
|
func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
|
|
|
|
// Test that a push to default branch closes issue in another repo
|
|
|
|
// If the user also has push permissions to that repo
|
|
|
|
pushCommits := []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef1",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User Two",
|
|
|
|
Message: "close user2/repo1#1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
|
|
|
|
commentBean := &Comment{
|
|
|
|
Type: CommentTypeCommitRef,
|
|
|
|
CommitSHA: "abcdef1",
|
|
|
|
PosterID: user.ID,
|
|
|
|
IssueID: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
issueBean := &Issue{RepoID: 1, Index: 1, ID: 1}
|
|
|
|
|
|
|
|
AssertNotExistsBean(t, commentBean)
|
|
|
|
AssertNotExistsBean(t, issueBean, "is_closed=1")
|
|
|
|
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
|
|
|
|
AssertExistsAndLoadBean(t, commentBean)
|
|
|
|
AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
|
|
|
|
CheckConsistencyFor(t, &Action{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: 10}).(*User)
|
|
|
|
|
|
|
|
// Test that a push with close reference *can not* close issue
|
|
|
|
// If the commiter doesn't have push rights in that repo
|
|
|
|
pushCommits := []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef3",
|
|
|
|
CommitterEmail: "user10@example.com",
|
|
|
|
CommitterName: "User Ten",
|
|
|
|
AuthorEmail: "user10@example.com",
|
|
|
|
AuthorName: "User Ten",
|
|
|
|
Message: "close user3/repo3#1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: 6}).(*Repository)
|
|
|
|
commentBean := &Comment{
|
|
|
|
Type: CommentTypeCommitRef,
|
|
|
|
CommitSHA: "abcdef3",
|
|
|
|
PosterID: user.ID,
|
|
|
|
IssueID: 6,
|
|
|
|
}
|
|
|
|
|
|
|
|
issueBean := &Issue{RepoID: 3, Index: 1, ID: 6}
|
|
|
|
|
|
|
|
AssertNotExistsBean(t, commentBean)
|
|
|
|
AssertNotExistsBean(t, issueBean, "is_closed=1")
|
|
|
|
assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
|
|
|
|
AssertExistsAndLoadBean(t, commentBean)
|
|
|
|
AssertNotExistsBean(t, issueBean, "is_closed=1")
|
|
|
|
CheckConsistencyFor(t, &Action{})
|
|
|
|
}
|
|
|
|
|
2017-09-21 13:13:26 +05:30
|
|
|
func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
|
|
|
|
AssertNotExistsBean(t, actionBean)
|
|
|
|
assert.NoError(t, CommitRepoAction(opts))
|
|
|
|
AssertExistsAndLoadBean(t, actionBean)
|
|
|
|
CheckConsistencyFor(t, &Action{})
|
|
|
|
}
|
2017-01-09 08:38:36 +05:30
|
|
|
|
2017-09-21 13:13:26 +05:30
|
|
|
func TestCommitRepoAction(t *testing.T) {
|
|
|
|
samples := []struct {
|
|
|
|
userID int64
|
|
|
|
repositoryID int64
|
|
|
|
commitRepoActionOptions CommitRepoActionOptions
|
|
|
|
action Action
|
|
|
|
}{
|
2017-01-09 08:38:36 +05:30
|
|
|
{
|
2017-09-21 13:13:26 +05:30
|
|
|
userID: 2,
|
|
|
|
repositoryID: 2,
|
|
|
|
commitRepoActionOptions: CommitRepoActionOptions{
|
|
|
|
RefFullName: "refName",
|
|
|
|
OldCommitID: "oldCommitID",
|
|
|
|
NewCommitID: "newCommitID",
|
|
|
|
Commits: &PushCommits{
|
|
|
|
avatars: make(map[string]string),
|
|
|
|
Commits: []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef1",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user4@example.com",
|
|
|
|
AuthorName: "User Four",
|
|
|
|
Message: "message1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Sha1: "abcdef2",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User Two",
|
|
|
|
Message: "message2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Len: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
action: Action{
|
|
|
|
OpType: ActionCommitRepo,
|
|
|
|
RefName: "refName",
|
|
|
|
},
|
2017-01-09 08:38:36 +05:30
|
|
|
},
|
|
|
|
{
|
2017-09-21 13:13:26 +05:30
|
|
|
userID: 2,
|
|
|
|
repositoryID: 1,
|
|
|
|
commitRepoActionOptions: CommitRepoActionOptions{
|
|
|
|
RefFullName: git.TagPrefix + "v1.1",
|
|
|
|
OldCommitID: git.EmptySHA,
|
|
|
|
NewCommitID: "newCommitID",
|
|
|
|
Commits: &PushCommits{},
|
|
|
|
},
|
|
|
|
action: Action{
|
|
|
|
OpType: ActionPushTag,
|
|
|
|
RefName: "v1.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userID: 2,
|
|
|
|
repositoryID: 1,
|
|
|
|
commitRepoActionOptions: CommitRepoActionOptions{
|
|
|
|
RefFullName: git.TagPrefix + "v1.1",
|
|
|
|
OldCommitID: "oldCommitID",
|
|
|
|
NewCommitID: git.EmptySHA,
|
|
|
|
Commits: &PushCommits{},
|
|
|
|
},
|
|
|
|
action: Action{
|
|
|
|
OpType: ActionDeleteTag,
|
|
|
|
RefName: "v1.1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
userID: 2,
|
|
|
|
repositoryID: 1,
|
|
|
|
commitRepoActionOptions: CommitRepoActionOptions{
|
|
|
|
RefFullName: git.BranchPrefix + "feature/1",
|
|
|
|
OldCommitID: "oldCommitID",
|
|
|
|
NewCommitID: git.EmptySHA,
|
|
|
|
Commits: &PushCommits{},
|
|
|
|
},
|
|
|
|
action: Action{
|
|
|
|
OpType: ActionDeleteBranch,
|
|
|
|
RefName: "feature/1",
|
|
|
|
},
|
2017-01-09 08:38:36 +05:30
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-09-21 13:13:26 +05:30
|
|
|
for _, s := range samples {
|
2017-11-30 21:22:15 +05:30
|
|
|
PrepareTestEnv(t)
|
2017-09-21 13:13:26 +05:30
|
|
|
|
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
|
|
|
|
repo.Owner = user
|
|
|
|
|
|
|
|
s.commitRepoActionOptions.PusherName = user.Name
|
|
|
|
s.commitRepoActionOptions.RepoOwnerID = user.ID
|
|
|
|
s.commitRepoActionOptions.RepoName = repo.Name
|
|
|
|
|
|
|
|
s.action.ActUserID = user.ID
|
|
|
|
s.action.RepoID = repo.ID
|
2018-03-03 10:51:16 +05:30
|
|
|
s.action.Repo = repo
|
2017-09-21 13:13:26 +05:30
|
|
|
s.action.IsPrivate = repo.IsPrivate
|
|
|
|
|
|
|
|
testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTransferRepoAction(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
|
|
|
|
2017-01-25 08:19:51 +05:30
|
|
|
user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
|
2017-01-09 08:38:36 +05:30
|
|
|
|
|
|
|
repo.OwnerID = user4.ID
|
|
|
|
repo.Owner = user4
|
|
|
|
|
|
|
|
actionBean := &Action{
|
2017-05-26 07:08:18 +05:30
|
|
|
OpType: ActionTransferRepo,
|
|
|
|
ActUserID: user2.ID,
|
|
|
|
ActUser: user2,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
AssertNotExistsBean(t, actionBean)
|
|
|
|
assert.NoError(t, TransferRepoAction(user2, user2, repo))
|
|
|
|
AssertExistsAndLoadBean(t, actionBean)
|
2017-02-28 07:12:10 +05:30
|
|
|
|
2017-10-05 10:13:04 +05:30
|
|
|
_, err := x.ID(repo.ID).Cols("owner_id").Update(repo)
|
2017-02-28 07:12:10 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
CheckConsistencyFor(t, &Action{})
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestMergePullRequestAction(t *testing.T) {
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
2017-01-25 08:19:51 +05:30
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
|
|
|
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
|
2017-01-09 08:38:36 +05:30
|
|
|
repo.Owner = user
|
2017-01-25 08:19:51 +05:30
|
|
|
issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
|
2017-01-09 08:38:36 +05:30
|
|
|
|
|
|
|
actionBean := &Action{
|
2017-05-26 07:08:18 +05:30
|
|
|
OpType: ActionMergePullRequest,
|
|
|
|
ActUserID: user.ID,
|
|
|
|
ActUser: user,
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Repo: repo,
|
|
|
|
IsPrivate: repo.IsPrivate,
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
AssertNotExistsBean(t, actionBean)
|
|
|
|
assert.NoError(t, MergePullRequestAction(user, repo, issue))
|
|
|
|
AssertExistsAndLoadBean(t, actionBean)
|
2017-02-28 07:12:10 +05:30
|
|
|
CheckConsistencyFor(t, &Action{})
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFeeds(t *testing.T) {
|
|
|
|
// test with an individual user
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
2017-01-25 08:19:51 +05:30
|
|
|
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
|
2017-01-09 08:38:36 +05:30
|
|
|
|
2017-06-02 06:12:25 +05:30
|
|
|
actions, err := GetFeeds(GetFeedsOptions{
|
|
|
|
RequestedUser: user,
|
|
|
|
RequestingUserID: user.ID,
|
|
|
|
IncludePrivate: true,
|
|
|
|
OnlyPerformedBy: false,
|
2017-06-25 23:50:29 +05:30
|
|
|
IncludeDeleted: true,
|
2017-06-02 06:12:25 +05:30
|
|
|
})
|
2017-01-09 08:38:36 +05:30
|
|
|
assert.NoError(t, err)
|
2017-08-28 14:47:45 +05:30
|
|
|
if assert.Len(t, actions, 1) {
|
|
|
|
assert.EqualValues(t, 1, actions[0].ID)
|
|
|
|
assert.EqualValues(t, user.ID, actions[0].UserID)
|
|
|
|
}
|
2017-06-02 06:12:25 +05:30
|
|
|
|
|
|
|
actions, err = GetFeeds(GetFeedsOptions{
|
|
|
|
RequestedUser: user,
|
|
|
|
RequestingUserID: user.ID,
|
|
|
|
IncludePrivate: false,
|
|
|
|
OnlyPerformedBy: false,
|
|
|
|
})
|
2017-01-09 08:38:36 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, actions, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFeeds2(t *testing.T) {
|
|
|
|
// test with an organization user
|
|
|
|
assert.NoError(t, PrepareTestDatabase())
|
2017-06-02 06:12:25 +05:30
|
|
|
org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
|
2018-01-08 13:18:37 +05:30
|
|
|
const userID = 2 // user2 is an owner of the organization
|
2017-06-02 06:12:25 +05:30
|
|
|
|
|
|
|
actions, err := GetFeeds(GetFeedsOptions{
|
|
|
|
RequestedUser: org,
|
|
|
|
RequestingUserID: userID,
|
|
|
|
IncludePrivate: true,
|
|
|
|
OnlyPerformedBy: false,
|
2017-06-25 23:50:29 +05:30
|
|
|
IncludeDeleted: true,
|
2017-06-02 06:12:25 +05:30
|
|
|
})
|
2017-01-09 08:38:36 +05:30
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, actions, 1)
|
2017-08-28 14:47:45 +05:30
|
|
|
if assert.Len(t, actions, 1) {
|
|
|
|
assert.EqualValues(t, 2, actions[0].ID)
|
|
|
|
assert.EqualValues(t, org.ID, actions[0].UserID)
|
|
|
|
}
|
2017-06-02 06:12:25 +05:30
|
|
|
|
|
|
|
actions, err = GetFeeds(GetFeedsOptions{
|
|
|
|
RequestedUser: org,
|
|
|
|
RequestingUserID: userID,
|
|
|
|
IncludePrivate: false,
|
|
|
|
OnlyPerformedBy: false,
|
2017-06-25 23:50:29 +05:30
|
|
|
IncludeDeleted: true,
|
2017-06-02 06:12:25 +05:30
|
|
|
})
|
2017-01-09 08:38:36 +05:30
|
|
|
assert.NoError(t, err)
|
2017-02-28 07:12:10 +05:30
|
|
|
assert.Len(t, actions, 0)
|
2017-01-09 08:38:36 +05:30
|
|
|
}
|