Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

action_test.go 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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: "abcdef1",
  73. CommitterEmail: "user2@example.com",
  74. CommitterName: "User Two",
  75. AuthorEmail: "user4@example.com",
  76. AuthorName: "User Four",
  77. Message: "message1",
  78. },
  79. {
  80. Sha1: "abcdef2",
  81. CommitterEmail: "user2@example.com",
  82. CommitterName: "User Two",
  83. AuthorEmail: "user2@example.com",
  84. AuthorName: "User Two",
  85. Message: "message2",
  86. },
  87. }
  88. pushCommits.Len = len(pushCommits.Commits)
  89. payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame")
  90. assert.Len(t, payloadCommits, 2)
  91. assert.Equal(t, "abcdef1", payloadCommits[0].ID)
  92. assert.Equal(t, "message1", payloadCommits[0].Message)
  93. assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
  94. assert.Equal(t, "User Two", payloadCommits[0].Committer.Name)
  95. assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
  96. assert.Equal(t, "User Four", payloadCommits[0].Author.Name)
  97. assert.Equal(t, "user4", payloadCommits[0].Author.UserName)
  98. assert.Equal(t, "abcdef2", payloadCommits[1].ID)
  99. assert.Equal(t, "message2", payloadCommits[1].Message)
  100. assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL)
  101. assert.Equal(t, "User Two", payloadCommits[1].Committer.Name)
  102. assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
  103. assert.Equal(t, "User Two", payloadCommits[1].Author.Name)
  104. assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
  105. }
  106. func TestPushCommits_AvatarLink(t *testing.T) {
  107. pushCommits := NewPushCommits()
  108. pushCommits.Commits = []*PushCommit{
  109. {
  110. Sha1: "abcdef1",
  111. CommitterEmail: "user2@example.com",
  112. CommitterName: "User Two",
  113. AuthorEmail: "user4@example.com",
  114. AuthorName: "User Four",
  115. Message: "message1",
  116. },
  117. {
  118. Sha1: "abcdef2",
  119. CommitterEmail: "user2@example.com",
  120. CommitterName: "User Two",
  121. AuthorEmail: "user2@example.com",
  122. AuthorName: "User Two",
  123. Message: "message2",
  124. },
  125. }
  126. pushCommits.Len = len(pushCommits.Commits)
  127. assert.Equal(t,
  128. "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f",
  129. pushCommits.AvatarLink("user2@example.com"))
  130. assert.Equal(t,
  131. "https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154",
  132. pushCommits.AvatarLink("nonexistent@example.com"))
  133. }
  134. func TestUpdateIssuesCommit(t *testing.T) {
  135. assert.NoError(t, PrepareTestDatabase())
  136. pushCommits := []*PushCommit{
  137. {
  138. Sha1: "abcdef1",
  139. CommitterEmail: "user2@example.com",
  140. CommitterName: "User Two",
  141. AuthorEmail: "user4@example.com",
  142. AuthorName: "User Four",
  143. Message: "start working on #1",
  144. },
  145. {
  146. Sha1: "abcdef2",
  147. CommitterEmail: "user2@example.com",
  148. CommitterName: "User Two",
  149. AuthorEmail: "user2@example.com",
  150. AuthorName: "User Two",
  151. Message: "a plain message",
  152. },
  153. {
  154. Sha1: "abcdef2",
  155. CommitterEmail: "user2@example.com",
  156. CommitterName: "User Two",
  157. AuthorEmail: "user2@example.com",
  158. AuthorName: "User Two",
  159. Message: "close #2",
  160. },
  161. }
  162. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  163. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  164. repo.Owner = user
  165. commentBean := &Comment{
  166. Type: CommentTypeCommitRef,
  167. CommitSHA: "abcdef1",
  168. PosterID: user.ID,
  169. IssueID: 1,
  170. }
  171. issueBean := &Issue{RepoID: repo.ID, Index: 2}
  172. AssertNotExistsBean(t, commentBean)
  173. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  174. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits))
  175. AssertExistsAndLoadBean(t, commentBean)
  176. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  177. CheckConsistencyFor(t, &Action{})
  178. }
  179. func TestCommitRepoAction(t *testing.T) {
  180. assert.NoError(t, PrepareTestDatabase())
  181. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  182. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2, OwnerID: user.ID}).(*Repository)
  183. repo.Owner = user
  184. pushCommits := NewPushCommits()
  185. pushCommits.Commits = []*PushCommit{
  186. {
  187. Sha1: "abcdef1",
  188. CommitterEmail: "user2@example.com",
  189. CommitterName: "User Two",
  190. AuthorEmail: "user4@example.com",
  191. AuthorName: "User Four",
  192. Message: "message1",
  193. },
  194. {
  195. Sha1: "abcdef2",
  196. CommitterEmail: "user2@example.com",
  197. CommitterName: "User Two",
  198. AuthorEmail: "user2@example.com",
  199. AuthorName: "User Two",
  200. Message: "message2",
  201. },
  202. }
  203. pushCommits.Len = len(pushCommits.Commits)
  204. actionBean := &Action{
  205. OpType: ActionCommitRepo,
  206. ActUserID: user.ID,
  207. ActUser: user,
  208. RepoID: repo.ID,
  209. Repo: repo,
  210. RefName: "refName",
  211. IsPrivate: repo.IsPrivate,
  212. }
  213. AssertNotExistsBean(t, actionBean)
  214. assert.NoError(t, CommitRepoAction(CommitRepoActionOptions{
  215. PusherName: user.Name,
  216. RepoOwnerID: user.ID,
  217. RepoName: repo.Name,
  218. RefFullName: "refName",
  219. OldCommitID: "oldCommitID",
  220. NewCommitID: "newCommitID",
  221. Commits: pushCommits,
  222. }))
  223. AssertExistsAndLoadBean(t, actionBean)
  224. CheckConsistencyFor(t, &Action{})
  225. }
  226. func TestTransferRepoAction(t *testing.T) {
  227. assert.NoError(t, PrepareTestDatabase())
  228. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  229. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  230. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
  231. repo.OwnerID = user4.ID
  232. repo.Owner = user4
  233. actionBean := &Action{
  234. OpType: ActionTransferRepo,
  235. ActUserID: user2.ID,
  236. ActUser: user2,
  237. RepoID: repo.ID,
  238. Repo: repo,
  239. IsPrivate: repo.IsPrivate,
  240. }
  241. AssertNotExistsBean(t, actionBean)
  242. assert.NoError(t, TransferRepoAction(user2, user2, repo))
  243. AssertExistsAndLoadBean(t, actionBean)
  244. _, err := x.Id(repo.ID).Cols("owner_id").Update(repo)
  245. assert.NoError(t, err)
  246. CheckConsistencyFor(t, &Action{})
  247. }
  248. func TestMergePullRequestAction(t *testing.T) {
  249. assert.NoError(t, PrepareTestDatabase())
  250. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  251. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
  252. repo.Owner = user
  253. issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
  254. actionBean := &Action{
  255. OpType: ActionMergePullRequest,
  256. ActUserID: user.ID,
  257. ActUser: user,
  258. RepoID: repo.ID,
  259. Repo: repo,
  260. IsPrivate: repo.IsPrivate,
  261. }
  262. AssertNotExistsBean(t, actionBean)
  263. assert.NoError(t, MergePullRequestAction(user, repo, issue))
  264. AssertExistsAndLoadBean(t, actionBean)
  265. CheckConsistencyFor(t, &Action{})
  266. }
  267. func TestGetFeeds(t *testing.T) {
  268. // test with an individual user
  269. assert.NoError(t, PrepareTestDatabase())
  270. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  271. actions, err := GetFeeds(user, user.ID, 0, false)
  272. assert.NoError(t, err)
  273. assert.Len(t, actions, 1)
  274. assert.Equal(t, int64(1), actions[0].ID)
  275. assert.Equal(t, user.ID, actions[0].UserID)
  276. actions, err = GetFeeds(user, user.ID, 0, true)
  277. assert.NoError(t, err)
  278. assert.Len(t, actions, 0)
  279. }
  280. func TestGetFeeds2(t *testing.T) {
  281. // test with an organization user
  282. assert.NoError(t, PrepareTestDatabase())
  283. user := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  284. actions, err := GetFeeds(user, user.ID, 0, false)
  285. assert.NoError(t, err)
  286. assert.Len(t, actions, 1)
  287. assert.Equal(t, int64(2), actions[0].ID)
  288. assert.Equal(t, user.ID, actions[0].UserID)
  289. actions, err = GetFeeds(user, user.ID, 0, true)
  290. assert.NoError(t, err)
  291. assert.Len(t, actions, 0)
  292. }