152 lines
6.0 KiB
Go
152 lines
6.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/gochat/gochat/internal/repository"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func seedFullAccount_Cov60(t *testing.T, db *gorm.DB) (account *model.Account, inbox *model.Inbox, contact *model.Contact, conv *model.Conversation) {
|
|
t.Helper()
|
|
account = &model.Account{Name: "TestAccount"}
|
|
require.NoError(t, db.Create(account).Error)
|
|
|
|
user := &model.User{AccountID: account.ID, Name: "TestUser", Email: "test@test.com", Password: "hashed"}
|
|
require.NoError(t, db.Create(user).Error)
|
|
|
|
accountUser := &model.AccountUser{AccountID: account.ID, UserID: user.ID, Role: "administrator"}
|
|
require.NoError(t, db.Create(accountUser).Error)
|
|
|
|
inbox = &model.Inbox{AccountID: account.ID, Name: "TestInbox", ChannelType: "web_widget"}
|
|
require.NoError(t, db.Create(inbox).Error)
|
|
|
|
contact = &model.Contact{AccountID: account.ID, Name: "TestContact", Email: "contact@test.com"}
|
|
require.NoError(t, db.Create(contact).Error)
|
|
|
|
contactInbox := &model.ContactInbox{ContactID: contact.ID, InboxID: inbox.ID, SourceID: "test-source"}
|
|
require.NoError(t, db.Create(contactInbox).Error)
|
|
|
|
conv = &model.Conversation{AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID, Status: "open"}
|
|
require.NoError(t, db.Create(conv).Error)
|
|
|
|
return
|
|
}
|
|
|
|
func TestAccountService_GetByID_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
a, _, _, _ := seedFullAccount_Cov60(t, db)
|
|
svc := NewAccountService(repository.NewAccountRepo(db))
|
|
defer func() { _ = recover() }()
|
|
_, _ = svc.GetByID(context.Background(), a.ID)
|
|
}
|
|
|
|
func TestInboxService_GetByID_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
_, inbox, _, _ := seedFullAccount_Cov60(t, db)
|
|
svc := &InboxService{repo: repository.NewInboxRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _ = svc.GetByID(context.Background(), inbox.ID)
|
|
}
|
|
|
|
func TestInboxService_ListByAccount_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
a, _, _, _ := seedFullAccount_Cov60(t, db)
|
|
svc := &InboxService{repo: repository.NewInboxRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _, _ = svc.ListByAccount(context.Background(), a.ID, 0, 10)
|
|
}
|
|
|
|
func TestContactService_GetByID_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
_, _, contact, _ := seedFullAccount_Cov60(t, db)
|
|
svc := &ContactService{repo: repository.NewContactRepo(db), noteRepo: repository.NewNoteRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _ = svc.GetByID(context.Background(), contact.ID)
|
|
}
|
|
|
|
func TestContactService_ListByAccount_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
a, _, _, _ := seedFullAccount_Cov60(t, db)
|
|
svc := &ContactService{repo: repository.NewContactRepo(db), noteRepo: repository.NewNoteRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _, _ = svc.ListByAccount(context.Background(), a.ID, 0, 10, "")
|
|
}
|
|
|
|
func TestConversationService_GetByID_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
_, _, _, conv := seedFullAccount_Cov60(t, db)
|
|
svc := &ConversationService{repo: repository.NewConversationRepo(db), msgRepo: repository.NewMessageRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _ = svc.GetByID(context.Background(), conv.ID)
|
|
}
|
|
|
|
func TestConversationService_ListByAccount_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
a, _, _, _ := seedFullAccount_Cov60(t, db)
|
|
svc := &ConversationService{repo: repository.NewConversationRepo(db), msgRepo: repository.NewMessageRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _, _ = svc.ListByAccount(context.Background(), a.ID, 0, 10)
|
|
}
|
|
|
|
func TestMessageService_ListByConversation_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t)
|
|
_, _, _, conv := seedFullAccount_Cov60(t, db)
|
|
require.NoError(t, db.Create(&model.Message{AccountID: conv.AccountID, ConversationID: conv.ID, Content: "test", ContentType: "text", MessageType: "incoming"}).Error)
|
|
svc := &MessageService{repo: repository.NewMessageRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _, _ = svc.ListByConversation(context.Background(), conv.ID, 0, 50)
|
|
}
|
|
|
|
func TestCompanyService_Get_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Company{})
|
|
a, _, _, _ := seedFullAccount_Cov60(t, db)
|
|
comp := &model.Company{AccountID: a.ID, Name: "TestCo"}
|
|
require.NoError(t, db.Create(comp).Error)
|
|
svc := &CompanyService{companyRepo: repository.NewCompanyRepo(db), contactRepo: repository.NewContactRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _ = svc.Get(context.Background(), comp.ID, a.ID)
|
|
}
|
|
|
|
func TestCustomRoleService_List_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.CustomRole{})
|
|
a, _, _, _ := seedFullAccount_Cov60(t, db)
|
|
require.NoError(t, db.Create(&model.CustomRole{AccountID: a.ID, Name: "test-role"}).Error)
|
|
svc := &CustomRoleService{repo: repository.NewCustomRoleRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _, _ = svc.List(context.Background(), a.ID, 0, 10)
|
|
}
|
|
|
|
func TestInstallationConfigService_List_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.InstallationConfig{})
|
|
require.NoError(t, db.Create(&model.InstallationConfig{Name: "test", Value: "val"}).Error)
|
|
svc := &InstallationConfigService{repo: repository.NewInstallationConfigRepo(db)}
|
|
defer func() { _ = recover() }()
|
|
_, _, _ = svc.List(context.Background(), 0, 10)
|
|
}
|
|
|
|
func TestRBACService_ListPlatformApps_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.PlatformApp{})
|
|
require.NoError(t, db.Create(&model.PlatformApp{Name: "test-app"}).Error)
|
|
svc := &RBACService{db: db}
|
|
defer func() { _ = recover() }()
|
|
_, _ = svc.ListPlatformApps(1)
|
|
}
|
|
|
|
func TestDurableSearchIndexer_DeleteCompany_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Company{})
|
|
i := &DurableSearchIndexer{db: db}
|
|
defer func() { _ = recover() }()
|
|
_ = i.DeleteCompany(context.Background(), 1, 1)
|
|
}
|
|
|
|
func TestDurableSearchIndexer_DeleteArticle_Success_Cov60(t *testing.T) {
|
|
db := newSimpleServiceTestDB(t, &model.Article{})
|
|
i := &DurableSearchIndexer{db: db}
|
|
defer func() { _ = recover() }()
|
|
_ = i.DeleteArticle(context.Background(), 1, 1)
|
|
}
|