All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: #21
196 lines
5.4 KiB
Go
196 lines
5.4 KiB
Go
package service_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/domain"
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/service"
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/service/internal/mock"
|
|
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMonitor_Create(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeMonitorRepository{}
|
|
repo.ListReturns(nil, nil)
|
|
repo.CreateCalls(func(ctx context.Context, params domain.CreateMonitorParams) (domain.Monitor, error) {
|
|
return domain.Monitor{
|
|
ID: uuid.NewString(),
|
|
TribeID: params.TribeID(),
|
|
GroupID: params.GroupID(),
|
|
CreatedAt: time.Now(),
|
|
}, nil
|
|
})
|
|
|
|
groupSvc := &mock.FakeGroupGetter{}
|
|
groupSvc.GetCalls(func(ctx context.Context, groupID, serverID string) (domain.Group, error) {
|
|
return domain.Group{
|
|
ID: groupID,
|
|
ServerID: serverID,
|
|
ChannelGains: "",
|
|
ChannelLosses: "",
|
|
ServerKey: "pl151",
|
|
VersionCode: "pl",
|
|
CreatedAt: time.Now(),
|
|
}, nil
|
|
})
|
|
|
|
client := &mock.FakeTWHelpClient{}
|
|
tribe := twhelp.Tribe{
|
|
ID: 150,
|
|
Tag: "*TAG*",
|
|
Name: uuid.NewString(),
|
|
ProfileURL: "https://pl151.plemiona.pl/game.php?screen=info_player&id=150",
|
|
DeletedAt: time.Time{},
|
|
}
|
|
client.GetTribeByTagReturns(tribe, nil)
|
|
|
|
groupID := uuid.NewString()
|
|
|
|
monitor, err := service.NewMonitor(repo, groupSvc, client).
|
|
Create(context.Background(), groupID, uuid.NewString(), tribe.Tag)
|
|
assert.NoError(t, err)
|
|
assert.NotEmpty(t, monitor.ID)
|
|
assert.Equal(t, groupID, monitor.GroupID)
|
|
assert.Equal(t, tribe.ID, monitor.TribeID)
|
|
assert.NotEmpty(t, monitor.CreatedAt)
|
|
})
|
|
|
|
t.Run("ERR: group doesn't exist", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
groupID := uuid.NewString()
|
|
groupSvc := &mock.FakeGroupGetter{}
|
|
groupSvc.GetReturns(domain.Group{}, domain.GroupNotFoundError{ID: groupID})
|
|
|
|
monitor, err := service.NewMonitor(nil, groupSvc, nil).
|
|
Create(context.Background(), groupID, uuid.NewString(), "tag")
|
|
assert.ErrorIs(t, err, domain.GroupDoesNotExistError{
|
|
ID: groupID,
|
|
})
|
|
assert.Zero(t, monitor)
|
|
})
|
|
|
|
t.Run("ERR: monitor limit has been reached", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const maxMonitorsPerGroup = 10
|
|
|
|
repo := &mock.FakeMonitorRepository{}
|
|
repo.ListReturns(make([]domain.Monitor, maxMonitorsPerGroup), nil)
|
|
|
|
groupSvc := &mock.FakeGroupGetter{}
|
|
groupSvc.GetCalls(func(ctx context.Context, groupID, serverID string) (domain.Group, error) {
|
|
return domain.Group{
|
|
ID: groupID,
|
|
ServerID: serverID,
|
|
ChannelGains: "",
|
|
ChannelLosses: "",
|
|
ServerKey: "pl151",
|
|
VersionCode: "pl",
|
|
CreatedAt: time.Now(),
|
|
}, nil
|
|
})
|
|
|
|
monitor, err := service.NewMonitor(repo, groupSvc, nil).
|
|
Create(context.Background(), uuid.NewString(), uuid.NewString(), "TAG")
|
|
assert.ErrorIs(t, err, domain.MonitorLimitReachedError{
|
|
Current: maxMonitorsPerGroup,
|
|
Limit: maxMonitorsPerGroup,
|
|
})
|
|
assert.Zero(t, monitor)
|
|
})
|
|
|
|
t.Run("ERR: tribe doesn't exist", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("API error", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeMonitorRepository{}
|
|
repo.ListReturns(nil, nil)
|
|
repo.CreateCalls(func(ctx context.Context, params domain.CreateMonitorParams) (domain.Monitor, error) {
|
|
return domain.Monitor{
|
|
ID: uuid.NewString(),
|
|
TribeID: params.TribeID(),
|
|
GroupID: params.GroupID(),
|
|
CreatedAt: time.Now(),
|
|
}, nil
|
|
})
|
|
|
|
groupSvc := &mock.FakeGroupGetter{}
|
|
groupSvc.GetCalls(func(ctx context.Context, groupID, serverID string) (domain.Group, error) {
|
|
return domain.Group{
|
|
ID: groupID,
|
|
ServerID: serverID,
|
|
ChannelGains: "",
|
|
ChannelLosses: "",
|
|
ServerKey: "pl151",
|
|
VersionCode: "pl",
|
|
CreatedAt: time.Now(),
|
|
}, nil
|
|
})
|
|
|
|
client := &mock.FakeTWHelpClient{}
|
|
client.GetTribeByTagReturns(twhelp.Tribe{}, twhelp.APIError{
|
|
Code: twhelp.ErrorCodeEntityNotFound,
|
|
Message: "tribe not found",
|
|
})
|
|
|
|
tag := "TAG"
|
|
|
|
monitor, err := service.NewMonitor(repo, groupSvc, client).
|
|
Create(context.Background(), uuid.NewString(), uuid.NewString(), tag)
|
|
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
|
|
Tag: tag,
|
|
})
|
|
assert.Zero(t, monitor)
|
|
})
|
|
|
|
t.Run("tribe is deleted", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
repo := &mock.FakeMonitorRepository{}
|
|
repo.ListReturns(nil, nil)
|
|
|
|
groupSvc := &mock.FakeGroupGetter{}
|
|
groupSvc.GetCalls(func(ctx context.Context, groupID, serverID string) (domain.Group, error) {
|
|
return domain.Group{
|
|
ID: groupID,
|
|
ServerID: serverID,
|
|
ChannelGains: "",
|
|
ChannelLosses: "",
|
|
ServerKey: "pl151",
|
|
VersionCode: "pl",
|
|
CreatedAt: time.Now(),
|
|
}, nil
|
|
})
|
|
|
|
client := &mock.FakeTWHelpClient{}
|
|
tribe := twhelp.Tribe{
|
|
ID: 150,
|
|
Tag: "*TAG*",
|
|
Name: uuid.NewString(),
|
|
ProfileURL: "https://pl151.plemiona.pl/game.php?screen=info_player&id=150",
|
|
DeletedAt: time.Now(),
|
|
}
|
|
client.GetTribeByTagReturns(tribe, nil)
|
|
|
|
monitor, err := service.NewMonitor(repo, groupSvc, client).
|
|
Create(context.Background(), uuid.NewString(), uuid.NewString(), tribe.Tag)
|
|
assert.ErrorIs(t, err, domain.TribeDoesNotExistError{
|
|
Tag: tribe.Tag,
|
|
})
|
|
assert.Zero(t, monitor)
|
|
})
|
|
})
|
|
}
|