github.com/haalcala/mattermost-server-change-repo@v0.0.0-20210713015153-16753fbeee5f/web/context_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package web
     5  
     6  import (
     7  	"context"
     8  	"net/http"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/mattermost/mattermost-server/v5/model"
    15  	"github.com/mattermost/mattermost-server/v5/plugin/plugintest/mock"
    16  	"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
    17  )
    18  
    19  func TestRequireHookId(t *testing.T) {
    20  	c := &Context{}
    21  	t.Run("WhenHookIdIsValid", func(t *testing.T) {
    22  		c.Params = &Params{HookId: "abcdefghijklmnopqrstuvwxyz"}
    23  		c.RequireHookId()
    24  
    25  		require.Nil(t, c.Err, "Hook Id is Valid. Should not have set error in context")
    26  	})
    27  
    28  	t.Run("WhenHookIdIsInvalid", func(t *testing.T) {
    29  		c.Params = &Params{HookId: "abc"}
    30  		c.RequireHookId()
    31  
    32  		require.Error(t, c.Err, "Should have set Error in context")
    33  		require.Equal(t, http.StatusBadRequest, c.Err.StatusCode, "Should have set status as 400")
    34  	})
    35  }
    36  
    37  func TestCloudKeyRequired(t *testing.T) {
    38  	th := SetupWithStoreMock(t)
    39  	defer th.TearDown()
    40  
    41  	th.App.Srv().SetLicense(model.NewTestLicense("cloud"))
    42  
    43  	c := &Context{
    44  		App: th.App,
    45  	}
    46  
    47  	c.CloudKeyRequired()
    48  
    49  	assert.Equal(t, c.Err.Id, "api.context.session_expired.app_error")
    50  }
    51  
    52  func TestMfaRequired(t *testing.T) {
    53  	th := SetupWithStoreMock(t)
    54  	defer th.TearDown()
    55  
    56  	mockStore := th.App.Srv().Store.(*mocks.Store)
    57  	mockUserStore := mocks.UserStore{}
    58  	mockUserStore.On("Count", mock.Anything).Return(int64(10), nil)
    59  	mockUserStore.On("Get", context.Background(), "userid").Return(nil, model.NewAppError("Userstore.Get", "storeerror", nil, "store error", http.StatusInternalServerError))
    60  	mockPostStore := mocks.PostStore{}
    61  	mockPostStore.On("GetMaxPostSize").Return(65535, nil)
    62  	mockSystemStore := mocks.SystemStore{}
    63  	mockSystemStore.On("GetByName", "UpgradedFromTE").Return(&model.System{Name: "UpgradedFromTE", Value: "false"}, nil)
    64  	mockSystemStore.On("GetByName", "InstallationDate").Return(&model.System{Name: "InstallationDate", Value: "10"}, nil)
    65  
    66  	mockStore.On("User").Return(&mockUserStore)
    67  	mockStore.On("Post").Return(&mockPostStore)
    68  	mockStore.On("System").Return(&mockSystemStore)
    69  
    70  	th.App.Srv().SetLicense(model.NewTestLicense("mfa"))
    71  
    72  	th.App.SetSession(&model.Session{Id: "abc", UserId: "userid"})
    73  
    74  	th.App.UpdateConfig(func(cfg *model.Config) {
    75  		*cfg.AnnouncementSettings.UserNoticesEnabled = false
    76  		*cfg.AnnouncementSettings.AdminNoticesEnabled = false
    77  		*cfg.ServiceSettings.EnableMultifactorAuthentication = true
    78  		*cfg.ServiceSettings.EnforceMultifactorAuthentication = true
    79  	})
    80  
    81  	c := &Context{
    82  		App: th.App,
    83  	}
    84  
    85  	c.MfaRequired()
    86  
    87  	assert.Equal(t, c.Err.Id, "api.context.get_user.app_error")
    88  }