feature/refactoring #1
@@ -122,7 +122,7 @@ func (ha *HomeAssistant) handleGetLatestPoo() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ha *HomeAssistant) createActionTask(message haMessage) bool {
|
func (ha *HomeAssistant) createActionTask(message haMessage) bool {
|
||||||
if !viper.InConfig("homeassistant.actionTaskProjectId") {
|
if !viper.IsSet("homeassistant.actionTaskProjectId") {
|
||||||
slog.Warn("homeassistant.createActionTask: actionTaskProjectId not found in config file")
|
slog.Warn("homeassistant.createActionTask: actionTaskProjectId not found in config file")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,14 +2,17 @@ package homeassistant
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
"github.com/t-liu93/home-automation-backend/util/ticktickutil"
|
"github.com/t-liu93/home-automation-backend/util/ticktickutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,26 +21,26 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type MockTicktickUtil struct {
|
type MockTicktickUtil struct {
|
||||||
// Mock methods for testing
|
mock.Mock
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockTicktickUtil) HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
func (m *MockTicktickUtil) HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
||||||
// Mock implementation
|
m.Called(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockTicktickUtil) GetTasks(projectId string) []ticktickutil.Task {
|
func (m *MockTicktickUtil) GetTasks(projectId string) []ticktickutil.Task {
|
||||||
// Mock implementation
|
args := m.Called(projectId)
|
||||||
return []ticktickutil.Task{}
|
return args.Get(0).([]ticktickutil.Task)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockTicktickUtil) HasDuplicateTask(projectId string, taskTitle string) bool {
|
func (m *MockTicktickUtil) HasDuplicateTask(projectId string, taskTitile string) bool {
|
||||||
// Mock implementation
|
args := m.Called(projectId, taskTitile)
|
||||||
return false
|
return args.Bool(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockTicktickUtil) CreateTask(task ticktickutil.Task) error {
|
func (m *MockTicktickUtil) CreateTask(task ticktickutil.Task) error {
|
||||||
// Mock implementation
|
args := m.Called(task)
|
||||||
return nil
|
return args.Error(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupTearDown(t *testing.T) (func(), *HomeAssistant) {
|
func SetupTearDown(t *testing.T) (func(), *HomeAssistant) {
|
||||||
@@ -47,6 +50,7 @@ func SetupTearDown(t *testing.T) (func(), *HomeAssistant) {
|
|||||||
|
|
||||||
return func() {
|
return func() {
|
||||||
loggertearDown()
|
loggertearDown()
|
||||||
|
viper.Reset()
|
||||||
}, ha
|
}, ha
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,3 +176,105 @@ func TestHandleLocationRecorderMsgRequestErr(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
assert.Contains(t, loggerText.String(), "homeassistant.handleLocationRecorderMsg: Error sending request to location recorder")
|
assert.Contains(t, loggerText.String(), "homeassistant.handleLocationRecorderMsg: Error sending request to location recorder")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHandleTicktickMsgCreateActionTask(t *testing.T) {
|
||||||
|
teardown, _ := SetupTearDown(t)
|
||||||
|
defer teardown()
|
||||||
|
const expectedProjectId = "test_project_id"
|
||||||
|
const dueHour = 12
|
||||||
|
due := time.Now().Add(time.Hour * time.Duration(dueHour))
|
||||||
|
dueNextMidnight := time.Date(due.Year(), due.Month(), due.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, 1)
|
||||||
|
dueTicktick := dueNextMidnight.UTC().Format(ticktickutil.DateTimeLayout)
|
||||||
|
|
||||||
|
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
mockTicktick := &MockTicktickUtil{}
|
||||||
|
mockTicktick.On("CreateTask", mock.Anything).Return(nil)
|
||||||
|
ha := NewHomeAssistant(mockTicktick)
|
||||||
|
viper.Set("homeassistant.actionTaskProjectId", expectedProjectId)
|
||||||
|
ha.HandleHaMessage(w, req)
|
||||||
|
expectedTask := ticktickutil.Task{
|
||||||
|
Title: "test_action",
|
||||||
|
DueDate: dueTicktick,
|
||||||
|
ProjectId: expectedProjectId,
|
||||||
|
}
|
||||||
|
mockTicktick.AssertCalled(t, "CreateTask", expectedTask)
|
||||||
|
mockTicktick.AssertNumberOfCalls(t, "CreateTask", 1)
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Empty(t, loggerText.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleTicktickMsgUnknownAction(t *testing.T) {
|
||||||
|
teardown, ha := SetupTearDown(t)
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
requestBody := `{"target": "ticktick", "action": "unknown_action", "content": ""}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
ha.HandleHaMessage(w, req)
|
||||||
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
|
assert.Contains(t, loggerText.String(), "homeassistant.handleTicktickMsg: Unknown action")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleTicktickMsgProjectIdUnset(t *testing.T) {
|
||||||
|
teardown, ha := SetupTearDown(t)
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
ha.HandleHaMessage(w, req)
|
||||||
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
|
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: actionTaskProjectId not found in config file")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleTicktickMsgJsonError(t *testing.T) {
|
||||||
|
teardown, ha := SetupTearDown(t)
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
invalidRequestBody := ` { "target": "ticktick", "action": "create_action_task", "content": "{'title': 'tes, 'action': 'test_action', 'due_hour': 12}"}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(invalidRequestBody))
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
||||||
|
ha.HandleHaMessage(w, req)
|
||||||
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
|
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: Error unmarshalling")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleTicktickMsgTicktickUtilErr(t *testing.T) {
|
||||||
|
teardown, _ := SetupTearDown(t)
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
requestBody := `{"target": "ticktick", "action": "create_action_task", "content": "{'title': 'test', 'action': 'test_action', 'due_hour': 12}"}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
mockedTicktickUtil := &MockTicktickUtil{}
|
||||||
|
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
||||||
|
|
||||||
|
mockedTicktickUtil.On("CreateTask", mock.Anything).Return(errors.New("some error"))
|
||||||
|
|
||||||
|
ha := NewHomeAssistant(mockedTicktickUtil)
|
||||||
|
|
||||||
|
ha.HandleHaMessage(w, req)
|
||||||
|
|
||||||
|
mockedTicktickUtil.AssertCalled(t, "CreateTask", mock.Anything)
|
||||||
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
|
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: Error creating task")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleHaMessageUnknownTarget(t *testing.T) {
|
||||||
|
teardown, ha := SetupTearDown(t)
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
requestBody := `{"target": "unknown_target", "action": "record", "content": ""}`
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
ha.HandleHaMessage(w, req)
|
||||||
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||||
|
assert.Contains(t, loggerText.String(), "homeassistant.HandleHaMessage: Unknown target")
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ require (
|
|||||||
github.com/jomei/notionapi v1.13.2
|
github.com/jomei/notionapi v1.13.2
|
||||||
github.com/spf13/cobra v1.8.1
|
github.com/spf13/cobra v1.8.1
|
||||||
github.com/spf13/viper v1.19.0
|
github.com/spf13/viper v1.19.0
|
||||||
github.com/stretchr/testify v1.9.0
|
github.com/stretchr/testify v1.10.0
|
||||||
golang.org/x/term v0.24.0
|
golang.org/x/term v0.24.0
|
||||||
modernc.org/sqlite v1.33.1
|
modernc.org/sqlite v1.33.1
|
||||||
)
|
)
|
||||||
@@ -36,6 +36,7 @@ require (
|
|||||||
github.com/spf13/afero v1.11.0 // indirect
|
github.com/spf13/afero v1.11.0 // indirect
|
||||||
github.com/spf13/cast v1.6.0 // indirect
|
github.com/spf13/cast v1.6.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
|
github.com/stretchr/objx v0.5.2 // indirect
|
||||||
github.com/subosito/gotenv v1.6.0 // indirect
|
github.com/subosito/gotenv v1.6.0 // indirect
|
||||||
go.uber.org/atomic v1.9.0 // indirect
|
go.uber.org/atomic v1.9.0 // indirect
|
||||||
go.uber.org/multierr v1.9.0 // indirect
|
go.uber.org/multierr v1.9.0 // indirect
|
||||||
|
|||||||
@@ -72,13 +72,15 @@ github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+
|
|||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||||
|
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
|
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||||
|
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||||
|
|||||||
Reference in New Issue
Block a user