From adb1a98760d4637acb8d6add12aec08b3da2e095 Mon Sep 17 00:00:00 2001 From: Arshdeep Singh Chimni Date: Fri, 6 Sep 2019 16:54:38 +0530 Subject: [PATCH] Convert tests in "app/plugin_hooks_test.go" to use assert/require (#12061) * change t.Ftal to require * remove t.errorf * fix build error * adjust for custom error * fix TestUserWillLogIn_Blocked * address pr comments --- app/plugin_hooks_test.go | 85 +++++++++++----------------------------- 1 file changed, 23 insertions(+), 62 deletions(-) diff --git a/app/plugin_hooks_test.go b/app/plugin_hooks_test.go index a20a12a0bf..87398ed409 100644 --- a/app/plugin_hooks_test.go +++ b/app/plugin_hooks_test.go @@ -11,7 +11,6 @@ import ( "net/http/httptest" "os" "path/filepath" - "strings" "testing" "time" @@ -172,9 +171,8 @@ func TestHookMessageWillBePosted(t *testing.T) { CreateAt: model.GetMillis() - 10000, } post, err := th.App.CreatePost(post, th.BasicChannel, false) - if err != nil { - t.Fatal(err) - } + require.Nil(t, err) + assert.Equal(t, "message", post.Message) retrievedPost, errSingle := th.App.Srv.Store.Post().GetSingle(post.Id) require.Nil(t, errSingle) @@ -217,15 +215,12 @@ func TestHookMessageWillBePosted(t *testing.T) { CreateAt: model.GetMillis() - 10000, } post, err := th.App.CreatePost(post, th.BasicChannel, false) - if err != nil { - t.Fatal(err) - } + require.Nil(t, err) + assert.Equal(t, "message_fromplugin", post.Message) - if retrievedPost, errSingle := th.App.Srv.Store.Post().GetSingle(post.Id); err != nil { - t.Fatal(errSingle) - } else { - assert.Equal(t, "message_fromplugin", retrievedPost.Message) - } + retrievedPost, errSingle := th.App.Srv.Store.Post().GetSingle(post.Id) + require.Nil(t, errSingle) + assert.Equal(t, "message_fromplugin", retrievedPost.Message) }) t.Run("multiple updated", func(t *testing.T) { @@ -286,9 +281,7 @@ func TestHookMessageWillBePosted(t *testing.T) { CreateAt: model.GetMillis() - 10000, } post, err := th.App.CreatePost(post, th.BasicChannel, false) - if err != nil { - t.Fatal(err) - } + require.Nil(t, err) assert.Equal(t, "prefix_message_suffix", post.Message) }) } @@ -332,9 +325,7 @@ func TestHookMessageHasBeenPosted(t *testing.T) { CreateAt: model.GetMillis() - 10000, } _, err := th.App.CreatePost(post, th.BasicChannel, false) - if err != nil { - t.Fatal(err) - } + require.Nil(t, err) } func TestHookMessageWillBeUpdated(t *testing.T) { @@ -373,15 +364,11 @@ func TestHookMessageWillBeUpdated(t *testing.T) { CreateAt: model.GetMillis() - 10000, } post, err := th.App.CreatePost(post, th.BasicChannel, false) - if err != nil { - t.Fatal(err) - } + require.Nil(t, err) assert.Equal(t, "message_", post.Message) post.Message = post.Message + "edited_" post, err = th.App.UpdatePost(post, true) - if err != nil { - t.Fatal(err) - } + require.Nil(t, err) assert.Equal(t, "message_edited_fromplugin", post.Message) } @@ -425,15 +412,11 @@ func TestHookMessageHasBeenUpdated(t *testing.T) { CreateAt: model.GetMillis() - 10000, } post, err := th.App.CreatePost(post, th.BasicChannel, false) - if err != nil { - t.Fatal(err) - } + require.Nil(t, err) assert.Equal(t, "message_", post.Message) post.Message = post.Message + "edited" _, err = th.App.UpdatePost(post, true) - if err != nil { - t.Fatal(err) - } + require.Nil(t, err) } func TestHookFileWillBeUploaded(t *testing.T) { @@ -582,8 +565,8 @@ func TestHookFileWillBeUploaded(t *testing.T) { assert.Nil(t, err) assert.NotNil(t, response) assert.Equal(t, 1, len(response.FileInfos)) + fileId := response.FileInfos[0].Id - fileInfo, err := th.App.GetFileInfo(fileId) assert.Nil(t, err) assert.NotNil(t, fileInfo) @@ -678,11 +661,7 @@ func TestUserWillLogIn_Blocked(t *testing.T) { defer th.TearDown() err := th.App.UpdatePassword(th.BasicUser, "hunter2") - - if err != nil { - t.Errorf("Error updating user password: %s", err) - } - + assert.Nil(t, err, "Error updating user password: %s", err) tearDown, _, _ := SetAppEnvironmentWithPlugins(t, []string{ ` @@ -711,9 +690,7 @@ func TestUserWillLogIn_Blocked(t *testing.T) { w := httptest.NewRecorder() _, err = th.App.DoLogin(w, r, th.BasicUser, "") - if !strings.HasPrefix(err.Id, "Login rejected by plugin") { - t.Errorf("Expected Login rejected by plugin, got %s", err.Id) - } + assert.Contains(t, err.Id, "Login rejected by plugin", "Expected Login rejected by plugin, got %s", err.Id) } func TestUserWillLogInIn_Passed(t *testing.T) { @@ -722,9 +699,7 @@ func TestUserWillLogInIn_Passed(t *testing.T) { err := th.App.UpdatePassword(th.BasicUser, "hunter2") - if err != nil { - t.Errorf("Error updating user password: %s", err) - } + assert.Nil(t, err, "Error updating user password: %s", err) tearDown, _, _ := SetAppEnvironmentWithPlugins(t, []string{ @@ -754,13 +729,8 @@ func TestUserWillLogInIn_Passed(t *testing.T) { w := httptest.NewRecorder() session, err := th.App.DoLogin(w, r, th.BasicUser, "") - if err != nil { - t.Errorf("Expected nil, got %s", err) - } - - if session.UserId != th.BasicUser.Id { - t.Errorf("Expected %s, got %s", th.BasicUser.Id, session.UserId) - } + assert.Nil(t, err, "Expected nil, got %s", err) + assert.Equal(t, session.UserId, th.BasicUser.Id) } func TestUserHasLoggedIn(t *testing.T) { @@ -769,9 +739,7 @@ func TestUserHasLoggedIn(t *testing.T) { err := th.App.UpdatePassword(th.BasicUser, "hunter2") - if err != nil { - t.Errorf("Error updating user password: %s", err) - } + assert.Nil(t, err, "Error updating user password: %s", err) tearDown, _, _ := SetAppEnvironmentWithPlugins(t, []string{ @@ -802,17 +770,13 @@ func TestUserHasLoggedIn(t *testing.T) { w := httptest.NewRecorder() _, err = th.App.DoLogin(w, r, th.BasicUser, "") - if err != nil { - t.Errorf("Expected nil, got %s", err) - } + assert.Nil(t, err, "Expected nil, got %s", err) time.Sleep(2 * time.Second) user, _ := th.App.GetUser(th.BasicUser.Id) - if user.FirstName != "plugin-callback-success" { - t.Errorf("Expected firstname overwrite, got default") - } + assert.Equal(t, user.FirstName, "plugin-callback-success", "Expected firstname overwrite, got default") } func TestUserHasBeenCreated(t *testing.T) { @@ -858,7 +822,6 @@ func TestUserHasBeenCreated(t *testing.T) { user, err = th.App.GetUser(user.Id) require.Nil(t, err) - require.Equal(t, "plugin-callback-success", user.Nickname) } @@ -988,7 +951,5 @@ func TestHookContext(t *testing.T) { CreateAt: model.GetMillis() - 10000, } _, err := th.App.CreatePost(post, th.BasicChannel, false) - if err != nil { - t.Fatal(err) - } + require.Nil(t, err) }