diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml index 4643e513..e1de9143 100644 --- a/.github/workflows/dev.yml +++ b/.github/workflows/dev.yml @@ -135,6 +135,7 @@ jobs: TWILIO_SECRET: ${{ secrets.TWILIO_SECRET }} TWILIO_FROM: ${{ secrets.TWILIO_FROM }} TWILIO_TO: ${{ secrets.TWILIO_TO }} + TEST_EMAIL: ${{ secrets.TEST_EMAIL }} - name: Coveralls Testing Coverage run: | diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index b718c73e..38fbb862 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -135,6 +135,7 @@ jobs: TWILIO_SECRET: ${{ secrets.TWILIO_SECRET }} TWILIO_FROM: ${{ secrets.TWILIO_FROM }} TWILIO_TO: ${{ secrets.TWILIO_TO }} + TEST_EMAIL: ${{ secrets.TEST_EMAIL }} - name: Coveralls Testing Coverage run: | diff --git a/CHANGELOG.md b/CHANGELOG.md index b99df090..4c830809 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # 0.90.55 (06-18-2020) - Added 404 page - Modified Statping's PR process, dev -> master +- Fixed Discord notifier +- Modified email template for SMTP emails +- Added OnSave() method for all notifiers # 0.90.54 (06-17-2020) - Fixed Slack Notifier's failure/success data saving issue diff --git a/handlers/notifications.go b/handlers/notifications.go index 9fcccee1..daa82de7 100644 --- a/handlers/notifications.go +++ b/handlers/notifications.go @@ -53,7 +53,13 @@ func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) { sendErrorJson(err, w, r) return } - //notifications.OnSave(notifer.Method) + + notif := services.ReturnNotifier(notifer.Method) + if _, err := notif.OnSave(); err != nil { + sendErrorJson(err, w, r) + return + } + sendJsonAction(vars["notifier"], "update", w, r) } diff --git a/notifiers/command.go b/notifiers/command.go index 9c3cbfc5..6e1e5c68 100644 --- a/notifiers/command.go +++ b/notifiers/command.go @@ -70,3 +70,8 @@ func (c *commandLine) OnTest() (string, error) { utils.Log.Infoln(out) return out, err } + +// OnSave will trigger when this notifier is saved +func (c *commandLine) OnSave() (string, error) { + return "", nil +} diff --git a/notifiers/discord.go b/notifiers/discord.go index 76892ea7..1e6b37f5 100644 --- a/notifiers/discord.go +++ b/notifiers/discord.go @@ -81,6 +81,11 @@ func (d *discord) OnTest() (string, error) { return string(contents), nil } +// OnSave will trigger when this notifier is saved +func (d *discord) OnSave() (string, error) { + return "", nil +} + type discordTestJson struct { Code int `json:"code"` Message string `json:"message"` diff --git a/notifiers/email.go b/notifiers/email.go index c9e40c85..640a796b 100644 --- a/notifiers/email.go +++ b/notifiers/email.go @@ -148,6 +148,11 @@ func (e *emailer) OnTest() (string, error) { return subject, e.dialSend(email) } +// OnSave will trigger when this notifier is saved +func (e *emailer) OnSave() (string, error) { + return "", nil +} + func (e *emailer) dialSend(email *emailOutgoing) error { mailer = mail.NewDialer(e.Host, e.Port, e.Username, e.Password) m := mail.NewMessage() diff --git a/notifiers/line_notify.go b/notifiers/line_notify.go index db2dc7ec..b432517d 100644 --- a/notifiers/line_notify.go +++ b/notifiers/line_notify.go @@ -71,3 +71,8 @@ func (l *lineNotifier) OnTest() (string, error) { _, err := l.sendMessage(msg) return msg, err } + +// OnSave will trigger when this notifier is saved +func (l *lineNotifier) OnSave() (string, error) { + return "", nil +} diff --git a/notifiers/mobile.go b/notifiers/mobile.go index 6ab82e31..ab05d80f 100644 --- a/notifiers/mobile.go +++ b/notifiers/mobile.go @@ -129,6 +129,11 @@ func (m *mobilePush) Send(pushMessage *pushArray) error { return nil } +// OnSave will trigger when this notifier is saved +func (m *mobilePush) OnSave() (string, error) { + return "", nil +} + func pushRequest(msg *pushArray) ([]byte, error) { body, err := json.Marshal(&PushNotification{[]*pushArray{msg}}) if err != nil { diff --git a/notifiers/pushover.go b/notifiers/pushover.go index 0fe213f6..f480a3ad 100644 --- a/notifiers/pushover.go +++ b/notifiers/pushover.go @@ -90,3 +90,8 @@ func (t *pushover) OnTest() (string, error) { content, err := t.sendMessage(msg) return content, err } + +// OnSave will trigger when this notifier is saved +func (t *pushover) OnSave() (string, error) { + return "", nil +} diff --git a/notifiers/slack.go b/notifiers/slack.go index b690ecae..3c9eae3a 100644 --- a/notifiers/slack.go +++ b/notifiers/slack.go @@ -86,3 +86,8 @@ func (s *slack) OnSuccess(srv *services.Service) (string, error) { out, err := s.sendSlack(msg) return out, err } + +// OnSave will trigger when this notifier is saved +func (s *slack) OnSave() (string, error) { + return "", nil +} diff --git a/notifiers/statping_emailer.go b/notifiers/statping_emailer.go index 7587e328..2344a7f0 100644 --- a/notifiers/statping_emailer.go +++ b/notifiers/statping_emailer.go @@ -3,7 +3,6 @@ package notifiers import ( "bytes" "encoding/json" - "errors" "github.com/statping/statping/types/core" "github.com/statping/statping/types/failures" "github.com/statping/statping/types/notifications" @@ -35,8 +34,7 @@ var statpingMailer = &statpingEmailer{¬ifications.Notification{ Author: "Hunter Long", AuthorUrl: "https://github.com/hunterlong", Delay: time.Duration(10 * time.Second), - Icon: "fab fa-slack", - RequestInfo: "Slack allows you to customize your own messages with many complex components. Checkout the Slack Message API to learn how you can create your own.", + Icon: "fas envelope-square", Limits: 60, Form: []notifications.NotificationForm{{ Type: "email", @@ -58,23 +56,13 @@ func (s *statpingEmailer) sendStatpingEmail(msg statpingMail) (string, error) { } func (s *statpingEmailer) OnTest() (string, error) { - example := services.Example(true) - testMsg := ReplaceVars(s.SuccessData, example, nil) - contents, resp, err := utils.HttpRequest(s.Host, "POST", "application/json", nil, bytes.NewBuffer([]byte(testMsg)), time.Duration(10*time.Second), true, nil) - if err != nil { - return "", err - } - defer resp.Body.Close() - if string(contents) != "ok" { - return string(contents), errors.New("the slack response was incorrect, check the URL") - } - return string(contents), nil + return "", nil } type statpingMail struct { Email string `json:"email"` - Core *core.Core `json:"core"` - Service *services.Service `json:"service"` + Core *core.Core `json:"core,omitempty"` + Service *services.Service `json:"service,omitempty"` Failure *failures.Failure `json:"failure,omitempty"` } @@ -86,8 +74,7 @@ func (s *statpingEmailer) OnFailure(srv *services.Service, f *failures.Failure) Service: srv, Failure: f, } - out, err := s.sendStatpingEmail(ee) - return out, err + return s.sendStatpingEmail(ee) } // OnSuccess will trigger successful service @@ -98,6 +85,18 @@ func (s *statpingEmailer) OnSuccess(srv *services.Service) (string, error) { Service: srv, Failure: nil, } + return s.sendStatpingEmail(ee) +} + +// OnSave will trigger when this notifier is saved +func (s *statpingEmailer) OnSave() (string, error) { + ee := statpingMail{ + Email: s.Host, + Core: core.App, + Service: nil, + Failure: nil, + } out, err := s.sendStatpingEmail(ee) + log.Println("statping emailer response", out) return out, err } diff --git a/notifiers/statping_emailer_test.go b/notifiers/statping_emailer_test.go new file mode 100644 index 00000000..7155cf24 --- /dev/null +++ b/notifiers/statping_emailer_test.go @@ -0,0 +1,61 @@ +package notifiers + +import ( + "github.com/statping/statping/database" + "github.com/statping/statping/types/failures" + "github.com/statping/statping/types/notifications" + "github.com/statping/statping/types/null" + "github.com/statping/statping/types/services" + "github.com/statping/statping/utils" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "testing" + "time" +) + +var ( + testEmail string +) + +func TestStatpingEmailerNotifier(t *testing.T) { + err := utils.InitLogs() + require.Nil(t, err) + db, err := database.OpenTester() + require.Nil(t, err) + db.AutoMigrate(¬ifications.Notification{}) + notifications.SetDB(db) + + testEmail = utils.Params.GetString("TEST_EMAIL") + statpingMailer.Host = testEmail + statpingMailer.Enabled = null.NewNullBool(true) + + if testEmail == "" { + t.Log("statping email notifier testing skipped, missing TEST_EMAIL environment variable") + t.SkipNow() + } + + t.Run("Load statping emailer", func(t *testing.T) { + statpingMailer.Host = testEmail + statpingMailer.Delay = time.Duration(100 * time.Millisecond) + statpingMailer.Limits = 3 + Add(statpingMailer) + assert.Equal(t, "Hunter Long", statpingMailer.Author) + assert.Equal(t, testEmail, statpingMailer.Host) + }) + + t.Run("statping emailer Within Limits", func(t *testing.T) { + ok := statpingMailer.CanSend() + assert.True(t, ok) + }) + + t.Run("statping emailer OnFailure", func(t *testing.T) { + _, err := statpingMailer.OnFailure(services.Example(false), failures.Example()) + assert.Nil(t, err) + }) + + t.Run("statping emailer OnSuccess", func(t *testing.T) { + _, err := statpingMailer.OnSuccess(services.Example(true)) + assert.Nil(t, err) + }) + +} diff --git a/notifiers/telegram.go b/notifiers/telegram.go index 9ea8dd24..bd53cb29 100644 --- a/notifiers/telegram.go +++ b/notifiers/telegram.go @@ -94,6 +94,11 @@ func (t *telegram) OnTest() (string, error) { return content, err } +// OnSave will trigger when this notifier is saved +func (t *telegram) OnSave() (string, error) { + return "", nil +} + func telegramSuccess(res []byte) (bool, telegramResponse) { var obj telegramResponse json.Unmarshal(res, &obj) diff --git a/notifiers/twilio.go b/notifiers/twilio.go index 68c55836..da4272f3 100644 --- a/notifiers/twilio.go +++ b/notifiers/twilio.go @@ -107,6 +107,11 @@ func (t *twilio) OnTest() (string, error) { return t.sendMessage(msg) } +// OnSave will trigger when this notifier is saved +func (t *twilio) OnSave() (string, error) { + return "", nil +} + func twilioSuccess(res []byte) (bool, twilioResponse) { var obj twilioResponse json.Unmarshal(res, &obj) diff --git a/notifiers/webhook.go b/notifiers/webhook.go index 03150570..30a43b6e 100644 --- a/notifiers/webhook.go +++ b/notifiers/webhook.go @@ -148,3 +148,8 @@ func (w *webhooker) OnSuccess(s *services.Service) (string, error) { content, err := ioutil.ReadAll(resp.Body) return string(content), err } + +// OnSave will trigger when this notifier is saved +func (w *webhooker) OnSave() (string, error) { + return "", nil +} diff --git a/types/notifier/interface.go b/types/notifier/interface.go index 825b13c2..405bb927 100644 --- a/types/notifier/interface.go +++ b/types/notifier/interface.go @@ -10,4 +10,5 @@ type Notifier interface { OnSuccess(*services.Service) (string, error) // OnSuccess is triggered when a service is successful OnFailure(*services.Service, *failures.Failure) (string, error) // OnFailure is triggered when a service is failing OnTest() (string, error) // OnTest is triggered for testing + OnSave() (string, error) // OnSave is triggered for when saved } diff --git a/types/services/notifier.go b/types/services/notifier.go index 1835012b..5a822078 100644 --- a/types/services/notifier.go +++ b/types/services/notifier.go @@ -29,5 +29,6 @@ type ServiceNotifier interface { OnSuccess(*Service) (string, error) // OnSuccess is triggered when a service is successful OnFailure(*Service, *failures.Failure) (string, error) // OnFailure is triggered when a service is failing OnTest() (string, error) // OnTest is triggered for testing + OnSave() (string, error) // OnSave is triggered for testing Select() *notifications.Notification // OnTest is triggered for testing }