feat: 实现用户头像上传功能
- 创建头像上传API接口 (POST /api/v1/users/:id/avatar) - 实现完整的头像上传逻辑,包含权限验证和文件处理 - 添加头像图片处理功能,支持自动压缩和居中裁剪 - 完善静态文件服务,支持头像访问 - 创建完整的API测试用例 - 更新任务进度文档 任务11已完成,项目完成率提升至37.5%
This commit is contained in:
98
backend/test_avatar_upload.http
Normal file
98
backend/test_avatar_upload.http
Normal file
@ -0,0 +1,98 @@
|
||||
### 用户头像上传测试用例
|
||||
|
||||
### 1. 用户登录获取Token
|
||||
POST {{host}}/api/v1/auth/login
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"username": "admin",
|
||||
"password": "admin123"
|
||||
}
|
||||
|
||||
### 2. 上传用户头像 (正常场景)
|
||||
POST {{host}}/api/v1/users/1/avatar
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="avatar.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< ./test_images/avatar.jpg
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 3. 上传头像 - 文件过大 (错误场景)
|
||||
POST {{host}}/api/v1/users/1/avatar
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="large_image.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< ./test_images/large_image.jpg
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 4. 上传头像 - 非图片文件 (错误场景)
|
||||
POST {{host}}/api/v1/users/1/avatar
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="document.txt"
|
||||
Content-Type: text/plain
|
||||
|
||||
This is not an image file
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 5. 上传头像 - 未认证用户 (错误场景)
|
||||
POST {{host}}/api/v1/users/1/avatar
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="avatar.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< ./test_images/avatar.jpg
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 6. 上传其他用户头像 - 权限不足 (错误场景)
|
||||
POST {{host}}/api/v1/users/999/avatar
|
||||
Authorization: Bearer {{token}}
|
||||
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW
|
||||
Content-Disposition: form-data; name="avatar"; filename="avatar.jpg"
|
||||
Content-Type: image/jpeg
|
||||
|
||||
< ./test_images/avatar.jpg
|
||||
------WebKitFormBoundary7MA4YWxkTrZu0gW--
|
||||
|
||||
### 7. 获取用户信息查看头像URL
|
||||
GET {{host}}/api/v1/users/1
|
||||
Authorization: Bearer {{token}}
|
||||
|
||||
### 8. 访问头像静态文件
|
||||
GET {{host}}/uploads/avatars/avatar_1_1641234567_150x150.jpg
|
||||
|
||||
### 测试环境变量
|
||||
@host = http://localhost:8080
|
||||
@token = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
|
||||
|
||||
### 测试说明
|
||||
# 1. 确保在项目根目录下有 test_images 文件夹
|
||||
# 2. 准备以下测试文件:
|
||||
# - test_images/avatar.jpg (正常大小的头像图片, < 5MB)
|
||||
# - test_images/large_image.jpg (超过5MB的图片)
|
||||
# 3. 替换 {{token}} 为实际的JWT令牌
|
||||
# 4. 替换用户ID为实际存在的用户ID
|
||||
|
||||
### 预期结果
|
||||
# 1. 正常上传应返回 200 状态码和头像URL
|
||||
# 2. 文件过大应返回 400 错误
|
||||
# 3. 非图片文件应返回 400 错误
|
||||
# 4. 未认证用户应返回 401 错误
|
||||
# 5. 权限不足应返回 403 错误
|
||||
# 6. 头像文件应保存到 uploads/avatars/ 目录
|
||||
# 7. 应生成压缩版本 (150x150像素)
|
||||
# 8. 用户信息中的 avatar 字段应更新为新的头像URL
|
||||
Reference in New Issue
Block a user