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