Compare commits
1 Commits
872c7b356f
...
big_refact
| Author | SHA1 | Date | |
|---|---|---|---|
| 95d0fa07b8 |
22
.github/workflows/nightly.yml
vendored
22
.github/workflows/nightly.yml
vendored
@@ -1,22 +0,0 @@
|
||||
name: Run nightly tests
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 20 * * *' # Every day at 20:00 UTC
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
jobs:
|
||||
nightly-tests:
|
||||
runs-on: [ubuntu-latest, cloud]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '1.23'
|
||||
|
||||
- name: Test
|
||||
working-directory: ./src
|
||||
run: go test -v --short ./...
|
||||
14
.github/workflows/short-tests.yml
vendored
14
.github/workflows/short-tests.yml
vendored
@@ -13,16 +13,8 @@ jobs:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '1.24'
|
||||
go-version: '1.23'
|
||||
|
||||
- name: Run short tests with coverage
|
||||
- name: Test
|
||||
working-directory: ./src
|
||||
run: | # TODO: at this moment only Home Assistant component is tested
|
||||
go test -v --short ./components/homeassistant/... -cover -coverprofile=cover.out
|
||||
- name: Upload coverage report
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: coverage-report_build_${{ github.run_number }}
|
||||
path: ${{ github.workspace }}/src/cover.out
|
||||
retention-days: 1
|
||||
|
||||
run: go test -v --short ./...
|
||||
|
||||
@@ -27,8 +27,7 @@ import (
|
||||
var (
|
||||
port string
|
||||
scheduler gocron.Scheduler
|
||||
ticktick ticktickutil.TicktickUtil
|
||||
ha *homeassistant.HomeAssistant
|
||||
ticktick *ticktickutil.TicktickUtilImpl
|
||||
)
|
||||
|
||||
// serveCmd represents the serve command
|
||||
@@ -56,7 +55,7 @@ func initComponent() {
|
||||
// init location recorder
|
||||
locationRecorder.Init()
|
||||
// init homeassistant
|
||||
ha = homeassistant.NewHomeAssistant(ticktick)
|
||||
homeassistant.Init(ticktick)
|
||||
}
|
||||
|
||||
func serve(cmd *cobra.Command, args []string) {
|
||||
@@ -109,7 +108,7 @@ func serve(cmd *cobra.Command, args []string) {
|
||||
|
||||
router.HandleFunc("/poo/latest", pooRecorder.HandleNotifyLatestPoo).Methods("GET")
|
||||
router.HandleFunc("/poo/record", pooRecorder.HandleRecordPoo).Methods("POST")
|
||||
router.HandleFunc("/homeassistant/publish", ha.HandleHaMessage).Methods("POST")
|
||||
router.HandleFunc("/homeassistant/publish", homeassistant.HandleHaMessage).Methods("POST")
|
||||
|
||||
router.HandleFunc("/location/record", locationRecorder.HandleRecordLocation).Methods("POST")
|
||||
|
||||
|
||||
@@ -18,68 +18,63 @@ type haMessage struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type HomeAssistant struct {
|
||||
ticktickUtil ticktickutil.TicktickUtil
|
||||
}
|
||||
|
||||
type actionTask struct {
|
||||
Action string `json:"action"`
|
||||
DueHour int `json:"due_hour"`
|
||||
}
|
||||
|
||||
func NewHomeAssistant(ticktick ticktickutil.TicktickUtil) *HomeAssistant {
|
||||
return &HomeAssistant{
|
||||
ticktickUtil: ticktick,
|
||||
}
|
||||
var (
|
||||
ticktickUtil ticktickutil.TicktickUtil
|
||||
)
|
||||
|
||||
func Init(ticktick ticktickutil.TicktickUtil) {
|
||||
ticktickUtil = ticktick
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
||||
func HandleHaMessage(w http.ResponseWriter, r *http.Request) {
|
||||
var message haMessage
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
err := decoder.Decode(&message)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error decoding request body", err))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
slog.Warn(fmt.Sprintln("HandleHaMessage: Error decoding request body", err))
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
switch message.Target {
|
||||
case "poo_recorder":
|
||||
res := ha.handlePooRecorderMsg(message)
|
||||
res := handlePooRecorderMsg(message)
|
||||
if !res {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling poo recorder message"))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
http.Error(w, "Error handling poo recorder message", http.StatusInternalServerError)
|
||||
}
|
||||
case "location_recorder":
|
||||
res := ha.handleLocationRecorderMsg(message)
|
||||
res := handleLocationRecorderMsg(message)
|
||||
if !res {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling location recorder message"))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
http.Error(w, "Error handling location recorder message", http.StatusInternalServerError)
|
||||
}
|
||||
case "ticktick":
|
||||
res := ha.handleTicktickMsg(message)
|
||||
res := handleTicktickMsg(message)
|
||||
if !res {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Error handling ticktick message"))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
http.Error(w, "Error handling ticktick message", http.StatusInternalServerError)
|
||||
}
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("homeassistant.HandleHaMessage: Unknown target", message.Target))
|
||||
http.Error(w, "", http.StatusInternalServerError)
|
||||
slog.Warn(fmt.Sprintln("HandleHaMessage: Unknown target", message.Target))
|
||||
http.Error(w, "Unknown target", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) handlePooRecorderMsg(message haMessage) bool {
|
||||
func handlePooRecorderMsg(message haMessage) bool {
|
||||
switch message.Action {
|
||||
case "get_latest":
|
||||
return ha.handleGetLatestPoo()
|
||||
return handleGetLatestPoo()
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handlePooRecorderMsg: Unknown action", message.Action))
|
||||
slog.Warn(fmt.Sprintln("handlePooRecorderMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) handleLocationRecorderMsg(message haMessage) bool {
|
||||
func handleLocationRecorderMsg(message haMessage) bool {
|
||||
if message.Action == "record" {
|
||||
port := viper.GetString("port")
|
||||
client := &http.Client{
|
||||
@@ -87,43 +82,43 @@ func (ha *HomeAssistant) handleLocationRecorderMsg(message haMessage) bool {
|
||||
}
|
||||
_, err := client.Post("http://localhost:"+port+"/location/record", "application/json", strings.NewReader(strings.ReplaceAll(message.Content, "'", "\"")))
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleLocationRecorderMsg: Error sending request to location recorder", err))
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleLocationRecorderMsg: Unknown action", message.Action))
|
||||
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Error sending request to location recorder", err))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) handleTicktickMsg(message haMessage) bool {
|
||||
switch message.Action {
|
||||
case "create_action_task":
|
||||
return ha.createActionTask(message)
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleTicktickMsg: Unknown action", message.Action))
|
||||
} else {
|
||||
slog.Warn(fmt.Sprintln("handleLocationRecorderMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) handleGetLatestPoo() bool {
|
||||
func handleTicktickMsg(message haMessage) bool {
|
||||
switch message.Action {
|
||||
case "create_action_task":
|
||||
return createActionTask(message)
|
||||
default:
|
||||
slog.Warn(fmt.Sprintln("handleTicktickMsg: Unknown action", message.Action))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetLatestPoo() bool {
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 1,
|
||||
}
|
||||
port := viper.GetString("port")
|
||||
_, err := client.Get("http://localhost:" + port + "/poo/latest")
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.handleGetLatestPoo: Error sending request to poo recorder", err))
|
||||
slog.Warn(fmt.Sprintln("handleGetLatestPoo: Error sending request to poo recorder", err))
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (ha *HomeAssistant) createActionTask(message haMessage) bool {
|
||||
func createActionTask(message haMessage) bool {
|
||||
if !viper.IsSet("homeassistant.actionTaskProjectId") {
|
||||
slog.Warn("homeassistant.createActionTask: actionTaskProjectId not found in config file")
|
||||
slog.Warn("Homeassistant.createActionTask actionTaskProjectId not set")
|
||||
return false
|
||||
}
|
||||
projectId := viper.GetString("homeassistant.actionTaskProjectId")
|
||||
@@ -131,7 +126,7 @@ func (ha *HomeAssistant) createActionTask(message haMessage) bool {
|
||||
var task actionTask
|
||||
err := json.Unmarshal([]byte(detail), &task)
|
||||
if err != nil {
|
||||
slog.Warn(fmt.Sprintln("homeassistant.createActionTask: Error unmarshalling", err))
|
||||
slog.Warn(fmt.Sprintln("Homeassistant.createActionTask: Error unmarshalling", err))
|
||||
return false
|
||||
}
|
||||
dueHour := task.DueHour
|
||||
@@ -143,9 +138,9 @@ func (ha *HomeAssistant) createActionTask(message haMessage) bool {
|
||||
Title: task.Action,
|
||||
DueDate: dueTicktick,
|
||||
}
|
||||
err = ha.ticktickUtil.CreateTask(ticktickTask)
|
||||
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
|
||||
|
||||
@@ -20,38 +20,19 @@ var (
|
||||
loggerText = new(bytes.Buffer)
|
||||
)
|
||||
|
||||
type MockTicktickUtil struct {
|
||||
type (
|
||||
MockedTicktickUtil struct {
|
||||
mock.Mock
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
func (m *MockTicktickUtil) HandleAuthCode(w http.ResponseWriter, r *http.Request) {
|
||||
m.Called(w, r)
|
||||
}
|
||||
|
||||
func (m *MockTicktickUtil) GetTasks(projectId string) []ticktickutil.Task {
|
||||
args := m.Called(projectId)
|
||||
return args.Get(0).([]ticktickutil.Task)
|
||||
}
|
||||
|
||||
func (m *MockTicktickUtil) HasDuplicateTask(projectId string, taskTitile string) bool {
|
||||
args := m.Called(projectId, taskTitile)
|
||||
return args.Bool(0)
|
||||
}
|
||||
|
||||
func (m *MockTicktickUtil) CreateTask(task ticktickutil.Task) error {
|
||||
args := m.Called(task)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func SetupTearDown(t *testing.T) (func(), *HomeAssistant) {
|
||||
func SetupTearDown(t *testing.T) func() {
|
||||
loggertearDown := loggerSetupTeardown()
|
||||
mockTicktick := &MockTicktickUtil{}
|
||||
ha := NewHomeAssistant(mockTicktick)
|
||||
|
||||
return func() {
|
||||
loggertearDown()
|
||||
viper.Reset()
|
||||
}, ha
|
||||
}
|
||||
}
|
||||
|
||||
func loggerSetupTeardown() func() {
|
||||
@@ -65,21 +46,40 @@ 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, ha := SetupTearDown(t)
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
invalidRequestBody := ` { "target": "poo_recorder", "action": "get_latest", "content": " }`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(invalidRequestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.HandleHaMessage: Error decoding request body")
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "HandleHaMessage: Error decoding request body")
|
||||
}
|
||||
|
||||
func TestHandlePooRecorderMsgGetLatest(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
requestBody := `{"target": "poo_recorder", "action": "get_latest", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
@@ -92,26 +92,26 @@ func TestHandlePooRecorderMsgGetLatest(t *testing.T) {
|
||||
port := strings.Split(server.URL, ":")[2]
|
||||
viper.Set("port", port)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Empty(t, loggerText.String())
|
||||
}
|
||||
|
||||
func TestHandlePooRecorderMsgUnknownAction(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "poo_recorder", "action": "unknown_action", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.handlePooRecorderMsg: Unknown action")
|
||||
assert.Contains(t, loggerText.String(), "handlePooRecorderMsg: Unknown action")
|
||||
}
|
||||
|
||||
func TestHandlePooRecorderMsgGetLatestError(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "poo_recorder", "action": "get_latest", "content": ""}`
|
||||
@@ -121,13 +121,13 @@ func TestHandlePooRecorderMsgGetLatestError(t *testing.T) {
|
||||
port := "invalid port"
|
||||
viper.Set("port", port)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.handleGetLatestPoo: Error sending request to poo recorder")
|
||||
assert.Contains(t, loggerText.String(), "handleGetLatestPoo: Error sending request to poo recorder")
|
||||
}
|
||||
|
||||
func TestHandleLocationRecorderMsg(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "location_recorder", "action": "record", "content": "{'person': 'test', 'latitude': '1.0', 'longitude': '2.0', 'altitude': '3.0'}"}`
|
||||
@@ -143,26 +143,26 @@ func TestHandleLocationRecorderMsg(t *testing.T) {
|
||||
port := strings.Split(server.URL, ":")[2]
|
||||
viper.Set("port", port)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Empty(t, loggerText.String())
|
||||
}
|
||||
|
||||
func TestHandleLocationRecorderMsgUnknownAction(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "location_recorder", "action": "unknown_action", "content": ""}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/homeassistant/publish", strings.NewReader(requestBody))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.handleLocationRecorderMsg: Unknown action")
|
||||
assert.Contains(t, loggerText.String(), "handleLocationRecorderMsg: Unknown action")
|
||||
}
|
||||
|
||||
func TestHandleLocationRecorderMsgRequestErr(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(t)
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
|
||||
requestBody := `{"target": "location_recorder", "action": "record", "content": "{'person': 'test', 'latitude': '1.0', 'longitude': '2.0', 'altitude': '3.0'}"}`
|
||||
@@ -172,13 +172,13 @@ func TestHandleLocationRecorderMsgRequestErr(t *testing.T) {
|
||||
port := "invalid port"
|
||||
viper.Set("port", port)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
HandleHaMessage(w, req)
|
||||
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(), "handleLocationRecorderMsg: Error sending request to location recorder")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgCreateActionTask(t *testing.T) {
|
||||
teardown, _ := SetupTearDown(t)
|
||||
teardown := SetupTearDown(t)
|
||||
defer teardown()
|
||||
const expectedProjectId = "test_project_id"
|
||||
const dueHour = 12
|
||||
@@ -189,92 +189,96 @@ func TestHandleTicktickMsgCreateActionTask(t *testing.T) {
|
||||
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)
|
||||
mockedTicktickUtil := new(MockedTicktickUtil)
|
||||
viper.Set("homeassistant.actionTaskProjectId", expectedProjectId)
|
||||
ha.HandleHaMessage(w, req)
|
||||
|
||||
mockedTicktickUtil.On("CreateTask", mock.Anything).Return(nil)
|
||||
|
||||
Init(mockedTicktickUtil)
|
||||
|
||||
HandleHaMessage(w, req)
|
||||
|
||||
expectedTask := ticktickutil.Task{
|
||||
Title: "test_action",
|
||||
DueDate: dueTicktick,
|
||||
ProjectId: expectedProjectId,
|
||||
DueDate: dueTicktick,
|
||||
}
|
||||
mockTicktick.AssertCalled(t, "CreateTask", expectedTask)
|
||||
mockTicktick.AssertNumberOfCalls(t, "CreateTask", 1)
|
||||
|
||||
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, ha := SetupTearDown(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()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.handleTicktickMsg: Unknown action")
|
||||
assert.Contains(t, loggerText.String(), "handleTicktickMsg: Unknown action")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgProjectIdUnset(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(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()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: actionTaskProjectId not found in config file")
|
||||
assert.Contains(t, loggerText.String(), "Homeassistant.createActionTask actionTaskProjectId not set")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgJsonError(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(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")
|
||||
ha.HandleHaMessage(w, req)
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.createActionTask: Error unmarshalling")
|
||||
assert.Contains(t, loggerText.String(), "Homeassistant.createActionTask: Error unmarshalling")
|
||||
}
|
||||
|
||||
func TestHandleTicktickMsgTicktickUtilErr(t *testing.T) {
|
||||
teardown, _ := SetupTearDown(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{}
|
||||
mockedTicktickUtil := new(MockedTicktickUtil)
|
||||
viper.Set("homeassistant.actionTaskProjectId", "some project id")
|
||||
|
||||
mockedTicktickUtil.On("CreateTask", mock.Anything).Return(errors.New("some error"))
|
||||
|
||||
ha := NewHomeAssistant(mockedTicktickUtil)
|
||||
Init(mockedTicktickUtil)
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
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")
|
||||
assert.Contains(t, loggerText.String(), "Homeassistant.createActionTask: Error creating task")
|
||||
}
|
||||
|
||||
func TestHandleHaMessageUnknownTarget(t *testing.T) {
|
||||
teardown, ha := SetupTearDown(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()
|
||||
|
||||
ha.HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "homeassistant.HandleHaMessage: Unknown target")
|
||||
HandleHaMessage(w, req)
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Contains(t, loggerText.String(), "HandleHaMessage: Unknown target")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ require (
|
||||
github.com/jomei/notionapi v1.13.2
|
||||
github.com/spf13/cobra v1.8.1
|
||||
github.com/spf13/viper v1.19.0
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/stretchr/testify v1.9.0
|
||||
golang.org/x/term v0.24.0
|
||||
modernc.org/sqlite v1.33.1
|
||||
)
|
||||
|
||||
@@ -78,9 +78,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
|
||||
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.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.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/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
|
||||
|
||||
@@ -78,7 +78,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func Init() TicktickUtil { // TODO: Will modify Init to a proper behavior
|
||||
func Init() *TicktickUtilImpl { // TODO: Will modify Init to a proper behavior
|
||||
ticktickUtilImpl := &TicktickUtilImpl{}
|
||||
if !viper.InConfig("ticktick.clientId") {
|
||||
slog.Error("TickTick clientId not found in config file, exiting..")
|
||||
|
||||
Reference in New Issue
Block a user