statping/core/notifier/notifiers_test.go

130 lines
3.2 KiB
Go
Raw Normal View History

2018-09-12 04:14:22 +00:00
// +build bypass
2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// 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/>.
2018-09-12 04:14:22 +00:00
package notifier
2018-07-27 04:45:42 +00:00
import (
2018-09-12 04:14:22 +00:00
"github.com/hunterlong/statup/source"
2018-07-27 04:45:42 +00:00
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
2018-07-27 04:45:42 +00:00
"github.com/stretchr/testify/assert"
"testing"
)
var (
2018-09-12 04:14:22 +00:00
dir string
EXAMPLE_ID = "example"
2018-07-27 04:45:42 +00:00
)
func init() {
2018-08-20 03:48:28 +00:00
dir = utils.Directory
2018-07-27 04:45:42 +00:00
}
func injectDatabase() {
2018-09-12 04:14:22 +00:00
db, _ = gorm.Open("sqlite3", dir+"/statup.db")
2018-07-27 04:45:42 +00:00
}
2018-09-12 04:14:22 +00:00
func TestLoad(t *testing.T) {
source.Assets()
utils.InitLogs()
2018-07-27 04:45:42 +00:00
injectDatabase()
2018-09-12 04:14:22 +00:00
AllCommunications = Load()
assert.Len(t, AllCommunications, 1)
2018-07-27 04:45:42 +00:00
}
func TestIsInDatabase(t *testing.T) {
2018-09-12 04:14:22 +00:00
in := example.isInDatabase()
assert.True(t, in)
2018-07-27 04:45:42 +00:00
}
func TestInsertDatabase(t *testing.T) {
2018-09-12 04:14:22 +00:00
_, err := insertDatabase(example.Notification)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
2018-09-12 04:14:22 +00:00
assert.NotZero(t, example.Id)
2018-07-27 04:45:42 +00:00
2018-09-12 04:14:22 +00:00
in := example.isInDatabase()
2018-07-27 04:45:42 +00:00
assert.True(t, in)
}
func TestSelectNotification(t *testing.T) {
2018-09-12 04:14:22 +00:00
notifier, err := SelectNotification(EXAMPLE_ID)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
2018-09-12 04:14:22 +00:00
assert.Equal(t, "example", notifier.Method)
2018-07-27 04:45:42 +00:00
assert.False(t, notifier.Enabled)
}
func TestNotification_Update(t *testing.T) {
2018-09-12 04:14:22 +00:00
notifier, err := SelectNotification(EXAMPLE_ID)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
2018-09-12 04:14:22 +00:00
notifier.Host = "new host here"
2018-07-27 04:45:42 +00:00
notifier.Enabled = true
updated, err := notifier.Update()
assert.Nil(t, err)
2018-09-12 04:14:22 +00:00
selected, err := SelectNotification(updated.Method)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
2018-09-12 04:14:22 +00:00
assert.Equal(t, "new host here", selected.GetValue("host"))
2018-07-27 04:45:42 +00:00
assert.True(t, selected.Enabled)
}
func TestNotification_GetValue(t *testing.T) {
2018-09-12 04:14:22 +00:00
notifier, err := SelectNotification(EXAMPLE_ID)
2018-07-27 04:45:42 +00:00
assert.Nil(t, err)
val := notifier.GetValue("Host")
2018-09-12 04:14:22 +00:00
assert.Equal(t, "http://exmaplehost.com", val)
2018-07-27 04:45:42 +00:00
}
2018-09-12 04:14:22 +00:00
//func TestRun(t *testing.T) {
// err := example.Run()
// assert.Equal(t, "running", err.Error())
//}
2018-07-27 04:45:42 +00:00
2018-09-12 04:14:22 +00:00
//func TestTestIt(t *testing.T) {
// err := example.Test()
// assert.Equal(t, "testing", err.Error())
//}
2018-07-27 04:45:42 +00:00
func TestOnSuccess(t *testing.T) {
s := &types.Service{
Name: "Interpol - All The Rage Back Home",
Domain: "https://www.youtube.com/watch?v=-u6DvRyyKGU",
ExpectedStatus: 200,
Interval: 30,
Type: "http",
Method: "GET",
Timeout: 20,
}
OnSuccess(s)
}
func TestOnFailure(t *testing.T) {
s := &types.Service{
Name: "Interpol - All The Rage Back Home",
Domain: "https://www.youtube.com/watch?v=-u6DvRyyKGU",
ExpectedStatus: 200,
Interval: 30,
Type: "http",
Method: "GET",
Timeout: 20,
}
f := &types.Failure{
Issue: "testing",
}
OnFailure(s, f)
2018-07-27 04:45:42 +00:00
}