diff --git a/server/command_test.go b/server/command_test.go index 9e9d578..e023263 100644 --- a/server/command_test.go +++ b/server/command_test.go @@ -1,8 +1,10 @@ package main import ( + "encoding/json" "fmt" "testing" + "time" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/plugin/plugintest" @@ -80,166 +82,207 @@ func TestHandleCommand(t *testing.T) { t.Run("/remind list", func(t *testing.T) { - //channel := &model.Channel{ - // Id: model.NewId(), - // Name: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: channel.Id, - //} - // - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // api.On("GetDirectChannel", mock.Anything, mock.Anything).Return(channel, nil) - // api.On("CreatePost", post).Return(post, nil) - // - // return api - //} - // - //api := setupAPI() - //defer api.AssertExpectations(t) - // - //p := &Plugin{} - //p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - //p.router = p.InitAPI() - //p.API = api - // - //r, err := p.ExecuteCommand(nil, &model.CommandArgs{ - // Command: fmt.Sprintf("/%s list", trigger), - // UserId: "userID1", - //}) - // - //assert.NotNil(t,r) - //assert.Nil(t, err) + channel := &model.Channel{ + Id: model.NewId(), + Name: model.NewRandomString(10), + } + post := &model.Post{ + Id: model.NewId(), + ChannelId: channel.Id, + } + + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("SendEphemeralPost", mock.Anything, mock.Anything).Return(post) + + return api + } + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + r, err := p.ExecuteCommand(nil, &model.CommandArgs{ + Command: fmt.Sprintf("/%s list", trigger), + UserId: "userID1", + }) + + assert.NotNil(t, r) + assert.Nil(t, err) }) t.Run("/remind me foo in 2 seconds", func(t *testing.T) { - //channel := &model.Channel{ - // Id: model.NewId(), - // Name: model.NewRandomString(10), - //} - // - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in 2 seconds", - // Occurrences: 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("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // api.On("GetDirectChannel", mock.Anything, mock.Anything).Return(channel, nil) - // api.On("SendEphemeralPost", mock.Anything, mock.Anything).Return(nil) - // - // return api - //} - // - //api := setupAPI() - //defer api.AssertExpectations(t) - // - //p := &Plugin{} - //p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - //p.router = p.InitAPI() - //p.API = api - // - //r, err := p.ExecuteCommand(nil, &model.CommandArgs{ - // Command: fmt.Sprintf("/%s me Hello in 2 seconds", trigger), - // UserId: "userID1", - //}) - // - //assert.NotNil(t,r) - //assert.Nil(t, err) + channel := &model.Channel{ + Id: model.NewId(), + Name: model.NewRandomString(10), + } + + post := &model.Post{ + Id: model.NewId(), + ChannelId: channel.Id, + } + + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in 2 seconds", + Occurrences: occurrences, + }, + } + stringReminders, _ := json.Marshal(reminders) + stringOccurrences, _ := json.Marshal(occurrences) + + 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("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("KVGet", mock.Anything).Return(stringOccurrences, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + api.On("SendEphemeralPost", mock.Anything, mock.Anything).Return(post) + + return api + } + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + r, err := p.ExecuteCommand(nil, &model.CommandArgs{ + Command: fmt.Sprintf("/%s me Hello in 2 seconds", trigger), + UserId: "userID1", + }) + + assert.NotNil(t, r) + assert.Nil(t, err) }) t.Run("/remind __clear", func(t *testing.T) { - //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("GetUser", mock.Anything).Return(user, nil) - // api.On("SendEphemeralPost", mock.Anything, mock.Anything).Return(nil) - // return api - //} - // - //api := setupAPI() - //defer api.AssertExpectations(t) - // - //p := &Plugin{} - //p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - //p.router = p.InitAPI() - //p.API = api - // - //r, err := p.ExecuteCommand(nil, &model.CommandArgs{ - // Command: fmt.Sprintf("/%s __clear", trigger), - // UserId: "userID1", - //}) - // - //assert.NotNil(t,r) - //assert.Nil(t, err) + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetUser", mock.Anything).Return(user, nil) + api.On("SendEphemeralPost", mock.Anything, mock.Anything).Return(nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("KVDelete", user.Username).Return(nil) + return api + } + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + r, err := p.ExecuteCommand(nil, &model.CommandArgs{ + Command: fmt.Sprintf("/%s __clear", trigger), + UserId: "userID1", + }) + + assert.NotNil(t, r) + assert.Nil(t, err) }) t.Run("/remind __version", func(t *testing.T) { + channel := &model.Channel{ + Id: model.NewId(), + Name: model.NewRandomString(10), + } + + post := &model.Post{ + Id: model.NewId(), + ChannelId: channel.Id, + } + 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("GetUser", mock.Anything).Return(user, nil) - api.On("SendEphemeralPost", mock.Anything, mock.Anything).Return(nil) + api.On("SendEphemeralPost", mock.Anything, mock.Anything).Return(post) return api } diff --git a/server/http_test.go b/server/http_test.go index 5e14b29..a0fe8b3 100755 --- a/server/http_test.go +++ b/server/http_test.go @@ -68,336 +68,814 @@ func TestHandleDialog(t *testing.T) { func TestHandleViewEphmeral(t *testing.T) { - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: model.NewId(), - //} - //channel := &model.Channel{ - // Id: model.NewId(), - // Name: model.NewRandomString(10), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("GetPost", mock.Anything).Return(post, nil) - // api.On("UpdatePost", mock.Anything).Return(post, nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // api.On("GetDirectChannel", mock.Anything, mock.Anything).Return(channel, nil) - // api.On("CreatePost", mock.Anything).Maybe() - // - // return api - //} - // - // - //t.Run("view ephemeral", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{ - // UserId: "userID1", - // PostId: "postID1", - // Context: model.StringInterface{ - // "reminder_id": model.NewId(), - // "occurrence_id": model.NewId(), - // }, - // } - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/view/ephemeral", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - //}) + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("CreatePost", mock.Anything).Maybe() + + return api + } + + t.Run("view ephemeral", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{ + UserId: "userID1", + PostId: "postID1", + Context: model.StringInterface{ + "reminder_id": model.NewId(), + "occurrence_id": model.NewId(), + }, + } + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/view/ephemeral", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + + }) + +} + +func TestHandleComplete(t *testing.T) { + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + post := &model.Post{ + Id: model.NewId(), + ChannelId: model.NewId(), + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetPost", mock.Anything).Return(post, nil) + api.On("UpdatePost", mock.Anything).Return(post, nil) + api.On("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + + return api + } + + t.Run("complete", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{ + UserId: "userID1", + PostId: "postID1", + Context: model.StringInterface{ + "reminder_id": model.NewId(), + "occurrence_id": model.NewId(), + }, + } + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/complete", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + + }) + +} + +func TestHandleDelete(t *testing.T) { + + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + post := &model.Post{ + Id: model.NewId(), + ChannelId: model.NewId(), + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetPost", mock.Anything).Return(post, nil) + api.On("UpdatePost", mock.Anything).Return(post, nil) + api.On("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + + return api + } + + t.Run("delete", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{ + UserId: "userID1", + PostId: "postID1", + Context: model.StringInterface{ + "reminder_id": model.NewId(), + "occurrence_id": model.NewId(), + }, + } + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/delete", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + + }) + +} + +func TestHandleDeleteEphemeral(t *testing.T) { + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + post := &model.Post{ + Id: model.NewId(), + ChannelId: model.NewId(), + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + api.On("UpdateEphemeralPost", mock.Anything, mock.Anything).Return(post) + + return api + } + + t.Run("delete ephemeral", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{ + UserId: "userID1", + PostId: "postID1", + Context: model.StringInterface{ + "reminder_id": model.NewId(), + "occurrence_id": model.NewId(), + }, + } + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/delete/ephemeral", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + + }) + +} + +func TestHandleSnooze(t *testing.T) { + + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + post := &model.Post{ + Id: model.NewId(), + ChannelId: model.NewId(), + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetPost", mock.Anything).Return(post, nil) + api.On("UpdatePost", mock.Anything).Return(post, nil) + api.On("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + + return api + } + + for name, test := range map[string]struct { + SnoozeTime string + }{ + "snoozes item 20min": { + SnoozeTime: "20min", + }, + "snoozes item 1hr": { + SnoozeTime: "1hr", + }, + "snoozes item 3hrs": { + SnoozeTime: "3hrs", + }, + "snoozes item tomorrow": { + SnoozeTime: "tomorrow", + }, + "snoozes item nextweek": { + SnoozeTime: "nextweek", + }, + } { + + t.Run(name, func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{ + UserId: "userID1", + PostId: "postID1", + Context: model.StringInterface{ + "reminder_id": model.NewId(), + "occurrence_id": model.NewId(), + "selected_option": test.SnoozeTime, + }, + } + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/snooze", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + + }) + } +} + +func TestHandleNextReminders(t *testing.T) { + + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + post := &model.Post{ + Id: model.NewId(), + ChannelId: model.NewId(), + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("UpdateEphemeralPost", mock.Anything, mock.Anything).Return(post) + + return api + } + + t.Run("next reminders", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{ + UserId: user.Id, + PostId: post.Id, + Context: model.StringInterface{ + "reminder_id": model.NewId(), + "occurrence_id": model.NewId(), + "offset": 0, + }, + } + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/next/reminders", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + + }) +} + +func TestHandleCompleteList(t *testing.T) { + + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + + post := &model.Post{ + Id: model.NewId(), + ChannelId: model.NewId(), + } + + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + api.On("UpdateEphemeralPost", mock.Anything, mock.Anything).Return(post) + + return api + } + + t.Run("complete list", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{ + UserId: "userID1", + PostId: "postID1", + Context: model.StringInterface{ + "reminder_id": model.NewId(), + "occurrence_id": model.NewId(), + }, + } + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/complete/list", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + }) } -func TestHandleComplete(t *testing.T) { - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: model.NewId(), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("GetPost", mock.Anything).Return(post, nil) - // api.On("UpdatePost", mock.Anything).Return(post, nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // - // return api - //} - // - // - //t.Run("complete", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{ - // UserId: "userID1", - // PostId: "postID1", - // Context: model.StringInterface{ - // "reminder_id": model.NewId(), - // "occurrence_id": model.NewId(), - // }, - // } - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/complete", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - //}) +func TestHandleViewCompleteList(t *testing.T) { + + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + post := &model.Post{ + Id: model.NewId(), + ChannelId: model.NewId(), + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("UpdateEphemeralPost", mock.Anything, mock.Anything).Return(post) + + return api + } + + t.Run("view complete list", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{UserId: user.Id, PostId: post.Id} + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/view/complete/list", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + + }) } -func TestHandleDelete(t *testing.T) { - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: model.NewId(), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("GetPost", mock.Anything).Return(post, nil) - // api.On("UpdatePost", mock.Anything).Return(post, nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // - // return api - //} - // - // - //t.Run("delete", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{ - // UserId: "userID1", - // PostId: "postID1", - // Context: model.StringInterface{ - // "reminder_id": model.NewId(), - // "occurrence_id": model.NewId(), - // }, - // } - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/delete", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - //}) +func TestHandleDeleteList(t *testing.T) { + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + post := &model.Post{ + Id: model.NewId(), + ChannelId: model.NewId(), + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + api.On("UpdateEphemeralPost", mock.Anything, mock.Anything).Return(post) + + return api + } + + t.Run("delete list item", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{ + UserId: "userID1", + PostId: "postID1", + Context: model.StringInterface{ + "reminder_id": model.NewId(), + "occurrence_id": model.NewId(), + }, + } + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/delete/list", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + + }) } -func TestHandleDeleteEphemeral(t *testing.T) { - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: model.NewId(), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("GetPost", mock.Anything).Return(post, nil) - // api.On("UpdatePost", mock.Anything).Return(post, nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // - // return api - //} - // - // - //t.Run("delete ephemeral", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{ - // UserId: "userID1", - // PostId: "postID1", - // Context: model.StringInterface{ - // "reminder_id": model.NewId(), - // "occurrence_id": model.NewId(), - // }, - // } - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/delete/ephemeral", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - //}) +func TestHandleDeleteCompleteList(t *testing.T) { + + user := &model.User{ + Id: model.NewId(), + Username: model.NewRandomString(10), + } + post := &model.Post{ + Id: model.NewId(), + ChannelId: model.NewId(), + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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("GetUser", mock.Anything).Return(user, nil) + api.On("GetUserByUsername", mock.Anything).Return(user, nil) + api.On("UpdateEphemeralPost", mock.Anything, mock.Anything).Return(post) + api.On("KVGet", user.Username).Return(stringReminders, nil) + + return api + } + + t.Run("delete completed list", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) + p.router = p.InitAPI() + p.API = api + + request := &model.PostActionIntegrationRequest{UserId: "userID1", PostId: "postID1"} + + w := httptest.NewRecorder() + r := httptest.NewRequest("POST", "/delete/complete/list", bytes.NewReader(request.ToJson())) + p.ServeHTTP(nil, w, r) + + result := w.Result() + assert.NotNil(t, result) + + bodyBytes, err := ioutil.ReadAll(result.Body) + assert.Nil(t, err) + bodyString := string(bodyBytes) + assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") + }) } -func TestHandleSnooze(t *testing.T) { +func TestHandleSnoozeList(t *testing.T) { user := &model.User{ Id: model.NewId(), @@ -435,10 +913,10 @@ func TestHandleSnooze(t *testing.T) { api.On("LogError", mock.Anything, mock.Anything, mock.Anything).Maybe() api.On("LogInfo", mock.Anything).Maybe() api.On("GetPost", mock.Anything).Return(post, nil) - api.On("UpdatePost", mock.Anything).Return(post, nil) api.On("GetUser", mock.Anything).Return(user, nil) api.On("GetUserByUsername", mock.Anything).Return(user, nil) api.On("KVGet", user.Username).Return(stringReminders, nil) + api.On("UpdateEphemeralPost", mock.Anything, mock.Anything).Return(post) return api } @@ -446,19 +924,19 @@ func TestHandleSnooze(t *testing.T) { for name, test := range map[string]struct { SnoozeTime string }{ - "snoozes item 20min": { + "snoozes list item 20min": { SnoozeTime: "20min", }, - "snoozes item 1hr": { + "snoozes list item 1hr": { SnoozeTime: "1hr", }, - "snoozes item 3hrs": { + "snoozes list item 3hrs": { SnoozeTime: "3hrs", }, - "snoozes item tomorrow": { + "snoozes list item tomorrow": { SnoozeTime: "tomorrow", }, - "snoozes item nextweek": { + "snoozes list item nextweek": { SnoozeTime: "nextweek", }, } { @@ -484,7 +962,7 @@ func TestHandleSnooze(t *testing.T) { } w := httptest.NewRecorder() - r := httptest.NewRequest("POST", "/snooze", bytes.NewReader(request.ToJson())) + r := httptest.NewRequest("POST", "/snooze/list", bytes.NewReader(request.ToJson())) p.ServeHTTP(nil, w, r) result := w.Result() @@ -499,493 +977,6 @@ func TestHandleSnooze(t *testing.T) { } } -func TestHandleNextReminders(t *testing.T) { - - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: model.NewId(), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("DeletePost", mock.Anything).Return( nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // api.On("GetPost", post.Id).Return(post, nil) - // api.On("UpdatePost", post).Maybe() - // return api - //} - // - //t.Run("next reminders", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{ - // UserId: user.Id, - // PostId: post.Id, - // Context: model.StringInterface{ - // "reminder_id": model.NewId(), - // "occurrence_id": model.NewId(), - // "offset": 0, - // }, - // } - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/next/reminders", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - //}) -} - -func TestHandleCompleteList(t *testing.T) { - - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("DeletePost", mock.Anything).Return( nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // - // return api - //} - // - //t.Run("complete list", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{ - // UserId: "userID1", - // PostId: "postID1", - // Context: model.StringInterface{ - // "reminder_id": model.NewId(), - // "occurrence_id": model.NewId(), - // }, - // } - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/complete/list", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - //}) -} - -func TestHandleViewCompleteList(t *testing.T) { - - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: model.NewId(), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("UpdatePost", mock.Anything).Return( nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // api.On("GetPost", post.Id).Return(post, nil) - // api.On("UpdatePost", post).Maybe() - // - // return api - //} - // - //t.Run("view complete list", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{UserId: user.Id, PostId: post.Id} - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/view/complete/list", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - //}) - -} - -func TestHandleDeleteList(t *testing.T) { - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: model.NewId(), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("GetPost", mock.Anything).Return(post, nil) - // api.On("UpdatePost", mock.Anything).Return(post, nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // - // return api - //} - // - //t.Run("delete list item", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{ - // UserId: "userID1", - // PostId: "postID1", - // Context: model.StringInterface{ - // "reminder_id": model.NewId(), - // "occurrence_id": model.NewId(), - // }, - // } - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/delete/list", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - //}) - -} - -func TestHandleDeleteCompleteList(t *testing.T) { - - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: model.NewId(), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("GetPost", mock.Anything).Return(post, nil) - // api.On("UpdatePost", mock.Anything).Return(post, nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("UpdateEphemeralPost", mock.Anything, post).Return(post) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // - // return api - //} - // - //t.Run("delete completed list", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{UserId: "userID1", PostId: "postID1"} - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/delete/complete/list", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - //}) -} - -func TestHandleSnoozeList(t *testing.T) { - // - //user := &model.User{ - // Id: model.NewId(), - // Username: model.NewRandomString(10), - //} - //post := &model.Post{ - // Id: model.NewId(), - // ChannelId: model.NewId(), - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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("GetPost", mock.Anything).Return(post, nil) - // api.On("UpdatePost", mock.Anything).Return(post, nil) - // api.On("GetUser", mock.Anything).Return(user, nil) - // api.On("GetUserByUsername", mock.Anything).Return(user, nil) - // api.On("KVGet", user.Username).Return(stringReminders, nil) - // - // return api - //} - // - //for name, test := range map[string]struct { - // SnoozeTime string - //}{ - // "snoozes list item 20min": { - // SnoozeTime: "20min", - // }, - // "snoozes list item 1hr": { - // SnoozeTime: "1hr", - // }, - // "snoozes list item 3hrs": { - // SnoozeTime: "3hrs", - // }, - // "snoozes list item tomorrow": { - // SnoozeTime: "tomorrow", - // }, - // "snoozes list item nextweek": { - // SnoozeTime: "nextweek", - // }, - //} { - // - // t.Run(name, func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.URL = fmt.Sprintf("http://localhost/plugins/%s", manifest.Id) - // p.router = p.InitAPI() - // p.API = api - // - // request := &model.PostActionIntegrationRequest{ - // UserId: "userID1", - // PostId: "postID1", - // Context: model.StringInterface{ - // "reminder_id": model.NewId(), - // "occurrence_id": model.NewId(), - // "selected_option": test.SnoozeTime, - // }, - // } - // - // w := httptest.NewRecorder() - // r := httptest.NewRequest("POST", "/snooze/list", bytes.NewReader(request.ToJson())) - // p.ServeHTTP(nil, w, r) - // - // result := w.Result() - // assert.NotNil(t, result) - // - // bodyBytes, err := ioutil.ReadAll(result.Body) - // assert.Nil(t, err) - // bodyString := string(bodyBytes) - // assert.Equal(t, bodyString, "{\"update\":null,\"ephemeral_text\":\"\"}") - // - // }) - //} -} - func TestHandleCloseList(t *testing.T) { setupAPI := func() *plugintest.API { diff --git a/server/list_test.go b/server/list_test.go index 6a9b6e2..bd6870b 100755 --- a/server/list_test.go +++ b/server/list_test.go @@ -2,295 +2,266 @@ package main import ( "testing" + "time" + + "encoding/json" + "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/plugin/plugintest" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" ) func TestListReminders(t *testing.T) { - //user := &model.User{ - // Email: "-@-.-", - // Nickname: "TestUser", - // Password: model.NewId(), - // Username: "testuser", - // Roles: model.SYSTEM_USER_ROLE_ID, - // Locale: "en", - //} - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: "ididididid", - // Occurrence: time.Now(), - // }, - //} - // - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // Username: user.Username, - // Message: "Message", - // When: "in 1 second", - // Occurrences: occurrences, - // Completed: time.Time{}.AddDate(1, 1, 1), - // }, - //} - // - //stringReminders, _ := json.Marshal(reminders) - // - //channel := &model.Channel{ - // Id: model.NewId(), - //} - // - //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("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) - // api.On("GetDirectChannel", mock.Anything, mock.Anything).Return(channel, nil) - // api.On("CreatePost", mock.AnythingOfType("*model.Post")).Return(nil, nil) - // api.On("KVGet", mock.Anything).Return(stringReminders, nil) - // return api - //} - // - //t.Run("if list happens in remind channel", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.API = api - // - // output := p.ListReminders(user, channel.Id) - // - // assert.Equal(t, output, "") - // - //}) - // - //t.Run("if list happens in other channel", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.API = api - // - // output := p.ListReminders(user, model.NewId()) - // - // assert.Equal(t, output, "list.reminders") - // - //}) + user := &model.User{ + Email: "-@-.-", + Nickname: "TestUser", + Password: model.NewId(), + Username: "testuser", + Roles: model.SYSTEM_USER_ROLE_ID, + Locale: "en", + } + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: "ididididid", + Occurrence: time.Now(), + }, + } + + reminders := []Reminder{ + { + Id: model.NewId(), + Username: user.Username, + Message: "Message", + When: "in 1 second", + Occurrences: occurrences, + Completed: time.Time{}.AddDate(1, 1, 1), + }, + } + + stringReminders, _ := json.Marshal(reminders) + + channel := &model.Channel{ + Id: model.NewId(), + } + + 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("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) + api.On("KVGet", mock.Anything).Return(stringReminders, nil) + return api + } + + t.Run("if list happens", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + assert.NotNil(t, p.ListReminders(user, channel.Id)) + + }) } func TestUpdateListReminders(t *testing.T) { - // - //user := &model.User{ - // Email: "-@-.-", - // Nickname: "TestUser", - // Password: model.NewId(), - // Username: "testuser", - // Roles: model.SYSTEM_USER_ROLE_ID, - // Locale: "en", - //} - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: "ididididid", - // Occurrence: time.Now(), - // }, - //} - // - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // Username: user.Username, - // Message: "Message", - // When: "in 1 second", - // Occurrences: occurrences, - // Completed: time.Time{}.AddDate(1, 1, 1), - // }, - //} - // - //stringReminders, _ := json.Marshal(reminders) - // - //channel := &model.Channel{ - // Id: model.NewId(), - //} - // - //post := &model.Post{ - // ChannelId: channel.Id, - // PendingPostId: model.NewId(), - // UserId: user.Id, - // Props: model.StringInterface{}, - //} - // - //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("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) - // api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) - // api.On("UpdatePost", mock.AnythingOfType("*model.Post")).Return(nil, nil) - // api.On("GetPost", mock.AnythingOfType("string")).Return(post, nil) - // api.On("UpdateEphemeralPost", mock.Anything, post).Return(post) - // api.On("KVGet", mock.Anything).Return(stringReminders, nil) - // return api - //} - // - //t.Run("if update list happens", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.API = api - // - // p.UpdateListReminders(user.Id, model.NewId(), 0) - // - //}) + + user := &model.User{ + Email: "-@-.-", + Nickname: "TestUser", + Password: model.NewId(), + Username: "testuser", + Roles: model.SYSTEM_USER_ROLE_ID, + Locale: "en", + } + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: "ididididid", + Occurrence: time.Now(), + }, + } + + reminders := []Reminder{ + { + Id: model.NewId(), + Username: user.Username, + Message: "Message", + When: "in 1 second", + Occurrences: occurrences, + Completed: time.Time{}.AddDate(1, 1, 1), + }, + } + + stringReminders, _ := json.Marshal(reminders) + + channel := &model.Channel{ + Id: model.NewId(), + } + + post := &model.Post{ + ChannelId: channel.Id, + PendingPostId: model.NewId(), + UserId: user.Id, + Props: model.StringInterface{}, + } + + 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("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) + api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) + api.On("UpdateEphemeralPost", mock.Anything, mock.Anything).Return(post) + api.On("KVGet", mock.Anything).Return(stringReminders, nil) + return api + } + + t.Run("if update list happens", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + p.UpdateListReminders(user.Id, model.NewId(), channel.Id, 0) + + }) } func TestListCompletedReminders(t *testing.T) { - //user := &model.User{ - // Email: "-@-.-", - // Nickname: "TestUser", - // Password: model.NewId(), - // Username: "testuser", - // Roles: model.SYSTEM_USER_ROLE_ID, - // Locale: "en", - //} - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: "ididididid", - // Occurrence: time.Now(), - // }, - //} - // - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // Username: user.Username, - // Message: "Message", - // When: "in 1 second", - // Occurrences: occurrences, - // Completed: time.Time{}.AddDate(1, 1, 1), - // }, - //} - // - //stringReminders, _ := json.Marshal(reminders) - // - //channel := &model.Channel{ - // Id: model.NewId(), - //} - // - //post := &model.Post{ - // ChannelId: channel.Id, - // PendingPostId: model.NewId(), - // UserId: user.Id, - // Props: model.StringInterface{}, - //} - // - //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("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) - // api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) - // api.On("UpdatePost", mock.AnythingOfType("*model.Post")).Return(nil, nil) - // api.On("GetPost", mock.AnythingOfType("string")).Return(post, nil) - // api.On("UpdateEphemeralPost", mock.AnythingOfType("string"), mock.Anything).Return(post) - // api.On("KVGet", mock.Anything).Return(stringReminders, nil) - // return api - //} - // - //t.Run("if list completed happens", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.API = api - // - // p.ListCompletedReminders(user.Id, post.Id, channel.Id) - // - //}) + user := &model.User{ + Email: "-@-.-", + Nickname: "TestUser", + Password: model.NewId(), + Username: "testuser", + Roles: model.SYSTEM_USER_ROLE_ID, + Locale: "en", + } + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: "ididididid", + Occurrence: time.Now(), + }, + } + + reminders := []Reminder{ + { + Id: model.NewId(), + Username: user.Username, + Message: "Message", + When: "in 1 second", + Occurrences: occurrences, + Completed: time.Time{}.AddDate(1, 1, 1), + }, + } + + stringReminders, _ := json.Marshal(reminders) + + channel := &model.Channel{ + Id: model.NewId(), + } + + post := &model.Post{ + ChannelId: channel.Id, + PendingPostId: model.NewId(), + UserId: user.Id, + Props: model.StringInterface{}, + } + + 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("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) + api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) + api.On("UpdateEphemeralPost", mock.AnythingOfType("string"), mock.Anything).Return(post) + api.On("KVGet", mock.Anything).Return(stringReminders, nil) + return api + } + + t.Run("if list completed happens", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + p.ListCompletedReminders(user.Id, post.Id, channel.Id) + + }) } -//func TestDeleteCompletedReminders(t *testing.T) { -// -// user := &model.User{ -// Email: "-@-.-", -// Nickname: "TestUser", -// Password: model.NewId(), -// Username: "testuser", -// Roles: model.SYSTEM_USER_ROLE_ID, -// Locale: "en", -// } -// -// occurrences := []Occurrence{ -// { -// Id: model.NewId(), -// ReminderId: "ididididid", -// Occurrence: time.Now(), -// }, -// } -// -// reminders := []Reminder{ -// { -// Id: model.NewId(), -// Username: user.Username, -// Message: "Message", -// When: "in 1 second", -// Occurrences: occurrences, -// Completed: time.Time{}.AddDate(1, 1, 1), -// }, -// } -// -// stringReminders, _ := json.Marshal(reminders) -// -// channel := &model.Channel{ -// Id: model.NewId(), -// } -// -// post := &model.Post{ -// ChannelId: channel.Id, -// PendingPostId: model.NewId(), -// UserId: user.Id, -// Props: model.StringInterface{}, -// } -// -// 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("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) -// api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) -// api.On("UpdatePost", mock.AnythingOfType("*model.Post")).Return(nil, nil) -// api.On("GetPost", mock.AnythingOfType("string")).Return(post, nil) -// api.On("KVGet", mock.Anything).Return(stringReminders, nil) -// api.On("KVSet", user.Username, stringReminders ).Return(nil) -// return api -// } -// -// t.Run("if delete completed happens", func(t *testing.T) { -// -// api := setupAPI() -// defer api.AssertExpectations(t) -// -// p := &Plugin{} -// p.API = api -// -// p.DeleteCompletedReminders(user.Id) -// -// }) -// -//} +func TestDeleteCompletedReminders(t *testing.T) { + + user := &model.User{ + Email: "-@-.-", + Nickname: "TestUser", + Password: model.NewId(), + Username: "testuser", + Roles: model.SYSTEM_USER_ROLE_ID, + Locale: "en", + } + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: time.Now(), + }, + } + + reminders := []Reminder{ + { + Id: model.NewId(), + Username: user.Username, + Message: "Message", + When: "in 1 second", + Occurrences: occurrences, + Completed: time.Time{}.AddDate(1, 1, 1), + }, + } + + 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("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) + api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) + api.On("KVGet", mock.Anything).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + return api + } + + t.Run("if delete completed happens", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + p.DeleteCompletedReminders(user.Id) + + }) + +} diff --git a/server/occurrence_test.go b/server/occurrence_test.go index 0a4f12c..acb2b37 100755 --- a/server/occurrence_test.go +++ b/server/occurrence_test.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "strings" "testing" "time" @@ -17,34 +18,75 @@ func TestClearScheduledOccurrence(t *testing.T) { func TestCreateOccurrences(t *testing.T) { - //user := &model.User{ - // Email: "-@-.-", - // Nickname: "TestUser", - // Password: model.NewId(), - // Username: "testuser", - // Roles: model.SYSTEM_USER_ROLE_ID, - // Locale: "en", - //} - // - //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("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) - // return api + user := &model.User{ + Email: "-@-.-", + Nickname: "TestUser", + Password: model.NewId(), + Username: "testuser", + Roles: model.SYSTEM_USER_ROLE_ID, + Locale: "en", + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + + //reminders := []Reminder{ + // { + // Id: model.NewId(), + // TeamId: model.NewId(), + // Username: user.Username, + // Message: "Hello", + // Target: "me", + // When: "in one minute", + // Occurrences: occurrences, + // }, //} - // - //t.Run("if creates occurrences", func(t *testing.T) { - // - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.API = api - // - // - //}) + + request := &ReminderRequest{ + TeamId: model.NewId(), + Username: user.Username, + Payload: "Hello in one minute", + Reminder: Reminder{ + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Occurrences: occurrences, + Message: "Hello", + Target: "me", + When: "in one minute", + }, + } + + //stringReminders, _ := json.Marshal(reminders) + stringOccurrences, _ := json.Marshal(occurrences) + 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", 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 + } + + t.Run("if creates occurrences", func(t *testing.T) { + + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + assert.Nil(t, p.CreateOccurrences(request)) + + }) } func TestIn(t *testing.T) { diff --git a/server/reminder_test.go b/server/reminder_test.go index a6da46e..8befc97 100755 --- a/server/reminder_test.go +++ b/server/reminder_test.go @@ -202,140 +202,241 @@ func TestGetReminders(t *testing.T) { func TestUpdateReminder(t *testing.T) { - //user := &model.User{ - // Email: "-@-.-", - // Nickname: "TestUser", - // Password: model.NewId(), - // Username: "testuser", - // Roles: model.SYSTEM_USER_ROLE_ID, - // Locale: "en", - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - // - //reminders := []Reminder{ - // { - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // Occurrences: 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", user.Username).Return(stringReminders, nil) - // api.On("KVSet", user.Username).Maybe() - // api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) - // //api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) - // return api - //} - // - //t.Run("if updates reminders", func(t *testing.T) { - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.API = api - // - // assert.Nil(t, p.UpdateReminder(user.Id, reminders[0])) - // - //}) - // + user := &model.User{ + Email: "-@-.-", + Nickname: "TestUser", + Password: model.NewId(), + Username: "testuser", + Roles: model.SYSTEM_USER_ROLE_ID, + Locale: "en", + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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", user.Username).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) + return api + } + + t.Run("if updates reminders", func(t *testing.T) { + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + assert.Nil(t, p.UpdateReminder(user.Id, reminders[0])) + + }) } func TestUpsertReminder(t *testing.T) { - //user := &model.User{ - // Email: "-@-.-", - // Nickname: "TestUser", - // Password: model.NewId(), - // Username: "testuser", - // Roles: model.SYSTEM_USER_ROLE_ID, - // Locale: "en", - //} - //testTime := time.Now().UTC().Round(time.Second) - // - //occurrences := []Occurrence{ - // { - // Id: model.NewId(), - // ReminderId: model.NewId(), - // Occurrence: testTime, - // }, - //} - // - //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, - // Payload: "Hello in one minute", - // Reminder: Reminder{ - // Id: model.NewId(), - // TeamId: model.NewId(), - // Username: user.Username, - // Message: "Hello", - // Target: "me", - // When: "in one minute", - // }, - //} - // - //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", user.Username).Return(stringReminders, nil) - // api.On("KVSet", user.Username).Maybe() - // //api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) - // api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) - // return api - //} - // - //t.Run("if updates reminders", func(t *testing.T) { - // api := setupAPI() - // defer api.AssertExpectations(t) - // - // p := &Plugin{} - // p.API = api - // - // assert.Nil(t, p.UpsertReminder(request)) - // - //}) + user := &model.User{ + Email: "-@-.-", + Nickname: "TestUser", + Password: model.NewId(), + Username: "testuser", + Roles: model.SYSTEM_USER_ROLE_ID, + Locale: "en", + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + + 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, + Payload: "Hello in one minute", + Reminder: Reminder{ + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Occurrences: occurrences, + Message: "Hello", + Target: "me", + When: "in one minute", + }, + } + + 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", user.Username).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + api.On("GetUserByUsername", mock.AnythingOfType("string")).Return(user, nil) + return api + } + + t.Run("if updates reminders", func(t *testing.T) { + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + assert.Nil(t, p.UpsertReminder(request)) + + }) } func TestDeleteReminder(t *testing.T) { + user := &model.User{ + Email: "-@-.-", + Nickname: "TestUser", + Password: model.NewId(), + Username: "testuser", + Roles: model.SYSTEM_USER_ROLE_ID, + Locale: "en", + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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", user.Username).Return(stringReminders, nil) + api.On("KVSet", mock.Anything, mock.Anything).Return(nil) + api.On("GetUser", mock.AnythingOfType("string")).Return(user, nil) + return api + } + + t.Run("if deletes reminder", func(t *testing.T) { + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + assert.Nil(t, p.DeleteReminder(user.Id, reminders[0])) + + }) + } func TestDeleteReminders(t *testing.T) { + user := &model.User{ + Email: "-@-.-", + Nickname: "TestUser", + Password: model.NewId(), + Username: "testuser", + Roles: model.SYSTEM_USER_ROLE_ID, + Locale: "en", + } + testTime := time.Now().UTC().Round(time.Second) + + occurrences := []Occurrence{ + { + Id: model.NewId(), + ReminderId: model.NewId(), + Occurrence: testTime, + }, + } + + reminders := []Reminder{ + { + Id: model.NewId(), + TeamId: model.NewId(), + Username: user.Username, + Message: "Hello", + Target: "me", + When: "in one minute", + Occurrences: 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", user.Username).Return(stringReminders, nil) + api.On("KVDelete", user.Username).Return(nil) + return api + } + + t.Run("if deletes reminders", func(t *testing.T) { + api := setupAPI() + defer api.AssertExpectations(t) + + p := &Plugin{} + p.API = api + + assert.Equal(t, p.DeleteReminders(user), "clear.response") + + }) }