code.gitea.io/gitea@v1.22.3/tests/integration/editor_test.go (about) 1 // Copyright 2017 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package integration 5 6 import ( 7 "fmt" 8 "net/http" 9 "net/http/httptest" 10 "net/url" 11 "path" 12 "testing" 13 14 "code.gitea.io/gitea/modules/json" 15 gitea_context "code.gitea.io/gitea/services/context" 16 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func TestCreateFile(t *testing.T) { 21 onGiteaRun(t, func(t *testing.T, u *url.URL) { 22 session := loginUser(t, "user2") 23 testCreateFile(t, session, "user2", "repo1", "master", "test.txt", "Content") 24 }) 25 } 26 27 func testCreateFile(t *testing.T, session *TestSession, user, repo, branch, filePath, content string) *httptest.ResponseRecorder { 28 // Request editor page 29 newURL := fmt.Sprintf("/%s/%s/_new/%s/", user, repo, branch) 30 req := NewRequest(t, "GET", newURL) 31 resp := session.MakeRequest(t, req, http.StatusOK) 32 33 doc := NewHTMLParser(t, resp.Body) 34 lastCommit := doc.GetInputValueByName("last_commit") 35 assert.NotEmpty(t, lastCommit) 36 37 // Save new file to master branch 38 req = NewRequestWithValues(t, "POST", newURL, map[string]string{ 39 "_csrf": doc.GetCSRF(), 40 "last_commit": lastCommit, 41 "tree_path": filePath, 42 "content": content, 43 "commit_choice": "direct", 44 }) 45 return session.MakeRequest(t, req, http.StatusSeeOther) 46 } 47 48 func TestCreateFileOnProtectedBranch(t *testing.T) { 49 onGiteaRun(t, func(t *testing.T, u *url.URL) { 50 session := loginUser(t, "user2") 51 52 csrf := GetCSRF(t, session, "/user2/repo1/settings/branches") 53 // Change master branch to protected 54 req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{ 55 "_csrf": csrf, 56 "rule_name": "master", 57 "enable_push": "true", 58 }) 59 session.MakeRequest(t, req, http.StatusSeeOther) 60 // Check if master branch has been locked successfully 61 flashCookie := session.GetCookie(gitea_context.CookieNameFlash) 62 assert.NotNil(t, flashCookie) 63 assert.EqualValues(t, "success%3DBranch%2Bprotection%2Bfor%2Brule%2B%2522master%2522%2Bhas%2Bbeen%2Bupdated.", flashCookie.Value) 64 65 // Request editor page 66 req = NewRequest(t, "GET", "/user2/repo1/_new/master/") 67 resp := session.MakeRequest(t, req, http.StatusOK) 68 69 doc := NewHTMLParser(t, resp.Body) 70 lastCommit := doc.GetInputValueByName("last_commit") 71 assert.NotEmpty(t, lastCommit) 72 73 // Save new file to master branch 74 req = NewRequestWithValues(t, "POST", "/user2/repo1/_new/master/", map[string]string{ 75 "_csrf": doc.GetCSRF(), 76 "last_commit": lastCommit, 77 "tree_path": "test.txt", 78 "content": "Content", 79 "commit_choice": "direct", 80 }) 81 82 resp = session.MakeRequest(t, req, http.StatusOK) 83 // Check body for error message 84 assert.Contains(t, resp.Body.String(), "Cannot commit to protected branch "master".") 85 86 // remove the protected branch 87 csrf = GetCSRF(t, session, "/user2/repo1/settings/branches") 88 89 // Change master branch to protected 90 req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/1/delete", map[string]string{ 91 "_csrf": csrf, 92 }) 93 94 resp = session.MakeRequest(t, req, http.StatusOK) 95 96 res := make(map[string]string) 97 assert.NoError(t, json.NewDecoder(resp.Body).Decode(&res)) 98 assert.EqualValues(t, "/user2/repo1/settings/branches", res["redirect"]) 99 100 // Check if master branch has been locked successfully 101 flashCookie = session.GetCookie(gitea_context.CookieNameFlash) 102 assert.NotNil(t, flashCookie) 103 assert.EqualValues(t, "error%3DRemoving%2Bbranch%2Bprotection%2Brule%2B%25221%2522%2Bfailed.", flashCookie.Value) 104 }) 105 } 106 107 func testEditFile(t *testing.T, session *TestSession, user, repo, branch, filePath, newContent string) *httptest.ResponseRecorder { 108 // Get to the 'edit this file' page 109 req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath)) 110 resp := session.MakeRequest(t, req, http.StatusOK) 111 112 htmlDoc := NewHTMLParser(t, resp.Body) 113 lastCommit := htmlDoc.GetInputValueByName("last_commit") 114 assert.NotEmpty(t, lastCommit) 115 116 // Submit the edits 117 req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath), 118 map[string]string{ 119 "_csrf": htmlDoc.GetCSRF(), 120 "last_commit": lastCommit, 121 "tree_path": filePath, 122 "content": newContent, 123 "commit_choice": "direct", 124 }, 125 ) 126 session.MakeRequest(t, req, http.StatusSeeOther) 127 128 // Verify the change 129 req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", branch, filePath)) 130 resp = session.MakeRequest(t, req, http.StatusOK) 131 assert.EqualValues(t, newContent, resp.Body.String()) 132 133 return resp 134 } 135 136 func testEditFileToNewBranch(t *testing.T, session *TestSession, user, repo, branch, targetBranch, filePath, newContent string) *httptest.ResponseRecorder { 137 // Get to the 'edit this file' page 138 req := NewRequest(t, "GET", path.Join(user, repo, "_edit", branch, filePath)) 139 resp := session.MakeRequest(t, req, http.StatusOK) 140 141 htmlDoc := NewHTMLParser(t, resp.Body) 142 lastCommit := htmlDoc.GetInputValueByName("last_commit") 143 assert.NotEmpty(t, lastCommit) 144 145 // Submit the edits 146 req = NewRequestWithValues(t, "POST", path.Join(user, repo, "_edit", branch, filePath), 147 map[string]string{ 148 "_csrf": htmlDoc.GetCSRF(), 149 "last_commit": lastCommit, 150 "tree_path": filePath, 151 "content": newContent, 152 "commit_choice": "commit-to-new-branch", 153 "new_branch_name": targetBranch, 154 }, 155 ) 156 session.MakeRequest(t, req, http.StatusSeeOther) 157 158 // Verify the change 159 req = NewRequest(t, "GET", path.Join(user, repo, "raw/branch", targetBranch, filePath)) 160 resp = session.MakeRequest(t, req, http.StatusOK) 161 assert.EqualValues(t, newContent, resp.Body.String()) 162 163 return resp 164 } 165 166 func TestEditFile(t *testing.T) { 167 onGiteaRun(t, func(t *testing.T, u *url.URL) { 168 session := loginUser(t, "user2") 169 testEditFile(t, session, "user2", "repo1", "master", "README.md", "Hello, World (Edited)\n") 170 }) 171 } 172 173 func TestEditFileToNewBranch(t *testing.T) { 174 onGiteaRun(t, func(t *testing.T, u *url.URL) { 175 session := loginUser(t, "user2") 176 testEditFileToNewBranch(t, session, "user2", "repo1", "master", "feature/test", "README.md", "Hello, World (Edited)\n") 177 }) 178 }