Implement POST task (#28)
Closes #21 Reviewed-on: #28
This commit was merged in pull request #28.
This commit is contained in:
@@ -205,3 +205,80 @@ func TestDeleteUserGoalInvalidId(t *testing.T) {
|
||||
Status(400).
|
||||
JSON().Object().Value("error").IsEqual("Invalid goal ID")
|
||||
}
|
||||
|
||||
func TestCreateTask(t *testing.T) {
|
||||
e := startServer(t)
|
||||
|
||||
// Create a new task without assigned user
|
||||
requestBody := map[string]interface{}{
|
||||
"name": "Test Task",
|
||||
"reward": 100,
|
||||
}
|
||||
|
||||
response := e.POST("/tasks").
|
||||
WithJSON(requestBody).
|
||||
Expect().
|
||||
Status(201). // Expect Created status
|
||||
JSON().Object()
|
||||
|
||||
// Verify the response has an ID
|
||||
response.ContainsKey("id")
|
||||
taskId := response.Value("id").Number().Raw()
|
||||
|
||||
// Create a new task with assigned user
|
||||
assignedUserId := 1
|
||||
requestBodyWithUser := map[string]interface{}{
|
||||
"name": "Test Task Assigned",
|
||||
"reward": 200,
|
||||
"assigned": assignedUserId,
|
||||
}
|
||||
|
||||
responseWithUser := e.POST("/tasks").
|
||||
WithJSON(requestBodyWithUser).
|
||||
Expect().
|
||||
Status(201).
|
||||
JSON().Object()
|
||||
|
||||
responseWithUser.ContainsKey("id")
|
||||
responseWithUser.Value("id").Number().NotEqual(taskId) // Ensure different ID
|
||||
}
|
||||
|
||||
func TestCreateTaskNoName(t *testing.T) {
|
||||
e := startServer(t)
|
||||
|
||||
requestBody := map[string]interface{}{
|
||||
"reward": 100,
|
||||
}
|
||||
|
||||
e.POST("/tasks").WithJSON(requestBody).Expect().Status(400)
|
||||
}
|
||||
|
||||
func TestCreateTaskInvalidAssignedUser(t *testing.T) {
|
||||
e := startServer(t)
|
||||
|
||||
requestBody := map[string]interface{}{
|
||||
"name": "Test Task Invalid User",
|
||||
"reward": 100,
|
||||
"assigned": 999, // Non-existent user ID
|
||||
}
|
||||
|
||||
e.POST("/tasks").
|
||||
WithJSON(requestBody).
|
||||
Expect().
|
||||
Status(404). // Expect Not Found
|
||||
JSON().Object().Value("error").IsEqual(ErrUserNotFound)
|
||||
}
|
||||
|
||||
func TestCreateTaskInvalidRequestBody(t *testing.T) {
|
||||
e := startServer(t)
|
||||
|
||||
// Test with missing fields (name is required)
|
||||
invalidRequest := map[string]interface{}{
|
||||
"reward": 5000,
|
||||
}
|
||||
|
||||
e.POST("/tasks").
|
||||
WithJSON(invalidRequest).
|
||||
Expect().
|
||||
Status(400)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user