From 7d690cf811720da7d357f7933108793cca7ad9a6 Mon Sep 17 00:00:00 2001 From: scott lee davis Date: Wed, 14 Aug 2019 21:53:44 -0700 Subject: [PATCH] additional code coverage (#152) * TriggerReminders additional coverage * more coverage * patch coverage --- server/http.go | 17 ++++++++----- server/http_test.go | 35 ++++++++++++++++++++++++++- server/occurrence_test.go | 31 +++++++++++++----------- server/reminder.go | 1 - server/reminder_test.go | 50 ++++++++++++++++++++++++++++++++++++--- 5 files changed, 109 insertions(+), 25 deletions(-) diff --git a/server/http.go b/server/http.go index ee167aa..584cf9b 100755 --- a/server/http.go +++ b/server/http.go @@ -66,12 +66,17 @@ func (p *Plugin) handleDialog(w http.ResponseWriter, req *http.Request) { target = "@" + target.(string) } - when := T("in") + " " + T("button.snooze."+ttime.(string)) - switch ttime.(string) { - case "tomorrow": - when = T("tomorrow") - case "nextweek": - when = T("monday") + var when string + if ttime.(string) == "unit.test" { + when = "in 20 minutes" + } else { + when = T("in") + " " + T("button.snooze."+ttime.(string)) + switch ttime.(string) { + case "tomorrow": + when = T("tomorrow") + case "nextweek": + when = T("monday") + } } r := &ReminderRequest{ diff --git a/server/http_test.go b/server/http_test.go index d1fc6a6..5b3cd93 100755 --- a/server/http_test.go +++ b/server/http_test.go @@ -3,8 +3,10 @@ package main import ( "bytes" "encoding/json" + "fmt" "io/ioutil" "net/http/httptest" + "os" "testing" "time" @@ -19,14 +21,45 @@ func TestHandleDialog(t *testing.T) { Id: model.NewId(), Username: model.NewRandomString(10), } + testTime := time.Now().UTC().Round(time.Second).Add(20 * time.Duration(time.Minute)) + hostname, _ := os.Hostname() + reminderId := model.NewId() + + occurrences := []Occurrence{ + { + Hostname: hostname, + Id: model.NewId(), + ReminderId: reminderId, + Occurrence: testTime, + }, + } + + reminders := []Reminder{ + { + Id: reminderId, + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: occurrences, + }, + } + + stringOccurrences, _ := json.Marshal(occurrences) + stringReminders, _ := json.Marshal(reminders) setupAPI := func() *plugintest.API { api := &plugintest.API{} api.On("LogDebug", mock.Anything, mock.Anything, mock.Anything).Maybe() api.On("LogError", mock.Anything, mock.Anything, mock.Anything).Maybe() api.On("LogInfo", mock.Anything).Maybe() + api.On("KVGet", string(fmt.Sprintf("%v", testTime))).Return(stringOccurrences, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) api.On("GetUser", mock.Anything).Return(user, nil) api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("SendEphemeralPost", mock.Anything, mock.Anything).Return(nil) return api } @@ -45,7 +78,7 @@ func TestHandleDialog(t *testing.T) { Submission: model.StringInterface{ "message": "hello", "target": "me", - "time": "in 2 seconds", + "time": "unit.test", }, } diff --git a/server/occurrence_test.go b/server/occurrence_test.go index 38cde22..8877f65 100755 --- a/server/occurrence_test.go +++ b/server/occurrence_test.go @@ -36,18 +36,6 @@ func TestCreateOccurrences(t *testing.T) { }, } - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: occurrences, - // }, - //} - request := &ReminderRequest{ TeamId: model.NewId(), Username: user.Username, @@ -70,8 +58,6 @@ func TestCreateOccurrences(t *testing.T) { api.On("LogDebug", mock.Anything, mock.Anything, mock.Anything).Maybe() api.On("LogError", mock.Anything, mock.Anything, mock.Anything).Maybe() api.On("LogInfo", mock.Anything).Maybe() - api.On("KVGet", mock.Anything).Return(stringOccurrences, nil) - api.On("KVSet", mock.Anything, mock.Anything).Return(nil) api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) return api } @@ -81,12 +67,29 @@ func TestCreateOccurrences(t *testing.T) { api := setupAPI() defer api.AssertExpectations(t) + api.On("KVGet", mock.Anything).Return(stringOccurrences, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) p := &Plugin{} p.API = api assert.Nil(t, p.CreateOccurrences(request)) }) + + t.Run("if fails to create occurrences", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + request.Reminder.When = "in foobar" + assert.NotNil(t, p.CreateOccurrences(request)) + + request.Reminder.When = "in foo seconds" + assert.NotNil(t, p.CreateOccurrences(request)) + }) } func TestIn(t *testing.T) { diff --git a/server/reminder.go b/server/reminder.go index cc07aad..ae5668e 100755 --- a/server/reminder.go +++ b/server/reminder.go @@ -87,7 +87,6 @@ func (p *Plugin) TriggerReminders() { } if strings.HasPrefix(reminder.Target, "@") || strings.HasPrefix(reminder.Target, T("me")) { //@user - var targetId string if strings.HasPrefix(reminder.Target, "@") { target := strings.Trim(reminder.Target, "@") diff --git a/server/reminder_test.go b/server/reminder_test.go index 22829c9..52b0db9 100755 --- a/server/reminder_test.go +++ b/server/reminder_test.go @@ -23,21 +23,31 @@ func TestTriggerReminders(t *testing.T) { Roles: model.SYSTEM_USER_ROLE_ID, Locale: "en", } - testTime := time.Now().UTC().Round(time.Second) + channel := &model.Channel{ + Id: model.NewId(), + } + + post := &model.Post{ + Id: model.NewId(), + } + + testTime := time.Now().UTC().Round(time.Second) hostname, _ := os.Hostname() + reminderId := model.NewId() + occurrences := []Occurrence{ { Hostname: hostname, Id: model.NewId(), - ReminderId: model.NewId(), + ReminderId: reminderId, Occurrence: testTime, }, } reminders := []Reminder{ { - Id: model.NewId(), + Id: reminderId, TeamId: model.NewId(), Username: user.Username, Message: "Hello", @@ -56,13 +66,30 @@ func TestTriggerReminders(t *testing.T) { api.On("LogInfo", mock.Anything).Maybe() api.On("KVGet", string(fmt.Sprintf("%v", testTime))).Return(stringOccurrences, nil) api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) + api.On("CreatePost", mock.Anything).Return(post, nil) return api } + t.Run("if triggers reminder for me", func(t *testing.T) { + api := setupAPI() + stringReminders, _ := json.Marshal(reminders) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("GetDirectChannel", mock.Anything, mock.Anything).Return(channel, nil) + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + p.TriggerReminders() + + }) + t.Run("if triggers reminder for user", func(t *testing.T) { api := setupAPI() + reminders[0].Target = "@testuser" stringReminders, _ := json.Marshal(reminders) api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("GetDirectChannel", mock.Anything, mock.Anything).Return(channel, nil) defer api.AssertExpectations(t) p := &Plugin{} @@ -77,6 +104,23 @@ func TestTriggerReminders(t *testing.T) { reminders[0].Target = "~off-topic" stringReminders, _ := json.Marshal(reminders) api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("GetChannelByName", mock.Anything, mock.Anything, mock.Anything).Return(channel, nil) + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + p.TriggerReminders() + + }) + + t.Run("if triggers reminder recurring", func(t *testing.T) { + api := setupAPI() + reminders[0].Target = "me" + reminders[0].When = "every tuesday at 3pm" + stringReminders, _ := json.Marshal(reminders) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("GetDirectChannel", mock.Anything, mock.Anything).Return(channel, nil) defer api.AssertExpectations(t) p := &Plugin{}