github.com/mattermost/mattermost-server/v5@v5.39.3/api4/import_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package api4 5 6 import ( 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/mattermost/mattermost-server/v5/model" 14 "github.com/mattermost/mattermost-server/v5/utils/fileutils" 15 ) 16 17 func TestListImports(t *testing.T) { 18 th := Setup(t) 19 defer th.TearDown() 20 21 testsDir, _ := fileutils.FindDir("tests") 22 require.NotEmpty(t, testsDir) 23 24 uploadNewImport := func(c *model.Client4, t *testing.T) string { 25 file, err := os.Open(testsDir + "/import_test.zip") 26 require.NoError(t, err) 27 28 info, err := file.Stat() 29 require.NoError(t, err) 30 31 us := &model.UploadSession{ 32 Filename: info.Name(), 33 FileSize: info.Size(), 34 Type: model.UploadTypeImport, 35 } 36 37 if c == th.LocalClient { 38 us.UserId = model.UploadNoUserID 39 } 40 41 u, resp := c.CreateUpload(us) 42 require.Nil(t, resp.Error) 43 require.NotNil(t, u) 44 45 finfo, resp := c.UploadData(u.Id, file) 46 require.Nil(t, resp.Error) 47 require.NotNil(t, finfo) 48 49 return u.Id 50 } 51 52 t.Run("no permissions", func(t *testing.T) { 53 imports, resp := th.Client.ListImports() 54 require.NotNil(t, resp.Error) 55 require.Equal(t, "api.context.permissions.app_error", resp.Error.Id) 56 require.Nil(t, imports) 57 }) 58 59 dataDir, found := fileutils.FindDir("data") 60 require.True(t, found) 61 62 th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { 63 imports, resp := c.ListImports() 64 require.Nil(t, resp.Error) 65 require.Empty(t, imports) 66 }, "no imports") 67 68 th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { 69 id := uploadNewImport(c, t) 70 id2 := uploadNewImport(c, t) 71 72 importDir := filepath.Join(dataDir, "import") 73 f, err := os.Create(filepath.Join(importDir, "import.zip.tmp")) 74 require.NoError(t, err) 75 f.Close() 76 77 imports, resp := c.ListImports() 78 require.Nil(t, resp.Error) 79 require.NotEmpty(t, imports) 80 require.Len(t, imports, 2) 81 require.Contains(t, imports, id+"_import_test.zip") 82 require.Contains(t, imports, id2+"_import_test.zip") 83 84 require.NoError(t, os.RemoveAll(importDir)) 85 }, "expected imports") 86 87 th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) { 88 th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ImportSettings.Directory = "import_new" }) 89 defer th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ImportSettings.Directory = "import" }) 90 91 importDir := filepath.Join(dataDir, "import_new") 92 93 imports, resp := c.ListImports() 94 require.Nil(t, resp.Error) 95 require.Empty(t, imports) 96 97 id := uploadNewImport(c, t) 98 imports, resp = c.ListImports() 99 require.Nil(t, resp.Error) 100 require.NotEmpty(t, imports) 101 require.Len(t, imports, 1) 102 require.Equal(t, id+"_import_test.zip", imports[0]) 103 104 require.NoError(t, os.RemoveAll(importDir)) 105 }, "change import directory") 106 }