Rename endpoints (#42)

Closes #39

Reviewed-on: #42
This commit was merged in pull request #42.
This commit is contained in:
2025-05-14 17:14:58 +02:00
parent 790ee3c622
commit 1589bc9422
6 changed files with 125 additions and 125 deletions

View File

@@ -9,7 +9,7 @@ import (
)
const (
TestGoalName = "Test Goal"
TestAllowanceName = "Test History"
)
func startServer(t *testing.T) *httpexpect.Expect {
@@ -49,56 +49,56 @@ func TestGetUserBadId(t *testing.T) {
e.GET("/user/bad-id").Expect().Status(400)
}
func TestGetUserGoalsWhenNoGoalsPresent(t *testing.T) {
func TestGetUserAllowanceWhenNoAllowancePresent(t *testing.T) {
e := startServer(t)
result := e.GET("/user/1/goals").Expect().Status(200).JSON().Array()
result := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
result.Length().IsEqual(0)
}
func TestGetUserGoals(t *testing.T) {
func TestGetUserAllowance(t *testing.T) {
e := startServer(t)
// Create a new goal
// Create a new allowance
requestBody := map[string]interface{}{
"name": TestGoalName,
"name": TestAllowanceName,
"target": 5000,
"weight": 10,
}
e.POST("/user/1/goals").WithJSON(requestBody).Expect().Status(201)
e.POST("/user/1/allowance").WithJSON(requestBody).Expect().Status(201)
// Validate goal
result := e.GET("/user/1/goals").Expect().Status(200).JSON().Array()
// Validate allowance
result := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
result.Length().IsEqual(1)
item := result.Value(0).Object()
item.Value("id").IsEqual(1)
item.Value("name").IsEqual(TestGoalName)
item.Value("name").IsEqual(TestAllowanceName)
item.Value("target").IsEqual(5000)
item.Value("weight").IsEqual(10)
item.Value("progress").IsEqual(0)
item.NotContainsKey("user_id")
}
func TestGetUserGoalsNoUser(t *testing.T) {
func TestGetUserAllowanceNoUser(t *testing.T) {
e := startServer(t)
e.GET("/user/999/goals").Expect().Status(404)
e.GET("/user/999/allowance").Expect().Status(404)
}
func TestGetUserGoalsBadId(t *testing.T) {
func TestGetUserAllowanceBadId(t *testing.T) {
e := startServer(t)
e.GET("/user/bad-id/goals").Expect().Status(400)
e.GET("/user/bad-id/allowance").Expect().Status(400)
}
func TestCreateUserGoal(t *testing.T) {
func TestCreateUserAllowance(t *testing.T) {
e := startServer(t)
// Create a new goal
// Create a new allowance
requestBody := map[string]interface{}{
"name": TestGoalName,
"name": TestAllowanceName,
"target": 5000,
"weight": 10,
}
response := e.POST("/user/1/goals").
response := e.POST("/user/1/allowance").
WithJSON(requestBody).
Expect().
Status(201).
@@ -106,40 +106,40 @@ func TestCreateUserGoal(t *testing.T) {
// Verify the response has an ID
response.ContainsKey("id")
goalId := response.Value("id").Number().Raw()
allowanceId := response.Value("id").Number().Raw()
// Verify the goal exists in the list of goals
goals := e.GET("/user/1/goals").
// Verify the allowance exists in the list of allowances
allowances := e.GET("/user/1/allowance").
Expect().
Status(200).
JSON().Array()
goals.Length().IsEqual(1)
allowances.Length().IsEqual(1)
goal := goals.Value(0).Object()
goal.Value("id").IsEqual(goalId)
goal.Value("name").IsEqual(TestGoalName)
goal.Value("target").IsEqual(5000)
goal.Value("weight").IsEqual(10)
goal.Value("progress").IsEqual(0)
allowance := allowances.Value(0).Object()
allowance.Value("id").IsEqual(allowanceId)
allowance.Value("name").IsEqual(TestAllowanceName)
allowance.Value("target").IsEqual(5000)
allowance.Value("weight").IsEqual(10)
allowance.Value("progress").IsEqual(0)
}
func TestCreateUserGoalNoUser(t *testing.T) {
func TestCreateUserAllowanceNoUser(t *testing.T) {
e := startServer(t)
requestBody := map[string]interface{}{
"name": TestGoalName,
"name": TestAllowanceName,
"target": 5000,
"weight": 10,
}
e.POST("/user/999/goals").
e.POST("/user/999/allowance").
WithJSON(requestBody).
Expect().
Status(404)
}
func TestCreateUserGoalInvalidInput(t *testing.T) {
func TestCreateUserAllowanceInvalidInput(t *testing.T) {
e := startServer(t)
// Test with empty name
@@ -149,7 +149,7 @@ func TestCreateUserGoalInvalidInput(t *testing.T) {
"weight": 10,
}
e.POST("/user/1/goals").
e.POST("/user/1/allowance").
WithJSON(requestBody).
Expect().
Status(400)
@@ -159,76 +159,76 @@ func TestCreateUserGoalInvalidInput(t *testing.T) {
"target": 5000,
}
e.POST("/user/1/goals").
e.POST("/user/1/allowance").
WithJSON(invalidRequest).
Expect().
Status(400)
}
func TestCreateUserGoalBadId(t *testing.T) {
func TestCreateUserAllowanceBadId(t *testing.T) {
e := startServer(t)
requestBody := map[string]interface{}{
"name": TestGoalName,
"name": TestAllowanceName,
"target": 5000,
"weight": 10,
}
e.POST("/user/bad-id/goals").
e.POST("/user/bad-id/allowance").
WithJSON(requestBody).
Expect().
Status(400)
}
func TestDeleteUserGoal(t *testing.T) {
func TestDeleteUserAllowance(t *testing.T) {
e := startServer(t)
// Create a new goal to delete
// Create a new allowance to delete
createRequest := map[string]interface{}{
"name": TestGoalName,
"name": TestAllowanceName,
"target": 1000,
"weight": 5,
}
response := e.POST("/user/1/goals").
response := e.POST("/user/1/allowance").
WithJSON(createRequest).
Expect().
Status(201).
JSON().Object()
goalId := response.Value("id").Number().Raw()
allowanceId := response.Value("id").Number().Raw()
// Delete the goal
e.DELETE("/user/1/goal/" + strconv.Itoa(int(goalId))).
// Delete the allowance
e.DELETE("/user/1/allowance/" + strconv.Itoa(int(allowanceId))).
Expect().
Status(200).
JSON().Object().Value("message").IsEqual("Goal deleted successfully")
JSON().Object().Value("message").IsEqual("History deleted successfully")
// Verify the goal no longer exists
goals := e.GET("/user/1/goals").
// Verify the allowance no longer exists
allowances := e.GET("/user/1/allowance").
Expect().
Status(200).
JSON().Array()
goals.Length().IsEqual(0)
allowances.Length().IsEqual(0)
}
func TestDeleteUserGoalNotFound(t *testing.T) {
func TestDeleteUserAllowanceNotFound(t *testing.T) {
e := startServer(t)
// Attempt to delete a non-existent goal
e.DELETE("/user/1/goal/999").
// Attempt to delete a non-existent allowance
e.DELETE("/user/1/allowance/999").
Expect().
Status(404).
JSON().Object().Value("error").IsEqual("Goal not found")
JSON().Object().Value("error").IsEqual("History not found")
}
func TestDeleteUserGoalInvalidId(t *testing.T) {
func TestDeleteUserAllowanceInvalidId(t *testing.T) {
e := startServer(t)
// Attempt to delete a goal with an invalid ID
e.DELETE("/user/1/goal/invalid-id").
// Attempt to delete an allowance with an invalid ID
e.DELETE("/user/1/allowance/invalid-id").
Expect().
Status(400).
JSON().Object().Value("error").IsEqual("Invalid goal ID")
JSON().Object().Value("error").IsEqual("Invalid allowance ID")
}
func TestCreateTask(t *testing.T) {
@@ -323,7 +323,7 @@ func createTestTask(e *httpexpect.Expect) {
e.POST("/tasks").WithJSON(requestBody).Expect().Status(201)
}
func TestGetTaskSWhenTasks(t *testing.T) {
func TestGetTasksWhenTasks(t *testing.T) {
e := startServer(t)
createTestTask(e)
@@ -391,9 +391,9 @@ func TestPutTaskInvalidTaskId(t *testing.T) {
func TestPostAllowance(t *testing.T) {
e := startServer(t)
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200)
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200)
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: -10}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10}).Expect().Status(200)
response := e.GET("/user/1").Expect().Status(200).JSON().Object()
response.Value("allowance").Number().IsEqual(100 + 20 - 10)
@@ -402,16 +402,16 @@ func TestPostAllowance(t *testing.T) {
func TestPostAllowanceInvalidUserId(t *testing.T) {
e := startServer(t)
e.POST("/user/999/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().
e.POST("/user/999/history").WithJSON(PostHistory{Allowance: 100}).Expect().
Status(404)
}
func TestGetHistory(t *testing.T) {
e := startServer(t)
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200)
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200)
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: -10}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 100}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: 20}).Expect().Status(200)
e.POST("/user/1/history").WithJSON(PostHistory{Allowance: -10}).Expect().Status(200)
response := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
response.Length().IsEqual(3)