File size: 3,753 Bytes
9268438
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# API Testing Instructions

## Testing the Backend API Endpoints

This document provides instructions for testing the Todo Application API endpoints.

## Prerequisites

1. Ensure you have Python 3.13+ installed
2. Install the project dependencies:
   ```bash
   cd backend
   pip install -e .
   # Or if using uv:
   uv pip install -e .
   ```

## Starting the Development Server

1. Navigate to the backend directory:
   ```bash
   cd backend
   ```

2. Create a `.env` file with the required configuration:
   ```bash
   # Create the environment file
   cp .env.example .env

   # Edit .env to add your actual values:
   # - Generate a strong secret for BETTER_AUTH_SECRET
   # - Add your actual DATABASE_URL
   # - Set FRONTEND_URL to your frontend URL
   ```

3. Start the server:
   ```bash
   uvicorn main:app --reload
   ```

## API Endpoint Testing

### Authentication Testing
All endpoints require a valid JWT token in the Authorization header:
```
Authorization: Bearer <valid_token>
```

### Available Endpoints

1. **GET** `/api/{user_id}/tasks`
   - Get all tasks for a user
   - Query parameter: `status` (optional: "pending", "completed")
   - Requires: Valid token with matching user_id

2. **POST** `/api/{user_id}/tasks`
   - Create a new task
   - Request body: `{"title": "...", "description": "..."}`
   - Requires: Valid token with matching user_id

3. **GET** `/api/{user_id}/tasks/{task_id}`
   - Get a specific task
   - Requires: Valid token with matching user_id

4. **PUT** `/api/{user_id}/tasks/{task_id}`
   - Update a task
   - Request body: `{"title": "...", "description": "...", "completed": true/false}`
   - Requires: Valid token with matching user_id

5. **DELETE** `/api/{user_id}/tasks/{task_id}`
   - Delete a task
   - Requires: Valid token with matching user_id

6. **PATCH** `/api/{user_id}/tasks/{task_id}/complete`
   - Toggle task completion status
   - Request body: `{"completed": true/false}`
   - Requires: Valid token with matching user_id

### Manual Testing with curl

Example curl commands (replace `<token>` with a valid JWT token and `<user_id>` with actual user ID):

```bash
# Get user's tasks
curl -H "Authorization: Bearer <token>" http://localhost:8000/api/<user_id>/tasks

# Create a task
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <token>" \
  -d '{"title":"Test task","description":"This is a test task"}' \
  http://localhost:8000/api/<user_id>/tasks

# Update a task
curl -X PUT \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <token>" \
  -d '{"title":"Updated task","completed":true}' \
  http://localhost:8000/api/<user_id>/tasks/1

# Delete a task
curl -X DELETE \
  -H "Authorization: Bearer <token>" \
  http://localhost:8000/api/<user_id>/tasks/1

# Toggle task completion
curl -X PATCH \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <token>" \
  -d '{"completed":true}' \
  http://localhost:8000/api/<user_id>/tasks/1
```

### Automated Testing

Run the automated test script:
```bash
python test_api.py
```

## Security Testing

The API includes the following security measures:
- JWT token verification on all endpoints
- User ID validation (path parameter must match token's user ID)
- Data isolation (users can only access their own tasks)
- Proper error responses for unauthorized access

## Health Check

The API includes a health check endpoint:
```
GET /
GET /health
```

## Expected Responses

All endpoints return appropriate HTTP status codes:
- 200: Success
- 201: Created (for POST requests)
- 401: Unauthorized (invalid/missing token)
- 403: Forbidden (attempting to access another user's data)
- 404: Not Found (resource doesn't exist)
- 500: Server Error (internal server error)