2017-05-08 02:55:27 +00: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"
|
|
|
|
)
|
|
|
|
|
2017-10-02 20:23:41 +00:00
|
|
|
func assertProtectedBranch(t *testing.T, repoID int64, branchName string, isErr, isProtected bool) {
|
2017-05-08 02:55:27 +00:00
|
|
|
reqURL := fmt.Sprintf("/api/internal/branch/%d/%s", repoID, url.QueryEscape(branchName))
|
2017-06-10 00:41:36 +00:00
|
|
|
req := NewRequest(t, "GET", reqURL)
|
2017-05-08 02:55:27 +00:00
|
|
|
t.Log(reqURL)
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))
|
|
|
|
|
2017-07-07 19:36:47 +00:00
|
|
|
resp := MakeRequest(t, req, NoExpectedStatus)
|
2017-05-08 02:55:27 +00:00
|
|
|
if isErr {
|
2017-07-07 19:36:47 +00:00
|
|
|
assert.EqualValues(t, http.StatusInternalServerError, resp.HeaderCode)
|
2017-05-08 02:55:27 +00:00
|
|
|
} else {
|
|
|
|
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
|
|
|
|
var branch models.ProtectedBranch
|
|
|
|
t.Log(string(resp.Body))
|
|
|
|
assert.NoError(t, json.Unmarshal(resp.Body, &branch))
|
2017-10-02 20:23:41 +00:00
|
|
|
assert.Equal(t, isProtected, branch.IsProtected())
|
2017-05-08 02:55:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInternal_GetProtectedBranch(t *testing.T) {
|
|
|
|
prepareTestEnv(t)
|
|
|
|
|
2017-10-02 20:23:41 +00:00
|
|
|
assertProtectedBranch(t, 1, "master", false, false)
|
|
|
|
assertProtectedBranch(t, 1, "dev", false, false)
|
|
|
|
assertProtectedBranch(t, 1, "lunny/dev", false, false)
|
2017-05-08 02:55:27 +00:00
|
|
|
}
|