Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug when push a branch name with / & fix an integration test bug #1689

Merged
merged 1 commit into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions integrations/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ func initIntegrationTest() {
if err != nil {
log.Fatalf("db.Query: %v", err)
}
if rows.Next() {
break // database already exists
}
if _, err = db.Exec("CREATE DATABASE testgitea"); err != nil {
log.Fatalf("db.Exec: %v", err)
defer rows.Close()

if !rows.Next() {
if _, err = db.Exec("CREATE DATABASE testgitea"); err != nil {
log.Fatalf("db.Exec: %v", err)
}
}
}
routers.GlobalInit()
Expand Down
45 changes: 45 additions & 0 deletions integrations/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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))
req, err := http.NewRequest("GET", reqURL, nil)
t.Log(reqURL)
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", setting.InternalToken))

assert.NoError(t, err)
resp := MakeRequest(req)
if isErr {
assert.EqualValues(t, 500, resp.HeaderCode)
} 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))
assert.Equal(t, canPush, branch.CanPush)
}
}

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)
}
2 changes: 1 addition & 1 deletion routers/private/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// GetProtectedBranchBy get protected branch information
func GetProtectedBranchBy(ctx *macaron.Context) {
repoID := ctx.ParamsInt64(":id")
branchName := ctx.Params(":branch")
branchName := ctx.Params("*")
protectBranch, err := models.GetProtectedBranchBy(repoID, branchName)
if err != nil {
ctx.JSON(500, map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion routers/private/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/", func() {
m.Post("/ssh/:id/update", UpdatePublicKey)
m.Post("/push/update", PushUpdate)
m.Get("/branch/:id/:branch", GetProtectedBranchBy)
m.Get("/branch/:id/*", GetProtectedBranchBy)
}, CheckInternalToken)
}