|
8 | 8 | "encoding/base64"
|
9 | 9 | "fmt"
|
10 | 10 | "net/http"
|
| 11 | + "net/url" |
11 | 12 | "path/filepath"
|
12 | 13 | "testing"
|
13 | 14 |
|
@@ -91,125 +92,126 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon
|
91 | 92 | }
|
92 | 93 |
|
93 | 94 | func TestAPICreateFile(t *testing.T) {
|
94 |
| - prepareTestEnv(t) |
95 |
| - user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 |
96 |
| - user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org |
97 |
| - user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos |
98 |
| - repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo |
99 |
| - repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo |
100 |
| - repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo |
101 |
| - fileID := 0 |
102 |
| - |
103 |
| - // Get user2's token |
104 |
| - session := loginUser(t, user2.Name) |
105 |
| - token2 := getTokenForLoggedInUser(t, session) |
106 |
| - session = emptyTestSession(t) |
107 |
| - // Get user4's token |
108 |
| - session = loginUser(t, user4.Name) |
109 |
| - token4 := getTokenForLoggedInUser(t, session) |
110 |
| - session = emptyTestSession(t) |
111 |
| - |
112 |
| - // Test creating a file in repo1 which user2 owns, try both with branch and empty branch |
113 |
| - for _, branch := range [...]string{ |
114 |
| - "master", // Branch |
115 |
| - "", // Empty branch |
116 |
| - } { |
| 95 | + onGiteaRun(t, func(t *testing.T, u *url.URL) { |
| 96 | + user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) // owner of the repo1 & repo16 |
| 97 | + user3 := models.AssertExistsAndLoadBean(t, &models.User{ID: 3}).(*models.User) // owner of the repo3, is an org |
| 98 | + user4 := models.AssertExistsAndLoadBean(t, &models.User{ID: 4}).(*models.User) // owner of neither repos |
| 99 | + repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) // public repo |
| 100 | + repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 3}).(*models.Repository) // public repo |
| 101 | + repo16 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 16}).(*models.Repository) // private repo |
| 102 | + fileID := 0 |
| 103 | + |
| 104 | + // Get user2's token |
| 105 | + session := loginUser(t, user2.Name) |
| 106 | + token2 := getTokenForLoggedInUser(t, session) |
| 107 | + session = emptyTestSession(t) |
| 108 | + // Get user4's token |
| 109 | + session = loginUser(t, user4.Name) |
| 110 | + token4 := getTokenForLoggedInUser(t, session) |
| 111 | + session = emptyTestSession(t) |
| 112 | + |
| 113 | + // Test creating a file in repo1 which user2 owns, try both with branch and empty branch |
| 114 | + for _, branch := range [...]string{ |
| 115 | + "master", // Branch |
| 116 | + "", // Empty branch |
| 117 | + } { |
| 118 | + createFileOptions := getCreateFileOptions() |
| 119 | + createFileOptions.BranchName = branch |
| 120 | + fileID++ |
| 121 | + treePath := fmt.Sprintf("new/file%d.txt", fileID) |
| 122 | + url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2) |
| 123 | + req := NewRequestWithJSON(t, "POST", url, &createFileOptions) |
| 124 | + resp := session.MakeRequest(t, req, http.StatusCreated) |
| 125 | + gitRepo, _ := git.OpenRepository(repo1.RepoPath()) |
| 126 | + commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName) |
| 127 | + expectedFileResponse := getExpectedFileResponseForCreate(commitID, treePath) |
| 128 | + var fileResponse api.FileResponse |
| 129 | + DecodeJSON(t, resp, &fileResponse) |
| 130 | + assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content) |
| 131 | + assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA) |
| 132 | + assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL) |
| 133 | + assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email) |
| 134 | + assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name) |
| 135 | + } |
| 136 | + |
| 137 | + // Test creating a file in a new branch |
117 | 138 | createFileOptions := getCreateFileOptions()
|
118 |
| - createFileOptions.BranchName = branch |
| 139 | + createFileOptions.BranchName = repo1.DefaultBranch |
| 140 | + createFileOptions.NewBranchName = "new_branch" |
119 | 141 | fileID++
|
120 | 142 | treePath := fmt.Sprintf("new/file%d.txt", fileID)
|
121 | 143 | url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2)
|
122 | 144 | req := NewRequestWithJSON(t, "POST", url, &createFileOptions)
|
123 | 145 | resp := session.MakeRequest(t, req, http.StatusCreated)
|
124 |
| - gitRepo, _ := git.OpenRepository(repo1.RepoPath()) |
125 |
| - commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName) |
126 |
| - expectedFileResponse := getExpectedFileResponseForCreate(commitID, treePath) |
127 | 146 | var fileResponse api.FileResponse
|
128 | 147 | DecodeJSON(t, resp, &fileResponse)
|
129 |
| - assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content) |
130 |
| - assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA) |
131 |
| - assert.EqualValues(t, expectedFileResponse.Commit.HTMLURL, fileResponse.Commit.HTMLURL) |
132 |
| - assert.EqualValues(t, expectedFileResponse.Commit.Author.Email, fileResponse.Commit.Author.Email) |
133 |
| - assert.EqualValues(t, expectedFileResponse.Commit.Author.Name, fileResponse.Commit.Author.Name) |
134 |
| - } |
| 148 | + expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf" |
| 149 | + expectedHTMLURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/blob/new_branch/new/file%d.txt", fileID) |
| 150 | + expectedDownloadURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/raw/branch/new_branch/new/file%d.txt", fileID) |
| 151 | + assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA) |
| 152 | + assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL) |
| 153 | + assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL) |
| 154 | + |
| 155 | + // Test trying to create a file that already exists, should fail |
| 156 | + createFileOptions = getCreateFileOptions() |
| 157 | + treePath = "README.md" |
| 158 | + url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2) |
| 159 | + req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
| 160 | + resp = session.MakeRequest(t, req, http.StatusInternalServerError) |
| 161 | + expectedAPIError := context.APIError{ |
| 162 | + Message: "repository file already exists [path: " + treePath + "]", |
| 163 | + URL: base.DocURL, |
| 164 | + } |
| 165 | + var apiError context.APIError |
| 166 | + DecodeJSON(t, resp, &apiError) |
| 167 | + assert.Equal(t, expectedAPIError, apiError) |
| 168 | + |
| 169 | + // Test creating a file in repo1 by user4 who does not have write access |
| 170 | + createFileOptions = getCreateFileOptions() |
| 171 | + fileID++ |
| 172 | + treePath = fmt.Sprintf("new/file%d.txt", fileID) |
| 173 | + url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4) |
| 174 | + req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
| 175 | + session.MakeRequest(t, req, http.StatusNotFound) |
135 | 176 |
|
136 |
| - // Test creating a file in a new branch |
137 |
| - createFileOptions := getCreateFileOptions() |
138 |
| - createFileOptions.BranchName = repo1.DefaultBranch |
139 |
| - createFileOptions.NewBranchName = "new_branch" |
140 |
| - fileID++ |
141 |
| - treePath := fmt.Sprintf("new/file%d.txt", fileID) |
142 |
| - url := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2) |
143 |
| - req := NewRequestWithJSON(t, "POST", url, &createFileOptions) |
144 |
| - resp := session.MakeRequest(t, req, http.StatusCreated) |
145 |
| - var fileResponse api.FileResponse |
146 |
| - DecodeJSON(t, resp, &fileResponse) |
147 |
| - expectedSHA := "a635aa942442ddfdba07468cf9661c08fbdf0ebf" |
148 |
| - expectedHTMLURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/blob/new_branch/new/file%d.txt", fileID) |
149 |
| - expectedDownloadURL := fmt.Sprintf("http://localhost:"+setting.HTTPPort+"/user2/repo1/raw/branch/new_branch/new/file%d.txt", fileID) |
150 |
| - assert.EqualValues(t, expectedSHA, fileResponse.Content.SHA) |
151 |
| - assert.EqualValues(t, expectedHTMLURL, fileResponse.Content.HTMLURL) |
152 |
| - assert.EqualValues(t, expectedDownloadURL, fileResponse.Content.DownloadURL) |
153 |
| - |
154 |
| - // Test trying to create a file that already exists, should fail |
155 |
| - createFileOptions = getCreateFileOptions() |
156 |
| - treePath = "README.md" |
157 |
| - url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token2) |
158 |
| - req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
159 |
| - resp = session.MakeRequest(t, req, http.StatusInternalServerError) |
160 |
| - expectedAPIError := context.APIError{ |
161 |
| - Message: "repository file already exists [path: " + treePath + "]", |
162 |
| - URL: base.DocURL, |
163 |
| - } |
164 |
| - var apiError context.APIError |
165 |
| - DecodeJSON(t, resp, &apiError) |
166 |
| - assert.Equal(t, expectedAPIError, apiError) |
167 |
| - |
168 |
| - // Test creating a file in repo1 by user4 who does not have write access |
169 |
| - createFileOptions = getCreateFileOptions() |
170 |
| - fileID++ |
171 |
| - treePath = fmt.Sprintf("new/file%d.txt", fileID) |
172 |
| - url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token4) |
173 |
| - req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
174 |
| - session.MakeRequest(t, req, http.StatusNotFound) |
175 |
| - |
176 |
| - // Tests a repo with no token given so will fail |
177 |
| - createFileOptions = getCreateFileOptions() |
178 |
| - fileID++ |
179 |
| - treePath = fmt.Sprintf("new/file%d.txt", fileID) |
180 |
| - url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath) |
181 |
| - req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
182 |
| - session.MakeRequest(t, req, http.StatusNotFound) |
183 |
| - |
184 |
| - // Test using access token for a private repo that the user of the token owns |
185 |
| - createFileOptions = getCreateFileOptions() |
186 |
| - fileID++ |
187 |
| - treePath = fmt.Sprintf("new/file%d.txt", fileID) |
188 |
| - url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2) |
189 |
| - req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
190 |
| - session.MakeRequest(t, req, http.StatusCreated) |
191 |
| - |
192 |
| - // Test using org repo "user3/repo3" where user2 is a collaborator |
193 |
| - createFileOptions = getCreateFileOptions() |
194 |
| - fileID++ |
195 |
| - treePath = fmt.Sprintf("new/file%d.txt", fileID) |
196 |
| - url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2) |
197 |
| - req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
198 |
| - session.MakeRequest(t, req, http.StatusCreated) |
199 |
| - |
200 |
| - // Test using org repo "user3/repo3" with no user token |
201 |
| - createFileOptions = getCreateFileOptions() |
202 |
| - fileID++ |
203 |
| - treePath = fmt.Sprintf("new/file%d.txt", fileID) |
204 |
| - url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath) |
205 |
| - req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
206 |
| - session.MakeRequest(t, req, http.StatusNotFound) |
207 |
| - |
208 |
| - // Test using repo "user2/repo1" where user4 is a NOT collaborator |
209 |
| - createFileOptions = getCreateFileOptions() |
210 |
| - fileID++ |
211 |
| - treePath = fmt.Sprintf("new/file%d.txt", fileID) |
212 |
| - url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4) |
213 |
| - req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
214 |
| - session.MakeRequest(t, req, http.StatusForbidden) |
| 177 | + // Tests a repo with no token given so will fail |
| 178 | + createFileOptions = getCreateFileOptions() |
| 179 | + fileID++ |
| 180 | + treePath = fmt.Sprintf("new/file%d.txt", fileID) |
| 181 | + url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo16.Name, treePath) |
| 182 | + req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
| 183 | + session.MakeRequest(t, req, http.StatusNotFound) |
| 184 | + |
| 185 | + // Test using access token for a private repo that the user of the token owns |
| 186 | + createFileOptions = getCreateFileOptions() |
| 187 | + fileID++ |
| 188 | + treePath = fmt.Sprintf("new/file%d.txt", fileID) |
| 189 | + url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo16.Name, treePath, token2) |
| 190 | + req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
| 191 | + session.MakeRequest(t, req, http.StatusCreated) |
| 192 | + |
| 193 | + // Test using org repo "user3/repo3" where user2 is a collaborator |
| 194 | + createFileOptions = getCreateFileOptions() |
| 195 | + fileID++ |
| 196 | + treePath = fmt.Sprintf("new/file%d.txt", fileID) |
| 197 | + url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user3.Name, repo3.Name, treePath, token2) |
| 198 | + req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
| 199 | + session.MakeRequest(t, req, http.StatusCreated) |
| 200 | + |
| 201 | + // Test using org repo "user3/repo3" with no user token |
| 202 | + createFileOptions = getCreateFileOptions() |
| 203 | + fileID++ |
| 204 | + treePath = fmt.Sprintf("new/file%d.txt", fileID) |
| 205 | + url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user3.Name, repo3.Name, treePath) |
| 206 | + req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
| 207 | + session.MakeRequest(t, req, http.StatusNotFound) |
| 208 | + |
| 209 | + // Test using repo "user2/repo1" where user4 is a NOT collaborator |
| 210 | + createFileOptions = getCreateFileOptions() |
| 211 | + fileID++ |
| 212 | + treePath = fmt.Sprintf("new/file%d.txt", fileID) |
| 213 | + url = fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s?token=%s", user2.Name, repo1.Name, treePath, token4) |
| 214 | + req = NewRequestWithJSON(t, "POST", url, &createFileOptions) |
| 215 | + session.MakeRequest(t, req, http.StatusForbidden) |
| 216 | + }) |
215 | 217 | }
|
0 commit comments