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.

45 lines
1.2 KiB

  1. // Copyright 2017 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. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "testing"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr, canPush bool) {
  16. reqURL := fmt.Sprintf("/api/internal/branch/%d/%s", repoID, url.QueryEscape(branchName))
  17. req, err := http.NewRequest("GET", reqURL, nil)
  18. t.Log(reqURL)
  19. req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))
  20. assert.NoError(t, err)
  21. resp := MakeRequest(req)
  22. if isErr {
  23. assert.EqualValues(t, 500, resp.HeaderCode)
  24. } else {
  25. assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
  26. var branch models.ProtectedBranch
  27. t.Log(string(resp.Body))
  28. assert.NoError(t, json.Unmarshal(resp.Body, &branch))
  29. assert.Equal(t, canPush, branch.CanPush)
  30. }
  31. }
  32. func TestInternal_GetProtectedBranch(t *testing.T) {
  33. prepareTestEnv(t)
  34. assertProtectedBranch(t, 1, "master", false, true)
  35. assertProtectedBranch(t, 1, "dev", false, true)
  36. assertProtectedBranch(t, 1, "lunny/dev", false, true)
  37. }