code.gitea.io/gitea@v1.21.7/tests/integration/api_repo_branch_test.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package integration
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"io"
    10  	"net/http"
    11  	"net/url"
    12  	"testing"
    13  
    14  	auth_model "code.gitea.io/gitea/models/auth"
    15  	repo_model "code.gitea.io/gitea/models/repo"
    16  	"code.gitea.io/gitea/models/unittest"
    17  	user_model "code.gitea.io/gitea/models/user"
    18  	"code.gitea.io/gitea/modules/json"
    19  	"code.gitea.io/gitea/modules/setting"
    20  	api "code.gitea.io/gitea/modules/structs"
    21  	"code.gitea.io/gitea/tests"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestAPIRepoBranchesPlain(t *testing.T) {
    27  	onGiteaRun(t, func(*testing.T, *url.URL) {
    28  		repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
    29  		user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    30  		session := loginUser(t, user1.LowerName)
    31  		token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
    32  
    33  		link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/org3/%s/branches", repo3.Name)) // a plain repo
    34  		link.RawQuery = url.Values{"token": {token}}.Encode()
    35  		resp := MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
    36  		bs, err := io.ReadAll(resp.Body)
    37  		assert.NoError(t, err)
    38  
    39  		var branches []*api.Branch
    40  		assert.NoError(t, json.Unmarshal(bs, &branches))
    41  		assert.Len(t, branches, 2)
    42  		assert.EqualValues(t, "test_branch", branches[0].Name)
    43  		assert.EqualValues(t, "master", branches[1].Name)
    44  
    45  		link2, _ := url.Parse(fmt.Sprintf("/api/v1/repos/org3/%s/branches/test_branch", repo3.Name))
    46  		link2.RawQuery = url.Values{"token": {token}}.Encode()
    47  		resp = MakeRequest(t, NewRequest(t, "GET", link2.String()), http.StatusOK)
    48  		bs, err = io.ReadAll(resp.Body)
    49  		assert.NoError(t, err)
    50  		var branch api.Branch
    51  		assert.NoError(t, json.Unmarshal(bs, &branch))
    52  		assert.EqualValues(t, "test_branch", branch.Name)
    53  
    54  		req := NewRequest(t, "POST", link.String())
    55  		req.Header.Add("Content-Type", "application/json")
    56  		req.Body = io.NopCloser(bytes.NewBufferString(`{"new_branch_name":"test_branch2", "old_branch_name": "test_branch", "old_ref_name":"refs/heads/test_branch"}`))
    57  		resp = MakeRequest(t, req, http.StatusCreated)
    58  		bs, err = io.ReadAll(resp.Body)
    59  		assert.NoError(t, err)
    60  		var branch2 api.Branch
    61  		assert.NoError(t, json.Unmarshal(bs, &branch2))
    62  		assert.EqualValues(t, "test_branch2", branch2.Name)
    63  		assert.EqualValues(t, branch.Commit.ID, branch2.Commit.ID)
    64  
    65  		resp = MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
    66  		bs, err = io.ReadAll(resp.Body)
    67  		assert.NoError(t, err)
    68  
    69  		branches = []*api.Branch{}
    70  		assert.NoError(t, json.Unmarshal(bs, &branches))
    71  		assert.Len(t, branches, 3)
    72  		assert.EqualValues(t, "test_branch", branches[0].Name)
    73  		assert.EqualValues(t, "test_branch2", branches[1].Name)
    74  		assert.EqualValues(t, "master", branches[2].Name)
    75  
    76  		link3, _ := url.Parse(fmt.Sprintf("/api/v1/repos/org3/%s/branches/test_branch2", repo3.Name))
    77  		MakeRequest(t, NewRequest(t, "DELETE", link3.String()), http.StatusNotFound)
    78  
    79  		link3.RawQuery = url.Values{"token": {token}}.Encode()
    80  		MakeRequest(t, NewRequest(t, "DELETE", link3.String()), http.StatusNoContent)
    81  		assert.NoError(t, err)
    82  	})
    83  }
    84  
    85  func TestAPIRepoBranchesMirror(t *testing.T) {
    86  	defer tests.PrepareTestEnv(t)()
    87  
    88  	repo5 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 5})
    89  	user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
    90  	session := loginUser(t, user1.LowerName)
    91  	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
    92  
    93  	link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/org3/%s/branches", repo5.Name)) // a mirror repo
    94  	link.RawQuery = url.Values{"token": {token}}.Encode()
    95  	resp := MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
    96  	bs, err := io.ReadAll(resp.Body)
    97  	assert.NoError(t, err)
    98  
    99  	var branches []*api.Branch
   100  	assert.NoError(t, json.Unmarshal(bs, &branches))
   101  	assert.Len(t, branches, 2)
   102  	assert.EqualValues(t, "test_branch", branches[0].Name)
   103  	assert.EqualValues(t, "master", branches[1].Name)
   104  
   105  	link2, _ := url.Parse(fmt.Sprintf("/api/v1/repos/org3/%s/branches/test_branch", repo5.Name))
   106  	link2.RawQuery = url.Values{"token": {token}}.Encode()
   107  	resp = MakeRequest(t, NewRequest(t, "GET", link2.String()), http.StatusOK)
   108  	bs, err = io.ReadAll(resp.Body)
   109  	assert.NoError(t, err)
   110  	var branch api.Branch
   111  	assert.NoError(t, json.Unmarshal(bs, &branch))
   112  	assert.EqualValues(t, "test_branch", branch.Name)
   113  
   114  	req := NewRequest(t, "POST", link.String())
   115  	req.Header.Add("Content-Type", "application/json")
   116  	req.Body = io.NopCloser(bytes.NewBufferString(`{"new_branch_name":"test_branch2", "old_branch_name": "test_branch", "old_ref_name":"refs/heads/test_branch"}`))
   117  	resp = MakeRequest(t, req, http.StatusForbidden)
   118  	bs, err = io.ReadAll(resp.Body)
   119  	assert.NoError(t, err)
   120  	assert.EqualValues(t, "{\"message\":\"Git Repository is a mirror.\",\"url\":\""+setting.AppURL+"api/swagger\"}\n", string(bs))
   121  
   122  	resp = MakeRequest(t, NewRequest(t, "DELETE", link2.String()), http.StatusForbidden)
   123  	bs, err = io.ReadAll(resp.Body)
   124  	assert.NoError(t, err)
   125  	assert.EqualValues(t, "{\"message\":\"Git Repository is a mirror.\",\"url\":\""+setting.AppURL+"api/swagger\"}\n", string(bs))
   126  }