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 12 kB

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