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