github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/services/sharedchannel/permalink_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package sharedchannel
     5  
     6  import (
     7  	"context"
     8  	"net/url"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/masterhung0112/hk_server/v5/model"
    14  	"github.com/masterhung0112/hk_server/v5/plugin/plugintest/mock"
    15  	"github.com/masterhung0112/hk_server/v5/store/storetest/mocks"
    16  	"github.com/masterhung0112/hk_server/v5/utils"
    17  )
    18  
    19  func TestProcessPermalinkToRemote(t *testing.T) {
    20  	scs := &Service{
    21  		server: &MockServerIface{},
    22  		app:    &MockAppIface{},
    23  	}
    24  
    25  	mockStore := &mocks.Store{}
    26  	mockPostStore := mocks.PostStore{}
    27  	utils.TranslationsPreInit()
    28  
    29  	pl := &model.PostList{}
    30  	mockPostStore.On("Get", context.Background(), "postID", true, false, false, "").Return(pl, nil)
    31  
    32  	mockStore.On("Post").Return(&mockPostStore)
    33  
    34  	mockServer := scs.server.(*MockServerIface)
    35  	mockServer.On("GetStore").Return(mockStore)
    36  
    37  	mockApp := scs.app.(*MockAppIface)
    38  	mockApp.On("SendEphemeralPost", "user", mock.AnythingOfType("*model.Post")).Return(&model.Post{}).Times(1)
    39  	defer mockApp.AssertExpectations(t)
    40  
    41  	t.Run("same channel", func(t *testing.T) {
    42  		post := &model.Post{
    43  			Message:   "hello world https://comm.matt.com/team/pl/postID link",
    44  			ChannelId: "sourceChan",
    45  			UserId:    "user",
    46  		}
    47  
    48  		*pl = model.PostList{
    49  			Order: []string{"1"},
    50  			Posts: map[string]*model.Post{
    51  				"1": {
    52  					ChannelId: "sourceChan",
    53  					UserId:    "user",
    54  				},
    55  			},
    56  		}
    57  
    58  		out := scs.processPermalinkToRemote(post)
    59  		assert.Equal(t, "hello world https://comm.matt.com/team/plshared/postID link", out)
    60  	})
    61  
    62  	t.Run("different channel", func(t *testing.T) {
    63  		post := &model.Post{
    64  			Message:   "hello world https://comm.matt.com/team/pl/postID link https://comm.matt.com/team/pl/postID ",
    65  			ChannelId: "sourceChan",
    66  			UserId:    "user",
    67  		}
    68  
    69  		*pl = model.PostList{
    70  			Order: []string{"1"},
    71  			Posts: map[string]*model.Post{
    72  				"1": {
    73  					ChannelId: "otherChan",
    74  				},
    75  			},
    76  		}
    77  		out := scs.processPermalinkToRemote(post)
    78  		assert.Equal(t, "hello world https://comm.matt.com/team/pl/postID link https://comm.matt.com/team/pl/postID ", out)
    79  	})
    80  }
    81  
    82  func TestProcessPermalinkFromRemote(t *testing.T) {
    83  	t.Run("has match", func(t *testing.T) {
    84  		parsed, _ := url.Parse("http://mysite.com")
    85  		scs := &Service{
    86  			server:  &MockServerIface{},
    87  			siteURL: parsed,
    88  		}
    89  
    90  		out := scs.processPermalinkFromRemote(&model.Post{Message: "hello world https://comm.matt.com/team/plshared/postID link"},
    91  			&model.Team{Name: "myteam"})
    92  		assert.Equal(t,
    93  			"hello world http://mysite.com/myteam/pl/postID link",
    94  			out)
    95  	})
    96  
    97  	t.Run("does not match", func(t *testing.T) {
    98  		parsed, _ := url.Parse("http://mysite.com")
    99  		scs := &Service{
   100  			server:  &MockServerIface{},
   101  			siteURL: parsed,
   102  		}
   103  
   104  		out := scs.processPermalinkFromRemote(&model.Post{Message: "hello world https://comm.matt.com/team/pl/postID link"},
   105  			&model.Team{Name: "myteam"})
   106  		assert.Equal(t,
   107  			"hello world https://comm.matt.com/team/pl/postID link",
   108  			out)
   109  	})
   110  }