1
0
mirror of https://github.com/nikoksr/notify.git synced 2024-11-28 08:39:13 +02:00
notify/service/plivo/plivo_test.go
renovate[bot] 78f30df750
fix(deps): update module github.com/plivo/plivo-go to v7.8.0 (#224)
Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Niko Köser <koeserniko@gmail.com>
2022-03-25 08:10:28 +01:00

81 lines
1.9 KiB
Go

package plivo
import (
"context"
"errors"
"testing"
plivo "github.com/plivo/plivo-go/v7"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
assert := require.New(t)
// nil ClientOptions
svc, err := New(nil, &MessageOptions{})
assert.NotNil(err)
assert.Nil(svc)
// nil MessageOptions
svc, err = New(&ClientOptions{}, nil)
assert.NotNil(err)
assert.Nil(svc)
// empty source
svc, err = New(&ClientOptions{}, &MessageOptions{})
assert.NotNil(err)
assert.Nil(svc)
// success
svc, err = New(&ClientOptions{}, &MessageOptions{Source: "12345"})
assert.Nil(err)
assert.NotNil(svc)
}
func TestAddReceivers(t *testing.T) {
assert := require.New(t)
svc, err := New(&ClientOptions{}, &MessageOptions{Source: "12345"})
assert.Nil(err)
assert.NotNil(svc)
nums := []string{"1", "2", "3", "4", "5"}
svc.AddReceivers(nums...)
assert.Equal(svc.destinations, nums)
}
func TestSend(t *testing.T) {
assert := require.New(t)
svc, err := New(&ClientOptions{}, &MessageOptions{Source: "12345"})
assert.Nil(err)
assert.NotNil(svc)
// no receivers added
ctx := context.Background()
err = svc.Send(ctx, "message", "test")
assert.NotNil(err)
// test plivo client returning error
mockClient := new(mockPlivoMsgClient)
mockClient.On("Create", plivo.MessageCreateParams{Src: "12345", Dst: "67890", Text: "message\ntest"}).
Return(nil, errors.New("some error"))
svc.client = mockClient
svc.AddReceivers("67890")
err = svc.Send(ctx, "message", "test")
assert.NotNil(err)
mockClient.AssertExpectations(t)
// test success and multiple receivers
mockClient = new(mockPlivoMsgClient)
mockClient.On("Create", plivo.MessageCreateParams{Src: "12345", Dst: "67890<09876", Text: "message\ntest"}).
Return(nil, nil)
svc.client = mockClient
svc.AddReceivers("09876")
err = svc.Send(ctx, "message", "test")
assert.Nil(err)
mockClient.AssertExpectations(t)
}