You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

action_test.go 14 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package models
  2. import (
  3. "fmt"
  4. "path"
  5. "strings"
  6. "testing"
  7. "code.gitea.io/git"
  8. "code.gitea.io/gitea/modules/setting"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestAction_GetRepoPath(t *testing.T) {
  12. assert.NoError(t, PrepareTestDatabase())
  13. repo := AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
  14. owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  15. action := &Action{RepoID: repo.ID}
  16. assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
  17. }
  18. func TestAction_GetRepoLink(t *testing.T) {
  19. assert.NoError(t, PrepareTestDatabase())
  20. repo := AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
  21. owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  22. action := &Action{RepoID: repo.ID}
  23. setting.AppSubURL = "/suburl/"
  24. expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
  25. assert.Equal(t, expected, action.GetRepoLink())
  26. }
  27. func TestNewRepoAction(t *testing.T) {
  28. assert.NoError(t, PrepareTestDatabase())
  29. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  30. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
  31. repo.Owner = user
  32. actionBean := &Action{
  33. OpType: ActionCreateRepo,
  34. ActUserID: user.ID,
  35. RepoID: repo.ID,
  36. ActUser: user,
  37. Repo: repo,
  38. IsPrivate: repo.IsPrivate,
  39. }
  40. AssertNotExistsBean(t, actionBean)
  41. assert.NoError(t, NewRepoAction(user, repo))
  42. AssertExistsAndLoadBean(t, actionBean)
  43. CheckConsistencyFor(t, &Action{})
  44. }
  45. func TestRenameRepoAction(t *testing.T) {
  46. assert.NoError(t, PrepareTestDatabase())
  47. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  48. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
  49. repo.Owner = user
  50. oldRepoName := repo.Name
  51. const newRepoName = "newRepoName"
  52. repo.Name = newRepoName
  53. repo.LowerName = strings.ToLower(newRepoName)
  54. actionBean := &Action{
  55. OpType: ActionRenameRepo,
  56. ActUserID: user.ID,
  57. ActUser: user,
  58. RepoID: repo.ID,
  59. Repo: repo,
  60. IsPrivate: repo.IsPrivate,
  61. Content: oldRepoName,
  62. }
  63. AssertNotExistsBean(t, actionBean)
  64. assert.NoError(t, RenameRepoAction(user, oldRepoName, repo))
  65. AssertExistsAndLoadBean(t, actionBean)
  66. _, err := x.ID(repo.ID).Cols("name", "lower_name").Update(repo)
  67. assert.NoError(t, err)
  68. CheckConsistencyFor(t, &Action{})
  69. }
  70. func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
  71. pushCommits := NewPushCommits()
  72. pushCommits.Commits = []*PushCommit{
  73. {
  74. Sha1: "abcdef1",
  75. CommitterEmail: "user2@example.com",
  76. CommitterName: "User Two",
  77. AuthorEmail: "user4@example.com",
  78. AuthorName: "User Four",
  79. Message: "message1",
  80. },
  81. {
  82. Sha1: "abcdef2",
  83. CommitterEmail: "user2@example.com",
  84. CommitterName: "User Two",
  85. AuthorEmail: "user2@example.com",
  86. AuthorName: "User Two",
  87. Message: "message2",
  88. },
  89. }
  90. pushCommits.Len = len(pushCommits.Commits)
  91. payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame")
  92. if assert.Len(t, payloadCommits, 2) {
  93. assert.Equal(t, "abcdef1", payloadCommits[0].ID)
  94. assert.Equal(t, "message1", payloadCommits[0].Message)
  95. assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
  96. assert.Equal(t, "User Two", payloadCommits[0].Committer.Name)
  97. assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
  98. assert.Equal(t, "User Four", payloadCommits[0].Author.Name)
  99. assert.Equal(t, "user4", payloadCommits[0].Author.UserName)
  100. assert.Equal(t, "abcdef2", payloadCommits[1].ID)
  101. assert.Equal(t, "message2", payloadCommits[1].Message)
  102. assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL)
  103. assert.Equal(t, "User Two", payloadCommits[1].Committer.Name)
  104. assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
  105. assert.Equal(t, "User Two", payloadCommits[1].Author.Name)
  106. assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
  107. }
  108. }
  109. func TestPushCommits_AvatarLink(t *testing.T) {
  110. pushCommits := NewPushCommits()
  111. pushCommits.Commits = []*PushCommit{
  112. {
  113. Sha1: "abcdef1",
  114. CommitterEmail: "user2@example.com",
  115. CommitterName: "User Two",
  116. AuthorEmail: "user4@example.com",
  117. AuthorName: "User Four",
  118. Message: "message1",
  119. },
  120. {
  121. Sha1: "abcdef2",
  122. CommitterEmail: "user2@example.com",
  123. CommitterName: "User Two",
  124. AuthorEmail: "user2@example.com",
  125. AuthorName: "User Two",
  126. Message: "message2",
  127. },
  128. }
  129. pushCommits.Len = len(pushCommits.Commits)
  130. assert.Equal(t,
  131. "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon",
  132. pushCommits.AvatarLink("user2@example.com"))
  133. assert.Equal(t,
  134. "https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154?d=identicon",
  135. pushCommits.AvatarLink("nonexistent@example.com"))
  136. }
  137. func Test_getIssueFromRef(t *testing.T) {
  138. assert.NoError(t, PrepareTestDatabase())
  139. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  140. for _, test := range []struct {
  141. Ref string
  142. ExpectedIssueID int64
  143. }{
  144. {"#2", 2},
  145. {"reopen #2", 2},
  146. {"user2/repo2#1", 4},
  147. {"fixes user2/repo2#1", 4},
  148. } {
  149. issue, err := getIssueFromRef(repo, test.Ref)
  150. assert.NoError(t, err)
  151. if assert.NotNil(t, issue) {
  152. assert.EqualValues(t, test.ExpectedIssueID, issue.ID)
  153. }
  154. }
  155. for _, badRef := range []string{
  156. "doesnotexist/doesnotexist#1",
  157. fmt.Sprintf("#%d", NonexistentID),
  158. } {
  159. issue, err := getIssueFromRef(repo, badRef)
  160. assert.NoError(t, err)
  161. assert.Nil(t, issue)
  162. }
  163. }
  164. func TestUpdateIssuesCommit(t *testing.T) {
  165. assert.NoError(t, PrepareTestDatabase())
  166. pushCommits := []*PushCommit{
  167. {
  168. Sha1: "abcdef1",
  169. CommitterEmail: "user2@example.com",
  170. CommitterName: "User Two",
  171. AuthorEmail: "user4@example.com",
  172. AuthorName: "User Four",
  173. Message: "start working on #FST-1, #1",
  174. },
  175. {
  176. Sha1: "abcdef2",
  177. CommitterEmail: "user2@example.com",
  178. CommitterName: "User Two",
  179. AuthorEmail: "user2@example.com",
  180. AuthorName: "User Two",
  181. Message: "a plain message",
  182. },
  183. {
  184. Sha1: "abcdef2",
  185. CommitterEmail: "user2@example.com",
  186. CommitterName: "User Two",
  187. AuthorEmail: "user2@example.com",
  188. AuthorName: "User Two",
  189. Message: "close #2",
  190. },
  191. }
  192. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  193. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  194. repo.Owner = user
  195. commentBean := &Comment{
  196. Type: CommentTypeCommitRef,
  197. CommitSHA: "abcdef1",
  198. PosterID: user.ID,
  199. IssueID: 1,
  200. }
  201. issueBean := &Issue{RepoID: repo.ID, Index: 2}
  202. AssertNotExistsBean(t, commentBean)
  203. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  204. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  205. AssertExistsAndLoadBean(t, commentBean)
  206. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  207. CheckConsistencyFor(t, &Action{})
  208. // Test that push to a non-default branch closes no issue.
  209. pushCommits = []*PushCommit{
  210. {
  211. Sha1: "abcdef1",
  212. CommitterEmail: "user2@example.com",
  213. CommitterName: "User Two",
  214. AuthorEmail: "user4@example.com",
  215. AuthorName: "User Four",
  216. Message: "close #1",
  217. },
  218. }
  219. repo = AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  220. commentBean = &Comment{
  221. Type: CommentTypeCommitRef,
  222. CommitSHA: "abcdef1",
  223. PosterID: user.ID,
  224. IssueID: 6,
  225. }
  226. issueBean = &Issue{RepoID: repo.ID, Index: 1}
  227. AssertNotExistsBean(t, commentBean)
  228. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 1}, "is_closed=1")
  229. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  230. AssertExistsAndLoadBean(t, commentBean)
  231. AssertNotExistsBean(t, issueBean, "is_closed=1")
  232. CheckConsistencyFor(t, &Action{})
  233. }
  234. func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
  235. AssertNotExistsBean(t, actionBean)
  236. assert.NoError(t, CommitRepoAction(opts))
  237. AssertExistsAndLoadBean(t, actionBean)
  238. CheckConsistencyFor(t, &Action{})
  239. }
  240. func TestCommitRepoAction(t *testing.T) {
  241. samples := []struct {
  242. userID int64
  243. repositoryID int64
  244. commitRepoActionOptions CommitRepoActionOptions
  245. action Action
  246. }{
  247. {
  248. userID: 2,
  249. repositoryID: 2,
  250. commitRepoActionOptions: CommitRepoActionOptions{
  251. RefFullName: "refName",
  252. OldCommitID: "oldCommitID",
  253. NewCommitID: "newCommitID",
  254. Commits: &PushCommits{
  255. avatars: make(map[string]string),
  256. Commits: []*PushCommit{
  257. {
  258. Sha1: "abcdef1",
  259. CommitterEmail: "user2@example.com",
  260. CommitterName: "User Two",
  261. AuthorEmail: "user4@example.com",
  262. AuthorName: "User Four",
  263. Message: "message1",
  264. },
  265. {
  266. Sha1: "abcdef2",
  267. CommitterEmail: "user2@example.com",
  268. CommitterName: "User Two",
  269. AuthorEmail: "user2@example.com",
  270. AuthorName: "User Two",
  271. Message: "message2",
  272. },
  273. },
  274. Len: 2,
  275. },
  276. },
  277. action: Action{
  278. OpType: ActionCommitRepo,
  279. RefName: "refName",
  280. },
  281. },
  282. {
  283. userID: 2,
  284. repositoryID: 1,
  285. commitRepoActionOptions: CommitRepoActionOptions{
  286. RefFullName: git.TagPrefix + "v1.1",
  287. OldCommitID: git.EmptySHA,
  288. NewCommitID: "newCommitID",
  289. Commits: &PushCommits{},
  290. },
  291. action: Action{
  292. OpType: ActionPushTag,
  293. RefName: "v1.1",
  294. },
  295. },
  296. {
  297. userID: 2,
  298. repositoryID: 1,
  299. commitRepoActionOptions: CommitRepoActionOptions{
  300. RefFullName: git.TagPrefix + "v1.1",
  301. OldCommitID: "oldCommitID",
  302. NewCommitID: git.EmptySHA,
  303. Commits: &PushCommits{},
  304. },
  305. action: Action{
  306. OpType: ActionDeleteTag,
  307. RefName: "v1.1",
  308. },
  309. },
  310. {
  311. userID: 2,
  312. repositoryID: 1,
  313. commitRepoActionOptions: CommitRepoActionOptions{
  314. RefFullName: git.BranchPrefix + "feature/1",
  315. OldCommitID: "oldCommitID",
  316. NewCommitID: git.EmptySHA,
  317. Commits: &PushCommits{},
  318. },
  319. action: Action{
  320. OpType: ActionDeleteBranch,
  321. RefName: "feature/1",
  322. },
  323. },
  324. }
  325. for _, s := range samples {
  326. PrepareTestEnv(t)
  327. user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
  328. repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
  329. repo.Owner = user
  330. s.commitRepoActionOptions.PusherName = user.Name
  331. s.commitRepoActionOptions.RepoOwnerID = user.ID
  332. s.commitRepoActionOptions.RepoName = repo.Name
  333. s.action.ActUserID = user.ID
  334. s.action.RepoID = repo.ID
  335. s.action.Repo = repo
  336. s.action.IsPrivate = repo.IsPrivate
  337. testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
  338. }
  339. }
  340. func TestTransferRepoAction(t *testing.T) {
  341. assert.NoError(t, PrepareTestDatabase())
  342. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  343. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  344. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
  345. repo.OwnerID = user4.ID
  346. repo.Owner = user4
  347. actionBean := &Action{
  348. OpType: ActionTransferRepo,
  349. ActUserID: user2.ID,
  350. ActUser: user2,
  351. RepoID: repo.ID,
  352. Repo: repo,
  353. IsPrivate: repo.IsPrivate,
  354. }
  355. AssertNotExistsBean(t, actionBean)
  356. assert.NoError(t, TransferRepoAction(user2, user2, repo))
  357. AssertExistsAndLoadBean(t, actionBean)
  358. _, err := x.ID(repo.ID).Cols("owner_id").Update(repo)
  359. assert.NoError(t, err)
  360. CheckConsistencyFor(t, &Action{})
  361. }
  362. func TestMergePullRequestAction(t *testing.T) {
  363. assert.NoError(t, PrepareTestDatabase())
  364. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  365. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
  366. repo.Owner = user
  367. issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
  368. actionBean := &Action{
  369. OpType: ActionMergePullRequest,
  370. ActUserID: user.ID,
  371. ActUser: user,
  372. RepoID: repo.ID,
  373. Repo: repo,
  374. IsPrivate: repo.IsPrivate,
  375. }
  376. AssertNotExistsBean(t, actionBean)
  377. assert.NoError(t, MergePullRequestAction(user, repo, issue))
  378. AssertExistsAndLoadBean(t, actionBean)
  379. CheckConsistencyFor(t, &Action{})
  380. }
  381. func TestGetFeeds(t *testing.T) {
  382. // test with an individual user
  383. assert.NoError(t, PrepareTestDatabase())
  384. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  385. actions, err := GetFeeds(GetFeedsOptions{
  386. RequestedUser: user,
  387. RequestingUserID: user.ID,
  388. IncludePrivate: true,
  389. OnlyPerformedBy: false,
  390. IncludeDeleted: true,
  391. })
  392. assert.NoError(t, err)
  393. if assert.Len(t, actions, 1) {
  394. assert.EqualValues(t, 1, actions[0].ID)
  395. assert.EqualValues(t, user.ID, actions[0].UserID)
  396. }
  397. actions, err = GetFeeds(GetFeedsOptions{
  398. RequestedUser: user,
  399. RequestingUserID: user.ID,
  400. IncludePrivate: false,
  401. OnlyPerformedBy: false,
  402. })
  403. assert.NoError(t, err)
  404. assert.Len(t, actions, 0)
  405. }
  406. func TestGetFeeds2(t *testing.T) {
  407. // test with an organization user
  408. assert.NoError(t, PrepareTestDatabase())
  409. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  410. const userID = 2 // user2 is an owner of the organization
  411. actions, err := GetFeeds(GetFeedsOptions{
  412. RequestedUser: org,
  413. RequestingUserID: userID,
  414. IncludePrivate: true,
  415. OnlyPerformedBy: false,
  416. IncludeDeleted: true,
  417. })
  418. assert.NoError(t, err)
  419. assert.Len(t, actions, 1)
  420. if assert.Len(t, actions, 1) {
  421. assert.EqualValues(t, 2, actions[0].ID)
  422. assert.EqualValues(t, org.ID, actions[0].UserID)
  423. }
  424. actions, err = GetFeeds(GetFeedsOptions{
  425. RequestedUser: org,
  426. RequestingUserID: userID,
  427. IncludePrivate: false,
  428. OnlyPerformedBy: false,
  429. IncludeDeleted: true,
  430. })
  431. assert.NoError(t, err)
  432. assert.Len(t, actions, 0)
  433. }