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