mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 19:06:18 +01:00 
			
		
		
		
	Catch and handle unallowed file type errors in issue attachment API (#30791)
Before, we would just throw 500 if a user passes an attachment that is not an allowed type. This commit catches this error and throws a 422 instead since this should be considered a validation error.
This commit is contained in:
		| @@ -120,6 +120,34 @@ func TestAPICreateCommentAttachment(t *testing.T) { | ||||
| 	unittest.AssertExistsAndLoadBean(t, &repo_model.Attachment{ID: apiAttachment.ID, CommentID: comment.ID}) | ||||
| } | ||||
|  | ||||
| func TestAPICreateCommentAttachmentWithUnallowedFile(t *testing.T) { | ||||
| 	defer tests.PrepareTestEnv(t)() | ||||
|  | ||||
| 	comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 2}) | ||||
| 	issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: comment.IssueID}) | ||||
| 	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID}) | ||||
| 	repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) | ||||
|  | ||||
| 	session := loginUser(t, repoOwner.Name) | ||||
| 	token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue) | ||||
|  | ||||
| 	filename := "file.bad" | ||||
| 	body := &bytes.Buffer{} | ||||
|  | ||||
| 	// Setup multi-part. | ||||
| 	writer := multipart.NewWriter(body) | ||||
| 	_, err := writer.CreateFormFile("attachment", filename) | ||||
| 	assert.NoError(t, err) | ||||
| 	err = writer.Close() | ||||
| 	assert.NoError(t, err) | ||||
|  | ||||
| 	req := NewRequestWithBody(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/issues/comments/%d/assets", repoOwner.Name, repo.Name, comment.ID), body). | ||||
| 		AddTokenAuth(token). | ||||
| 		SetHeader("Content-Type", writer.FormDataContentType()) | ||||
|  | ||||
| 	session.MakeRequest(t, req, http.StatusUnprocessableEntity) | ||||
| } | ||||
|  | ||||
| func TestAPIEditCommentAttachment(t *testing.T) { | ||||
| 	defer tests.PrepareTestEnv(t)() | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user