2017-05-07 20:55:27 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-03-18 08:00:23 -06:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-05-07 20:55:27 -06:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr, canPush bool) {
|
2019-03-18 08:00:23 -06:00
|
|
|
reqURL := fmt.Sprintf("/api/internal/branch/%d/%s", repoID, util.PathEscapeSegments(branchName))
|
2017-06-09 18:41:36 -06:00
|
|
|
req := NewRequest(t, "GET", reqURL)
|
2017-05-07 20:55:27 -06:00
|
|
|
t.Log(reqURL)
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))
|
|
|
|
|
2017-07-07 13:36:47 -06:00
|
|
|
resp := MakeRequest(t, req, NoExpectedStatus)
|
2017-05-07 20:55:27 -06:00
|
|
|
if isErr {
|
2017-12-03 15:46:01 -07:00
|
|
|
assert.EqualValues(t, http.StatusInternalServerError, resp.Code)
|
2017-05-07 20:55:27 -06:00
|
|
|
} else {
|
2017-12-03 15:46:01 -07:00
|
|
|
assert.EqualValues(t, http.StatusOK, resp.Code)
|
2017-05-07 20:55:27 -06:00
|
|
|
var branch models.ProtectedBranch
|
2017-12-03 15:46:01 -07:00
|
|
|
t.Log(resp.Body.String())
|
|
|
|
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &branch))
|
2017-09-14 02:16:22 -06:00
|
|
|
assert.Equal(t, canPush, !branch.IsProtected())
|
2017-05-07 20:55:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInternal_GetProtectedBranch(t *testing.T) {
|
|
|
|
prepareTestEnv(t)
|
|
|
|
|
|
|
|
assertProtectedBranch(t, 1, "master", false, true)
|
|
|
|
assertProtectedBranch(t, 1, "dev", false, true)
|
|
|
|
assertProtectedBranch(t, 1, "lunny/dev", false, true)
|
|
|
|
}
|