github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/localcachelayer/main_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package localcachelayer
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/mock"
    11  
    12  	"github.com/mattermost/mattermost-server/v5/mlog"
    13  	"github.com/mattermost/mattermost-server/v5/model"
    14  	"github.com/mattermost/mattermost-server/v5/services/cache"
    15  	cachemocks "github.com/mattermost/mattermost-server/v5/services/cache/mocks"
    16  	"github.com/mattermost/mattermost-server/v5/store"
    17  	"github.com/mattermost/mattermost-server/v5/store/storetest/mocks"
    18  	"github.com/mattermost/mattermost-server/v5/testlib"
    19  )
    20  
    21  var mainHelper *testlib.MainHelper
    22  
    23  func getMockCacheProvider() cache.Provider {
    24  	mockCacheProvider := cachemocks.Provider{}
    25  	mockCacheProvider.On("NewCache", mock.Anything).
    26  		Return(cache.NewLRU(cache.LRUOptions{Size: 128}), nil)
    27  	return &mockCacheProvider
    28  }
    29  
    30  func getMockStore() *mocks.Store {
    31  	mockStore := mocks.Store{}
    32  
    33  	fakeReaction := model.Reaction{PostId: "123"}
    34  	mockReactionsStore := mocks.ReactionStore{}
    35  	mockReactionsStore.On("Save", &fakeReaction).Return(&model.Reaction{}, nil)
    36  	mockReactionsStore.On("Delete", &fakeReaction).Return(&model.Reaction{}, nil)
    37  	mockReactionsStore.On("GetForPost", "123", false).Return([]*model.Reaction{&fakeReaction}, nil)
    38  	mockReactionsStore.On("GetForPost", "123", true).Return([]*model.Reaction{&fakeReaction}, nil)
    39  	mockStore.On("Reaction").Return(&mockReactionsStore)
    40  
    41  	fakeRole := model.Role{Id: "123", Name: "role-name"}
    42  	mockRolesStore := mocks.RoleStore{}
    43  	mockRolesStore.On("Save", &fakeRole).Return(&model.Role{}, nil)
    44  	mockRolesStore.On("Delete", "123").Return(&fakeRole, nil)
    45  	mockRolesStore.On("GetByName", "role-name").Return(&fakeRole, nil)
    46  	mockRolesStore.On("GetByNames", []string{"role-name"}).Return([]*model.Role{&fakeRole}, nil)
    47  	mockRolesStore.On("PermanentDeleteAll").Return(nil)
    48  	mockStore.On("Role").Return(&mockRolesStore)
    49  
    50  	fakeScheme := model.Scheme{Id: "123", Name: "scheme-name"}
    51  	mockSchemesStore := mocks.SchemeStore{}
    52  	mockSchemesStore.On("Save", &fakeScheme).Return(&model.Scheme{}, nil)
    53  	mockSchemesStore.On("Delete", "123").Return(&model.Scheme{}, nil)
    54  	mockSchemesStore.On("Get", "123").Return(&fakeScheme, nil)
    55  	mockSchemesStore.On("PermanentDeleteAll").Return(nil)
    56  	mockStore.On("Scheme").Return(&mockSchemesStore)
    57  
    58  	fakeFileInfo := model.FileInfo{PostId: "123"}
    59  	mockFileInfoStore := mocks.FileInfoStore{}
    60  	mockFileInfoStore.On("GetForPost", "123", true, true, false).Return([]*model.FileInfo{&fakeFileInfo}, nil)
    61  	mockFileInfoStore.On("GetForPost", "123", true, true, true).Return([]*model.FileInfo{&fakeFileInfo}, nil)
    62  	mockStore.On("FileInfo").Return(&mockFileInfoStore)
    63  
    64  	fakeWebhook := model.IncomingWebhook{Id: "123"}
    65  	mockWebhookStore := mocks.WebhookStore{}
    66  	mockWebhookStore.On("GetIncoming", "123", true).Return(&fakeWebhook, nil)
    67  	mockWebhookStore.On("GetIncoming", "123", false).Return(&fakeWebhook, nil)
    68  	mockStore.On("Webhook").Return(&mockWebhookStore)
    69  
    70  	fakeEmoji := model.Emoji{Id: "123", Name: "name123"}
    71  	mockEmojiStore := mocks.EmojiStore{}
    72  	mockEmojiStore.On("Get", "123", true).Return(&fakeEmoji, nil)
    73  	mockEmojiStore.On("Get", "123", false).Return(&fakeEmoji, nil)
    74  	mockEmojiStore.On("GetByName", "name123", true).Return(&fakeEmoji, nil)
    75  	mockEmojiStore.On("GetByName", "name123", false).Return(&fakeEmoji, nil)
    76  	mockEmojiStore.On("Delete", &fakeEmoji, int64(0)).Return(nil)
    77  	mockStore.On("Emoji").Return(&mockEmojiStore)
    78  
    79  	mockCount := int64(10)
    80  	mockGuestCount := int64(12)
    81  	channelId := "channel1"
    82  	fakeChannelId := model.Channel{Id: channelId}
    83  	mockChannelStore := mocks.ChannelStore{}
    84  	mockChannelStore.On("ClearCaches").Return()
    85  	mockChannelStore.On("GetMemberCount", "id", true).Return(mockCount, nil)
    86  	mockChannelStore.On("GetMemberCount", "id", false).Return(mockCount, nil)
    87  	mockChannelStore.On("GetGuestCount", "id", true).Return(mockGuestCount, nil)
    88  	mockChannelStore.On("GetGuestCount", "id", false).Return(mockGuestCount, nil)
    89  	mockChannelStore.On("Get", channelId, true).Return(&fakeChannelId, nil)
    90  	mockChannelStore.On("Get", channelId, false).Return(&fakeChannelId, nil)
    91  	mockStore.On("Channel").Return(&mockChannelStore)
    92  
    93  	mockPinnedPostsCount := int64(10)
    94  	mockChannelStore.On("GetPinnedPostCount", "id", true).Return(mockPinnedPostsCount, nil)
    95  	mockChannelStore.On("GetPinnedPostCount", "id", false).Return(mockPinnedPostsCount, nil)
    96  
    97  	fakePosts := &model.PostList{}
    98  	fakeOptions := model.GetPostsOptions{ChannelId: "123", PerPage: 30}
    99  	mockPostStore := mocks.PostStore{}
   100  	mockPostStore.On("GetPosts", fakeOptions, true).Return(fakePosts, nil)
   101  	mockPostStore.On("GetPosts", fakeOptions, false).Return(fakePosts, nil)
   102  	mockPostStore.On("InvalidateLastPostTimeCache", "12360")
   103  
   104  	mockPostStoreOptions := model.GetPostsSinceOptions{
   105  		ChannelId:        "channelId",
   106  		Time:             1,
   107  		SkipFetchThreads: false,
   108  	}
   109  
   110  	mockPostStoreEtagResult := fmt.Sprintf("%v.%v", model.CurrentVersion, 1)
   111  	mockPostStore.On("ClearCaches")
   112  	mockPostStore.On("InvalidateLastPostTimeCache", "channelId")
   113  	mockPostStore.On("GetEtag", "channelId", true, false).Return(mockPostStoreEtagResult)
   114  	mockPostStore.On("GetEtag", "channelId", false, false).Return(mockPostStoreEtagResult)
   115  	mockPostStore.On("GetPostsSince", mockPostStoreOptions, true).Return(model.NewPostList(), nil)
   116  	mockPostStore.On("GetPostsSince", mockPostStoreOptions, false).Return(model.NewPostList(), nil)
   117  	mockStore.On("Post").Return(&mockPostStore)
   118  
   119  	fakeTermsOfService := model.TermsOfService{Id: "123", CreateAt: 11111, UserId: "321", Text: "Terms of service test"}
   120  	mockTermsOfServiceStore := mocks.TermsOfServiceStore{}
   121  	mockTermsOfServiceStore.On("InvalidateTermsOfService", "123")
   122  	mockTermsOfServiceStore.On("Save", &fakeTermsOfService).Return(&fakeTermsOfService, nil)
   123  	mockTermsOfServiceStore.On("GetLatest", true).Return(&fakeTermsOfService, nil)
   124  	mockTermsOfServiceStore.On("GetLatest", false).Return(&fakeTermsOfService, nil)
   125  	mockTermsOfServiceStore.On("Get", "123", true).Return(&fakeTermsOfService, nil)
   126  	mockTermsOfServiceStore.On("Get", "123", false).Return(&fakeTermsOfService, nil)
   127  	mockStore.On("TermsOfService").Return(&mockTermsOfServiceStore)
   128  
   129  	fakeUser := []*model.User{{
   130  		Id:          "123",
   131  		AuthData:    model.NewString("authData"),
   132  		AuthService: "authService",
   133  	}}
   134  	mockUserStore := mocks.UserStore{}
   135  	mockUserStore.On("GetProfileByIds", mock.Anything, []string{"123"}, &store.UserGetByIdsOpts{}, true).Return(fakeUser, nil)
   136  	mockUserStore.On("GetProfileByIds", mock.Anything, []string{"123"}, &store.UserGetByIdsOpts{}, false).Return(fakeUser, nil)
   137  
   138  	fakeProfilesInChannelMap := map[string]*model.User{
   139  		"456": {Id: "456"},
   140  	}
   141  	mockUserStore.On("GetAllProfilesInChannel", mock.Anything, "123", true).Return(fakeProfilesInChannelMap, nil)
   142  	mockUserStore.On("GetAllProfilesInChannel", mock.Anything, "123", false).Return(fakeProfilesInChannelMap, nil)
   143  
   144  	mockUserStore.On("Get", mock.Anything, "123").Return(fakeUser[0], nil)
   145  	users := []*model.User{
   146  		fakeUser[0],
   147  		{
   148  			Id:          "456",
   149  			AuthData:    model.NewString("authData"),
   150  			AuthService: "authService",
   151  		},
   152  	}
   153  	mockUserStore.On("GetMany", mock.Anything, []string{"123", "456"}).Return(users, nil)
   154  	mockUserStore.On("GetMany", mock.Anything, []string{"123"}).Return(users[0:1], nil)
   155  	mockStore.On("User").Return(&mockUserStore)
   156  
   157  	fakeUserTeamIds := []string{"1", "2", "3"}
   158  	mockTeamStore := mocks.TeamStore{}
   159  	mockTeamStore.On("GetUserTeamIds", "123", true).Return(fakeUserTeamIds, nil)
   160  	mockTeamStore.On("GetUserTeamIds", "123", false).Return(fakeUserTeamIds, nil)
   161  	mockStore.On("Team").Return(&mockTeamStore)
   162  
   163  	return &mockStore
   164  }
   165  
   166  func TestMain(m *testing.M) {
   167  	mlog.DisableZap()
   168  	mainHelper = testlib.NewMainHelperWithOptions(nil)
   169  	defer mainHelper.Close()
   170  
   171  	initStores()
   172  	mainHelper.Main(m)
   173  	tearDownStores()
   174  }