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.

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