2023-07-10 12:39:25 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
|
|
|
"git.ctrlz.es/mgdelacroix/birthdaybot/notification"
|
2023-07-10 12:43:59 +01:00
|
|
|
notification_mocks "git.ctrlz.es/mgdelacroix/birthdaybot/notification/mocks"
|
|
|
|
server_mocks "git.ctrlz.es/mgdelacroix/birthdaybot/server/mocks"
|
2023-07-10 12:39:25 +01:00
|
|
|
"github.com/charmbracelet/log"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestHelper struct {
|
|
|
|
t *testing.T
|
|
|
|
ctrl *gomock.Controller
|
2023-07-10 12:43:59 +01:00
|
|
|
mockNotificationService *notification_mocks.MockNotificationService
|
|
|
|
mockWorker *server_mocks.MockWorker
|
2023-07-10 12:39:25 +01:00
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfig(t *testing.T) *model.Config {
|
|
|
|
f, err := ioutil.TempFile("", "birthdaybot-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, f.Close())
|
|
|
|
require.NoError(t, os.Remove(f.Name()))
|
|
|
|
|
2023-07-11 12:27:50 +01:00
|
|
|
// ToDo: allow for a random port to be used
|
|
|
|
return &model.Config{
|
|
|
|
Web: &model.WebConfig{Port: 9090},
|
|
|
|
Birthdays: &model.BirthdaysConfig{File: f.Name()},
|
|
|
|
}
|
2023-07-10 12:39:25 +01:00
|
|
|
}
|
|
|
|
|
2023-07-11 09:10:56 +01:00
|
|
|
func SetupTestHelper(t *testing.T, opts ...Option) *TestHelper {
|
2023-07-10 12:39:25 +01:00
|
|
|
th := &TestHelper{t: t}
|
|
|
|
th.ctrl = gomock.NewController(t)
|
|
|
|
|
2023-07-10 12:43:59 +01:00
|
|
|
th.mockNotificationService = notification_mocks.NewMockNotificationService(th.ctrl)
|
2023-07-10 12:39:25 +01:00
|
|
|
notificationServices := []notification.NotificationService{th.mockNotificationService}
|
|
|
|
|
2023-07-10 12:43:59 +01:00
|
|
|
th.mockWorker = server_mocks.NewMockWorker(th.ctrl)
|
2023-07-10 12:39:25 +01:00
|
|
|
workers := []Worker{th.mockWorker}
|
|
|
|
th.mockWorker.EXPECT().Start().Times(1)
|
|
|
|
th.mockWorker.EXPECT().Stop().Times(1)
|
|
|
|
|
|
|
|
birthdays := []*model.Birthday{
|
|
|
|
{
|
|
|
|
Name: "John",
|
|
|
|
Email: "john@doe.com",
|
|
|
|
Phone: "1234",
|
|
|
|
YearOfBirth: 2022,
|
|
|
|
MonthOfBirth: 1,
|
|
|
|
DayOfBirth: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-07-11 09:10:56 +01:00
|
|
|
serverOpts := append([]Option{
|
2023-07-10 12:39:25 +01:00
|
|
|
WithConfig(testConfig(t)),
|
|
|
|
WithLogger(log.New(os.Stderr)),
|
|
|
|
WithBirthdays(birthdays),
|
|
|
|
WithNotificationServices(notificationServices),
|
|
|
|
WithWorkers(workers),
|
2023-07-11 09:10:56 +01:00
|
|
|
}, opts...)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
th.srv, err = New(serverOpts...)
|
2023-07-10 12:39:25 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
th.srv.Start()
|
|
|
|
|
|
|
|
return th
|
|
|
|
}
|
|
|
|
|
|
|
|
func (th *TestHelper) TearDown() {
|
|
|
|
th.srv.Stop()
|
|
|
|
th.ctrl.Finish()
|
|
|
|
}
|