github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/utils/utils_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"net/http"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestStringArrayIntersection(t *testing.T) {
    14  	a := []string{
    15  		"abc",
    16  		"def",
    17  		"ghi",
    18  	}
    19  	b := []string{
    20  		"jkl",
    21  	}
    22  	c := []string{
    23  		"def",
    24  	}
    25  
    26  	assert.Empty(t, StringArrayIntersection(a, b))
    27  	assert.Len(t, StringArrayIntersection(a, c), 1)
    28  }
    29  
    30  func TestRemoveDuplicatesFromStringArray(t *testing.T) {
    31  	a := []string{
    32  		"a",
    33  		"b",
    34  		"a",
    35  		"a",
    36  		"b",
    37  		"c",
    38  		"a",
    39  	}
    40  
    41  	assert.Len(t, RemoveDuplicatesFromStringArray(a), 3)
    42  }
    43  
    44  func TestStringSliceDiff(t *testing.T) {
    45  	a := []string{"one", "two", "three", "four", "five", "six"}
    46  	b := []string{"two", "seven", "four", "six"}
    47  	expected := []string{"one", "three", "five"}
    48  
    49  	assert.Equal(t, expected, StringSliceDiff(a, b))
    50  }
    51  
    52  func TestGetIpAddress(t *testing.T) {
    53  	// Test with a single IP in the X-Forwarded-For
    54  	httpRequest1 := http.Request{
    55  		Header: http.Header{
    56  			"X-Forwarded-For": []string{"10.0.0.1"},
    57  			"X-Real-Ip":       []string{"10.1.0.1"},
    58  		},
    59  		RemoteAddr: "10.2.0.1:12345",
    60  	}
    61  
    62  	assert.Equal(t, "10.0.0.1", GetIpAddress(&httpRequest1, []string{"X-Forwarded-For"}))
    63  
    64  	// Test with multiple IPs in the X-Forwarded-For
    65  	httpRequest2 := http.Request{
    66  		Header: http.Header{
    67  			"X-Forwarded-For": []string{"10.0.0.1,  10.0.0.2, 10.0.0.3"},
    68  			"X-Real-Ip":       []string{"10.1.0.1"},
    69  		},
    70  		RemoteAddr: "10.2.0.1:12345",
    71  	}
    72  
    73  	assert.Equal(t, "10.0.0.1", GetIpAddress(&httpRequest2, []string{"X-Forwarded-For"}))
    74  
    75  	// Test with an empty X-Forwarded-For
    76  	httpRequest3 := http.Request{
    77  		Header: http.Header{
    78  			"X-Forwarded-For": []string{""},
    79  			"X-Real-Ip":       []string{"10.1.0.1"},
    80  		},
    81  		RemoteAddr: "10.2.0.1:12345",
    82  	}
    83  
    84  	assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest3, []string{"X-Forwarded-For", "X-Real-Ip"}))
    85  
    86  	// Test without an X-Fowarded-For
    87  	httpRequest4 := http.Request{
    88  		Header: http.Header{
    89  			"X-Real-Ip": []string{"10.1.0.1"},
    90  		},
    91  		RemoteAddr: "10.2.0.1:12345",
    92  	}
    93  
    94  	assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest4, []string{"X-Forwarded-For", "X-Real-Ip"}))
    95  
    96  	// Test without any headers
    97  	httpRequest5 := http.Request{
    98  		RemoteAddr: "10.2.0.1:12345",
    99  	}
   100  
   101  	assert.Equal(t, "10.2.0.1", GetIpAddress(&httpRequest5, []string{"X-Forwarded-For", "X-Real-Ip"}))
   102  
   103  	// Test with both headers, but both untrusted
   104  	httpRequest6 := http.Request{
   105  		Header: http.Header{
   106  			"X-Forwarded-For": []string{"10.3.0.1"},
   107  			"X-Real-Ip":       []string{"10.1.0.1"},
   108  		},
   109  		RemoteAddr: "10.2.0.1:12345",
   110  	}
   111  
   112  	assert.Equal(t, "10.2.0.1", GetIpAddress(&httpRequest6, nil))
   113  
   114  	// Test with both headers, but only X-Real-Ip trusted
   115  	httpRequest7 := http.Request{
   116  		Header: http.Header{
   117  			"X-Forwarded-For": []string{"10.3.0.1"},
   118  			"X-Real-Ip":       []string{"10.1.0.1"},
   119  		},
   120  		RemoteAddr: "10.2.0.1:12345",
   121  	}
   122  
   123  	assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest7, []string{"X-Real-Ip"}))
   124  
   125  	// Test with X-Forwarded-For, comma separated, untrusted
   126  	httpRequest8 := http.Request{
   127  		Header: http.Header{
   128  			"X-Forwarded-For": []string{"10.3.0.1, 10.1.0.1"},
   129  		},
   130  		RemoteAddr: "10.2.0.1:12345",
   131  	}
   132  
   133  	assert.Equal(t, "10.2.0.1", GetIpAddress(&httpRequest8, nil))
   134  
   135  	// Test with X-Forwarded-For, comma separated, untrusted
   136  	httpRequest9 := http.Request{
   137  		Header: http.Header{
   138  			"X-Forwarded-For": []string{"10.3.0.1, 10.1.0.1"},
   139  		},
   140  		RemoteAddr: "10.2.0.1:12345",
   141  	}
   142  
   143  	assert.Equal(t, "10.3.0.1", GetIpAddress(&httpRequest9, []string{"X-Forwarded-For"}))
   144  
   145  	// Test with both headers, both allowed, first one in trusted used
   146  	httpRequest10 := http.Request{
   147  		Header: http.Header{
   148  			"X-Forwarded-For": []string{"10.3.0.1"},
   149  			"X-Real-Ip":       []string{"10.1.0.1"},
   150  		},
   151  		RemoteAddr: "10.2.0.1:12345",
   152  	}
   153  
   154  	assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest10, []string{"X-Real-Ip", "X-Forwarded-For"}))
   155  }
   156  
   157  func TestRemoveStringFromSlice(t *testing.T) {
   158  	a := []string{"one", "two", "three", "four", "five", "six"}
   159  	expected := []string{"one", "two", "three", "five", "six"}
   160  
   161  	assert.Equal(t, RemoveStringFromSlice("four", a), expected)
   162  }
   163  
   164  func TestAppendQueryParamsToURL(t *testing.T) {
   165  	url := "mattermost://callback"
   166  	redirectUrl := AppendQueryParamsToURL(url, map[string]string{
   167  		"key1": "value1",
   168  		"key2": "value2",
   169  	})
   170  	expected := url + "?key1=value1&key2=value2"
   171  	assert.Equal(t, redirectUrl, expected)
   172  }