github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/services/imageproxy/atmos_camo_test.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package imageproxy
     5  
     6  import (
     7  	"io/ioutil"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"testing"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  	"github.com/mattermost/mattermost-server/services/httpservice"
    14  	"github.com/mattermost/mattermost-server/utils/testutils"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func makeTestAtmosCamoProxy() *ImageProxy {
    20  	configService := &testutils.StaticConfigService{
    21  		Cfg: &model.Config{
    22  			ServiceSettings: model.ServiceSettings{
    23  				SiteURL:                             model.NewString("https://mattermost.example.com"),
    24  				AllowedUntrustedInternalConnections: model.NewString("127.0.0.1"),
    25  			},
    26  			ImageProxySettings: model.ImageProxySettings{
    27  				Enable:                  model.NewBool(true),
    28  				ImageProxyType:          model.NewString(model.IMAGE_PROXY_TYPE_ATMOS_CAMO),
    29  				RemoteImageProxyURL:     model.NewString("http://images.example.com"),
    30  				RemoteImageProxyOptions: model.NewString("7e5f3fab20b94782b43cdb022a66985ef28ba355df2c5d5da3c9a05e4b697bac"),
    31  			},
    32  		},
    33  	}
    34  
    35  	return MakeImageProxy(configService, httpservice.MakeHTTPService(configService))
    36  }
    37  
    38  func TestAtmosCamoBackend_GetImage(t *testing.T) {
    39  	imageURL := "http://www.mattermost.org/wp-content/uploads/2016/03/logoHorizontalWhite.png"
    40  	proxiedURL := "http://images.example.com/62183a1cf0a4927c3b56d249366c2745e34ffe63/687474703a2f2f7777772e6d61747465726d6f73742e6f72672f77702d636f6e74656e742f75706c6f6164732f323031362f30332f6c6f676f486f72697a6f6e74616c57686974652e706e67"
    41  
    42  	proxy := makeTestAtmosCamoProxy()
    43  
    44  	recorder := httptest.NewRecorder()
    45  	request, _ := http.NewRequest(http.MethodGet, "", nil)
    46  	proxy.GetImage(recorder, request, imageURL)
    47  	resp := recorder.Result()
    48  
    49  	assert.Equal(t, http.StatusFound, resp.StatusCode)
    50  	assert.Equal(t, proxiedURL, resp.Header.Get("Location"))
    51  }
    52  
    53  func TestAtmosCamoBackend_GetImageDirect(t *testing.T) {
    54  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    55  		w.Header().Set("Cache-Control", "max-age=2592000, private")
    56  		w.Header().Set("Content-Type", "image/png")
    57  		w.Header().Set("Content-Length", "10")
    58  
    59  		w.WriteHeader(http.StatusOK)
    60  		w.Write([]byte("1111111111"))
    61  	})
    62  
    63  	mock := httptest.NewServer(handler)
    64  	defer mock.Close()
    65  
    66  	proxy := makeTestAtmosCamoProxy()
    67  	proxy.ConfigService.(*testutils.StaticConfigService).Cfg.ImageProxySettings.RemoteImageProxyURL = model.NewString(mock.URL)
    68  
    69  	body, contentType, err := proxy.GetImageDirect("https://example.com/image.png")
    70  
    71  	assert.Nil(t, err)
    72  	assert.Equal(t, "image/png", contentType)
    73  
    74  	require.NotNil(t, body)
    75  	respBody, _ := ioutil.ReadAll(body)
    76  	assert.Equal(t, []byte("1111111111"), respBody)
    77  }
    78  
    79  func TestAtmosCamoBackend_GetProxiedImageURL(t *testing.T) {
    80  	imageURL := "http://www.mattermost.org/wp-content/uploads/2016/03/logoHorizontal.png"
    81  	proxiedURL := "http://images.example.com/5b6f6661516bc837b89b54566eb619d14a5c3eca/687474703a2f2f7777772e6d61747465726d6f73742e6f72672f77702d636f6e74656e742f75706c6f6164732f323031362f30332f6c6f676f486f72697a6f6e74616c2e706e67"
    82  
    83  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    84  	})
    85  
    86  	mock := httptest.NewServer(handler)
    87  	defer mock.Close()
    88  
    89  	proxy := makeTestAtmosCamoProxy()
    90  
    91  	// Most of this logic is tested in TestGetAtmosCamoImageURL
    92  	assert.Equal(t, proxiedURL, proxy.GetProxiedImageURL(imageURL))
    93  }
    94  
    95  func TestGetAtmosCamoImageURL(t *testing.T) {
    96  	imageURL := "http://www.mattermost.org/wp-content/uploads/2016/03/logoHorizontal.png"
    97  	proxiedURL := "http://images.example.com/5b6f6661516bc837b89b54566eb619d14a5c3eca/687474703a2f2f7777772e6d61747465726d6f73742e6f72672f77702d636f6e74656e742f75706c6f6164732f323031362f30332f6c6f676f486f72697a6f6e74616c2e706e67"
    98  
    99  	defaultSiteURL := "https://mattermost.example.com"
   100  	proxyURL := "http://images.example.com"
   101  	options := "7e5f3fab20b94782b43cdb022a66985ef28ba355df2c5d5da3c9a05e4b697bac"
   102  
   103  	for _, test := range []struct {
   104  		Name     string
   105  		Input    string
   106  		SiteURL  string
   107  		Expected string
   108  	}{
   109  		{
   110  			Name:     "should proxy image",
   111  			Input:    imageURL,
   112  			SiteURL:  defaultSiteURL,
   113  			Expected: proxiedURL,
   114  		},
   115  		{
   116  			Name:     "should proxy image when no site URL is set",
   117  			Input:    imageURL,
   118  			SiteURL:  "",
   119  			Expected: proxiedURL,
   120  		},
   121  		{
   122  			Name:     "should proxy image when a site URL with a subpath is set",
   123  			Input:    imageURL,
   124  			SiteURL:  proxyURL + "/subpath",
   125  			Expected: proxiedURL,
   126  		},
   127  		{
   128  			Name:     "should not proxy a relative image",
   129  			Input:    "/static/logo.png",
   130  			SiteURL:  defaultSiteURL,
   131  			Expected: "/static/logo.png",
   132  		},
   133  		{
   134  			Name:     "should not proxy an image on the Mattermost server",
   135  			Input:    "https://mattermost.example.com/static/logo.png",
   136  			SiteURL:  defaultSiteURL,
   137  			Expected: "https://mattermost.example.com/static/logo.png",
   138  		},
   139  		{
   140  			Name:     "should not proxy an image on the Mattermost server when a subpath is set",
   141  			Input:    "https://mattermost.example.com/static/logo.png",
   142  			SiteURL:  defaultSiteURL + "/static",
   143  			Expected: "https://mattermost.example.com/static/logo.png",
   144  		},
   145  		{
   146  			Name:     "should not proxy an image that has already been proxied",
   147  			Input:    proxiedURL,
   148  			SiteURL:  defaultSiteURL,
   149  			Expected: proxiedURL,
   150  		},
   151  	} {
   152  		t.Run(test.Name, func(t *testing.T) {
   153  			assert.Equal(t, test.Expected, getAtmosCamoImageURL(test.Input, test.SiteURL, proxyURL, options))
   154  		})
   155  	}
   156  
   157  }
   158  
   159  func TestAtmosCamoBackend_GetUnproxiedImageURL(t *testing.T) {
   160  	imageURL := "http://www.mattermost.org/wp-content/uploads/2016/03/logoHorizontal.png"
   161  	proxiedURL := "http://images.example.com/5b6f6661516bc837b89b54566eb619d14a5c3eca/687474703a2f2f7777772e6d61747465726d6f73742e6f72672f77702d636f6e74656e742f75706c6f6164732f323031362f30332f6c6f676f486f72697a6f6e74616c2e706e67"
   162  
   163  	proxy := makeTestAtmosCamoProxy()
   164  
   165  	for _, test := range []struct {
   166  		Name     string
   167  		Input    string
   168  		Expected string
   169  	}{
   170  		{
   171  			Name:     "should remove proxy",
   172  			Input:    proxiedURL,
   173  			Expected: imageURL,
   174  		},
   175  		{
   176  			Name:     "should not remove proxy from a relative image",
   177  			Input:    "/static/logo.png",
   178  			Expected: "/static/logo.png",
   179  		},
   180  		{
   181  			Name:     "should not remove proxy from an image on the Mattermost server",
   182  			Input:    "https://mattermost.example.com/static/logo.png",
   183  			Expected: "https://mattermost.example.com/static/logo.png",
   184  		},
   185  		{
   186  			Name:     "should not remove proxy from a non-proxied image",
   187  			Input:    imageURL,
   188  			Expected: imageURL,
   189  		},
   190  	} {
   191  		t.Run(test.Name, func(t *testing.T) {
   192  			assert.Equal(t, test.Expected, proxy.GetUnproxiedImageURL(test.Input))
   193  		})
   194  	}
   195  }