2017-05-08 11:55:27 +09: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 23:00:23 +09:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2017-05-08 11:55:27 +09:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr, canPush bool) {
|
2019-03-18 23:00:23 +09:00
|
|
|
reqURL := fmt.Sprintf("/api/internal/branch/%d/%s", repoID, util.PathEscapeSegments(branchName))
|
2017-06-10 09:41:36 +09:00
|
|
|
req := NewRequest(t, "GET", reqURL)
|
2017-05-08 11:55:27 +09:00
|
|
|
t.Log(reqURL)
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))
|
|
|
|
|
2017-07-08 04:36:47 +09:00
|
|
|
resp := MakeRequest(t, req, NoExpectedStatus)
|
2017-05-08 11:55:27 +09:00
|
|
|
if isErr {
|
2017-12-04 07:46:01 +09:00
|
|
|
assert.EqualValues(t, http.StatusInternalServerError, resp.Code)
|
2017-05-08 11:55:27 +09:00
|
|
|
} else {
|
2017-12-04 07:46:01 +09:00
|
|
|
assert.EqualValues(t, http.StatusOK, resp.Code)
|
2017-05-08 11:55:27 +09:00
|
|
|
var branch models.ProtectedBranch
|
2017-12-04 07:46:01 +09:00
|
|
|
t.Log(resp.Body.String())
|
|
|
|
assert.NoError(t, json.Unmarshal(resp.Body.Bytes(), &branch))
|
2017-09-14 17:16:22 +09:00
|
|
|
assert.Equal(t, canPush, !branch.IsProtected())
|
2017-05-08 11:55:27 +09: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)
|
|
|
|
}
|