Files
schedtxt_api/internal/handler/helper_test.go
T
2026-01-01 13:56:51 -05:00

112 lines
3.4 KiB
Go

package handler
import (
"context"
"encoding/json"
"fmt"
"os"
"path"
"testing"
"time"
"git.kundeng.us/phoenix/textsender-models/tx0/contact"
"git.kundeng.us/phoenix/textsender-models/tx0/message"
"git.kundeng.us/phoenix/textsender-models/tx0/message/scheduling"
"github.com/google/uuid"
"github.com/joho/godotenv"
"git.kundeng.us/phoenix/textsender-api/internal/app"
"git.kundeng.us/phoenix/textsender-api/internal/config"
"git.kundeng.us/phoenix/textsender-api/internal/db"
)
func GetApp() (*app.App, error) {
cwd, _ := os.Getwd()
envPath := path.Join(cwd, ".env")
err := godotenv.Load(envPath)
if err != nil {
envPath = path.Join(cwd, "../..", ".env")
if err := godotenv.Load(envPath); err != nil {
panic("Error loading .env file: " + err.Error())
}
}
if tCfg, err := config.TwilioConfig(); err != nil {
return nil, fmt.Errorf("Error: %v", err)
} else {
return &app.App{TwilioConfig: tCfg}, nil
}
}
func ResetTestDB(t *testing.T, tableName string) {
t.Helper()
_, err := db.Pool.Exec(context.Background(), fmt.Sprintf("DELETE FROM ", tableName))
if err != nil {
t.Fatalf("Failed to reset test database: %v", err)
}
}
func ContactTest(id uuid.UUID, userId uuid.UUID) contact.Contact {
if id == uuid.Nil {
id = uuid.New()
return contact.Contact{Id: &id, PhoneNumber: "+10123456789", UserId: &userId}
} else {
return contact.Contact{Id: &id, PhoneNumber: "+10123456789", UserId: &userId}
}
}
func MessageTest(id uuid.UUID, userId uuid.UUID) message.Message {
if id == uuid.Nil {
id = uuid.New()
return message.Message{Id: &id, Content: "Oh how the mighty have fallen", UserId: &userId}
} else {
return message.Message{Id: &id, Content: "Oh how the mighty have fallen", UserId: &userId}
}
}
func ScheduledMessageTest(id uuid.UUID, userId uuid.UUID, now time.Time) scheduling.ScheduledMessage {
if id == uuid.Nil {
return scheduling.ScheduledMessage{Id: uuid.New(), UserId: userId, Scheduled: now.Add(20 * time.Minute), Status: scheduling.Pending}
} else {
return scheduling.ScheduledMessage{Id: id, UserId: userId, Scheduled: now.Add(20 * time.Minute), Status: scheduling.Pending}
}
}
func ScheduledMessageEventTest(messageId, contactId, scheduledMessageId uuid.UUID) scheduling.ScheduledMessageEvent {
return scheduling.ScheduledMessageEvent{MessageId: messageId, ContactId: contactId, ScheduledMessageId: scheduledMessageId}
}
func MERResponseInString() string {
return `{
"body": "Whoknows?",
"num_segments": "0",
"direction": "outbound-api",
"from": "+12243026041",
"to": "+16303831708",
"date_updated": "Sat,29Nov202519:06:59+0000",
"uri": "/2010-04-01/Accounts/ACefa1ef516314c9d1a68cbd657de49277/Messages/SM1193a529e7f7a840667cd1e0f13ea95a.json",
"account_sid": "ACefa1ef516314c9d1a68cbd657de49277",
"num_media": "0",
"status": "scheduled",
"messaging_service_sid": "MG803f3676706b92eb02e18dd820c447f2",
"sid": "SM1193a529e7f7a840667cd1e0f13ea95a",
"date_created": "Sat,29Nov202519:06:59+0000",
"api_version": "2010-04-01",
"subresource_uris": {
"media": "/2010-04-01/Accounts/ACefa1ef516314c9d1a68cbd657de49277/Messages/SM1193a529e7f7a840667cd1e0f13ea95a/Media.json"
}
}`
}
func ConvertStringToMapOfAny(response string) (map[string]any, error) {
bytes := []byte(response)
var merResponse map[string]any
err := json.Unmarshal(bytes, &merResponse)
if err != nil {
return nil, err
} else {
return merResponse, nil
}
}