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.

90 lines
2.9 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. 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. for _, ref := range [...]string{
  19. "commits/master", // Branch
  20. "commits/v1.1", // Tag
  21. } {
  22. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/%s?token="+token, user.Name, ref)
  23. session.MakeRequest(t, req, http.StatusOK)
  24. }
  25. // Test getting non-existent refs
  26. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/git/commits/unknown?token="+token, user.Name)
  27. session.MakeRequest(t, req, http.StatusNotFound)
  28. }
  29. func TestAPIReposGitCommitList(t *testing.T) {
  30. prepareTestEnv(t)
  31. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  32. // Login as User2.
  33. session := loginUser(t, user.Name)
  34. token := getTokenForLoggedInUser(t, session)
  35. // Test getting commits (Page 1)
  36. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token, user.Name)
  37. resp := session.MakeRequest(t, req, http.StatusOK)
  38. var apiData []api.Commit
  39. DecodeJSON(t, resp, &apiData)
  40. assert.Equal(t, 3, len(apiData))
  41. assert.Equal(t, "69554a64c1e6030f051e5c3f94bfbd773cd6a324", apiData[0].CommitMeta.SHA)
  42. assert.Equal(t, "27566bd5738fc8b4e3fef3c5e72cce608537bd95", apiData[1].CommitMeta.SHA)
  43. assert.Equal(t, "5099b81332712fe655e34e8dd63574f503f61811", apiData[2].CommitMeta.SHA)
  44. }
  45. func TestAPIReposGitCommitListPage2Empty(t *testing.T) {
  46. prepareTestEnv(t)
  47. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  48. // Login as User2.
  49. session := loginUser(t, user.Name)
  50. token := getTokenForLoggedInUser(t, session)
  51. // Test getting commits (Page=2)
  52. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&page=2", user.Name)
  53. resp := session.MakeRequest(t, req, http.StatusOK)
  54. var apiData []api.Commit
  55. DecodeJSON(t, resp, &apiData)
  56. assert.Equal(t, 0, len(apiData))
  57. }
  58. func TestAPIReposGitCommitListDifferentBranch(t *testing.T) {
  59. prepareTestEnv(t)
  60. user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
  61. // Login as User2.
  62. session := loginUser(t, user.Name)
  63. token := getTokenForLoggedInUser(t, session)
  64. // Test getting commits (Page=1, Branch=good-sign)
  65. req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&sha=good-sign", user.Name)
  66. resp := session.MakeRequest(t, req, http.StatusOK)
  67. var apiData []api.Commit
  68. DecodeJSON(t, resp, &apiData)
  69. assert.Equal(t, 1, len(apiData))
  70. assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA)
  71. }