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.

324 lines
9.3 KiB

  1. package models
  2. import (
  3. "testing"
  4. "code.gitea.io/gitea/modules/setting"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestAction_GetRepoPath(t *testing.T) {
  8. action := &Action{
  9. RepoUserName: "username",
  10. RepoName: "reponame",
  11. }
  12. assert.Equal(t, "username/reponame", action.GetRepoPath())
  13. }
  14. func TestAction_GetRepoLink(t *testing.T) {
  15. action := &Action{
  16. RepoUserName: "username",
  17. RepoName: "reponame",
  18. }
  19. setting.AppSubURL = "/suburl/"
  20. assert.Equal(t, "/suburl/username/reponame", action.GetRepoLink())
  21. setting.AppSubURL = ""
  22. assert.Equal(t, "/username/reponame", action.GetRepoLink())
  23. }
  24. func TestNewRepoAction(t *testing.T) {
  25. assert.NoError(t, PrepareTestDatabase())
  26. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  27. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
  28. repo.Owner = user
  29. actionBean := &Action{
  30. OpType: ActionCreateRepo,
  31. ActUserID: user.ID,
  32. RepoID: repo.ID,
  33. ActUserName: user.Name,
  34. RepoName: repo.Name,
  35. RepoUserName: repo.Owner.Name,
  36. IsPrivate: repo.IsPrivate,
  37. }
  38. AssertNotExistsBean(t, actionBean)
  39. assert.NoError(t, NewRepoAction(user, repo))
  40. AssertExistsAndLoadBean(t, actionBean)
  41. }
  42. func TestRenameRepoAction(t *testing.T) {
  43. assert.NoError(t, PrepareTestDatabase())
  44. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  45. repo := AssertExistsAndLoadBean(t, &Repository{OwnerID: user.ID}).(*Repository)
  46. repo.Owner = user
  47. oldRepoName := repo.Name
  48. const newRepoName = "newRepoName"
  49. repo.Name = newRepoName
  50. actionBean := &Action{
  51. OpType: ActionRenameRepo,
  52. ActUserID: user.ID,
  53. ActUserName: user.Name,
  54. RepoID: repo.ID,
  55. RepoName: repo.Name,
  56. RepoUserName: repo.Owner.Name,
  57. IsPrivate: repo.IsPrivate,
  58. Content: oldRepoName,
  59. }
  60. AssertNotExistsBean(t, actionBean)
  61. assert.NoError(t, RenameRepoAction(user, oldRepoName, repo))
  62. AssertExistsAndLoadBean(t, actionBean)
  63. }
  64. func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
  65. pushCommits := NewPushCommits()
  66. pushCommits.Commits = []*PushCommit{
  67. {
  68. Sha1: "abcdef1",
  69. CommitterEmail: "user2@example.com",
  70. CommitterName: "User Two",
  71. AuthorEmail: "user4@example.com",
  72. AuthorName: "User Four",
  73. Message: "message1",
  74. },
  75. {
  76. Sha1: "abcdef2",
  77. CommitterEmail: "user2@example.com",
  78. CommitterName: "User Two",
  79. AuthorEmail: "user2@example.com",
  80. AuthorName: "User Two",
  81. Message: "message2",
  82. },
  83. }
  84. pushCommits.Len = len(pushCommits.Commits)
  85. payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame")
  86. assert.Len(t, payloadCommits, 2)
  87. assert.Equal(t, "abcdef1", payloadCommits[0].ID)
  88. assert.Equal(t, "message1", payloadCommits[0].Message)
  89. assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
  90. assert.Equal(t, "User Two", payloadCommits[0].Committer.Name)
  91. assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
  92. assert.Equal(t, "User Four", payloadCommits[0].Author.Name)
  93. assert.Equal(t, "user4", payloadCommits[0].Author.UserName)
  94. assert.Equal(t, "abcdef2", payloadCommits[1].ID)
  95. assert.Equal(t, "message2", payloadCommits[1].Message)
  96. assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL)
  97. assert.Equal(t, "User Two", payloadCommits[1].Committer.Name)
  98. assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
  99. assert.Equal(t, "User Two", payloadCommits[1].Author.Name)
  100. assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
  101. }
  102. func TestPushCommits_AvatarLink(t *testing.T) {
  103. pushCommits := NewPushCommits()
  104. pushCommits.Commits = []*PushCommit{
  105. {
  106. Sha1: "abcdef1",
  107. CommitterEmail: "user2@example.com",
  108. CommitterName: "User Two",
  109. AuthorEmail: "user4@example.com",
  110. AuthorName: "User Four",
  111. Message: "message1",
  112. },
  113. {
  114. Sha1: "abcdef2",
  115. CommitterEmail: "user2@example.com",
  116. CommitterName: "User Two",
  117. AuthorEmail: "user2@example.com",
  118. AuthorName: "User Two",
  119. Message: "message2",
  120. },
  121. }
  122. pushCommits.Len = len(pushCommits.Commits)
  123. assert.Equal(t,
  124. "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f",
  125. pushCommits.AvatarLink("user2@example.com"))
  126. assert.Equal(t,
  127. "https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154",
  128. pushCommits.AvatarLink("nonexistent@example.com"))
  129. }
  130. func TestUpdateIssuesCommit(t *testing.T) {
  131. assert.NoError(t, PrepareTestDatabase())
  132. pushCommits := []*PushCommit{
  133. {
  134. Sha1: "abcdef1",
  135. CommitterEmail: "user2@example.com",
  136. CommitterName: "User Two",
  137. AuthorEmail: "user4@example.com",
  138. AuthorName: "User Four",
  139. Message: "start working on #1",
  140. },
  141. {
  142. Sha1: "abcdef2",
  143. CommitterEmail: "user2@example.com",
  144. CommitterName: "User Two",
  145. AuthorEmail: "user2@example.com",
  146. AuthorName: "User Two",
  147. Message: "a plain message",
  148. },
  149. {
  150. Sha1: "abcdef2",
  151. CommitterEmail: "user2@example.com",
  152. CommitterName: "User Two",
  153. AuthorEmail: "user2@example.com",
  154. AuthorName: "User Two",
  155. Message: "close #2",
  156. },
  157. }
  158. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  159. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  160. repo.Owner = user
  161. commentBean := &Comment{
  162. Type: CommentTypeCommitRef,
  163. CommitSHA: "abcdef1",
  164. PosterID: user.ID,
  165. IssueID: 1,
  166. }
  167. issueBean := &Issue{RepoID: repo.ID, Index: 2}
  168. AssertNotExistsBean(t, commentBean)
  169. AssertNotExistsBean(t, &Issue{RepoID: repo.ID, Index: 2}, "is_closed=1")
  170. assert.NoError(t, UpdateIssuesCommit(user, repo, pushCommits))
  171. AssertExistsAndLoadBean(t, commentBean)
  172. AssertExistsAndLoadBean(t, issueBean, "is_closed=1")
  173. }
  174. func TestCommitRepoAction(t *testing.T) {
  175. assert.NoError(t, PrepareTestDatabase())
  176. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  177. repo := AssertExistsAndLoadBean(t, &Repository{ID: 2, OwnerID: user.ID}).(*Repository)
  178. repo.Owner = user
  179. pushCommits := NewPushCommits()
  180. pushCommits.Commits = []*PushCommit{
  181. {
  182. Sha1: "abcdef1",
  183. CommitterEmail: "user2@example.com",
  184. CommitterName: "User Two",
  185. AuthorEmail: "user4@example.com",
  186. AuthorName: "User Four",
  187. Message: "message1",
  188. },
  189. {
  190. Sha1: "abcdef2",
  191. CommitterEmail: "user2@example.com",
  192. CommitterName: "User Two",
  193. AuthorEmail: "user2@example.com",
  194. AuthorName: "User Two",
  195. Message: "message2",
  196. },
  197. }
  198. pushCommits.Len = len(pushCommits.Commits)
  199. actionBean := &Action{
  200. OpType: ActionCommitRepo,
  201. ActUserID: user.ID,
  202. ActUserName: user.Name,
  203. RepoID: repo.ID,
  204. RepoName: repo.Name,
  205. RefName: "refName",
  206. IsPrivate: repo.IsPrivate,
  207. }
  208. AssertNotExistsBean(t, actionBean)
  209. assert.NoError(t, CommitRepoAction(CommitRepoActionOptions{
  210. PusherName: user.Name,
  211. RepoOwnerID: user.ID,
  212. RepoName: repo.Name,
  213. RefFullName: "refName",
  214. OldCommitID: "oldCommitID",
  215. NewCommitID: "newCommitID",
  216. Commits: pushCommits,
  217. }))
  218. AssertExistsAndLoadBean(t, actionBean)
  219. }
  220. func TestTransferRepoAction(t *testing.T) {
  221. assert.NoError(t, PrepareTestDatabase())
  222. user2 := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  223. user4 := AssertExistsAndLoadBean(t, &User{ID: 4}).(*User)
  224. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user2.ID}).(*Repository)
  225. repo.OwnerID = user4.ID
  226. repo.Owner = user4
  227. actionBean := &Action{
  228. OpType: ActionTransferRepo,
  229. ActUserID: user2.ID,
  230. ActUserName: user2.Name,
  231. RepoID: repo.ID,
  232. RepoName: repo.Name,
  233. RepoUserName: repo.Owner.Name,
  234. IsPrivate: repo.IsPrivate,
  235. }
  236. AssertNotExistsBean(t, actionBean)
  237. assert.NoError(t, TransferRepoAction(user2, user2, repo))
  238. AssertExistsAndLoadBean(t, actionBean)
  239. }
  240. func TestMergePullRequestAction(t *testing.T) {
  241. assert.NoError(t, PrepareTestDatabase())
  242. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  243. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1, OwnerID: user.ID}).(*Repository)
  244. repo.Owner = user
  245. issue := AssertExistsAndLoadBean(t, &Issue{ID: 3, RepoID: repo.ID}).(*Issue)
  246. actionBean := &Action{
  247. OpType: ActionMergePullRequest,
  248. ActUserID: user.ID,
  249. ActUserName: user.Name,
  250. RepoID: repo.ID,
  251. RepoName: repo.Name,
  252. RepoUserName: repo.Owner.Name,
  253. IsPrivate: repo.IsPrivate,
  254. }
  255. AssertNotExistsBean(t, actionBean)
  256. assert.NoError(t, MergePullRequestAction(user, repo, issue))
  257. AssertExistsAndLoadBean(t, actionBean)
  258. }
  259. func TestGetFeeds(t *testing.T) {
  260. // test with an individual user
  261. assert.NoError(t, PrepareTestDatabase())
  262. user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
  263. actions, err := GetFeeds(user, user.ID, 0, false)
  264. assert.NoError(t, err)
  265. assert.Len(t, actions, 1)
  266. assert.Equal(t, int64(1), actions[0].ID)
  267. assert.Equal(t, user.ID, actions[0].UserID)
  268. actions, err = GetFeeds(user, user.ID, 0, true)
  269. assert.NoError(t, err)
  270. assert.Len(t, actions, 0)
  271. }
  272. func TestGetFeeds2(t *testing.T) {
  273. // test with an organization user
  274. assert.NoError(t, PrepareTestDatabase())
  275. user := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)
  276. actions, err := GetFeeds(user, user.ID, 0, false)
  277. assert.NoError(t, err)
  278. assert.Len(t, actions, 1)
  279. assert.Equal(t, int64(2), actions[0].ID)
  280. assert.Equal(t, user.ID, actions[0].UserID)
  281. actions, err = GetFeeds(user, user.ID, 0, true)
  282. assert.NoError(t, err)
  283. assert.Len(t, actions, 1)
  284. assert.Equal(t, int64(2), actions[0].ID)
  285. assert.Equal(t, user.ID, actions[0].UserID)
  286. }