github.com/twilio/twilio-go@v1.20.1/cluster_test.go (about)

     1  //go:build cluster
     2  // +build cluster
     3  
     4  package twilio
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	Api "github.com/twilio/twilio-go/rest/api/v2010"
    12  	ChatV2 "github.com/twilio/twilio-go/rest/chat/v2"
    13  	EventsV1 "github.com/twilio/twilio-go/rest/events/v1"
    14  )
    15  
    16  var from string
    17  var to string
    18  var testClient *RestClient
    19  
    20  func TestMain(m *testing.M) {
    21  	from = os.Getenv("TWILIO_FROM_NUMBER")
    22  	to = os.Getenv("TWILIO_TO_NUMBER")
    23  	var apiKey = os.Getenv("TWILIO_API_KEY")
    24  	var secret = os.Getenv("TWILIO_API_SECRET")
    25  	var accountSid = os.Getenv("TWILIO_ACCOUNT_SID")
    26  
    27  	testClient = NewRestClientWithParams(ClientParams{apiKey, secret, accountSid, nil})
    28  	ret := m.Run()
    29  	os.Exit(ret)
    30  }
    31  
    32  func TestSendingAText(t *testing.T) {
    33  	params := &Api.CreateMessageParams{}
    34  	params.SetTo(to)
    35  	params.SetFrom(from)
    36  	params.SetBody("Hello there")
    37  
    38  	resp, err := testClient.Api.CreateMessage(params)
    39  	assert.Nil(t, err)
    40  	assert.NotNil(t, resp)
    41  	assert.Equal(t, "Hello there", *resp.Body)
    42  	assert.Equal(t, from, *resp.From)
    43  	assert.Equal(t, to, *resp.To)
    44  }
    45  
    46  func TestListingNumbers(t *testing.T) {
    47  	resp, err := testClient.Api.ListIncomingPhoneNumber(nil)
    48  	assert.Nil(t, err)
    49  	assert.NotNil(t, resp)
    50  	// from, to numbers plus any other numbers that's configured for the account.
    51  	assert.GreaterOrEqual(t, len(resp), 2)
    52  }
    53  
    54  func TestListingANumber(t *testing.T) {
    55  	params := &Api.ListIncomingPhoneNumberParams{}
    56  	params.SetPhoneNumber(from)
    57  
    58  	resp, err := testClient.Api.ListIncomingPhoneNumber(params)
    59  	assert.Nil(t, err)
    60  	assert.NotNil(t, resp)
    61  	assert.Equal(t, 1, len(resp))
    62  	assert.Equal(t, from, *resp[0].PhoneNumber)
    63  }
    64  
    65  func TestSpecialCharacters(t *testing.T) {
    66  	serviceParams := &ChatV2.CreateServiceParams{}
    67  	serviceParams.SetFriendlyName("service|friendly&name")
    68  
    69  	service, err := testClient.ChatV2.CreateService(serviceParams)
    70  	assert.Nil(t, err)
    71  	assert.NotNil(t, service)
    72  
    73  	userParams := &ChatV2.CreateUserParams{}
    74  	userParams.SetIdentity("user|identity&string")
    75  
    76  	user, err := testClient.ChatV2.CreateUser(*service.Sid, userParams)
    77  	assert.Nil(t, err)
    78  	assert.NotNil(t, user)
    79  
    80  	err = testClient.ChatV2.DeleteUser(*service.Sid, *user.Identity)
    81  	assert.Nil(t, err)
    82  	err = testClient.ChatV2.DeleteService(*service.Sid)
    83  	assert.Nil(t, err)
    84  }
    85  
    86  func TestListParams(t *testing.T) {
    87  	sinkConfig := map[string]interface{}{
    88  		"destination":  "http://example.org/webhook",
    89  		"method":       "post",
    90  		"batch_events": false,
    91  	}
    92  	sinkParams := &EventsV1.CreateSinkParams{}
    93  	sinkParams.SetSinkConfiguration(sinkConfig)
    94  	sinkParams.SetDescription("test sink")
    95  	sinkParams.SetSinkType("webhook")
    96  	sink, err := testClient.EventsV1.CreateSink(sinkParams)
    97  	assert.Nil(t, err)
    98  	assert.NotNil(t, sink)
    99  
   100  	types := []interface{}{
   101  		map[string]interface{}{
   102  			"type": "com.twilio.messaging.message.delivered",
   103  		},
   104  		map[string]interface{}{
   105  			"type": "com.twilio.messaging.message.sent",
   106  		},
   107  	}
   108  	subscriptionsParams := &EventsV1.CreateSubscriptionParams{}
   109  	subscriptionsParams.SetSinkSid(*sink.Sid)
   110  	subscriptionsParams.SetTypes(types)
   111  	subscriptionsParams.SetDescription("test subscription")
   112  	subscription, err := testClient.EventsV1.CreateSubscription(subscriptionsParams)
   113  	assert.Nil(t, err)
   114  	assert.NotNil(t, subscription)
   115  
   116  	// Clean up the resources we've created
   117  	err = testClient.EventsV1.DeleteSubscription(*subscription.Sid)
   118  	assert.Nil(t, err)
   119  	err = testClient.EventsV1.DeleteSink(*sink.Sid)
   120  	assert.Nil(t, err)
   121  }
   122  
   123  func TestListingAvailableNumber(t *testing.T) {
   124  	params := &Api.ListAvailablePhoneNumberTollFreeParams{}
   125  	params.SetLimit(2)
   126  
   127  	resp, err := testClient.Api.ListAvailablePhoneNumberTollFree("US", params)
   128  	assert.Nil(t, err)
   129  	assert.NotNil(t, resp)
   130  	assert.Equal(t, 2, len(resp))
   131  }