75198ed00f
- Added section 8 'Тестирование' with run instructions, structure, features, and diagnostics - Also includes test file route path adjustments and import reordering
158 lines
4.1 KiB
Go
158 lines
4.1 KiB
Go
package integration
|
|
|
|
import (
|
|
"testing"
|
|
"api_yal/tests/testutils"
|
|
)
|
|
|
|
func TestAccountEndpoints(t *testing.T) {
|
|
config := testutils.NewTestConfig()
|
|
|
|
t.Run("GetProfile", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
resp, err := config.Request("GET", "/profile", nil, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get profile: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
var profile map[string]interface{}
|
|
if err := config.ParseResponse(resp, &profile); err != nil {
|
|
t.Fatalf("Failed to parse response: %v", err)
|
|
}
|
|
|
|
expectedFields := []string{"id", "email", "full_name", "first_name", "last_name", "role", "stats"}
|
|
for _, field := range expectedFields {
|
|
if _, ok := profile[field]; !ok {
|
|
t.Errorf("Expected field %s not found", field)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("GetOwnAccount", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
resp, err := config.Request("GET", "/me", nil, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get account: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
})
|
|
|
|
t.Run("UpdateAccount", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
updateData := map[string]interface{}{
|
|
"phone": "+79998887766",
|
|
"city": "Moscow",
|
|
}
|
|
|
|
resp, err := config.Request("PUT", "/me", updateData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to update account: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
var updatedAccount map[string]interface{}
|
|
if err := config.ParseResponse(resp, &updatedAccount); err != nil {
|
|
t.Fatalf("Failed to parse response: %v", err)
|
|
}
|
|
|
|
if phone, ok := updatedAccount["phone"].(string); !ok || phone != "+79998887766" {
|
|
t.Error("Phone number not updated correctly")
|
|
}
|
|
})
|
|
|
|
t.Run("ChangePassword", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
newPassword := "newpass123"
|
|
resp, err := config.Request("POST", "/change-password", map[string]interface{}{
|
|
"current_password": user.Password,
|
|
"new_password": newPassword,
|
|
}, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to change password: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
newToken, err := config.GetAuthToken(user.Email, newPassword)
|
|
if err != nil {
|
|
t.Errorf("Failed to login with new password: %v", err)
|
|
}
|
|
if newToken == "" {
|
|
t.Error("Failed to get token with new password")
|
|
}
|
|
})
|
|
|
|
t.Run("ChangePasswordWrongCurrent", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
resp, err := config.Request("POST", "/change-password", map[string]interface{}{
|
|
"current_password": "wrongpassword",
|
|
"new_password": "newpass123",
|
|
}, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to change password: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 400 && resp.StatusCode != 401 {
|
|
t.Errorf("Expected status 400 or 401, got %d", resp.StatusCode)
|
|
}
|
|
})
|
|
|
|
t.Run("DeleteAccount", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
|
|
resp, err := config.Request("DELETE", "/me", nil, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to delete account: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
_, err = config.GetAuthToken(user.Email, user.Password)
|
|
if err == nil {
|
|
t.Error("Should not be able to login after account deletion")
|
|
}
|
|
})
|
|
|
|
t.Run("UnauthorizedAccess", func(t *testing.T) {
|
|
resp, err := config.Request("GET", "/profile", nil, "")
|
|
if err != nil {
|
|
t.Fatalf("Failed to make request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 401 {
|
|
t.Errorf("Expected status 401, got %d", resp.StatusCode)
|
|
}
|
|
})
|
|
}
|