github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/twilio/twilio_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 twilio
    20  
    21  import (
    22  	"context"
    23  	"testing"
    24  	"time"
    25  
    26  	notifyCommon "github.com/e154/smart-home/plugins/notify/common"
    27  	. "github.com/smartystreets/goconvey/convey"
    28  
    29  	"github.com/e154/smart-home/adaptors"
    30  	"github.com/e154/smart-home/common"
    31  	m "github.com/e154/smart-home/models"
    32  	"github.com/e154/smart-home/plugins/notify"
    33  	"github.com/e154/smart-home/plugins/twilio"
    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 TestTwilio(t *testing.T) {
    41  
    42  	Convey("twilio", 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, "twilio")
    51  
    52  			settings := twilio.NewSettings()
    53  			settings[twilio.AttrAuthToken].Value = "XXXX"
    54  			settings[twilio.AttrSid].Value = "YYYY"
    55  			settings[twilio.AttrFrom].Value = "YYYY"
    56  
    57  			sensorEnt := &m.Entity{
    58  				Id:         common.EntityId("twilio.twilio"),
    59  				PluginName: "twilio",
    60  				AutoLoad:   true,
    61  			}
    62  			sensorEnt.Settings = settings
    63  			err := adaptors.Entity.Add(context.Background(), sensorEnt)
    64  			ctx.So(err, ShouldBeNil)
    65  
    66  			serviceCh := WaitService(eventBus, time.Second*5, "Supervisor")
    67  			pluginsCh := WaitPlugins(eventBus, time.Second*5, "twilio", "notify")
    68  			supervisor.Start(context.Background())
    69  			defer supervisor.Shutdown(context.Background())
    70  			So(<-serviceCh, ShouldBeTrue)
    71  			So(<-pluginsCh, ShouldBeTrue)
    72  
    73  			t.Run("succeed", func(t *testing.T) {
    74  				Convey("", t, func(ctx C) {
    75  
    76  					const (
    77  						phone = "+79990000001"
    78  						body  = "some text"
    79  					)
    80  
    81  					eventBus.Publish(notify.TopicNotify, notifyCommon.Message{
    82  						EntityId: common.NewEntityId("twilio.twilio"),
    83  						Attributes: map[string]interface{}{
    84  							twilio.AttrPhone: phone,
    85  							twilio.AttrBody:  body,
    86  						},
    87  					})
    88  
    89  					//todo: fix
    90  					time.Sleep(time.Millisecond * 1000)
    91  
    92  					list, total, err := adaptors.MessageDelivery.List(context.Background(), 10, 0, "", "", nil)
    93  					ctx.So(err, ShouldBeNil)
    94  					ctx.So(total, ShouldEqual, 1)
    95  
    96  					del := list[0]
    97  					//ctx.So(del.Status, ShouldEqual, m.MessageStatusInProgress)
    98  					ctx.So(del.Address, ShouldEqual, phone)
    99  					ctx.So(del.ErrorMessageBody, ShouldBeNil)
   100  					ctx.So(del.ErrorMessageStatus, ShouldBeNil)
   101  					ctx.So(del.Message.Type, ShouldEqual, twilio.Name)
   102  
   103  					attr := twilio.NewMessageParams()
   104  					_, _ = attr.Deserialize(del.Message.Attributes)
   105  					ctx.So(attr[twilio.AttrPhone].String(), ShouldEqual, phone)
   106  					ctx.So(attr[twilio.AttrBody].String(), ShouldEqual, body)
   107  
   108  				})
   109  			})
   110  
   111  		})
   112  	})
   113  }