2017-01-09 06:08:36 +03:00
package models
import (
2017-05-26 04:38:18 +03:00
"path"
2017-02-28 04:42:10 +03:00
"strings"
2017-01-09 06:08:36 +03:00
"testing"
2017-09-21 10:43:26 +03:00
"code.gitea.io/git"
2017-01-09 06:08:36 +03:00
"code.gitea.io/gitea/modules/setting"
2017-02-28 04:42:10 +03:00
2017-01-09 06:08:36 +03:00
"github.com/stretchr/testify/assert"
)
func TestAction_GetRepoPath ( t * testing . T ) {
2017-05-26 04:38:18 +03:00
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 06:08:36 +03:00
}
func TestAction_GetRepoLink ( t * testing . T ) {
2017-05-26 04:38:18 +03:00
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 06:08:36 +03:00
setting . AppSubURL = "/suburl/"
2017-05-26 04:38:18 +03:00
expected := path . Join ( setting . AppSubURL , owner . Name , repo . Name )
assert . Equal ( t , expected , action . GetRepoLink ( ) )
2017-01-09 06:08:36 +03:00
}
func TestNewRepoAction ( t * testing . T ) {
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
user := AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
repo := AssertExistsAndLoadBean ( t , & Repository { OwnerID : user . ID } ) . ( * Repository )
2017-01-09 06:08:36 +03:00
repo . Owner = user
actionBean := & Action {
2017-05-26 04:38:18 +03:00
OpType : ActionCreateRepo ,
ActUserID : user . ID ,
RepoID : repo . ID ,
ActUser : user ,
Repo : repo ,
IsPrivate : repo . IsPrivate ,
2017-01-09 06:08:36 +03:00
}
AssertNotExistsBean ( t , actionBean )
assert . NoError ( t , NewRepoAction ( user , repo ) )
AssertExistsAndLoadBean ( t , actionBean )
2017-02-28 04:42:10 +03:00
CheckConsistencyFor ( t , & Action { } )
2017-01-09 06:08:36 +03:00
}
func TestRenameRepoAction ( t * testing . T ) {
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
user := AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
repo := AssertExistsAndLoadBean ( t , & Repository { OwnerID : user . ID } ) . ( * Repository )
2017-01-09 06:08:36 +03:00
repo . Owner = user
oldRepoName := repo . Name
const newRepoName = "newRepoName"
repo . Name = newRepoName
2017-02-28 04:42:10 +03:00
repo . LowerName = strings . ToLower ( newRepoName )
2017-01-09 06:08:36 +03:00
actionBean := & Action {
2017-05-26 04:38:18 +03:00
OpType : ActionRenameRepo ,
ActUserID : user . ID ,
ActUser : user ,
RepoID : repo . ID ,
Repo : repo ,
IsPrivate : repo . IsPrivate ,
Content : oldRepoName ,
2017-01-09 06:08:36 +03:00
}
AssertNotExistsBean ( t , actionBean )
assert . NoError ( t , RenameRepoAction ( user , oldRepoName , repo ) )
AssertExistsAndLoadBean ( t , actionBean )
2017-02-28 04:42:10 +03:00
2017-10-05 07:43:04 +03:00
_ , err := x . ID ( repo . ID ) . Cols ( "name" , "lower_name" ) . Update ( repo )
2017-02-28 04:42:10 +03:00
assert . NoError ( t , err )
CheckConsistencyFor ( t , & Action { } )
2017-01-09 06:08:36 +03:00
}
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 12:17:45 +03:00
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 06:08:36 +03:00
}
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 ,
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f" ,
pushCommits . AvatarLink ( "user2@example.com" ) )
assert . Equal ( t ,
"https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154" ,
pushCommits . AvatarLink ( "nonexistent@example.com" ) )
}
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 06:35:47 +03:00
Message : "start working on #FST-1, #1" ,
2017-01-09 06:08:36 +03:00
} ,
{
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 05:49:51 +03:00
user := AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
repo := AssertExistsAndLoadBean ( t , & Repository { ID : 1 } ) . ( * Repository )
2017-01-09 06:08:36 +03:00
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" )
assert . NoError ( t , UpdateIssuesCommit ( user , repo , pushCommits ) )
AssertExistsAndLoadBean ( t , commentBean )
AssertExistsAndLoadBean ( t , issueBean , "is_closed=1" )
2017-02-28 04:42:10 +03:00
CheckConsistencyFor ( t , & Action { } )
2017-01-09 06:08:36 +03:00
}
2017-09-21 10:43:26 +03:00
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 06:08:36 +03:00
2017-09-21 10:43:26 +03:00
func TestCommitRepoAction ( t * testing . T ) {
samples := [ ] struct {
userID int64
repositoryID int64
commitRepoActionOptions CommitRepoActionOptions
action Action
} {
2017-01-09 06:08:36 +03:00
{
2017-09-21 10:43:26 +03:00
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 06:08:36 +03:00
} ,
{
2017-09-21 10:43:26 +03:00
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 06:08:36 +03:00
} ,
}
2017-09-21 10:43:26 +03:00
for _ , s := range samples {
prepareTestEnv ( t )
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
s . action . IsPrivate = repo . IsPrivate
testCorrectRepoAction ( t , s . commitRepoActionOptions , & s . action )
2017-01-09 06:08:36 +03:00
}
}
func TestTransferRepoAction ( t * testing . T ) {
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
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 06:08:36 +03:00
repo . OwnerID = user4 . ID
repo . Owner = user4
actionBean := & Action {
2017-05-26 04:38:18 +03:00
OpType : ActionTransferRepo ,
ActUserID : user2 . ID ,
ActUser : user2 ,
RepoID : repo . ID ,
Repo : repo ,
IsPrivate : repo . IsPrivate ,
2017-01-09 06:08:36 +03:00
}
AssertNotExistsBean ( t , actionBean )
assert . NoError ( t , TransferRepoAction ( user2 , user2 , repo ) )
AssertExistsAndLoadBean ( t , actionBean )
2017-02-28 04:42:10 +03:00
2017-10-05 07:43:04 +03:00
_ , err := x . ID ( repo . ID ) . Cols ( "owner_id" ) . Update ( repo )
2017-02-28 04:42:10 +03:00
assert . NoError ( t , err )
CheckConsistencyFor ( t , & Action { } )
2017-01-09 06:08:36 +03:00
}
func TestMergePullRequestAction ( t * testing . T ) {
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
user := AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
repo := AssertExistsAndLoadBean ( t , & Repository { ID : 1 , OwnerID : user . ID } ) . ( * Repository )
2017-01-09 06:08:36 +03:00
repo . Owner = user
2017-01-25 05:49:51 +03:00
issue := AssertExistsAndLoadBean ( t , & Issue { ID : 3 , RepoID : repo . ID } ) . ( * Issue )
2017-01-09 06:08:36 +03:00
actionBean := & Action {
2017-05-26 04:38:18 +03:00
OpType : ActionMergePullRequest ,
ActUserID : user . ID ,
ActUser : user ,
RepoID : repo . ID ,
Repo : repo ,
IsPrivate : repo . IsPrivate ,
2017-01-09 06:08:36 +03:00
}
AssertNotExistsBean ( t , actionBean )
assert . NoError ( t , MergePullRequestAction ( user , repo , issue ) )
AssertExistsAndLoadBean ( t , actionBean )
2017-02-28 04:42:10 +03:00
CheckConsistencyFor ( t , & Action { } )
2017-01-09 06:08:36 +03:00
}
func TestGetFeeds ( t * testing . T ) {
// test with an individual user
assert . NoError ( t , PrepareTestDatabase ( ) )
2017-01-25 05:49:51 +03:00
user := AssertExistsAndLoadBean ( t , & User { ID : 2 } ) . ( * User )
2017-01-09 06:08:36 +03:00
2017-06-02 03:42:25 +03:00
actions , err := GetFeeds ( GetFeedsOptions {
RequestedUser : user ,
RequestingUserID : user . ID ,
IncludePrivate : true ,
OnlyPerformedBy : false ,
2017-06-25 21:20:29 +03:00
IncludeDeleted : true ,
2017-06-02 03:42:25 +03:00
} )
2017-01-09 06:08:36 +03:00
assert . NoError ( t , err )
2017-08-28 12:17:45 +03:00
if assert . Len ( t , actions , 1 ) {
assert . EqualValues ( t , 1 , actions [ 0 ] . ID )
assert . EqualValues ( t , user . ID , actions [ 0 ] . UserID )
}
2017-06-02 03:42:25 +03:00
actions , err = GetFeeds ( GetFeedsOptions {
RequestedUser : user ,
RequestingUserID : user . ID ,
IncludePrivate : false ,
OnlyPerformedBy : false ,
} )
2017-01-09 06:08:36 +03:00
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 03:42:25 +03:00
org := AssertExistsAndLoadBean ( t , & User { ID : 3 } ) . ( * User )
userID := AssertExistsAndLoadBean ( t , & OrgUser { OrgID : org . ID , IsOwner : true } ) . ( * OrgUser ) . UID
actions , err := GetFeeds ( GetFeedsOptions {
RequestedUser : org ,
RequestingUserID : userID ,
IncludePrivate : true ,
OnlyPerformedBy : false ,
2017-06-25 21:20:29 +03:00
IncludeDeleted : true ,
2017-06-02 03:42:25 +03:00
} )
2017-01-09 06:08:36 +03:00
assert . NoError ( t , err )
assert . Len ( t , actions , 1 )
2017-08-28 12:17:45 +03:00
if assert . Len ( t , actions , 1 ) {
assert . EqualValues ( t , 2 , actions [ 0 ] . ID )
assert . EqualValues ( t , org . ID , actions [ 0 ] . UserID )
}
2017-06-02 03:42:25 +03:00
actions , err = GetFeeds ( GetFeedsOptions {
RequestedUser : org ,
RequestingUserID : userID ,
IncludePrivate : false ,
OnlyPerformedBy : false ,
2017-06-25 21:20:29 +03:00
IncludeDeleted : true ,
2017-06-02 03:42:25 +03:00
} )
2017-01-09 06:08:36 +03:00
assert . NoError ( t , err )
2017-02-28 04:42:10 +03:00
assert . Len ( t , actions , 0 )
2017-01-09 06:08:36 +03:00
}