mirror of https://github.com/statping/statping
notifier testing - JS updates
parent
0f208c979f
commit
2385cf88e1
|
@ -103,13 +103,6 @@ func (n *Example) OnSave() error {
|
|||
return errors.New("onsave triggered")
|
||||
}
|
||||
|
||||
// REQUIRED
|
||||
func (n *Example) Test() error {
|
||||
msg := fmt.Sprintf("received a test trigger\n")
|
||||
n.AddQueue(msg)
|
||||
return errors.New("test triggered")
|
||||
}
|
||||
|
||||
// REQUIRED - BASIC EVENT
|
||||
func (n *Example) OnSuccess(s *types.Service) {
|
||||
msg := fmt.Sprintf("received a count trigger for service: %v\n", s.Name)
|
||||
|
@ -123,9 +116,9 @@ func (n *Example) OnFailure(s *types.Service, f *types.Failure) {
|
|||
}
|
||||
|
||||
// OPTIONAL Test function before user saves
|
||||
func (n *Example) OnTest(s Notification) (bool, error) {
|
||||
fmt.Printf("received a test trigger with form data: %v\n", s.Host)
|
||||
return true, nil
|
||||
func (n *Example) OnTest() error {
|
||||
fmt.Printf("received a test trigger with form data: %v\n", n.Host)
|
||||
return nil
|
||||
}
|
||||
|
||||
// OPTIONAL
|
||||
|
|
|
@ -57,8 +57,8 @@ type Notification struct {
|
|||
Delay time.Duration `gorm:"-" json:"-"`
|
||||
Queue []interface{} `gorm:"-" json:"-"`
|
||||
Running chan bool `gorm:"-" json:"-"`
|
||||
CanTest bool `gorm:"-" json:"-"`
|
||||
Online bool `gorm:"-" json:"-"`
|
||||
testable bool
|
||||
}
|
||||
|
||||
type NotificationForm struct {
|
||||
|
@ -80,6 +80,10 @@ func (n *Notification) AddQueue(msg interface{}) {
|
|||
n.Queue = append(n.Queue, msg)
|
||||
}
|
||||
|
||||
func (n *Notification) CanTest() bool {
|
||||
return n.testable
|
||||
}
|
||||
|
||||
// db will return the notifier database column/record
|
||||
func modelDb(n *Notification) *gorm.DB {
|
||||
return db.Model(&Notification{}).Where("method = ?", n.Method).Find(n)
|
||||
|
@ -233,6 +237,7 @@ func Init(n Notifier) (*Notification, error) {
|
|||
var notify *Notification
|
||||
if err == nil {
|
||||
notify, _ = SelectNotification(n)
|
||||
notify.testable = isType(n, new(Tester))
|
||||
notify.Form = n.Select().Form
|
||||
}
|
||||
return notify, err
|
||||
|
|
|
@ -217,13 +217,15 @@ func testNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
|||
apiSecret := form.Get("api_secret")
|
||||
limits := int(utils.StringInt(form.Get("limits")))
|
||||
|
||||
notifer, notif, err := notifier.SelectNotifier(method)
|
||||
fakeNotifer, notif, err := notifier.SelectNotifier(method)
|
||||
if err != nil {
|
||||
utils.Log(3, fmt.Sprintf("issue saving notifier %v: %v", method, err))
|
||||
executeResponse(w, r, "settings.html", core.CoreApp, "/settings")
|
||||
return
|
||||
}
|
||||
|
||||
notifer := *fakeNotifer
|
||||
|
||||
if host != "" {
|
||||
notifer.Host = host
|
||||
}
|
||||
|
@ -254,7 +256,6 @@ func testNotificationHandler(w http.ResponseWriter, r *http.Request) {
|
|||
notifer.Enabled = enabled == "on"
|
||||
|
||||
err = notif.(notifier.Tester).OnTest()
|
||||
|
||||
if err == nil {
|
||||
w.Write([]byte("ok"))
|
||||
} else {
|
||||
|
|
|
@ -17,9 +17,12 @@ package notifiers
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/hunterlong/statup/core/notifier"
|
||||
"github.com/hunterlong/statup/types"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
@ -91,3 +94,36 @@ func (u *Discord) OnSave() error {
|
|||
u.AddQueue(msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
// OnSave triggers when this notifier has been saved
|
||||
func (u *Discord) OnTest() error {
|
||||
outError := errors.New("Incorrect Discord URL, please confirm URL is correct")
|
||||
message := `{"content": "Testing the Discord notifier"}`
|
||||
req, _ := http.NewRequest("POST", discorder.Host, bytes.NewBuffer([]byte(message)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
contents, _ := ioutil.ReadAll(resp.Body)
|
||||
if string(contents) == "" {
|
||||
return nil
|
||||
}
|
||||
var d discordTestJson
|
||||
err = json.Unmarshal(contents, &d)
|
||||
if err != nil {
|
||||
return outError
|
||||
}
|
||||
if d.Code == 0 {
|
||||
return outError
|
||||
}
|
||||
fmt.Println("discord: ", string(contents))
|
||||
return nil
|
||||
}
|
||||
|
||||
type discordTestJson struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
|
|
@ -54,6 +54,10 @@ func TestDiscordNotifier(t *testing.T) {
|
|||
notifier.Load()
|
||||
})
|
||||
|
||||
t.Run("Discord Notifier Tester", func(t *testing.T) {
|
||||
assert.True(t, discorder.CanTest())
|
||||
})
|
||||
|
||||
t.Run("Discord Within Limits", func(t *testing.T) {
|
||||
ok, err := discorder.WithinLimits()
|
||||
assert.Nil(t, err)
|
||||
|
|
|
@ -46,7 +46,6 @@ var slacker = &Slack{¬ifier.Notification{
|
|||
AuthorUrl: "https://github.com/hunterlong",
|
||||
Delay: time.Duration(10 * time.Second),
|
||||
Host: "https://webhooksurl.slack.com/***",
|
||||
CanTest: true,
|
||||
Form: []notifier.NotificationForm{{
|
||||
Type: "text",
|
||||
Title: "Incoming Webhook Url",
|
||||
|
@ -107,7 +106,7 @@ func (u *Slack) OnTest() error {
|
|||
defer res.Body.Close()
|
||||
contents, _ := ioutil.ReadAll(res.Body)
|
||||
if string(contents) != "ok" {
|
||||
return errors.New("incorrect url")
|
||||
return errors.New("The Slack response was incorrect, check the URL")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -61,6 +61,10 @@ func TestSlackNotifier(t *testing.T) {
|
|||
notifier.Load()
|
||||
})
|
||||
|
||||
t.Run("Slack Notifier Tester", func(t *testing.T) {
|
||||
assert.True(t, slacker.CanTest())
|
||||
})
|
||||
|
||||
t.Run("Slack parse message", func(t *testing.T) {
|
||||
err := parseSlackMessage(SLACK_TEXT, "this is a test!")
|
||||
assert.Nil(t, err)
|
||||
|
|
|
@ -24,20 +24,31 @@ $('.service_li').on('click', function() {
|
|||
});
|
||||
|
||||
$('.test_notifier').on('click', function(e) {
|
||||
var btn = $(this);
|
||||
var form = $(this).parents('form:first');
|
||||
var values = form.serialize();
|
||||
var notifier = form.find('input[name=notifier]').val();
|
||||
var success = $('#'+notifier+'-success');
|
||||
var error = $('#'+notifier+'-error');
|
||||
btn.prop("disabled", true);
|
||||
$.ajax({
|
||||
url: form.attr("action")+"/test",
|
||||
type: 'POST',
|
||||
data: values,
|
||||
success: function(data) {
|
||||
if (data === 'ok') {
|
||||
$('#'+notifier+'-success').removeClass('d-none');
|
||||
success.removeClass('d-none');
|
||||
setTimeout(function() {
|
||||
success.addClass('d-none');
|
||||
}, 5000)
|
||||
} else {
|
||||
$('#'+notifier+'-error').removeClass('d-none');
|
||||
$('#'+notifier+'-error').html(data);
|
||||
error.removeClass('d-none');
|
||||
error.html(data);
|
||||
setTimeout(function() {
|
||||
error.addClass('d-none');
|
||||
}, 8000)
|
||||
}
|
||||
btn.prop("disabled", false);
|
||||
}
|
||||
});
|
||||
e.preventDefault();
|
||||
|
|
|
@ -233,8 +233,6 @@
|
|||
The {{$n.Method}} notifier is working correctly!
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
</div>
|
||||
|
|
Loading…
Reference in New Issue