On branch main
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
This commit is contained in:
@@ -0,0 +1 @@
|
||||
package testutils
|
||||
@@ -0,0 +1,94 @@
|
||||
package testutils
|
||||
|
||||
import "testing"
|
||||
|
||||
type CreateObjectRequest struct {
|
||||
ShortName string `json:"short_name"`
|
||||
LongName string `json:"long_name"`
|
||||
Type string `json:"type"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
Site string `json:"site"`
|
||||
ShortDescription string `json:"short_description"`
|
||||
Description string `json:"description"`
|
||||
Address string `json:"address"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
IsActive bool `json:"is_active"`
|
||||
IsVerified bool `json:"is_verified"`
|
||||
}
|
||||
|
||||
type CreateFeedbackRequest struct {
|
||||
ObjectID uint `json:"object_id"`
|
||||
Rating int `json:"rating"`
|
||||
Text string `json:"text"`
|
||||
Platform string `json:"platform"`
|
||||
MediaURLs []string `json:"media_urls"`
|
||||
}
|
||||
|
||||
type CreateCommentRequest struct {
|
||||
FeedbackID uint `json:"feedback_id"`
|
||||
Text string `json:"text"`
|
||||
ParentID *uint `json:"parent_id"`
|
||||
}
|
||||
|
||||
func (c *TestConfig) CreateTestObject(t *testing.T, token string) uint {
|
||||
req := CreateObjectRequest{
|
||||
ShortName: "Test Object",
|
||||
LongName: "Test Object for Testing",
|
||||
Type: "cafe",
|
||||
Phone: "+71234567890",
|
||||
Email: "test@object.com",
|
||||
Site: "https://test.com",
|
||||
ShortDescription: "Test description",
|
||||
Description: "Full test description",
|
||||
Address: "Test Address 123",
|
||||
Latitude: 55.751244,
|
||||
Longitude: 37.618423,
|
||||
IsActive: true,
|
||||
IsVerified: false,
|
||||
}
|
||||
|
||||
resp, err := c.Request("POST", "/objects", req, token)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test object: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := c.ParseResponse(resp, &result); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if id, ok := result["id"].(float64); ok {
|
||||
return uint(id)
|
||||
}
|
||||
t.Fatal("Failed to get object ID")
|
||||
return 0
|
||||
}
|
||||
|
||||
func (c *TestConfig) CreateTestFeedback(t *testing.T, token string, objectID uint) uint {
|
||||
req := CreateFeedbackRequest{
|
||||
ObjectID: objectID,
|
||||
Rating: 5,
|
||||
Text: "Test feedback content",
|
||||
Platform: "tourist",
|
||||
}
|
||||
|
||||
resp, err := c.Request("POST", "/feedbacks", req, token)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test feedback: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := c.ParseResponse(resp, &result); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if id, ok := result["id"].(float64); ok {
|
||||
return uint(id)
|
||||
}
|
||||
t.Fatal("Failed to get feedback ID")
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package testutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TestConfig struct {
|
||||
BaseURL string
|
||||
Client *http.Client
|
||||
}
|
||||
|
||||
type TestUser struct {
|
||||
Email string
|
||||
Password string
|
||||
FirstName string
|
||||
LastName string
|
||||
Token string
|
||||
UserID uint
|
||||
}
|
||||
|
||||
func NewTestConfig() *TestConfig {
|
||||
jar, _ := cookiejar.New(nil)
|
||||
return &TestConfig{
|
||||
BaseURL: "http://localhost:8088/api/v1",
|
||||
Client: &http.Client{
|
||||
Jar: jar,
|
||||
Timeout: 30 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TestConfig) Request(method, path string, body interface{}, token string) (*http.Response, error) {
|
||||
var reqBody io.Reader
|
||||
if body != nil {
|
||||
jsonBody, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reqBody = bytes.NewBuffer(jsonBody)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, c.BaseURL+path, reqBody)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
|
||||
return c.Client.Do(req)
|
||||
}
|
||||
|
||||
func (c *TestConfig) ParseResponse(resp *http.Response, target interface{}) error {
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(body, target)
|
||||
}
|
||||
|
||||
func (c *TestConfig) CreateTestUser(t *testing.T) *TestUser {
|
||||
user := &TestUser{
|
||||
Email: fmt.Sprintf("test_%d@example.com", time.Now().UnixNano()),
|
||||
Password: "test123456",
|
||||
FirstName: "Test",
|
||||
LastName: "User",
|
||||
}
|
||||
|
||||
resp, err := c.Request("POST", "/auth/register", map[string]interface{}{
|
||||
"email": user.Email,
|
||||
"password": user.Password,
|
||||
"first_name": user.FirstName,
|
||||
"last_name": user.LastName,
|
||||
}, "")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create test user: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := c.ParseResponse(resp, &result); err != nil {
|
||||
t.Fatalf("Failed to parse response: %v", err)
|
||||
}
|
||||
|
||||
if token, ok := result["token"].(string); ok {
|
||||
user.Token = token
|
||||
}
|
||||
if userData, ok := result["user"].(map[string]interface{}); ok {
|
||||
if id, ok := userData["id"].(float64); ok {
|
||||
user.UserID = uint(id)
|
||||
}
|
||||
}
|
||||
|
||||
return user
|
||||
}
|
||||
|
||||
func (c *TestConfig) CleanupTestUser(t *testing.T, user *TestUser) {
|
||||
if user.Token != "" {
|
||||
_, err := c.Request("DELETE", "/account", nil, user.Token)
|
||||
if err != nil {
|
||||
t.Logf("Warning: Failed to cleanup test user: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *TestConfig) GetAuthToken(email, password string) (string, error) {
|
||||
resp, err := c.Request("POST", "/auth/login", map[string]interface{}{
|
||||
"email": email,
|
||||
"password": password,
|
||||
}, "")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := c.ParseResponse(resp, &result); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if token, ok := result["token"].(string); ok {
|
||||
return token, nil
|
||||
}
|
||||
return "", fmt.Errorf("token not found in response")
|
||||
}
|
||||
Reference in New Issue
Block a user