github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/email/email_test.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package email 20 21 import ( 22 "context" 23 "testing" 24 "time" 25 26 . "github.com/smartystreets/goconvey/convey" 27 28 "github.com/e154/smart-home/adaptors" 29 "github.com/e154/smart-home/common" 30 m "github.com/e154/smart-home/models" 31 "github.com/e154/smart-home/plugins/email" 32 "github.com/e154/smart-home/plugins/notify" 33 notifyCommon "github.com/e154/smart-home/plugins/notify/common" 34 "github.com/e154/smart-home/system/bus" 35 "github.com/e154/smart-home/system/scripts" 36 "github.com/e154/smart-home/system/supervisor" 37 . "github.com/e154/smart-home/tests/plugins" 38 ) 39 40 func TestEmail(t *testing.T) { 41 42 Convey("email", t, func(ctx C) { 43 _ = container.Invoke(func(adaptors *adaptors.Adaptors, 44 scriptService scripts.ScriptService, 45 supervisor supervisor.Supervisor, 46 eventBus bus.Bus) { 47 48 // register plugins 49 AddPlugin(adaptors, "notify") 50 AddPlugin(adaptors, "email") 51 52 settings := email.NewSettings() 53 settings[email.AttrAuth].Value = "XXX" 54 settings[email.AttrPass].Value = "XXX" 55 settings[email.AttrSmtp].Value = "XXX" 56 settings[email.AttrPort].Value = 123 57 settings[email.AttrSender].Value = "XXX" 58 59 sensorEnt := &m.Entity{ 60 Id: common.EntityId("email.email"), 61 PluginName: "email", 62 AutoLoad: true, 63 } 64 sensorEnt.Settings = settings 65 err := adaptors.Entity.Add(context.Background(), sensorEnt) 66 ctx.So(err, ShouldBeNil) 67 68 serviceCh := WaitService(eventBus, time.Second*5, "Supervisor") 69 pluginsCh := WaitPlugins(eventBus, time.Second*5, "notify", "email") 70 supervisor.Start(context.Background()) 71 defer supervisor.Shutdown(context.Background()) 72 So(<-serviceCh, ShouldBeTrue) 73 So(<-pluginsCh, ShouldBeTrue) 74 75 t.Run("succeed", func(t *testing.T) { 76 Convey("", t, func(ctx C) { 77 78 eventBus.Publish(notify.TopicNotify, notifyCommon.Message{ 79 EntityId: common.NewEntityId("email.email"), 80 Attributes: map[string]interface{}{ 81 "addresses": "test@e154.ru,test2@e154.ru", 82 "subject": "subject", 83 "body": "body", 84 }, 85 }) 86 87 //todo: fix 88 time.Sleep(time.Millisecond * 500) 89 90 list, total, err := adaptors.MessageDelivery.List(context.Background(), 10, 0, "", "", nil) 91 ctx.So(err, ShouldBeNil) 92 ctx.So(total, ShouldEqual, 2) 93 94 for _, del := range list { 95 ctx.So(del.Status, ShouldEqual, m.MessageStatusSucceed) 96 ctx.So(del.Address, ShouldBeIn, []string{"test@e154.ru", "test2@e154.ru"}) 97 ctx.So(del.ErrorMessageBody, ShouldBeNil) 98 ctx.So(del.ErrorMessageStatus, ShouldBeNil) 99 ctx.So(del.Message.Type, ShouldEqual, email.Name) 100 101 attr := email.NewMessageParams() 102 _, _ = attr.Deserialize(del.Message.Attributes) 103 ctx.So(attr[email.AttrSubject].String(), ShouldEqual, "subject") 104 ctx.So(attr[email.AttrBody].String(), ShouldEqual, "body") 105 } 106 }) 107 }) 108 }) 109 }) 110 }