Skip to content

Commit

Permalink
update notify system
Browse files Browse the repository at this point in the history
  • Loading branch information
e154 committed Oct 18, 2023
1 parent 96d5511 commit 95b7a4a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 97 deletions.
20 changes: 16 additions & 4 deletions tests/plugins/email/email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ package email

import (
"context"
"github.com/e154/smart-home/common"
"testing"
"time"

"github.com/e154/smart-home/adaptors"
m "github.com/e154/smart-home/models"
Expand All @@ -43,13 +45,23 @@ func TestEmail(t *testing.T) {

// register plugins
AddPlugin(adaptors, "notify")
AddPlugin(adaptors, "email")

settings := email.NewSettings()
settings[email.AttrAuth].Value = "XXX"
settings[email.AttrPass].Value = "XXX"
settings[email.AttrSmtp].Value = "XXX"
settings[email.AttrPort].Value = 123
settings[email.AttrSender].Value = "XXX"
AddPlugin(adaptors, "email", settings.Serialize())

sensorEnt := &m.Entity{
Id: common.EntityId("email.email"),
PluginName: "email",
AutoLoad: true,
}
sensorEnt.Settings = settings
err := adaptors.Entity.Add(context.Background(), sensorEnt)
ctx.So(err, ShouldBeNil)

supervisor.Start(context.Background())
WaitSupervisor(eventBus)
Expand All @@ -58,16 +70,16 @@ func TestEmail(t *testing.T) {
Convey("", t, func(ctx C) {

eventBus.Publish(notify.TopicNotify, notify.Message{
Type: email.Name,
EntityId: common.NewEntityId("email.email"),
Attributes: map[string]interface{}{
"addresses": "[email protected],[email protected]",
"subject": "subject",
"body": "body",
},
})

ok := WaitStateChanged(eventBus)
ctx.So(ok, ShouldBeTrue)
//todo: fix
time.Sleep(time.Millisecond * 500)

list, total, err := adaptors.MessageDelivery.List(context.Background(), 10, 0, "", "", nil)
ctx.So(err, ShouldBeNil)
Expand Down
39 changes: 16 additions & 23 deletions tests/plugins/messagebird/messagebird_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ package messagebird

import (
"context"
"github.com/e154/smart-home/common"
"testing"
"time"

"github.com/e154/smart-home/common/events"

"github.com/e154/smart-home/adaptors"
m "github.com/e154/smart-home/models"
"github.com/e154/smart-home/plugins/messagebird"
Expand All @@ -48,55 +47,49 @@ func TestMessagebird(t *testing.T) {

// register plugins
AddPlugin(adaptors, "notify")
AddPlugin(adaptors, "messagebird")

settings := messagebird.NewSettings()
settings[messagebird.AttrAccessKey].Value = "XXXX"
settings[messagebird.AttrName].Value = "YYYY"
AddPlugin(adaptors, "messagebird", settings.Serialize())

sensorEnt := &m.Entity{
Id: common.EntityId("messagebird.messagebird"),
PluginName: "messagebird",
AutoLoad: true,
}
sensorEnt.Settings = settings
err := adaptors.Entity.Add(context.Background(), sensorEnt)
ctx.So(err, ShouldBeNil)

supervisor.Start(context.Background())
WaitSupervisor(eventBus)

t.Run("succeed", func(t *testing.T) {
Convey("", t, func(ctx C) {

ch := make(chan interface{}, 1)
fn := func(topic string, message interface{}) {
switch v := message.(type) {
case events.EventStateChanged:
ch <- v
default:
}

}

eventBus.Subscribe("system/entities/+", fn)
defer eventBus.Unsubscribe("system/entities/+", fn)

const (
phone = "+79990000001"
body = "some text"
)

eventBus.Publish(notify.TopicNotify, notify.Message{
Type: messagebird.Name,
EntityId: common.NewEntityId("messagebird.messagebird"),
Attributes: map[string]interface{}{
messagebird.AttrPhone: phone,
messagebird.AttrBody: body,
},
})

ok := Wait(5, ch)

ctx.So(ok, ShouldBeTrue)

time.Sleep(time.Second * 2)
//todo: fix
time.Sleep(time.Millisecond * 100)

list, total, err := adaptors.MessageDelivery.List(context.Background(), 10, 0, "", "", nil)
ctx.So(err, ShouldBeNil)
ctx.So(total, ShouldEqual, 1)

del := list[0]
ctx.So(del.Status, ShouldEqual, m.MessageStatusSucceed)
ctx.So(del.Status, ShouldEqual, m.MessageStatusInProgress)
ctx.So(del.Address, ShouldEqual, phone)
ctx.So(del.ErrorMessageBody, ShouldBeNil)
ctx.So(del.ErrorMessageStatus, ShouldBeNil)
Expand Down
22 changes: 6 additions & 16 deletions tests/plugins/telegram/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package telegram

import (
"context"
"github.com/e154/smart-home/common"
"testing"
"time"

Expand Down Expand Up @@ -101,36 +102,25 @@ telegramAction = (entityId, actionName)->
t.Run("succeed", func(t *testing.T) {
Convey("", t, func(ctx C) {

ch := make(chan interface{}, 2)
fn := func(topic string, message interface{}) {
switch v := message.(type) {
case events.EventStateChanged:
ch <- v
default:
}

}
_ = eventBus.Subscribe("system/entities/+", fn)

time.Sleep(time.Millisecond * 500)

eventBus.Publish(notify.TopicNotify, notify.Message{
Type: telegram.Name,
EntityId: common.NewEntityId("telegram.clavicus"),
Attributes: map[string]interface{}{
"name": "clavicus",
"chat_id": 123,
"body": "body",
},
})

ok := Wait(3, ch)
ctx.So(ok, ShouldBeTrue)
//todo: fix
time.Sleep(time.Millisecond * 500)

list, total, err := adaptors.MessageDelivery.List(context.Background(), 10, 0, "", "", nil)
ctx.So(err, ShouldBeNil)
ctx.So(total, ShouldEqual, 1)

ctx.So(list[0].Status, ShouldEqual, m.MessageStatusSucceed)
ctx.So(list[0].Address, ShouldBeIn, []string{"clavicus"})
ctx.So(list[0].Address, ShouldEqual, "123")
ctx.So(list[0].ErrorMessageBody, ShouldBeNil)
ctx.So(list[0].ErrorMessageStatus, ShouldBeNil)
ctx.So(list[0].Message.Type, ShouldEqual, telegram.Name)
Expand Down
105 changes: 52 additions & 53 deletions tests/plugins/twilio/twilio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ package twilio
import (
"context"
"github.com/e154/smart-home/adaptors"
"github.com/e154/smart-home/common"
m "github.com/e154/smart-home/models"
"github.com/e154/smart-home/plugins/notify"
"github.com/e154/smart-home/plugins/twilio"
"github.com/e154/smart-home/system/bus"
"github.com/e154/smart-home/system/scripts"
"github.com/e154/smart-home/system/supervisor"
. "github.com/e154/smart-home/tests/plugins"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)

func TestTwilio(t *testing.T) {
Expand All @@ -40,67 +44,62 @@ func TestTwilio(t *testing.T) {

// register plugins
AddPlugin(adaptors, "notify")
AddPlugin(adaptors, "twilio")

settings := twilio.NewSettings()
settings[twilio.AttrAuthToken].Value = "XXXX"
settings[twilio.AttrSid].Value = "YYYY"
settings[twilio.AttrFrom].Value = "YYYY"
AddPlugin(adaptors, "twilio", settings.Serialize())

sensorEnt := &m.Entity{
Id: common.EntityId("twilio.twilio"),
PluginName: "twilio",
AutoLoad: true,
}
sensorEnt.Settings = settings
err := adaptors.Entity.Add(context.Background(), sensorEnt)
ctx.So(err, ShouldBeNil)

supervisor.Start(context.Background())
WaitSupervisor(eventBus)

//t.Run("succeed", func(t *testing.T) {
// Convey("", t, func(ctx C) {
//
// ch := make(chan interface{}, 1)
// fn := func(topic string, message interface{}) {
// switch v := message.(type) {
// case events.EventStateChanged:
// ch <- v
// default:
// }
//
// }
// _ = eventBus.Subscribe("system/entities/+", fn)
// defer func() { _ = eventBus.Unsubscribe("system/entities/+", fn) }()
//
// const (
// phone = "+79990000001"
// body = "some text"
// )
//
// eventBus.Publish(notify.TopicNotify, notify.Message{
// Type: twilio.Name,
// Attributes: map[string]interface{}{
// twilio.AttrPhone: phone,
// twilio.AttrBody: body,
// },
// })
//
// ok := Wait(5, ch)
//
// ctx.So(ok, ShouldBeTrue)
//
// time.Sleep(time.Second * 2)
//
// list, total, err := adaptors.MessageDelivery.List(context.Background(), 10, 0, "", "", nil)
// ctx.So(err, ShouldBeNil)
// ctx.So(total, ShouldEqual, 1)
//
// del := list[0]
// ctx.So(del.Status, ShouldEqual, m.MessageStatusSucceed)
// ctx.So(del.Address, ShouldEqual, phone)
// ctx.So(del.ErrorMessageBody, ShouldBeNil)
// ctx.So(del.ErrorMessageStatus, ShouldBeNil)
// ctx.So(del.Message.Type, ShouldEqual, twilio.Name)
//
// attr := twilio.NewMessageParams()
// _, _ = attr.Deserialize(del.Message.Attributes)
// ctx.So(attr[twilio.AttrPhone].String(), ShouldEqual, phone)
// ctx.So(attr[twilio.AttrBody].String(), ShouldEqual, body)
//
// })
//})
t.Run("succeed", func(t *testing.T) {
Convey("", t, func(ctx C) {

const (
phone = "+79990000001"
body = "some text"
)

eventBus.Publish(notify.TopicNotify, notify.Message{
EntityId: common.NewEntityId("twilio.twilio"),
Attributes: map[string]interface{}{
twilio.AttrPhone: phone,
twilio.AttrBody: body,
},
})

//todo: fix
time.Sleep(time.Millisecond * 500)

list, total, err := adaptors.MessageDelivery.List(context.Background(), 10, 0, "", "", nil)
ctx.So(err, ShouldBeNil)
ctx.So(total, ShouldEqual, 1)

del := list[0]
ctx.So(del.Status, ShouldEqual, m.MessageStatusInProgress)
ctx.So(del.Address, ShouldEqual, phone)
ctx.So(del.ErrorMessageBody, ShouldBeNil)
ctx.So(del.ErrorMessageStatus, ShouldBeNil)
ctx.So(del.Message.Type, ShouldEqual, twilio.Name)

attr := twilio.NewMessageParams()
_, _ = attr.Deserialize(del.Message.Attributes)
ctx.So(attr[twilio.AttrPhone].String(), ShouldEqual, phone)
ctx.So(attr[twilio.AttrBody].String(), ShouldEqual, body)

})
})

})
})
Expand Down
2 changes: 1 addition & 1 deletion tests/system/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestConfig(t *testing.T) {
ctx.So(conf.PgHost, ShouldEqual, "127.0.0.1")
ctx.So(conf.PgName, ShouldEqual, "smart_home_test")
ctx.So(conf.PgPort, ShouldEqual, "5432")
ctx.So(conf.PgDebug, ShouldEqual, true)
ctx.So(conf.PgDebug, ShouldEqual, false)
ctx.So(conf.PgMaxIdleConns, ShouldEqual, 10)
ctx.So(conf.PgMaxOpenConns, ShouldEqual, 50)
ctx.So(conf.PgConnMaxLifeTime, ShouldEqual, 30)
Expand Down

0 comments on commit 95b7a4a

Please sign in to comment.