forked from mtusi2026/mtusi2026lab5
fix: validate message request fields
This commit is contained in:
@@ -263,6 +263,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{
|
||||
|
||||
Reference in New Issue
Block a user