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.

190 lines
6.3 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repository
  5. import (
  6. "container/list"
  7. "testing"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/git"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
  14. assert.NoError(t, models.PrepareTestDatabase())
  15. pushCommits := NewPushCommits()
  16. pushCommits.Commits = []*PushCommit{
  17. {
  18. Sha1: "69554a6",
  19. CommitterEmail: "user2@example.com",
  20. CommitterName: "User2",
  21. AuthorEmail: "user2@example.com",
  22. AuthorName: "User2",
  23. Message: "not signed commit",
  24. },
  25. {
  26. Sha1: "27566bd",
  27. CommitterEmail: "user2@example.com",
  28. CommitterName: "User2",
  29. AuthorEmail: "user2@example.com",
  30. AuthorName: "User2",
  31. Message: "good signed commit (with not yet validated email)",
  32. },
  33. {
  34. Sha1: "5099b81",
  35. CommitterEmail: "user2@example.com",
  36. CommitterName: "User2",
  37. AuthorEmail: "user2@example.com",
  38. AuthorName: "User2",
  39. Message: "good signed commit",
  40. },
  41. }
  42. pushCommits.Len = len(pushCommits.Commits)
  43. repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository)
  44. payloadCommits, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
  45. assert.NoError(t, err)
  46. assert.EqualValues(t, 3, len(payloadCommits))
  47. assert.Equal(t, "69554a6", payloadCommits[0].ID)
  48. assert.Equal(t, "not signed commit", payloadCommits[0].Message)
  49. assert.Equal(t, "/user2/repo16/commit/69554a6", payloadCommits[0].URL)
  50. assert.Equal(t, "User2", payloadCommits[0].Committer.Name)
  51. assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
  52. assert.Equal(t, "User2", payloadCommits[0].Author.Name)
  53. assert.Equal(t, "user2", payloadCommits[0].Author.UserName)
  54. assert.EqualValues(t, []string{}, payloadCommits[0].Added)
  55. assert.EqualValues(t, []string{}, payloadCommits[0].Removed)
  56. assert.EqualValues(t, []string{"readme.md"}, payloadCommits[0].Modified)
  57. assert.Equal(t, "27566bd", payloadCommits[1].ID)
  58. assert.Equal(t, "good signed commit (with not yet validated email)", payloadCommits[1].Message)
  59. assert.Equal(t, "/user2/repo16/commit/27566bd", payloadCommits[1].URL)
  60. assert.Equal(t, "User2", payloadCommits[1].Committer.Name)
  61. assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
  62. assert.Equal(t, "User2", payloadCommits[1].Author.Name)
  63. assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
  64. assert.EqualValues(t, []string{}, payloadCommits[1].Added)
  65. assert.EqualValues(t, []string{}, payloadCommits[1].Removed)
  66. assert.EqualValues(t, []string{"readme.md"}, payloadCommits[1].Modified)
  67. assert.Equal(t, "5099b81", payloadCommits[2].ID)
  68. assert.Equal(t, "good signed commit", payloadCommits[2].Message)
  69. assert.Equal(t, "/user2/repo16/commit/5099b81", payloadCommits[2].URL)
  70. assert.Equal(t, "User2", payloadCommits[2].Committer.Name)
  71. assert.Equal(t, "user2", payloadCommits[2].Committer.UserName)
  72. assert.Equal(t, "User2", payloadCommits[2].Author.Name)
  73. assert.Equal(t, "user2", payloadCommits[2].Author.UserName)
  74. assert.EqualValues(t, []string{"readme.md"}, payloadCommits[2].Added)
  75. assert.EqualValues(t, []string{}, payloadCommits[2].Removed)
  76. assert.EqualValues(t, []string{}, payloadCommits[2].Modified)
  77. }
  78. func TestPushCommits_AvatarLink(t *testing.T) {
  79. assert.NoError(t, models.PrepareTestDatabase())
  80. pushCommits := NewPushCommits()
  81. pushCommits.Commits = []*PushCommit{
  82. {
  83. Sha1: "abcdef1",
  84. CommitterEmail: "user2@example.com",
  85. CommitterName: "User Two",
  86. AuthorEmail: "user4@example.com",
  87. AuthorName: "User Four",
  88. Message: "message1",
  89. },
  90. {
  91. Sha1: "abcdef2",
  92. CommitterEmail: "user2@example.com",
  93. CommitterName: "User Two",
  94. AuthorEmail: "user2@example.com",
  95. AuthorName: "User Two",
  96. Message: "message2",
  97. },
  98. }
  99. pushCommits.Len = len(pushCommits.Commits)
  100. assert.Equal(t,
  101. "/user/avatar/user2/-1",
  102. pushCommits.AvatarLink("user2@example.com"))
  103. assert.Equal(t,
  104. "https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154?d=identicon",
  105. pushCommits.AvatarLink("nonexistent@example.com"))
  106. }
  107. func TestCommitToPushCommit(t *testing.T) {
  108. now := time.Now()
  109. sig := &git.Signature{
  110. Email: "example@example.com",
  111. Name: "John Doe",
  112. When: now,
  113. }
  114. const hexString = "0123456789abcdef0123456789abcdef01234567"
  115. sha1, err := git.NewIDFromString(hexString)
  116. assert.NoError(t, err)
  117. pushCommit := CommitToPushCommit(&git.Commit{
  118. ID: sha1,
  119. Author: sig,
  120. Committer: sig,
  121. CommitMessage: "Commit Message",
  122. })
  123. assert.Equal(t, hexString, pushCommit.Sha1)
  124. assert.Equal(t, "Commit Message", pushCommit.Message)
  125. assert.Equal(t, "example@example.com", pushCommit.AuthorEmail)
  126. assert.Equal(t, "John Doe", pushCommit.AuthorName)
  127. assert.Equal(t, "example@example.com", pushCommit.CommitterEmail)
  128. assert.Equal(t, "John Doe", pushCommit.CommitterName)
  129. assert.Equal(t, now, pushCommit.Timestamp)
  130. }
  131. func TestListToPushCommits(t *testing.T) {
  132. now := time.Now()
  133. sig := &git.Signature{
  134. Email: "example@example.com",
  135. Name: "John Doe",
  136. When: now,
  137. }
  138. const hexString1 = "0123456789abcdef0123456789abcdef01234567"
  139. hash1, err := git.NewIDFromString(hexString1)
  140. assert.NoError(t, err)
  141. const hexString2 = "fedcba9876543210fedcba9876543210fedcba98"
  142. hash2, err := git.NewIDFromString(hexString2)
  143. assert.NoError(t, err)
  144. l := list.New()
  145. l.PushBack(&git.Commit{
  146. ID: hash1,
  147. Author: sig,
  148. Committer: sig,
  149. CommitMessage: "Message1",
  150. })
  151. l.PushBack(&git.Commit{
  152. ID: hash2,
  153. Author: sig,
  154. Committer: sig,
  155. CommitMessage: "Message2",
  156. })
  157. pushCommits := ListToPushCommits(l)
  158. assert.Equal(t, 2, pushCommits.Len)
  159. if assert.Len(t, pushCommits.Commits, 2) {
  160. assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
  161. assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1)
  162. assert.Equal(t, "example@example.com", pushCommits.Commits[0].AuthorEmail)
  163. assert.Equal(t, now, pushCommits.Commits[0].Timestamp)
  164. assert.Equal(t, "Message2", pushCommits.Commits[1].Message)
  165. assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1)
  166. assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail)
  167. assert.Equal(t, now, pushCommits.Commits[1].Timestamp)
  168. }
  169. }
  170. // TODO TestPushUpdate