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.

454 lines
12 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.Repo = repo
  311. s.action.IsPrivate = repo.IsPrivate
  312. testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
  313. }
  314. }
  315. func TestTransferRepoAction(t *testing.T) {
  316. assert.NoError(t, PrepareTestDatabase())
  317. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  318. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  319. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
  320. repo.OwnerID = user4.ID
  321. repo.Owner = user4
  322. actionBean := &Action{
  323. OpType: ActionTransferRepo,
  324. ActUserID: user2.ID,
  325. ActUser: user2,
  326. RepoID: repo.ID,
  327. Repo: repo,
  328. IsPrivate: repo.IsPrivate,
  329. }
  330. AssertNotExistsBean(t, actionBean)
  331. assert.NoError(t, TransferRepoAction(user2, user2, repo))
  332. AssertExistsAndLoadBean(t, actionBean)
  333. _, err := x.ID(repo.ID).Cols("owner_id").Update(repo)
  334. assert.NoError(t, err)
  335. CheckConsistencyFor(t, &Action{})
  336. }
  337. func TestMergePullRequestAction(t *testing.T) {
  338. assert.NoError(t, PrepareTestDatabase())
  339. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  340. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
  341. repo.Owner = user
  342. issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
  343. actionBean := &Action{
  344. OpType: ActionMergePullRequest,
  345. ActUserID: user.ID,
  346. ActUser: user,
  347. RepoID: repo.ID,
  348. Repo: repo,
  349. IsPrivate: repo.IsPrivate,
  350. }
  351. AssertNotExistsBean(t, actionBean)
  352. assert.NoError(t, MergePullRequestAction(user, repo, issue))
  353. AssertExistsAndLoadBean(t, actionBean)
  354. CheckConsistencyFor(t, &Action{})
  355. }
  356. func TestGetFeeds(t *testing.T) {
  357. // test with an individual user
  358. assert.NoError(t, PrepareTestDatabase())
  359. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  360. actions, err := GetFeeds(GetFeedsOptions{
  361. RequestedUser: user,
  362. RequestingUserID: user.ID,
  363. IncludePrivate: true,
  364. OnlyPerformedBy: false,
  365. IncludeDeleted: true,
  366. })
  367. assert.NoError(t, err)
  368. if assert.Len(t, actions, 1) {
  369. assert.EqualValues(t, 1, actions[0].ID)
  370. assert.EqualValues(t, user.ID, actions[0].UserID)
  371. }
  372. actions, err = GetFeeds(GetFeedsOptions{
  373. RequestedUser: user,
  374. RequestingUserID: user.ID,
  375. IncludePrivate: false,
  376. OnlyPerformedBy: false,
  377. })
  378. assert.NoError(t, err)
  379. assert.Len(t, actions, 0)
  380. }
  381. func TestGetFeeds2(t *testing.T) {
  382. // test with an organization user
  383. assert.NoError(t, PrepareTestDatabase())
  384. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  385. const userID = 2 // user2 is an owner of the organization
  386. actions, err := GetFeeds(GetFeedsOptions{
  387. RequestedUser: org,
  388. RequestingUserID: userID,
  389. IncludePrivate: true,
  390. OnlyPerformedBy: false,
  391. IncludeDeleted: true,
  392. })
  393. assert.NoError(t, err)
  394. assert.Len(t, actions, 1)
  395. if assert.Len(t, actions, 1) {
  396. assert.EqualValues(t, 2, actions[0].ID)
  397. assert.EqualValues(t, org.ID, actions[0].UserID)
  398. }
  399. actions, err = GetFeeds(GetFeedsOptions{
  400. RequestedUser: org,
  401. RequestingUserID: userID,
  402. IncludePrivate: false,
  403. OnlyPerformedBy: false,
  404. IncludeDeleted: true,
  405. })
  406. assert.NoError(t, err)
  407. assert.Len(t, actions, 0)
  408. }