2017-05-08 05:55:27 +03: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 03:41:36 +03:00
req := NewRequest ( t , "GET" , reqURL )
2017-05-08 05:55:27 +03:00
t . Log ( reqURL )
req . Header . Add ( "Authorization" , fmt . Sprintf ( "Bearer %s" , setting . InternalToken ) )
2017-07-07 22:36:47 +03:00
resp := MakeRequest ( t , req , NoExpectedStatus )
2017-05-08 05:55:27 +03:00
if isErr {
2017-12-04 01:46:01 +03:00
assert . EqualValues ( t , http . StatusInternalServerError , resp . Code )
2017-05-08 05:55:27 +03:00
} else {
2017-12-04 01:46:01 +03:00
assert . EqualValues ( t , http . StatusOK , resp . Code )
2017-05-08 05:55:27 +03:00
var branch models . ProtectedBranch
2017-12-04 01:46:01 +03:00
t . Log ( resp . Body . String ( ) )
assert . NoError ( t , json . Unmarshal ( resp . Body . Bytes ( ) , & branch ) )
2017-09-14 11:16:22 +03:00
assert . Equal ( t , canPush , ! branch . IsProtected ( ) )
2017-05-08 05:55:27 +03: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 )
}