本站源代码
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.

494 lines
15KB

  1. package models
  2. import (
  3. "path"
  4. "strings"
  5. "testing"
  6. "code.gitea.io/gitea/modules/setting"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestAction_GetRepoPath(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. repo := AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
  12. owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  13. action := &Action{RepoID: repo.ID}
  14. assert.Equal(t, path.Join(owner.Name, repo.Name), action.GetRepoPath())
  15. }
  16. func TestAction_GetRepoLink(t *testing.T) {
  17. assert.NoError(t, PrepareTestDatabase())
  18. repo := AssertExistsAndLoadBean(t, &Repository{}).(*Repository)
  19. owner := AssertExistsAndLoadBean(t, &User{ID: repo.OwnerID}).(*User)
  20. action := &Action{RepoID: repo.ID}
  21. setting.AppSubURL = "/suburl/"
  22. expected := path.Join(setting.AppSubURL, owner.Name, repo.Name)
  23. assert.Equal(t, expected, action.GetRepoLink())
  24. }
  25. func TestNewRepoAction(t *testing.T) {
  26. assert.NoError(t, PrepareTestDatabase())
  27. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  28. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
  29. repo.Owner = user
  30. actionBean := &Action{
  31. OpType: ActionCreateRepo,
  32. ActUserID: user.ID,
  33. RepoID: repo.ID,
  34. ActUser: user,
  35. Repo: repo,
  36. IsPrivate: repo.IsPrivate,
  37. }
  38. AssertNotExistsBean(t, actionBean)
  39. assert.NoError(t, NewRepoAction(user, repo))
  40. AssertExistsAndLoadBean(t, actionBean)
  41. CheckConsistencyFor(t, &Action{})
  42. }
  43. func TestRenameRepoAction(t *testing.T) {
  44. assert.NoError(t, PrepareTestDatabase())
  45. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  46. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
  47. repo.Owner = user
  48. oldRepoName := repo.Name
  49. const newRepoName = "newRepoName"
  50. repo.Name = newRepoName
  51. repo.LowerName = strings.ToLower(newRepoName)
  52. actionBean := &Action{
  53. OpType: ActionRenameRepo,
  54. ActUserID: user.ID,
  55. ActUser: user,
  56. RepoID: repo.ID,
  57. Repo: repo,
  58. IsPrivate: repo.IsPrivate,
  59. Content: oldRepoName,
  60. }
  61. AssertNotExistsBean(t, actionBean)
  62. assert.NoError(t, RenameRepoAction(user, oldRepoName, repo))
  63. AssertExistsAndLoadBean(t, actionBean)
  64. _, err := x.ID(repo.ID).Cols("name", "lower_name").Update(repo)
  65. assert.NoError(t, err)
  66. CheckConsistencyFor(t, &Action{})
  67. }
  68. func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
  69. pushCommits := NewPushCommits()
  70. pushCommits.Commits = []*PushCommit{
  71. {
  72. Sha1: "69554a6",
  73. CommitterEmail: "user2@example.com",
  74. CommitterName: "User2",
  75. AuthorEmail: "user2@example.com",
  76. AuthorName: "User2",
  77. Message: "not signed commit",
  78. },
  79. {
  80. Sha1: "27566bd",
  81. CommitterEmail: "user2@example.com",
  82. CommitterName: "User2",
  83. AuthorEmail: "user2@example.com",
  84. AuthorName: "User2",
  85. Message: "good signed commit (with not yet validated email)",
  86. },
  87. {
  88. Sha1: "5099b81",
  89. CommitterEmail: "user2@example.com",
  90. CommitterName: "User2",
  91. AuthorEmail: "user2@example.com",
  92. AuthorName: "User2",
  93. Message: "good signed commit",
  94. },
  95. }
  96. pushCommits.Len = len(pushCommits.Commits)
  97. repo := AssertExistsAndLoadBean(t, &Repository{ID: 16}).(*Repository)
  98. payloadCommits, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
  99. assert.NoError(t, err)
  100. assert.EqualValues(t, 3, len(payloadCommits))
  101. assert.Equal(t, "69554a6", payloadCommits[0].ID)
  102. assert.Equal(t, "not signed commit", payloadCommits[0].Message)
  103. assert.Equal(t, "/user2/repo16/commit/69554a6", payloadCommits[0].URL)
  104. assert.Equal(t, "User2", payloadCommits[0].Committer.Name)
  105. assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
  106. assert.Equal(t, "User2", payloadCommits[0].Author.Name)
  107. assert.Equal(t, "user2", payloadCommits[0].Author.UserName)
  108. assert.EqualValues(t, []string{}, payloadCommits[0].Added)
  109. assert.EqualValues(t, []string{}, payloadCommits[0].Removed)
  110. assert.EqualValues(t, []string{"readme.md"}, payloadCommits[0].Modified)
  111. assert.Equal(t, "27566bd", payloadCommits[1].ID)
  112. assert.Equal(t, "good signed commit (with not yet validated email)", payloadCommits[1].Message)
  113. assert.Equal(t, "/user2/repo16/commit/27566bd", payloadCommits[1].URL)
  114. assert.Equal(t, "User2", payloadCommits[1].Committer.Name)
  115. assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
  116. assert.Equal(t, "User2", payloadCommits[1].Author.Name)
  117. assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
  118. assert.EqualValues(t, []string{}, payloadCommits[1].Added)
  119. assert.EqualValues(t, []string{}, payloadCommits[1].Removed)
  120. assert.EqualValues(t, []string{"readme.md"}, payloadCommits[1].Modified)
  121. assert.Equal(t, "5099b81", payloadCommits[2].ID)
  122. assert.Equal(t, "good signed commit", payloadCommits[2].Message)
  123. assert.Equal(t, "/user2/repo16/commit/5099b81", payloadCommits[2].URL)
  124. assert.Equal(t, "User2", payloadCommits[2].Committer.Name)
  125. assert.Equal(t, "user2", payloadCommits[2].Committer.UserName)
  126. assert.Equal(t, "User2", payloadCommits[2].Author.Name)
  127. assert.Equal(t, "user2", payloadCommits[2].Author.UserName)
  128. assert.EqualValues(t, []string{"readme.md"}, payloadCommits[2].Added)
  129. assert.EqualValues(t, []string{}, payloadCommits[2].Removed)
  130. assert.EqualValues(t, []string{}, payloadCommits[2].Modified)
  131. }
  132. func TestPushCommits_AvatarLink(t *testing.T) {
  133. pushCommits := NewPushCommits()
  134. pushCommits.Commits = []*PushCommit{
  135. {
  136. Sha1: "abcdef1",
  137. CommitterEmail: "user2@example.com",
  138. CommitterName: "User Two",
  139. AuthorEmail: "user4@example.com",
  140. AuthorName: "User Four",
  141. Message: "message1",
  142. },
  143. {
  144. Sha1: "abcdef2",
  145. CommitterEmail: "user2@example.com",
  146. CommitterName: "User Two",
  147. AuthorEmail: "user2@example.com",
  148. AuthorName: "User Two",
  149. Message: "message2",
  150. },
  151. }
  152. pushCommits.Len = len(pushCommits.Commits)
  153. assert.Equal(t,
  154. "/suburl/user/avatar/user2/-1",
  155. pushCommits.AvatarLink("user2@example.com"))
  156. assert.Equal(t,
  157. "https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154?d=identicon",
  158. pushCommits.AvatarLink("nonexistent@example.com"))
  159. }
  160. func TestUpdateIssuesCommit(t *testing.T) {
  161. assert.NoError(t, PrepareTestDatabase())
  162. pushCommits := []*PushCommit{
  163. {
  164. Sha1: "abcdef1",
  165. CommitterEmail: "user2@example.com",
  166. CommitterName: "User Two",
  167. AuthorEmail: "user4@example.com",
  168. AuthorName: "User Four",
  169. Message: "start working on #FST-1, #1",
  170. },
  171. {
  172. Sha1: "abcdef2",
  173. CommitterEmail: "user2@example.com",
  174. CommitterName: "User Two",
  175. AuthorEmail: "user2@example.com",
  176. AuthorName: "User Two",
  177. Message: "a plain message",
  178. },
  179. {
  180. Sha1: "abcdef2",
  181. CommitterEmail: "user2@example.com",
  182. CommitterName: "User Two",
  183. AuthorEmail: "user2@example.com",
  184. AuthorName: "User Two",
  185. Message: "close #2",
  186. },
  187. }
  188. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  189. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  190. repo.Owner = user
  191. commentBean := &Comment{
  192. Type: CommentTypeCommitRef,
  193. CommitSHA: "abcdef1",
  194. PosterID: user.ID,
  195. IssueID: 1,
  196. }
  197. issueBean := &Issue{RepoID: repo.ID, Index: 2}
  198. AssertNotExistsBean(t, commentBean)
  199. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  200. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  201. AssertExistsAndLoadBean(t, commentBean)
  202. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  203. CheckConsistencyFor(t, &Action{})
  204. // Test that push to a non-default branch closes no issue.
  205. pushCommits = []*PushCommit{
  206. {
  207. Sha1: "abcdef1",
  208. CommitterEmail: "user2@example.com",
  209. CommitterName: "User Two",
  210. AuthorEmail: "user4@example.com",
  211. AuthorName: "User Four",
  212. Message: "close #1",
  213. },
  214. }
  215. repo = AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
  216. commentBean = &Comment{
  217. Type: CommentTypeCommitRef,
  218. CommitSHA: "abcdef1",
  219. PosterID: user.ID,
  220. IssueID: 6,
  221. }
  222. issueBean = &Issue{RepoID: repo.ID, Index: 1}
  223. AssertNotExistsBean(t, commentBean)
  224. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 1}, "is_closed=1")
  225. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  226. AssertExistsAndLoadBean(t, commentBean)
  227. AssertNotExistsBean(t, issueBean, "is_closed=1")
  228. CheckConsistencyFor(t, &Action{})
  229. }
  230. func TestUpdateIssuesCommit_Colon(t *testing.T) {
  231. assert.NoError(t, PrepareTestDatabase())
  232. pushCommits := []*PushCommit{
  233. {
  234. Sha1: "abcdef2",
  235. CommitterEmail: "user2@example.com",
  236. CommitterName: "User Two",
  237. AuthorEmail: "user2@example.com",
  238. AuthorName: "User Two",
  239. Message: "close: #2",
  240. },
  241. }
  242. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  243. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  244. repo.Owner = user
  245. issueBean := &Issue{RepoID: repo.ID, Index: 2}
  246. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  247. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  248. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  249. CheckConsistencyFor(t, &Action{})
  250. }
  251. func TestUpdateIssuesCommit_Issue5957(t *testing.T) {
  252. assert.NoError(t, PrepareTestDatabase())
  253. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  254. // Test that push to a non-default branch closes an issue.
  255. pushCommits := []*PushCommit{
  256. {
  257. Sha1: "abcdef1",
  258. CommitterEmail: "user2@example.com",
  259. CommitterName: "User Two",
  260. AuthorEmail: "user4@example.com",
  261. AuthorName: "User Four",
  262. Message: "close #2",
  263. },
  264. }
  265. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  266. commentBean := &Comment{
  267. Type: CommentTypeCommitRef,
  268. CommitSHA: "abcdef1",
  269. PosterID: user.ID,
  270. IssueID: 7,
  271. }
  272. issueBean := &Issue{RepoID: repo.ID, Index: 2, ID: 7}
  273. AssertNotExistsBean(t, commentBean)
  274. AssertNotExistsBean(t, issueBean, "is_closed=1")
  275. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, "non-existing-branch"))
  276. AssertExistsAndLoadBean(t, commentBean)
  277. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  278. CheckConsistencyFor(t, &Action{})
  279. }
  280. func TestUpdateIssuesCommit_AnotherRepo(t *testing.T) {
  281. assert.NoError(t, PrepareTestDatabase())
  282. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  283. // Test that a push to default branch closes issue in another repo
  284. // If the user also has push permissions to that repo
  285. pushCommits := []*PushCommit{
  286. {
  287. Sha1: "abcdef1",
  288. CommitterEmail: "user2@example.com",
  289. CommitterName: "User Two",
  290. AuthorEmail: "user2@example.com",
  291. AuthorName: "User Two",
  292. Message: "close user2/repo1#1",
  293. },
  294. }
  295. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
  296. commentBean := &Comment{
  297. Type: CommentTypeCommitRef,
  298. CommitSHA: "abcdef1",
  299. PosterID: user.ID,
  300. IssueID: 1,
  301. }
  302. issueBean := &Issue{RepoID: 1, Index: 1, ID: 1}
  303. AssertNotExistsBean(t, commentBean)
  304. AssertNotExistsBean(t, issueBean, "is_closed=1")
  305. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  306. AssertExistsAndLoadBean(t, commentBean)
  307. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  308. CheckConsistencyFor(t, &Action{})
  309. }
  310. func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
  311. assert.NoError(t, PrepareTestDatabase())
  312. user := AssertExistsAndLoadBean(t, &User{ID: 10}).(*User)
  313. // Test that a push with close reference *can not* close issue
  314. // If the commiter doesn't have push rights in that repo
  315. pushCommits := []*PushCommit{
  316. {
  317. Sha1: "abcdef3",
  318. CommitterEmail: "user10@example.com",
  319. CommitterName: "User Ten",
  320. AuthorEmail: "user10@example.com",
  321. AuthorName: "User Ten",
  322. Message: "close user3/repo3#1",
  323. },
  324. }
  325. repo := AssertExistsAndLoadBean(t, &Repository{ID: 6}).(*Repository)
  326. commentBean := &Comment{
  327. Type: CommentTypeCommitRef,
  328. CommitSHA: "abcdef3",
  329. PosterID: user.ID,
  330. IssueID: 6,
  331. }
  332. issueBean := &Issue{RepoID: 3, Index: 1, ID: 6}
  333. AssertNotExistsBean(t, commentBean)
  334. AssertNotExistsBean(t, issueBean, "is_closed=1")
  335. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits, repo.DefaultBranch))
  336. AssertNotExistsBean(t, commentBean)
  337. AssertNotExistsBean(t, issueBean, "is_closed=1")
  338. CheckConsistencyFor(t, &Action{})
  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. }
上海开阖软件有限公司 沪ICP备12045867号-1