statping/notifiers/twilio_test.go

91 lines
2.4 KiB
Go

// Statping
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statping
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package notifiers
import (
"github.com/statping/statping/database"
"github.com/statping/statping/types/notifications"
"github.com/statping/statping/types/null"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
)
var (
TWILIO_SID = os.Getenv("TWILIO_SID")
TWILIO_SECRET = os.Getenv("TWILIO_SECRET")
TWILIO_FROM = os.Getenv("TWILIO_FROM")
TWILIO_TO = os.Getenv("TWILIO_TO")
)
func init() {
TWILIO_SID = os.Getenv("TWILIO_SID")
TWILIO_SECRET = os.Getenv("TWILIO_SECRET")
TWILIO_FROM = os.Getenv("TWILIO_FROM")
TWILIO_TO = os.Getenv("TWILIO_TO")
}
func TestTwilioNotifier(t *testing.T) {
t.SkipNow()
db, err := database.OpenTester()
require.Nil(t, err)
db.AutoMigrate(&notifications.Notification{})
notifications.SetDB(db)
if TWILIO_SID == "" || TWILIO_SECRET == "" || TWILIO_FROM == "" {
t.Log("twilio notifier testing skipped, missing TWILIO_SID environment variable")
t.SkipNow()
}
t.Run("Load Twilio", func(t *testing.T) {
Twilio.ApiKey = TWILIO_SID
Twilio.ApiSecret = TWILIO_SECRET
Twilio.Var1 = TWILIO_TO
Twilio.Var2 = TWILIO_FROM
Twilio.Delay = time.Duration(100 * time.Millisecond)
Twilio.Enabled = null.NewNullBool(true)
Add(Twilio)
assert.Nil(t, err)
assert.Equal(t, "Hunter Long", Twilio.Author)
assert.Equal(t, TWILIO_SID, Twilio.ApiKey)
})
t.Run("Twilio Within Limits", func(t *testing.T) {
assert.True(t, Twilio.CanSend())
})
t.Run("Twilio OnFailure", func(t *testing.T) {
err := Twilio.OnFailure(exampleService, exampleFailure)
assert.Nil(t, err)
})
t.Run("Twilio OnSuccess", func(t *testing.T) {
err := Twilio.OnSuccess(exampleService)
assert.Nil(t, err)
})
t.Run("Twilio Test", func(t *testing.T) {
err := Twilio.OnTest()
assert.Nil(t, err)
})
}