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