8dfe7e8b4a
new file: main_dc/yalarba/api_yal/cmd/testrunner/README.md new file: main_dc/yalarba/api_yal/cmd/testrunner/main.go new file: main_dc/yalarba/api_yal/cmd/testrunner/runner.go deleted: main_dc/yalarba/api_yal/test/intergration/auth_integration_test.go deleted: main_dc/yalarba/api_yal/test/intergration/objects_integration_test.go deleted: main_dc/yalarba/api_yal/test/intergration/setup_test.go deleted: main_dc/yalarba/api_yal/test/setup_test.go new file: main_dc/yalarba/api_yal/tests/integration/account_test.go new file: main_dc/yalarba/api_yal/tests/integration/appeal_test.go new file: main_dc/yalarba/api_yal/tests/integration/auth_test.go new file: main_dc/yalarba/api_yal/tests/integration/comment_test.go new file: main_dc/yalarba/api_yal/tests/integration/feedback_test.go new file: main_dc/yalarba/api_yal/tests/integration/object_test.go new file: main_dc/yalarba/api_yal/tests/integration/rating_test.go new file: main_dc/yalarba/api_yal/tests/testutils/client.go new file: main_dc/yalarba/api_yal/tests/testutils/fixtures.go new file: main_dc/yalarba/api_yal/tests/testutils/setup.go write tests
240 lines
7.7 KiB
Go
240 lines
7.7 KiB
Go
package integration
|
|
|
|
import (
|
|
"testing"
|
|
"api_yal/tests/testutils"
|
|
)
|
|
|
|
func TestRatingEndpoints(t *testing.T) {
|
|
config := testutils.NewTestConfig()
|
|
|
|
t.Run("CreateRating", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
objectID := config.CreateTestObject(t, user.Token)
|
|
|
|
ratingData := map[string]interface{}{
|
|
"object_id": objectID,
|
|
"platform": "tourist",
|
|
}
|
|
|
|
resp, err := config.Request("POST", "/ratings", ratingData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create rating: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 201 {
|
|
t.Errorf("Expected status 201, got %d", resp.StatusCode)
|
|
}
|
|
})
|
|
|
|
t.Run("CastVote", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
objectID := config.CreateTestObject(t, user.Token)
|
|
|
|
// Сначала создаем рейтинг
|
|
ratingData := map[string]interface{}{
|
|
"object_id": objectID,
|
|
"platform": "tourist",
|
|
}
|
|
resp, err := config.Request("POST", "/ratings", ratingData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create rating: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var createdRating map[string]interface{}
|
|
if err := config.ParseResponse(resp, &createdRating); err != nil {
|
|
t.Fatalf("Failed to parse response: %v", err)
|
|
}
|
|
|
|
ratingID := createdRating["id"].(float64)
|
|
|
|
// Голосуем
|
|
voteData := map[string]interface{}{
|
|
"score": 5,
|
|
}
|
|
|
|
voteResp, err := config.Request("POST", "/ratings/"+string(rune(ratingID))+"/vote/tourist", voteData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to cast vote: %v", err)
|
|
}
|
|
defer voteResp.Body.Close()
|
|
|
|
if voteResp.StatusCode != 200 {
|
|
t.Errorf("Expected status 200, got %d", voteResp.StatusCode)
|
|
}
|
|
})
|
|
|
|
t.Run("GetMyVote", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
objectID := config.CreateTestObject(t, user.Token)
|
|
|
|
// Создаем рейтинг и голосуем
|
|
ratingData := map[string]interface{}{
|
|
"object_id": objectID,
|
|
"platform": "tourist",
|
|
}
|
|
resp, err := config.Request("POST", "/ratings", ratingData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create rating: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var createdRating map[string]interface{}
|
|
if err := config.ParseResponse(resp, &createdRating); err != nil {
|
|
t.Fatalf("Failed to parse response: %v", err)
|
|
}
|
|
|
|
ratingID := createdRating["id"].(float64)
|
|
|
|
voteData := map[string]interface{}{
|
|
"score": 5,
|
|
}
|
|
_, err = config.Request("POST", "/ratings/"+string(rune(ratingID))+"/vote/tourist", voteData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to cast vote: %v", err)
|
|
}
|
|
|
|
// Получаем мой голос
|
|
myVoteResp, err := config.Request("GET", "/ratings/"+string(rune(ratingID))+"/my-vote/tourist", nil, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get my vote: %v", err)
|
|
}
|
|
defer myVoteResp.Body.Close()
|
|
|
|
if myVoteResp.StatusCode != 200 {
|
|
t.Errorf("Expected status 200, got %d", myVoteResp.StatusCode)
|
|
}
|
|
|
|
var voteInfo map[string]interface{}
|
|
if err := config.ParseResponse(myVoteResp, &voteInfo); err != nil {
|
|
t.Fatalf("Failed to parse response: %v", err)
|
|
}
|
|
|
|
if hasVoted, ok := voteInfo["has_voted"].(bool); !ok || !hasVoted {
|
|
t.Error("has_voted should be true")
|
|
}
|
|
})
|
|
|
|
t.Run("UpdateMyVote", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
objectID := config.CreateTestObject(t, user.Token)
|
|
|
|
ratingData := map[string]interface{}{
|
|
"object_id": objectID,
|
|
"platform": "tourist",
|
|
}
|
|
resp, err := config.Request("POST", "/ratings", ratingData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create rating: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var createdRating map[string]interface{}
|
|
if err := config.ParseResponse(resp, &createdRating); err != nil {
|
|
t.Fatalf("Failed to parse response: %v", err)
|
|
}
|
|
|
|
ratingID := createdRating["id"].(float64)
|
|
|
|
// Голосуем
|
|
voteData := map[string]interface{}{
|
|
"score": 5,
|
|
}
|
|
_, err = config.Request("POST", "/ratings/"+string(rune(ratingID))+"/vote/tourist", voteData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to cast vote: %v", err)
|
|
}
|
|
|
|
// Обновляем голос
|
|
updateData := map[string]interface{}{
|
|
"score": 4,
|
|
}
|
|
updateResp, err := config.Request("PUT", "/ratings/"+string(rune(ratingID))+"/my-vote/tourist", updateData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to update vote: %v", err)
|
|
}
|
|
defer updateResp.Body.Close()
|
|
|
|
if updateResp.StatusCode != 200 {
|
|
t.Errorf("Expected status 200, got %d", updateResp.StatusCode)
|
|
}
|
|
})
|
|
|
|
t.Run("GetRatingStats", func(t *testing.T) {
|
|
resp, err := config.Request("GET", "/ratings/stats", nil, "")
|
|
if err != nil {
|
|
t.Fatalf("Failed to get rating stats: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != 200 {
|
|
t.Errorf("Expected status 200, got %d", resp.StatusCode)
|
|
}
|
|
|
|
var stats map[string]interface{}
|
|
if err := config.ParseResponse(resp, &stats); err != nil {
|
|
t.Fatalf("Failed to parse response: %v", err)
|
|
}
|
|
|
|
expectedFields := []string{"total_ratings", "total_votes", "platform_distribution"}
|
|
for _, field := range expectedFields {
|
|
if _, ok := stats[field]; !ok {
|
|
t.Errorf("Expected field %s not found", field)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("DeleteMyVote", func(t *testing.T) {
|
|
user := config.CreateTestUser(t)
|
|
defer config.CleanupTestUser(t, user)
|
|
|
|
objectID := config.CreateTestObject(t, user.Token)
|
|
|
|
ratingData := map[string]interface{}{
|
|
"object_id": objectID,
|
|
"platform": "tourist",
|
|
}
|
|
resp, err := config.Request("POST", "/ratings", ratingData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create rating: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var createdRating map[string]interface{}
|
|
if err := config.ParseResponse(resp, &createdRating); err != nil {
|
|
t.Fatalf("Failed to parse response: %v", err)
|
|
}
|
|
|
|
ratingID := createdRating["id"].(float64)
|
|
|
|
// Голосуем
|
|
voteData := map[string]interface{}{
|
|
"score": 5,
|
|
}
|
|
_, err = config.Request("POST", "/ratings/"+string(rune(ratingID))+"/vote/tourist", voteData, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to cast vote: %v", err)
|
|
}
|
|
|
|
// Удаляем голос
|
|
deleteResp, err := config.Request("DELETE", "/ratings/"+string(rune(ratingID))+"/my-vote/tourist", nil, user.Token)
|
|
if err != nil {
|
|
t.Fatalf("Failed to delete vote: %v", err)
|
|
}
|
|
defer deleteResp.Body.Close()
|
|
|
|
if deleteResp.StatusCode != 204 {
|
|
t.Errorf("Expected status 204, got %d", deleteResp.StatusCode)
|
|
}
|
|
})
|
|
} |