diff options
author | Daniel Schalla <daniel@schalla.me> | 2018-07-30 20:55:38 +0200 |
---|---|---|
committer | Christopher Speller <crspeller@gmail.com> | 2018-07-30 11:55:38 -0700 |
commit | d23ca07133e9bc5eed14d87af563471b4ef963cd (patch) | |
tree | ae01bb8f09600110c22a5f881be24e8d6f204986 /app/plugin_hooks_test.go | |
parent | 08e54ed8244e2ec9a278d16f80d5ed2a8e2964f4 (diff) | |
download | chat-d23ca07133e9bc5eed14d87af563471b4ef963cd.tar.gz chat-d23ca07133e9bc5eed14d87af563471b4ef963cd.tar.bz2 chat-d23ca07133e9bc5eed14d87af563471b4ef963cd.zip |
Login Hooks (#9177)
Tests; gofmt
Diffstat (limited to 'app/plugin_hooks_test.go')
-rw-r--r-- | app/plugin_hooks_test.go | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/app/plugin_hooks_test.go b/app/plugin_hooks_test.go index 488d81757..3f447179f 100644 --- a/app/plugin_hooks_test.go +++ b/app/plugin_hooks_test.go @@ -19,6 +19,7 @@ import ( "github.com/mattermost/mattermost-server/plugin/plugintest/mock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "time" ) func compileGo(t *testing.T, sourceCode, outputPath string) { @@ -371,3 +372,144 @@ func TestHookFileWillBeUploaded(t *testing.T) { io.Copy(&resultBuf, fileReader) assert.Equal(t, "changedtext", resultBuf.String()) } + +func TestUserWillLogIn_Blocked(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + err := th.App.UpdatePassword(th.BasicUser, "hunter2") + + if err != nil { + t.Errorf("Error updating user password: %s", err) + } + + SetAppEnvironmentWithPlugins(t, + []string{ + ` + package main + + import ( + "github.com/mattermost/mattermost-server/plugin" + "github.com/mattermost/mattermost-server/model" + ) + + type MyPlugin struct { + plugin.MattermostPlugin + } + + func (p *MyPlugin) UserWillLogIn(c *plugin.Context, user *model.User) string { + return "Blocked By Plugin" + } + + func main() { + plugin.ClientMain(&MyPlugin{}) + } + `}, th.App, th.App.NewPluginAPI) + + user, err := th.App.AuthenticateUserForLogin("", th.BasicUser.Email, "hunter2", "", false) + + if user != nil { + t.Errorf("Expected nil, got %+v", user) + } + + if err == nil { + t.Errorf("Expected err, got nil") + } +} + +func TestUserWillLogInIn_Passed(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + err := th.App.UpdatePassword(th.BasicUser, "hunter2") + + if err != nil { + t.Errorf("Error updating user password: %s", err) + } + + SetAppEnvironmentWithPlugins(t, + []string{ + ` + package main + + import ( + "github.com/mattermost/mattermost-server/plugin" + "github.com/mattermost/mattermost-server/model" + ) + + type MyPlugin struct { + plugin.MattermostPlugin + } + + func (p *MyPlugin) UserWillLogIn(c *plugin.Context, user *model.User) string { + return "" + } + + func main() { + plugin.ClientMain(&MyPlugin{}) + } + `}, th.App, th.App.NewPluginAPI) + + user, err := th.App.AuthenticateUserForLogin("", th.BasicUser.Email, "hunter2", "", false) + + if user == nil { + t.Errorf("Expected user object, got nil") + } + + if err != nil { + t.Errorf("Expected nil, got %s", err) + } +} + +func TestUserHasLoggedIn(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + err := th.App.UpdatePassword(th.BasicUser, "hunter2") + + if err != nil { + t.Errorf("Error updating user password: %s", err) + } + + SetAppEnvironmentWithPlugins(t, + []string{ + ` + package main + + import ( + "github.com/mattermost/mattermost-server/plugin" + "github.com/mattermost/mattermost-server/model" + ) + + type MyPlugin struct { + plugin.MattermostPlugin + } + + func (p *MyPlugin) UserHasLoggedIn(c *plugin.Context, user *model.User) { + user.FirstName = "plugin-callback-success" + p.API.UpdateUser(user) + } + + func main() { + plugin.ClientMain(&MyPlugin{}) + } + `}, th.App, th.App.NewPluginAPI) + + user, err := th.App.AuthenticateUserForLogin("", th.BasicUser.Email, "hunter2", "", false) + + if user == nil { + t.Errorf("Expected user object, got nil") + } + + if err != nil { + t.Errorf("Expected nil, got %s", err) + } + + time.Sleep(2 * time.Second) + + user, err = th.App.GetUser(th.BasicUser.Id) + + if user.FirstName != "plugin-callback-success" { + t.Errorf("Expected firstname overwrite, got default") + } +} |