From baa4ac49ce9eac6ac947bda45f1b1056919a3d49 Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Mon, 17 Sep 2018 15:13:42 -0700 Subject: [PATCH] timestamp - tests --- Makefile | 2 +- core/database.go | 40 +++++++++++++++++++++++++++++++++++++++ core/services.go | 40 --------------------------------------- handlers/routes.go | 2 +- notifiers/discord_test.go | 1 + notifiers/email_test.go | 7 +++++++ notifiers/slack_test.go | 3 ++- notifiers/twilio_test.go | 17 +++++++++++------ utils/utils.go | 1 - utils/utils_test.go | 9 ++++----- 10 files changed, 67 insertions(+), 55 deletions(-) diff --git a/Makefile b/Makefile index d20174d3..a3ced665 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=0.61 +VERSION=0.63 BINARY_NAME=statup GOPATH:=$(GOPATH) GOCMD=go diff --git a/core/database.go b/core/database.go index 4754d962..9b8045d1 100644 --- a/core/database.go +++ b/core/database.go @@ -83,6 +83,46 @@ func (db *DbConfig) Close() error { return DbSession.DB().Close() } +func (s *Service) AfterFind() (err error) { + s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone) + return +} + +func (s *Hit) AfterFind() (err error) { + s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone) + return +} + +func (f *Failure) AfterFind() (err error) { + f.CreatedAt = utils.Timezoner(f.CreatedAt, CoreApp.Timezone) + return +} + +func (u *User) AfterFind() (err error) { + u.CreatedAt = utils.Timezoner(u.CreatedAt, CoreApp.Timezone) + return +} + +func (u *Hit) BeforeCreate() (err error) { + u.CreatedAt = time.Now().UTC() + return +} + +func (u *Failure) BeforeCreate() (err error) { + u.CreatedAt = time.Now().UTC() + return +} + +func (u *User) BeforeCreate() (err error) { + u.CreatedAt = time.Now().UTC() + return +} + +func (u *Service) BeforeCreate() (err error) { + u.CreatedAt = time.Now().UTC() + return +} + // InsertCore create the single row for the Core settings in Statup func (db *DbConfig) InsertCore() (*Core, error) { CoreApp = &Core{Core: &types.Core{ diff --git a/core/services.go b/core/services.go index d5115492..babf4121 100644 --- a/core/services.go +++ b/core/services.go @@ -166,46 +166,6 @@ func GroupDataBy(column string, id int64, tm time.Time, increment string) string return sql } -func (s *Service) AfterFind() (err error) { - s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone) - return -} - -func (s *Hit) AfterFind() (err error) { - s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone) - return -} - -func (s *Failure) AfterFind() (err error) { - s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone) - return -} - -func (s *User) AfterFind() (err error) { - s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone) - return -} - -func (u *Hit) BeforeCreate() (err error) { - u.CreatedAt = time.Now().UTC() - return -} - -func (u *Failure) BeforeCreate() (err error) { - u.CreatedAt = time.Now().UTC() - return -} - -func (u *User) BeforeCreate() (err error) { - u.CreatedAt = time.Now().UTC() - return -} - -func (u *Service) BeforeCreate() (err error) { - u.CreatedAt = time.Now().UTC() - return -} - func (s *Service) Downtime() time.Duration { hits, _ := s.Hits() fails := s.LimitedFailures() diff --git a/handlers/routes.go b/handlers/routes.go index 72607ed6..c1449363 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -47,8 +47,8 @@ func Router() *mux.Router { r.PathPrefix("/statup.png").Handler(http.FileServer(source.TmplBox.HTTPBox())) } r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(source.JsBox.HTTPBox()))) - r.Handle("/charts.js", http.HandlerFunc(renderServiceChartsHandler)) r.Handle("/charts/{id}.js", http.HandlerFunc(renderServiceChartHandler)) + r.Handle("/charts.js", http.HandlerFunc(renderServiceChartsHandler)) r.Handle("/setup", http.HandlerFunc(setupHandler)).Methods("GET") r.Handle("/setup", http.HandlerFunc(processSetupHandler)).Methods("POST") r.Handle("/dashboard", http.HandlerFunc(dashboardHandler)).Methods("GET") diff --git a/notifiers/discord_test.go b/notifiers/discord_test.go index 1f5086a8..41c325fa 100644 --- a/notifiers/discord_test.go +++ b/notifiers/discord_test.go @@ -14,6 +14,7 @@ var ( ) func init() { + DISCORD_URL = os.Getenv("DISCORD_URL") discorder.Host = DISCORD_URL } diff --git a/notifiers/email_test.go b/notifiers/email_test.go index b630d95e..5df58e73 100644 --- a/notifiers/email_test.go +++ b/notifiers/email_test.go @@ -22,6 +22,13 @@ var ( var testEmail *EmailOutgoing func init() { + EMAIL_HOST = os.Getenv("EMAIL_HOST") + EMAIL_USER = os.Getenv("EMAIL_USER") + EMAIL_PASS = os.Getenv("EMAIL_PASS") + EMAIL_OUTGOING = os.Getenv("EMAIL_OUTGOING") + EMAIL_SEND_TO = os.Getenv("EMAIL_SEND_TO") + EMAIL_PORT = utils.StringInt(os.Getenv("EMAIL_PORT")) + emailer.Host = EMAIL_HOST emailer.Username = EMAIL_USER emailer.Password = EMAIL_PASS diff --git a/notifiers/slack_test.go b/notifiers/slack_test.go index a6f684f7..f2315664 100644 --- a/notifiers/slack_test.go +++ b/notifiers/slack_test.go @@ -9,7 +9,7 @@ import ( ) var ( - SLACK_URL = os.Getenv("SLACK_URL") + SLACK_URL string slackMessage = `{"text":"this is a test from the Slack notifier!"}` slackTestMessage = SlackMessage{ Service: TestService, @@ -19,6 +19,7 @@ var ( ) func init() { + SLACK_URL = os.Getenv("SLACK_URL") slacker.Host = SLACK_URL } diff --git a/notifiers/twilio_test.go b/notifiers/twilio_test.go index 3e0e313d..bd2b2cac 100644 --- a/notifiers/twilio_test.go +++ b/notifiers/twilio_test.go @@ -17,6 +17,11 @@ var ( ) 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") + twilioNotifier.ApiKey = TWILIO_SID twilioNotifier.ApiSecret = TWILIO_SECRET twilioNotifier.Var1 = TWILIO_TO @@ -26,12 +31,12 @@ func init() { func TestTwilioNotifier(t *testing.T) { t.Parallel() if TWILIO_SID == "" || TWILIO_SECRET == "" || TWILIO_FROM == "" { - t.Log("twilioNotifier notifier testing skipped, missing TWILIO_SID environment variable") + t.Log("twilio notifier testing skipped, missing TWILIO_SID environment variable") t.SkipNow() } currentCount = CountNotifiers() - t.Run("Load twilioNotifier", func(t *testing.T) { + t.Run("Load Twilio", func(t *testing.T) { twilioNotifier.ApiKey = TWILIO_SID twilioNotifier.Delay = time.Duration(100 * time.Millisecond) err := notifier.AddNotifier(twilioNotifier) @@ -40,22 +45,22 @@ func TestTwilioNotifier(t *testing.T) { assert.Equal(t, TWILIO_SID, twilioNotifier.ApiKey) }) - t.Run("Load twilioNotifier Notifier", func(t *testing.T) { + t.Run("Load Twilio Notifier", func(t *testing.T) { notifier.Load() }) - t.Run("twilioNotifier Within Limits", func(t *testing.T) { + t.Run("Twilio Within Limits", func(t *testing.T) { ok, err := twilioNotifier.WithinLimits() assert.Nil(t, err) assert.True(t, ok) }) - t.Run("twilioNotifier Send", func(t *testing.T) { + t.Run("Twilio Send", func(t *testing.T) { err := twilioNotifier.Send(twilioMessage) assert.Nil(t, err) }) - t.Run("twilioNotifier Queue", func(t *testing.T) { + t.Run("Twilio Queue", func(t *testing.T) { go notifier.Queue(twilioNotifier) time.Sleep(1 * time.Second) assert.Equal(t, TWILIO_SID, twilioNotifier.ApiKey) diff --git a/utils/utils.go b/utils/utils.go index 614808bf..7374f317 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -62,7 +62,6 @@ func ToString(s interface{}) string { func Timezoner(t time.Time, zone float32) time.Time { zoneInt := float32(3600) * (zone + 1) - fmt.Println(int(zoneInt)) loc := time.FixedZone("", int(zoneInt)) timez := t.In(loc) return timez diff --git a/utils/utils_test.go b/utils/utils_test.go index 10c0d455..6cc2fc87 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -73,13 +73,12 @@ func TestDbTime(t *testing.T) { } func TestTimezone(t *testing.T) { - zone := -5 + zone := float32(-4.0) loc, _ := time.LoadLocation("America/Los_Angeles") - timestamp := time.Date(2018, 1, 1, 10, 0, 0, 0, loc).UTC() - correct := timestamp.Add(3 * time.Hour) + timestamp := time.Date(2018, 1, 1, 10, 0, 0, 0, loc) timezone := Timezoner(timestamp, zone) - assert.Equal(t, "2018-01-01 21:00:00 +0000 UTC", correct.String()) - assert.Equal(t, "2018-01-01 13:00:00 -0500 -0500", timezone.String()) + assert.Equal(t, "2018-01-01 10:00:00 -0800 PST", timestamp.String()) + assert.Equal(t, "2018-01-01 15:00:00 -0300 -0300", timezone.String()) } func TestTimestamp_Ago(t *testing.T) {