A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
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.

194 lines
6.5 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. "https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon&s=56",
  104. pushCommits.AvatarLink("user2@example.com"))
  105. assert.Equal(t,
  106. "https://secure.gravatar.com/avatar/"+
  107. fmt.Sprintf("%x", md5.Sum([]byte("nonexistent@example.com")))+
  108. "?d=identicon&s=56",
  109. pushCommits.AvatarLink("nonexistent@example.com"))
  110. }
  111. func TestCommitToPushCommit(t *testing.T) {
  112. now := time.Now()
  113. sig := &git.Signature{
  114. Email: "example@example.com",
  115. Name: "John Doe",
  116. When: now,
  117. }
  118. const hexString = "0123456789abcdef0123456789abcdef01234567"
  119. sha1, err := git.NewIDFromString(hexString)
  120. assert.NoError(t, err)
  121. pushCommit := CommitToPushCommit(&git.Commit{
  122. ID: sha1,
  123. Author: sig,
  124. Committer: sig,
  125. CommitMessage: "Commit Message",
  126. })
  127. assert.Equal(t, hexString, pushCommit.Sha1)
  128. assert.Equal(t, "Commit Message", pushCommit.Message)
  129. assert.Equal(t, "example@example.com", pushCommit.AuthorEmail)
  130. assert.Equal(t, "John Doe", pushCommit.AuthorName)
  131. assert.Equal(t, "example@example.com", pushCommit.CommitterEmail)
  132. assert.Equal(t, "John Doe", pushCommit.CommitterName)
  133. assert.Equal(t, now, pushCommit.Timestamp)
  134. }
  135. func TestListToPushCommits(t *testing.T) {
  136. now := time.Now()
  137. sig := &git.Signature{
  138. Email: "example@example.com",
  139. Name: "John Doe",
  140. When: now,
  141. }
  142. const hexString1 = "0123456789abcdef0123456789abcdef01234567"
  143. hash1, err := git.NewIDFromString(hexString1)
  144. assert.NoError(t, err)
  145. const hexString2 = "fedcba9876543210fedcba9876543210fedcba98"
  146. hash2, err := git.NewIDFromString(hexString2)
  147. assert.NoError(t, err)
  148. l := list.New()
  149. l.PushBack(&git.Commit{
  150. ID: hash1,
  151. Author: sig,
  152. Committer: sig,
  153. CommitMessage: "Message1",
  154. })
  155. l.PushBack(&git.Commit{
  156. ID: hash2,
  157. Author: sig,
  158. Committer: sig,
  159. CommitMessage: "Message2",
  160. })
  161. pushCommits := ListToPushCommits(l)
  162. assert.Equal(t, 2, pushCommits.Len)
  163. if assert.Len(t, pushCommits.Commits, 2) {
  164. assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
  165. assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1)
  166. assert.Equal(t, "example@example.com", pushCommits.Commits[0].AuthorEmail)
  167. assert.Equal(t, now, pushCommits.Commits[0].Timestamp)
  168. assert.Equal(t, "Message2", pushCommits.Commits[1].Message)
  169. assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1)
  170. assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail)
  171. assert.Equal(t, now, pushCommits.Commits[1].Timestamp)
  172. }
  173. }
  174. // TODO TestPushUpdate