fix: validate message request fields

This commit is contained in:
2026-10-30 14:01:03 +03:00
parent 3815be3493
commit 05d9bb0ff2
3 changed files with 65 additions and 6 deletions
+41
View File
@@ -259,6 +259,47 @@ func (s *MessengerSuite) TestMessageCreationValidatesIdempotencyKey() {
}
}
func (s *MessengerSuite) TestMessageCreationValidatesFieldPresenceAndTypes() {
store := messengerStoreStub{userByID: model.User{ID: "user-id"}}
server := s.newTestServer(store, sessionStub{userID: "user-id"}, passwordsStub{})
attachmentID := "00000000-0000-0000-0000-000000000002"
invalidBodies := []string{
`{"attachment_ids":["` + attachmentID + `"]}`,
`{"text":null,"attachment_ids":["` + attachmentID + `"]}`,
`{"text":"x","attachment_ids":null}`,
`{"text":1}`,
`{"text":"x","attachment_ids":{}}`,
}
for _, body := range invalidBodies {
response := createMessageRequestResponse(server, body)
s.Equal(http.StatusBadRequest, response.Code, body)
}
validBodies := []string{
`{"text":"message"}`,
`{"text":"message","attachment_ids":[]}`,
`{"text":"","attachment_ids":["` + attachmentID + `"]}`,
}
for _, body := range validBodies {
response := createMessageRequestResponse(server, body)
s.Equal(http.StatusCreated, response.Code, body)
}
}
func createMessageRequestResponse(server *Server, body string) *httptest.ResponseRecorder {
request := httptest.NewRequest(
http.MethodPost,
"/api/v1/chats/00000000-0000-0000-0000-000000000001/messages",
strings.NewReader(body),
)
request.Header.Set("Authorization", "Bearer valid-token")
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Idempotency-Key", "message-key")
response := httptest.NewRecorder()
server.Handler().ServeHTTP(response, request)
return response
}
func (s *MessengerSuite) TestMessageCreationReturnsConflictForReusedKeyWithDifferentBody() {
t := s.T()
store := messengerStoreStub{