From 09f0f0717cb2cbf5092bc62f63917f3fb1bba5a4 Mon Sep 17 00:00:00 2001 From: PrasannaSudharsan R Date: Mon, 22 Nov 2021 12:16:51 +0530 Subject: [PATCH] 425|Added unit tests for workspaces.go (#1800) --- server/app/workspaces.go | 4 +- server/app/workspaces_test.go | 165 ++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 server/app/workspaces_test.go diff --git a/server/app/workspaces.go b/server/app/workspaces.go index 0e826a915..7b890f111 100644 --- a/server/app/workspaces.go +++ b/server/app/workspaces.go @@ -20,12 +20,12 @@ func (a *App) GetRootWorkspace() (*model.Workspace, error) { } err := a.store.UpsertWorkspaceSignupToken(*workspace) if err != nil { - a.logger.Fatal("Unable to initialize workspace", mlog.Err(err)) + a.logger.Error("Unable to initialize workspace", mlog.Err(err)) return nil, err } workspace, err = a.store.GetWorkspace(workspaceID) if err != nil { - a.logger.Fatal("Unable to get initialized workspace", mlog.Err(err)) + a.logger.Error("Unable to get initialized workspace", mlog.Err(err)) return nil, err } diff --git a/server/app/workspaces_test.go b/server/app/workspaces_test.go new file mode 100644 index 000000000..c89eff837 --- /dev/null +++ b/server/app/workspaces_test.go @@ -0,0 +1,165 @@ +package app + +import ( + "database/sql" + "errors" + "testing" + + "github.com/golang/mock/gomock" + "github.com/mattermost/focalboard/server/model" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var errInvalidWorkspace = errors.New("invalid workspace id") + +var mockWorkspace = &model.Workspace{ + ID: "mock-workspace-id", + Title: "MockWorkspace", +} + +var mockUserWorkspaces = []model.UserWorkspace{ + { + ID: "mock-user-workspace-id", + Title: "MockUserWorkspace", + }, +} + +var errUpsertSignupToken = errors.New("upsert error") + +func TestGetRootWorkspace(t *testing.T) { + var newRootWorkspace = &model.Workspace{ + ID: "0", + Title: "NewRootWorkspace", + } + + testCases := []struct { + title string + workSpaceToReturnBeforeUpsert *model.Workspace + workSpaceToReturnAfterUpsert *model.Workspace + isError bool + }{ + { + "Success, Return new root workspace, when root workspace returned by mockstore is nil", + nil, + newRootWorkspace, + false, + }, + { + "Success, Return existing root workspace, when root workspace returned by mockstore is notnil", + newRootWorkspace, + nil, + false, + }, + { + "Fail, Return nil, when root workspace returned by mockstore is nil, and upsert new root workspace fails", + nil, + nil, + true, + }, + } + + for _, eachTestacase := range testCases { + t.Run(eachTestacase.title, func(t *testing.T) { + th, tearDown := SetupTestHelper(t) + defer tearDown() + t.Log(eachTestacase.title) + th.Store.EXPECT().GetWorkspace("0").Return(eachTestacase.workSpaceToReturnBeforeUpsert, nil) + th.Store.EXPECT().UpsertWorkspaceSignupToken(gomock.Any()).DoAndReturn( + func(arg0 model.Workspace) error { + if eachTestacase.isError { + return errUpsertSignupToken + } + th.Store.EXPECT().GetWorkspace("0").Return(eachTestacase.workSpaceToReturnAfterUpsert, nil) + return nil + }) + rootWorkSpace, err := th.App.GetRootWorkspace() + + if eachTestacase.isError { + require.Error(t, err) + } else { + assert.NotNil(t, rootWorkSpace.ID) + assert.NotNil(t, rootWorkSpace.SignupToken) + assert.Equal(t, "", rootWorkSpace.ModifiedBy) + assert.Equal(t, int64(0), rootWorkSpace.UpdateAt) + assert.Equal(t, "NewRootWorkspace", rootWorkSpace.Title) + require.NoError(t, err) + require.NotNil(t, rootWorkSpace) + } + }) + } +} + +func TestGetWorkspace(t *testing.T) { + th, tearDown := SetupTestHelper(t) + defer tearDown() + + testCases := []struct { + title string + workspaceID string + isError bool + }{ + { + "Success, Return new root workspace, when workspace returned by mockstore is not nil", + "mock-workspace-id", + false, + }, + { + "Success, Return nil, when get workspace returns an sql error", + "workspace-not-available-id", + false, + }, + { + "Fail, Return nil, when get workspace by mockstore retruns an error", + "invalid-workspace-id", + true, + }, + } + + th.Store.EXPECT().GetWorkspace("mock-workspace-id").Return(mockWorkspace, nil) + th.Store.EXPECT().GetWorkspace("invalid-workspace-id").Return(nil, errInvalidWorkspace) + th.Store.EXPECT().GetWorkspace("workspace-not-available-id").Return(nil, sql.ErrNoRows) + for _, eachTestacase := range testCases { + t.Run(eachTestacase.title, func(t *testing.T) { + t.Log(eachTestacase.title) + workSpace, err := th.App.GetWorkspace(eachTestacase.workspaceID) + + if eachTestacase.isError { + require.Error(t, err) + } else if eachTestacase.workspaceID != "workspace-not-available-id" { + assert.NotNil(t, workSpace.ID) + assert.NotNil(t, workSpace.SignupToken) + assert.Equal(t, "mock-workspace-id", workSpace.ID) + assert.Equal(t, "", workSpace.ModifiedBy) + assert.Equal(t, int64(0), workSpace.UpdateAt) + assert.Equal(t, "MockWorkspace", workSpace.Title) + require.NoError(t, err) + require.NotNil(t, workSpace) + } + }) + } +} + +func TestWorkspaceOperations(t *testing.T) { + th, tearDown := SetupTestHelper(t) + defer tearDown() + + th.Store.EXPECT().UpsertWorkspaceSettings(*mockWorkspace).Return(nil) + th.Store.EXPECT().UpsertWorkspaceSignupToken(*mockWorkspace).Return(nil) + th.Store.EXPECT().GetWorkspaceCount().Return(int64(10), nil) + th.Store.EXPECT().GetUserWorkspaces("mock-user-id").Return(mockUserWorkspaces, nil) + + errUpsertWorkspaceSettings := th.App.UpsertWorkspaceSettings(*mockWorkspace) + assert.NoError(t, errUpsertWorkspaceSettings) + + errUpsertWorkspaceSignupToken := th.App.UpsertWorkspaceSignupToken(*mockWorkspace) + assert.NoError(t, errUpsertWorkspaceSignupToken) + + count, errGetWorkspaceCount := th.App.GetWorkspaceCount() + assert.NoError(t, errGetWorkspaceCount) + assert.Equal(t, int64(10), count) + + userWorkSpace, errGetUserWorkSpace := th.App.GetUserWorkspaces("mock-user-id") + assert.NoError(t, errGetUserWorkSpace) + assert.NotNil(t, userWorkSpace) +}