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.

98 lines
3.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 integrations
  5. import (
  6. "net/http"
  7. "testing"
  8. "code.gitea.io/gitea/models"
  9. api "code.gitea.io/gitea/modules/structs"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestAPIReposGitCommits(t *testing.T) {
  13. defer prepareTestEnv(t)()
  14. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  15. // Login as User2.
  16. session := loginUser(t, user.Name)
  17. token := getTokenForLoggedInUser(t, session)
  18. // check invalid requests
  19. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/12345?token="+token, user.Name)
  20. session.MakeRequest(t, req, http.StatusNotFound)
  21. req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/..?token="+token, user.Name)
  22. session.MakeRequest(t, req, http.StatusUnprocessableEntity)
  23. req = NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/branch-not-exist?token="+token, user.Name)
  24. session.MakeRequest(t, req, http.StatusNotFound)
  25. for _, ref := range [...]string{
  26. "master", // Branch
  27. "v1.1", // Tag
  28. "65f1", // short sha
  29. "65f1bf27bc3bf70f64657658635e66094edbcb4d", // full sha
  30. } {
  31. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/%s?token="+token, user.Name, ref)
  32. session.MakeRequest(t, req, http.StatusOK)
  33. }
  34. }
  35. func TestAPIReposGitCommitList(t *testing.T) {
  36. defer prepareTestEnv(t)()
  37. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  38. // Login as User2.
  39. session := loginUser(t, user.Name)
  40. token := getTokenForLoggedInUser(t, session)
  41. // Test getting commits (Page 1)
  42. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token, user.Name)
  43. resp := session.MakeRequest(t, req, http.StatusOK)
  44. var apiData []api.Commit
  45. DecodeJSON(t, resp, &apiData)
  46. assert.Equal(t, 3, len(apiData))
  47. assert.Equal(t, "69554a64c1e6030f051e5c3f94bfbd773cd6a324", apiData[0].CommitMeta.SHA)
  48. assert.Equal(t, "27566bd5738fc8b4e3fef3c5e72cce608537bd95", apiData[1].CommitMeta.SHA)
  49. assert.Equal(t, "5099b81332712fe655e34e8dd63574f503f61811", apiData[2].CommitMeta.SHA)
  50. }
  51. func TestAPIReposGitCommitListPage2Empty(t *testing.T) {
  52. defer prepareTestEnv(t)()
  53. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  54. // Login as User2.
  55. session := loginUser(t, user.Name)
  56. token := getTokenForLoggedInUser(t, session)
  57. // Test getting commits (Page=2)
  58. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&page=2", user.Name)
  59. resp := session.MakeRequest(t, req, http.StatusOK)
  60. var apiData []api.Commit
  61. DecodeJSON(t, resp, &apiData)
  62. assert.Equal(t, 0, len(apiData))
  63. }
  64. func TestAPIReposGitCommitListDifferentBranch(t *testing.T) {
  65. defer prepareTestEnv(t)()
  66. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  67. // Login as User2.
  68. session := loginUser(t, user.Name)
  69. token := getTokenForLoggedInUser(t, session)
  70. // Test getting commits (Page=1, Branch=good-sign)
  71. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&sha=good-sign", user.Name)
  72. resp := session.MakeRequest(t, req, http.StatusOK)
  73. var apiData []api.Commit
  74. DecodeJSON(t, resp, &apiData)
  75. assert.Equal(t, 1, len(apiData))
  76. assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
  77. }