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.

453 lines
13 KiB

  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))
  205. AssertExistsAndLoadBean(t, commentBean)
  206. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  207. CheckConsistencyFor(t, &Action{})
  208. }
  209. func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
  210. AssertNotExistsBean(t, actionBean)
  211. assert.NoError(t, CommitRepoAction(opts))
  212. AssertExistsAndLoadBean(t, actionBean)
  213. CheckConsistencyFor(t, &Action{})
  214. }
  215. func TestCommitRepoAction(t *testing.T) {
  216. samples := []struct {
  217. userID int64
  218. repositoryID int64
  219. commitRepoActionOptions CommitRepoActionOptions
  220. action Action
  221. }{
  222. {
  223. userID: 2,
  224. repositoryID: 2,
  225. commitRepoActionOptions: CommitRepoActionOptions{
  226. RefFullName: "refName",
  227. OldCommitID: "oldCommitID",
  228. NewCommitID: "newCommitID",
  229. Commits: &PushCommits{
  230. avatars: make(map[string]string),
  231. Commits: []*PushCommit{
  232. {
  233. Sha1: "abcdef1",
  234. CommitterEmail: "user2@example.com",
  235. CommitterName: "User Two",
  236. AuthorEmail: "user4@example.com",
  237. AuthorName: "User Four",
  238. Message: "message1",
  239. },
  240. {
  241. Sha1: "abcdef2",
  242. CommitterEmail: "user2@example.com",
  243. CommitterName: "User Two",
  244. AuthorEmail: "user2@example.com",
  245. AuthorName: "User Two",
  246. Message: "message2",
  247. },
  248. },
  249. Len: 2,
  250. },
  251. },
  252. action: Action{
  253. OpType: ActionCommitRepo,
  254. RefName: "refName",
  255. },
  256. },
  257. {
  258. userID: 2,
  259. repositoryID: 1,
  260. commitRepoActionOptions: CommitRepoActionOptions{
  261. RefFullName: git.TagPrefix + "v1.1",
  262. OldCommitID: git.EmptySHA,
  263. NewCommitID: "newCommitID",
  264. Commits: &PushCommits{},
  265. },
  266. action: Action{
  267. OpType: ActionPushTag,
  268. RefName: "v1.1",
  269. },
  270. },
  271. {
  272. userID: 2,
  273. repositoryID: 1,
  274. commitRepoActionOptions: CommitRepoActionOptions{
  275. RefFullName: git.TagPrefix + "v1.1",
  276. OldCommitID: "oldCommitID",
  277. NewCommitID: git.EmptySHA,
  278. Commits: &PushCommits{},
  279. },
  280. action: Action{
  281. OpType: ActionDeleteTag,
  282. RefName: "v1.1",
  283. },
  284. },
  285. {
  286. userID: 2,
  287. repositoryID: 1,
  288. commitRepoActionOptions: CommitRepoActionOptions{
  289. RefFullName: git.BranchPrefix + "feature/1",
  290. OldCommitID: "oldCommitID",
  291. NewCommitID: git.EmptySHA,
  292. Commits: &PushCommits{},
  293. },
  294. action: Action{
  295. OpType: ActionDeleteBranch,
  296. RefName: "feature/1",
  297. },
  298. },
  299. }
  300. for _, s := range samples {
  301. PrepareTestEnv(t)
  302. user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
  303. repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
  304. repo.Owner = user
  305. s.commitRepoActionOptions.PusherName = user.Name
  306. s.commitRepoActionOptions.RepoOwnerID = user.ID
  307. s.commitRepoActionOptions.RepoName = repo.Name
  308. s.action.ActUserID = user.ID
  309. s.action.RepoID = repo.ID
  310. s.action.IsPrivate = repo.IsPrivate
  311. testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
  312. }
  313. }
  314. func TestTransferRepoAction(t *testing.T) {
  315. assert.NoError(t, PrepareTestDatabase())
  316. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  317. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  318. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
  319. repo.OwnerID = user4.ID
  320. repo.Owner = user4
  321. actionBean := &Action{
  322. OpType: ActionTransferRepo,
  323. ActUserID: user2.ID,
  324. ActUser: user2,
  325. RepoID: repo.ID,
  326. Repo: repo,
  327. IsPrivate: repo.IsPrivate,
  328. }
  329. AssertNotExistsBean(t, actionBean)
  330. assert.NoError(t, TransferRepoAction(user2, user2, repo))
  331. AssertExistsAndLoadBean(t, actionBean)
  332. _, err := x.ID(repo.ID).Cols("owner_id").Update(repo)
  333. assert.NoError(t, err)
  334. CheckConsistencyFor(t, &Action{})
  335. }
  336. func TestMergePullRequestAction(t *testing.T) {
  337. assert.NoError(t, PrepareTestDatabase())
  338. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  339. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
  340. repo.Owner = user
  341. issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
  342. actionBean := &Action{
  343. OpType: ActionMergePullRequest,
  344. ActUserID: user.ID,
  345. ActUser: user,
  346. RepoID: repo.ID,
  347. Repo: repo,
  348. IsPrivate: repo.IsPrivate,
  349. }
  350. AssertNotExistsBean(t, actionBean)
  351. assert.NoError(t, MergePullRequestAction(user, repo, issue))
  352. AssertExistsAndLoadBean(t, actionBean)
  353. CheckConsistencyFor(t, &Action{})
  354. }
  355. func TestGetFeeds(t *testing.T) {
  356. // test with an individual user
  357. assert.NoError(t, PrepareTestDatabase())
  358. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  359. actions, err := GetFeeds(GetFeedsOptions{
  360. RequestedUser: user,
  361. RequestingUserID: user.ID,
  362. IncludePrivate: true,
  363. OnlyPerformedBy: false,
  364. IncludeDeleted: true,
  365. })
  366. assert.NoError(t, err)
  367. if assert.Len(t, actions, 1) {
  368. assert.EqualValues(t, 1, actions[0].ID)
  369. assert.EqualValues(t, user.ID, actions[0].UserID)
  370. }
  371. actions, err = GetFeeds(GetFeedsOptions{
  372. RequestedUser: user,
  373. RequestingUserID: user.ID,
  374. IncludePrivate: false,
  375. OnlyPerformedBy: false,
  376. })
  377. assert.NoError(t, err)
  378. assert.Len(t, actions, 0)
  379. }
  380. func TestGetFeeds2(t *testing.T) {
  381. // test with an organization user
  382. assert.NoError(t, PrepareTestDatabase())
  383. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  384. userID := AssertExistsAndLoadBean(t, &OrgUser{OrgID: org.ID, IsOwner: true}).(*OrgUser).UID
  385. actions, err := GetFeeds(GetFeedsOptions{
  386. RequestedUser: org,
  387. RequestingUserID: userID,
  388. IncludePrivate: true,
  389. OnlyPerformedBy: false,
  390. IncludeDeleted: true,
  391. })
  392. assert.NoError(t, err)
  393. assert.Len(t, actions, 1)
  394. if assert.Len(t, actions, 1) {
  395. assert.EqualValues(t, 2, actions[0].ID)
  396. assert.EqualValues(t, org.ID, actions[0].UserID)
  397. }
  398. actions, err = GetFeeds(GetFeedsOptions{
  399. RequestedUser: org,
  400. RequestingUserID: userID,
  401. IncludePrivate: false,
  402. OnlyPerformedBy: false,
  403. IncludeDeleted: true,
  404. })
  405. assert.NoError(t, err)
  406. assert.Len(t, actions, 0)
  407. }