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

Commit

Permalink
all current unit tests uncommented and passing
Browse files Browse the repository at this point in the history
  • Loading branch information
scottleedavis committed May 15, 2019
1 parent 7210553 commit e725c04
Show file tree
Hide file tree
Showing 5 changed files with 1,527 additions and 1,379 deletions.
327 changes: 185 additions & 142 deletions server/command_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
Loading

0 comments on commit e725c04

Please sign in to comment.