github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/plugins/messagebird/messagebird_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 messagebird
    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/messagebird"
    33  	"github.com/e154/smart-home/plugins/notify"
    34  	"github.com/e154/smart-home/system/bus"
    35  	"github.com/e154/smart-home/system/migrations"
    36  	"github.com/e154/smart-home/system/scripts"
    37  	"github.com/e154/smart-home/system/supervisor"
    38  	. "github.com/e154/smart-home/tests/plugins"
    39  )
    40  
    41  func TestMessagebird(t *testing.T) {
    42  
    43  	Convey("messagbird", t, func(ctx C) {
    44  		_ = container.Invoke(func(adaptors *adaptors.Adaptors,
    45  			migrations *migrations.Migrations,
    46  			scriptService scripts.ScriptService,
    47  			supervisor supervisor.Supervisor,
    48  			eventBus bus.Bus) {
    49  
    50  			// register plugins
    51  			AddPlugin(adaptors, "notify")
    52  			AddPlugin(adaptors, "messagebird")
    53  
    54  			settings := messagebird.NewSettings()
    55  			settings[messagebird.AttrAccessKey].Value = "XXXX"
    56  			settings[messagebird.AttrName].Value = "YYYY"
    57  
    58  			sensorEnt := &m.Entity{
    59  				Id:         common.EntityId("messagebird.messagebird"),
    60  				PluginName: "messagebird",
    61  				AutoLoad:   true,
    62  			}
    63  			sensorEnt.Settings = settings
    64  			err := adaptors.Entity.Add(context.Background(), sensorEnt)
    65  			ctx.So(err, ShouldBeNil)
    66  
    67  			serviceCh := WaitService(eventBus, time.Second*5, "Supervisor")
    68  			pluginsCh := WaitPlugins(eventBus, time.Second*5, "notify", "messagebird")
    69  			supervisor.Start(context.Background())
    70  			defer supervisor.Shutdown(context.Background())
    71  			So(<-serviceCh, ShouldBeTrue)
    72  			So(<-pluginsCh, ShouldBeTrue)
    73  
    74  			t.Run("succeed", func(t *testing.T) {
    75  				Convey("", t, func(ctx C) {
    76  
    77  					const (
    78  						phone = "+79990000001"
    79  						body  = "some text"
    80  					)
    81  
    82  					eventBus.Publish(notify.TopicNotify, notifyCommon.Message{
    83  						EntityId: common.NewEntityId("messagebird.messagebird"),
    84  						Attributes: map[string]interface{}{
    85  							messagebird.AttrPhone: phone,
    86  							messagebird.AttrBody:  body,
    87  						},
    88  					})
    89  
    90  					//todo: fix
    91  					time.Sleep(time.Millisecond * 100)
    92  
    93  					list, total, err := adaptors.MessageDelivery.List(context.Background(), 10, 0, "", "", nil)
    94  					ctx.So(err, ShouldBeNil)
    95  					ctx.So(total, ShouldEqual, 1)
    96  
    97  					del := list[0]
    98  					ctx.So(del.Status, ShouldEqual, m.MessageStatusInProgress)
    99  					ctx.So(del.Address, ShouldEqual, phone)
   100  					ctx.So(del.ErrorMessageBody, ShouldBeNil)
   101  					ctx.So(del.ErrorMessageStatus, ShouldBeNil)
   102  					ctx.So(del.Message.Type, ShouldEqual, messagebird.Name)
   103  
   104  					attr := messagebird.NewMessageParams()
   105  					_, _ = attr.Deserialize(del.Message.Attributes)
   106  					ctx.So(attr[messagebird.AttrPhone].String(), ShouldEqual, phone)
   107  					ctx.So(attr[messagebird.AttrBody].String(), ShouldEqual, body)
   108  
   109  				})
   110  			})
   111  
   112  		})
   113  	})
   114  }