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.

354 lines
10 KiB

  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(GetFeedsOptions{
  272. RequestedUser: user,
  273. RequestingUserID: user.ID,
  274. IncludePrivate: true,
  275. OnlyPerformedBy: false,
  276. })
  277. assert.NoError(t, err)
  278. assert.Len(t, actions, 1)
  279. assert.EqualValues(t, 1, actions[0].ID)
  280. assert.EqualValues(t, user.ID, actions[0].UserID)
  281. actions, err = GetFeeds(GetFeedsOptions{
  282. RequestedUser: user,
  283. RequestingUserID: user.ID,
  284. IncludePrivate: false,
  285. OnlyPerformedBy: false,
  286. })
  287. assert.NoError(t, err)
  288. assert.Len(t, actions, 0)
  289. }
  290. func TestGetFeeds2(t *testing.T) {
  291. // test with an organization user
  292. assert.NoError(t, PrepareTestDatabase())
  293. org := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  294. userID := AssertExistsAndLoadBean(t, &OrgUser{OrgID: org.ID, IsOwner: true}).(*OrgUser).UID
  295. actions, err := GetFeeds(GetFeedsOptions{
  296. RequestedUser: org,
  297. RequestingUserID: userID,
  298. IncludePrivate: true,
  299. OnlyPerformedBy: false,
  300. })
  301. assert.NoError(t, err)
  302. assert.Len(t, actions, 1)
  303. assert.EqualValues(t, 2, actions[0].ID)
  304. assert.EqualValues(t, org.ID, actions[0].UserID)
  305. actions, err = GetFeeds(GetFeedsOptions{
  306. RequestedUser: org,
  307. RequestingUserID: userID,
  308. IncludePrivate: false,
  309. OnlyPerformedBy: false,
  310. })
  311. assert.NoError(t, err)
  312. assert.Len(t, actions, 0)
  313. }