Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

additional code coverage #152

Merged
merged 3 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
35 changes: 34 additions & 1 deletion server/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http/httptest"
"os"
"testing"
"time"

Expand All @@ -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
}
Expand All @@ -45,7 +78,7 @@ func TestHandleDialog(t *testing.T) {
Submission: model.StringInterface{
"message": "hello",
"target": "me",
"time": "in 2 seconds",
"time": "unit.test",
},
}

Expand Down
31 changes: 17 additions & 14 deletions server/occurrence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion server/reminder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "@")
Expand Down
50 changes: 47 additions & 3 deletions server/reminder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{}
Expand All @@ -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{}
Expand Down