2025-05-14 17:14:58 +02:00

429 lines
10 KiB
Go

package main
import (
"fmt"
"github.com/gavv/httpexpect/v2"
"strconv"
"testing"
"time"
)
const (
TestAllowanceName = "Test History"
)
func startServer(t *testing.T) *httpexpect.Expect {
config := ServerConfig{
Datasource: ":memory:",
Addr: ":0",
Started: make(chan bool),
}
go start(t.Context(), &config)
<-config.Started
return httpexpect.Default(t, fmt.Sprintf("http://localhost:%d/api", config.Port))
}
func TestGetUsers(t *testing.T) {
e := startServer(t)
result := e.GET("/users").Expect().Status(200).JSON()
result.Array().Length().IsEqual(2)
result.Path("$[0].name").InList("Seeseemelk", "Huffle")
result.Path("$[1].name").InList("Seeseemelk", "Huffle")
}
func TestGetUser(t *testing.T) {
e := startServer(t)
result := e.GET("/user/1").Expect().Status(200).JSON().Object()
result.Value("name").IsEqual("Seeseemelk")
result.Value("id").IsEqual(1)
result.Value("allowance").IsEqual(0)
}
func TestGetUserUnknown(t *testing.T) {
e := startServer(t)
e.GET("/user/999").Expect().Status(404)
}
func TestGetUserBadId(t *testing.T) {
e := startServer(t)
e.GET("/user/bad-id").Expect().Status(400)
}
func TestGetUserAllowanceWhenNoAllowancePresent(t *testing.T) {
e := startServer(t)
result := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
result.Length().IsEqual(0)
}
func TestGetUserAllowance(t *testing.T) {
e := startServer(t)
// Create a new allowance
requestBody := map[string]interface{}{
"name": TestAllowanceName,
"target": 5000,
"weight": 10,
}
e.POST("/user/1/allowance").WithJSON(requestBody).Expect().Status(201)
// 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(TestAllowanceName)
item.Value("target").IsEqual(5000)
item.Value("weight").IsEqual(10)
item.Value("progress").IsEqual(0)
item.NotContainsKey("user_id")
}
func TestGetUserAllowanceNoUser(t *testing.T) {
e := startServer(t)
e.GET("/user/999/allowance").Expect().Status(404)
}
func TestGetUserAllowanceBadId(t *testing.T) {
e := startServer(t)
e.GET("/user/bad-id/allowance").Expect().Status(400)
}
func TestCreateUserAllowance(t *testing.T) {
e := startServer(t)
// Create a new allowance
requestBody := map[string]interface{}{
"name": TestAllowanceName,
"target": 5000,
"weight": 10,
}
response := e.POST("/user/1/allowance").
WithJSON(requestBody).
Expect().
Status(201).
JSON().Object()
// Verify the response has an ID
response.ContainsKey("id")
allowanceId := response.Value("id").Number().Raw()
// Verify the allowance exists in the list of allowances
allowances := e.GET("/user/1/allowance").
Expect().
Status(200).
JSON().Array()
allowances.Length().IsEqual(1)
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 TestCreateUserAllowanceNoUser(t *testing.T) {
e := startServer(t)
requestBody := map[string]interface{}{
"name": TestAllowanceName,
"target": 5000,
"weight": 10,
}
e.POST("/user/999/allowance").
WithJSON(requestBody).
Expect().
Status(404)
}
func TestCreateUserAllowanceInvalidInput(t *testing.T) {
e := startServer(t)
// Test with empty name
requestBody := map[string]interface{}{
"name": "",
"target": 5000,
"weight": 10,
}
e.POST("/user/1/allowance").
WithJSON(requestBody).
Expect().
Status(400)
// Test with missing fields
invalidRequest := map[string]interface{}{
"target": 5000,
}
e.POST("/user/1/allowance").
WithJSON(invalidRequest).
Expect().
Status(400)
}
func TestCreateUserAllowanceBadId(t *testing.T) {
e := startServer(t)
requestBody := map[string]interface{}{
"name": TestAllowanceName,
"target": 5000,
"weight": 10,
}
e.POST("/user/bad-id/allowance").
WithJSON(requestBody).
Expect().
Status(400)
}
func TestDeleteUserAllowance(t *testing.T) {
e := startServer(t)
// Create a new allowance to delete
createRequest := map[string]interface{}{
"name": TestAllowanceName,
"target": 1000,
"weight": 5,
}
response := e.POST("/user/1/allowance").
WithJSON(createRequest).
Expect().
Status(201).
JSON().Object()
allowanceId := response.Value("id").Number().Raw()
// Delete the allowance
e.DELETE("/user/1/allowance/" + strconv.Itoa(int(allowanceId))).
Expect().
Status(200).
JSON().Object().Value("message").IsEqual("History deleted successfully")
// Verify the allowance no longer exists
allowances := e.GET("/user/1/allowance").
Expect().
Status(200).
JSON().Array()
allowances.Length().IsEqual(0)
}
func TestDeleteUserAllowanceNotFound(t *testing.T) {
e := startServer(t)
// Attempt to delete a non-existent allowance
e.DELETE("/user/1/allowance/999").
Expect().
Status(404).
JSON().Object().Value("error").IsEqual("History not found")
}
func TestDeleteUserAllowanceInvalidId(t *testing.T) {
e := startServer(t)
// 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 allowance 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)
}
func TestGetTaskWhenNoTasks(t *testing.T) {
e := startServer(t)
result := e.GET("/tasks").Expect().Status(200).JSON().Array()
result.Length().IsEqual(0)
}
func createTestTask(e *httpexpect.Expect) {
requestBody := map[string]interface{}{
"name": "Test Task",
"reward": 100,
}
e.POST("/tasks").WithJSON(requestBody).Expect().Status(201)
}
func TestGetTasksWhenTasks(t *testing.T) {
e := startServer(t)
createTestTask(e)
// Get the task
result := e.GET("/tasks").Expect().Status(200).JSON().Array()
result.Length().IsEqual(1)
item := result.Value(0).Object()
item.Value("id").IsEqual(1)
item.Value("name").IsEqual("Test Task")
item.Value("reward").IsEqual(100)
item.Value("assigned").IsNull()
}
func TestGetTask(t *testing.T) {
e := startServer(t)
createTestTask(e)
result := e.GET("/task/1").Expect().Status(200).JSON().Object()
result.Value("id").IsEqual(1)
result.Value("name").IsEqual("Test Task")
result.Value("reward").IsEqual(100)
result.Value("assigned").IsNull()
}
func TestGetTaskInvalidId(t *testing.T) {
e := startServer(t)
createTestTask(e)
e.GET("/task/2").Expect().Status(404)
}
func TestGetTaskBadId(t *testing.T) {
e := startServer(t)
createTestTask(e)
e.GET("/task/invalid").Expect().Status(400)
}
func TestPutTaskModifiesTask(t *testing.T) {
e := startServer(t)
createTestTask(e)
requestBody := map[string]interface{}{
"name": "Updated Task",
"reward": 100,
}
e.PUT("/task/1").WithJSON(requestBody).Expect().
Status(200).
JSON().Object()
// Verify the task is updated
result := e.GET("/task/1").Expect().Status(200).JSON().Object()
result.Value("id").IsEqual(1)
result.Value("name").IsEqual("Updated Task")
result.Value("reward").IsEqual(100)
}
func TestPutTaskInvalidTaskId(t *testing.T) {
e := startServer(t)
createTestTask(e)
requestBody := map[string]interface{}{
"name": "Updated Task",
}
e.PUT("/task/999").WithJSON(requestBody).Expect().Status(404)
}
func TestPostAllowance(t *testing.T) {
e := startServer(t)
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)
}
func TestPostAllowanceInvalidUserId(t *testing.T) {
e := startServer(t)
e.POST("/user/999/history").WithJSON(PostHistory{Allowance: 100}).Expect().
Status(404)
}
func TestGetHistory(t *testing.T) {
e := startServer(t)
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)
response.Value(0).Object().Value("allowance").Number().IsEqual(100)
response.Value(0).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
response.Value(1).Object().Value("allowance").Number().IsEqual(20)
response.Value(2).Object().Value("allowance").Number().IsEqual(-10)
}
func getDelta(base time.Time, delta float64) (time.Time, time.Time) {
start := base.Add(-time.Duration(delta) * time.Second)
end := base.Add(time.Duration(delta) * time.Second)
return start, end
}