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.

515 lines
14 KiB

  1. package models
  2. import (
  3. "fmt"
  4. "path"
  5. "strings"
  6. "testing"
  7. "code.gitea.io/gitea/modules/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 TestUpdateIssuesCommit_Issue5957(t *testing.T) {
  235. assert.NoError(t, PrepareTestDatabase())
  236. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  237. // Test that push to a non-default branch closes an issue.
  238. pushCommits := []*PushCommit{
  239. {
  240. Sha1: "abcdef1",
  241. CommitterEmail: "user2@example.com",
  242. CommitterName: "User Two",
  243. AuthorEmail: "user4@example.com",
  244. AuthorName: "User Four",
  245. Message: "close #2",
  246. },
  247. }
  248. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  249. commentBean := &Comment{
  250. Type: CommentTypeCommitRef,
  251. CommitSHA: "abcdef1",
  252. PosterID: user.ID,
  253. IssueID: 7,
  254. }
  255. issueBean := &Issue{RepoID: repo.ID, Index: 2, ID: 7}
  256. AssertNotExistsBean(t, commentBean)
  257. AssertNotExistsBean(t, issueBean, "is_closed=1")
  258. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  259. AssertExistsAndLoadBean(t, commentBean)
  260. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  261. CheckConsistencyFor(t, &Action{})
  262. }
  263. func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
  264. AssertNotExistsBean(t, actionBean)
  265. assert.NoError(t, CommitRepoAction(opts))
  266. AssertExistsAndLoadBean(t, actionBean)
  267. CheckConsistencyFor(t, &Action{})
  268. }
  269. func TestCommitRepoAction(t *testing.T) {
  270. samples := []struct {
  271. userID int64
  272. repositoryID int64
  273. commitRepoActionOptions CommitRepoActionOptions
  274. action Action
  275. }{
  276. {
  277. userID: 2,
  278. repositoryID: 2,
  279. commitRepoActionOptions: CommitRepoActionOptions{
  280. RefFullName: "refName",
  281. OldCommitID: "oldCommitID",
  282. NewCommitID: "newCommitID",
  283. Commits: &PushCommits{
  284. avatars: make(map[string]string),
  285. Commits: []*PushCommit{
  286. {
  287. Sha1: "abcdef1",
  288. CommitterEmail: "user2@example.com",
  289. CommitterName: "User Two",
  290. AuthorEmail: "user4@example.com",
  291. AuthorName: "User Four",
  292. Message: "message1",
  293. },
  294. {
  295. Sha1: "abcdef2",
  296. CommitterEmail: "user2@example.com",
  297. CommitterName: "User Two",
  298. AuthorEmail: "user2@example.com",
  299. AuthorName: "User Two",
  300. Message: "message2",
  301. },
  302. },
  303. Len: 2,
  304. },
  305. },
  306. action: Action{
  307. OpType: ActionCommitRepo,
  308. RefName: "refName",
  309. },
  310. },
  311. {
  312. userID: 2,
  313. repositoryID: 1,
  314. commitRepoActionOptions: CommitRepoActionOptions{
  315. RefFullName: git.TagPrefix + "v1.1",
  316. OldCommitID: git.EmptySHA,
  317. NewCommitID: "newCommitID",
  318. Commits: &PushCommits{},
  319. },
  320. action: Action{
  321. OpType: ActionPushTag,
  322. RefName: "v1.1",
  323. },
  324. },
  325. {
  326. userID: 2,
  327. repositoryID: 1,
  328. commitRepoActionOptions: CommitRepoActionOptions{
  329. RefFullName: git.TagPrefix + "v1.1",
  330. OldCommitID: "oldCommitID",
  331. NewCommitID: git.EmptySHA,
  332. Commits: &PushCommits{},
  333. },
  334. action: Action{
  335. OpType: ActionDeleteTag,
  336. RefName: "v1.1",
  337. },
  338. },
  339. {
  340. userID: 2,
  341. repositoryID: 1,
  342. commitRepoActionOptions: CommitRepoActionOptions{
  343. RefFullName: git.BranchPrefix + "feature/1",
  344. OldCommitID: "oldCommitID",
  345. NewCommitID: git.EmptySHA,
  346. Commits: &PushCommits{},
  347. },
  348. action: Action{
  349. OpType: ActionDeleteBranch,
  350. RefName: "feature/1",
  351. },
  352. },
  353. }
  354. for _, s := range samples {
  355. PrepareTestEnv(t)
  356. user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
  357. repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
  358. repo.Owner = user
  359. s.commitRepoActionOptions.PusherName = user.Name
  360. s.commitRepoActionOptions.RepoOwnerID = user.ID
  361. s.commitRepoActionOptions.RepoName = repo.Name
  362. s.action.ActUserID = user.ID
  363. s.action.RepoID = repo.ID
  364. s.action.Repo = repo
  365. s.action.IsPrivate = repo.IsPrivate
  366. testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
  367. }
  368. }
  369. func TestTransferRepoAction(t *testing.T) {
  370. assert.NoError(t, PrepareTestDatabase())
  371. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  372. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  373. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
  374. repo.OwnerID = user4.ID
  375. repo.Owner = user4
  376. actionBean := &Action{
  377. OpType: ActionTransferRepo,
  378. ActUserID: user2.ID,
  379. ActUser: user2,
  380. RepoID: repo.ID,
  381. Repo: repo,
  382. IsPrivate: repo.IsPrivate,
  383. }
  384. AssertNotExistsBean(t, actionBean)
  385. assert.NoError(t, TransferRepoAction(user2, user2, repo))
  386. AssertExistsAndLoadBean(t, actionBean)
  387. _, err := x.ID(repo.ID).Cols("owner_id").Update(repo)
  388. assert.NoError(t, err)
  389. CheckConsistencyFor(t, &Action{})
  390. }
  391. func TestMergePullRequestAction(t *testing.T) {
  392. assert.NoError(t, PrepareTestDatabase())
  393. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  394. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
  395. repo.Owner = user
  396. issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
  397. actionBean := &Action{
  398. OpType: ActionMergePullRequest,
  399. ActUserID: user.ID,
  400. ActUser: user,
  401. RepoID: repo.ID,
  402. Repo: repo,
  403. IsPrivate: repo.IsPrivate,
  404. }
  405. AssertNotExistsBean(t, actionBean)
  406. assert.NoError(t, MergePullRequestAction(user, repo, issue))
  407. AssertExistsAndLoadBean(t, actionBean)
  408. CheckConsistencyFor(t, &Action{})
  409. }
  410. func TestGetFeeds(t *testing.T) {
  411. // test with an individual user
  412. assert.NoError(t, PrepareTestDatabase())
  413. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  414. actions, err := GetFeeds(GetFeedsOptions{
  415. RequestedUser: user,
  416. RequestingUserID: user.ID,
  417. IncludePrivate: true,
  418. OnlyPerformedBy: false,
  419. IncludeDeleted: true,
  420. })
  421. assert.NoError(t, err)
  422. if assert.Len(t, actions, 1) {
  423. assert.EqualValues(t, 1, actions[0].ID)
  424. assert.EqualValues(t, user.ID, actions[0].UserID)
  425. }
  426. actions, err = GetFeeds(GetFeedsOptions{
  427. RequestedUser: user,
  428. RequestingUserID: user.ID,
  429. IncludePrivate: false,
  430. OnlyPerformedBy: false,
  431. })
  432. assert.NoError(t, err)
  433. assert.Len(t, actions, 0)
  434. }
  435. func TestGetFeeds2(t *testing.T) {
  436. // test with an organization user
  437. assert.NoError(t, PrepareTestDatabase())
  438. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  439. const userID = 2 // user2 is an owner of the organization
  440. actions, err := GetFeeds(GetFeedsOptions{
  441. RequestedUser: org,
  442. RequestingUserID: userID,
  443. IncludePrivate: true,
  444. OnlyPerformedBy: false,
  445. IncludeDeleted: true,
  446. })
  447. assert.NoError(t, err)
  448. assert.Len(t, actions, 1)
  449. if assert.Len(t, actions, 1) {
  450. assert.EqualValues(t, 2, actions[0].ID)
  451. assert.EqualValues(t, org.ID, actions[0].UserID)
  452. }
  453. actions, err = GetFeeds(GetFeedsOptions{
  454. RequestedUser: org,
  455. RequestingUserID: userID,
  456. IncludePrivate: false,
  457. OnlyPerformedBy: false,
  458. IncludeDeleted: true,
  459. })
  460. assert.NoError(t, err)
  461. assert.Len(t, actions, 0)
  462. }