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