Compare commits
1 Commits
8df89d3478
...
big_refact
| Author | SHA1 | Date | |
|---|---|---|---|
| 95d0fa07b8 |
@@ -54,9 +54,12 @@ func HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "Error handling location recorder message", http.StatusInternalServerError)
|
||||
}
|
||||
case "ticktick":
|
||||
handleTicktickMsg(message)
|
||||
res := handleTicktickMsg(message)
|
||||
if !res {
|
||||
http.Error(w, "Error handling ticktick message", http.StatusInternalServerError)
|
||||
}
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("HandleHaMessage Unknown target", message.Target))
|
||||
slog.Warn(fmt.Sprintln("HandleHaMessage: Unknown target", message.Target))
|
||||
http.Error(w, "Unknown target", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
@@ -82,17 +85,20 @@ func handleLocationRecorderMsg(message haMessage) bool {
|
||||
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Error sending request to location recorder", err))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
} else {
|
||||
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func handleTicktickMsg(message haMessage) {
|
||||
func handleTicktickMsg(message haMessage) bool {
|
||||
switch message.Action {
|
||||
case "create_action_task":
|
||||
createActionTask(message)
|
||||
return createActionTask(message)
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("handleTicktickMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,18 +116,18 @@ func handleGetLatestPoo() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func createActionTask(message haMessage) {
|
||||
if !viper.InConfig("homeassistant.actionTaskProjectId") {
|
||||
slog.Warn("Homeassistant.createActionTask actionTaskProjectId not found in config file")
|
||||
return
|
||||
func createActionTask(message haMessage) bool {
|
||||
if !viper.IsSet("homeassistant.actionTaskProjectId") {
|
||||
slog.Warn("Homeassistant.createActionTask actionTaskProjectId not set")
|
||||
return false
|
||||
}
|
||||
projectId := viper.GetString("homeassistant.actionTaskProjectId")
|
||||
detail := strings.ReplaceAll(message.Content, "'", "\"")
|
||||
var task actionTask
|
||||
err := json.Unmarshal([]byte(detail), &task)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("Homeassistant.createActionTask Error unmarshalling", err))
|
||||
return
|
||||
slog.Warn(fmt.Sprintln("Homeassistant.createActionTask: Error unmarshalling", err))
|
||||
return false
|
||||
}
|
||||
dueHour := task.DueHour
|
||||
due := time.Now().Add(time.Hour * time.Duration(dueHour))
|
||||
@@ -134,6 +140,8 @@ func createActionTask(message haMessage) {
|
||||
}
|
||||
err = ticktickUtil.CreateTask(ticktickTask)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintf("Homeassistant.createActionTask Error creating task %s", err))
|
||||
slog.Warn(fmt.Sprintf("Homeassistant.createActionTask: Error creating task %s", err))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -2,25 +2,36 @@ package homeassistant
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/t-liu93/home-automation-backend/util/ticktickutil"
|
||||
)
|
||||
|
||||
var (
|
||||
loggerText = new(bytes.Buffer)
|
||||
)
|
||||
|
||||
type (
|
||||
MockedTicktickUtil struct {
|
||||
mock.Mock
|
||||
}
|
||||
)
|
||||
|
||||
func SetupTearDown(t *testing.T) func() {
|
||||
loggertearDown := loggerSetupTeardown()
|
||||
|
||||
return func() {
|
||||
loggertearDown()
|
||||
viper.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +46,25 @@ func loggerSetupTeardown() func() {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockedTicktickUtil) HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
||||
m.Called(w, r)
|
||||
}
|
||||
|
||||
func (m *MockedTicktickUtil) GetTasks(projectId string) []ticktickutil.Task {
|
||||
args := m.Called(projectId)
|
||||
return args.Get(0).([]ticktickutil.Task)
|
||||
}
|
||||
|
||||
func (m *MockedTicktickUtil) HasDuplicateTask(projectId string, taskTitile string) bool {
|
||||
args := m.Called(projectId, taskTitile)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (m *MockedTicktickUtil) CreateTask(task ticktickutil.Task) error {
|
||||
args := m.Called(task)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func TestHandleHaMessageJsonDecodeError(t *testing.T) {
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
@@ -146,3 +176,109 @@ func TestHandleLocationRecorderMsgRequestErr(t *testing.T) {
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "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()
|
||||
mockedTicktickUtil := new(MockedTicktickUtil)
|
||||
viper.Set("homeassistant.actionTaskProjectId", expectedProjectId)
|
||||
|
||||
mockedTicktickUtil.On("CreateTask", mock.Anything).Return(nil)
|
||||
|
||||
Init(mockedTicktickUtil)
|
||||
|
||||
HandleHaMessage(w, req)
|
||||
|
||||
expectedTask := ticktickutil.Task{
|
||||
Title: "test_action",
|
||||
ProjectId: expectedProjectId,
|
||||
DueDate: dueTicktick,
|
||||
}
|
||||
|
||||
mockedTicktickUtil.AssertCalled(t, "CreateTask", expectedTask)
|
||||
mockedTicktickUtil.AssertNumberOfCalls(t, "CreateTask", 1)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Empty(t, loggerText.String())
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgUnknownAction(t *testing.T) {
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "ticktick", "action": "unknown_action", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "handleTicktickMsg: Unknown action")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgProjectIdUnset(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()
|
||||
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "Homeassistant.createActionTask actionTaskProjectId not set")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgJsonError(t *testing.T) {
|
||||
teardown := 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")
|
||||
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 := new(MockedTicktickUtil)
|
||||
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
||||
|
||||
mockedTicktickUtil.On("CreateTask", mock.Anything).Return(errors.New("some error"))
|
||||
|
||||
Init(mockedTicktickUtil)
|
||||
|
||||
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 := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "unknown_target", "action": "record", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "HandleHaMessage: Unknown target")
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ require (
|
||||
github.com/spf13/afero v1.11.0 // indirect
|
||||
github.com/spf13/cast v1.6.0 // 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
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.9.0 // indirect
|
||||
|
||||
@@ -72,6 +72,7 @@ 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.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.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
|
||||
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.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
|
||||
Reference in New Issue
Block a user