summaryrefslogtreecommitdiffstats
path: root/web/subpath_test.go
blob: 92b1a5d3c1b4d7f5a025d22cd9c0b6c88cd55bf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package web_test

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/web"
)

func TestUpdateAssetsSubpath(t *testing.T) {
	t.Run("no client dir", func(t *testing.T) {
		tempDir, err := ioutil.TempDir("", "test_update_assets_subpath")
		require.NoError(t, err)
		defer os.RemoveAll(tempDir)
		os.Chdir(tempDir)

		err = web.UpdateAssetsSubpath("/")
		require.Error(t, err)
	})

	t.Run("valid", func(t *testing.T) {
		tempDir, err := ioutil.TempDir("", "test_update_assets_subpath")
		require.NoError(t, err)
		defer os.RemoveAll(tempDir)
		os.Chdir(tempDir)

		err = os.Mkdir(model.CLIENT_DIR, 0700)
		require.NoError(t, err)

		testCases := []struct {
			Description      string
			RootHTML         string
			MainCSS          string
			Subpath          string
			ExpectedRootHTML string
			ExpectedMainCSS  string
		}{
			{
				"no changes required, empty subpath provided",
				baseRootHtml,
				baseCss,
				"",
				baseRootHtml,
				baseCss,
			},
			{
				"no changes required",
				baseRootHtml,
				baseCss,
				"/",
				baseRootHtml,
				baseCss,
			},
			{
				"subpath",
				baseRootHtml,
				baseCss,
				"/subpath",
				subpathRootHtml,
				subpathCss,
			},
			{
				"new subpath from old",
				subpathRootHtml,
				subpathCss,
				"/nested/subpath",
				newSubpathRootHtml,
				newSubpathCss,
			},
			{
				"resetting to /",
				subpathRootHtml,
				subpathCss,
				"/",
				resetRootHtml,
				baseCss,
			},
		}

		for _, testCase := range testCases {
			t.Run(testCase.Description, func(t *testing.T) {
				ioutil.WriteFile(filepath.Join(tempDir, model.CLIENT_DIR, "root.html"), []byte(testCase.RootHTML), 0700)
				ioutil.WriteFile(filepath.Join(tempDir, model.CLIENT_DIR, "main.css"), []byte(testCase.MainCSS), 0700)
				err := web.UpdateAssetsSubpath(testCase.Subpath)
				require.NoError(t, err)

				contents, err := ioutil.ReadFile(filepath.Join(tempDir, model.CLIENT_DIR, "root.html"))
				require.NoError(t, err)
				require.Equal(t, testCase.ExpectedRootHTML, string(contents))

				contents, err = ioutil.ReadFile(filepath.Join(tempDir, model.CLIENT_DIR, "main.css"))
				require.NoError(t, err)
				require.Equal(t, testCase.ExpectedMainCSS, string(contents))

			})
		}
	})
}