github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/model/mention_map_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"net/url"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestUserMentionMapFromURLValues(t *testing.T) {
    14  	fixture := []struct {
    15  		values   url.Values
    16  		expected UserMentionMap
    17  		error    bool
    18  	}{
    19  		{
    20  			url.Values{},
    21  			UserMentionMap{},
    22  			false,
    23  		},
    24  		{
    25  			url.Values{
    26  				userMentionsKey:    []string{},
    27  				userMentionsIdsKey: []string{},
    28  			},
    29  			UserMentionMap{},
    30  			false,
    31  		},
    32  		{
    33  			url.Values{
    34  				userMentionsKey:    []string{"one", "two", "three"},
    35  				userMentionsIdsKey: []string{"oneId", "twoId", "threeId"},
    36  			},
    37  			UserMentionMap{
    38  				"one":   "oneId",
    39  				"two":   "twoId",
    40  				"three": "threeId",
    41  			},
    42  			false,
    43  		},
    44  		{
    45  			url.Values{
    46  				"wrongKey":         []string{"one", "two", "three"},
    47  				userMentionsIdsKey: []string{"oneId", "twoId", "threeId"},
    48  			},
    49  			nil,
    50  			true,
    51  		},
    52  		{
    53  			url.Values{
    54  				userMentionsKey: []string{"one", "two", "three"},
    55  				"wrongKey":      []string{"oneId", "twoId", "threeId"},
    56  			},
    57  			nil,
    58  			true,
    59  		},
    60  		{
    61  			url.Values{
    62  				userMentionsKey:    []string{"one", "two"},
    63  				userMentionsIdsKey: []string{"justone"},
    64  			},
    65  			nil,
    66  			true,
    67  		},
    68  	}
    69  
    70  	for _, data := range fixture {
    71  		actualMap, actualError := UserMentionMapFromURLValues(data.values)
    72  		if data.error {
    73  			require.Error(t, actualError)
    74  			require.Nil(t, actualMap)
    75  		} else {
    76  			require.NoError(t, actualError)
    77  			require.Equal(t, actualMap, data.expected)
    78  		}
    79  	}
    80  }
    81  
    82  func TestUserMentionMap_ToURLValues(t *testing.T) {
    83  	fixture := []struct {
    84  		mentionMap UserMentionMap
    85  		expected   url.Values
    86  	}{
    87  		{
    88  			UserMentionMap{},
    89  			url.Values{},
    90  		},
    91  		{
    92  			UserMentionMap{"user": "id"},
    93  			url.Values{
    94  				userMentionsKey:    []string{"user"},
    95  				userMentionsIdsKey: []string{"id"},
    96  			},
    97  		},
    98  		{
    99  			UserMentionMap{"one": "id1", "two": "id2", "three": "id3"},
   100  			url.Values{
   101  				userMentionsKey:    []string{"one", "two", "three"},
   102  				userMentionsIdsKey: []string{"id1", "id2", "id3"},
   103  			},
   104  		},
   105  	}
   106  
   107  	for _, data := range fixture {
   108  		actualValues := data.mentionMap.ToURLValues()
   109  
   110  		// require.EqualValues does not work here directly on the url.Values, as
   111  		// the slices in the map values may be in different order; what we need to
   112  		// check is that the pairs are preserved, which can be checked converting
   113  		// back to a map with FromURLValues. We check that the test is well-formed
   114  		// by converting back the expected url.Values too.
   115  		require.Equal(t, len(actualValues), len(data.expected))
   116  
   117  		actualMentionMap, actualErr := UserMentionMapFromURLValues(actualValues)
   118  		expectedMentionMap, expectedErr := UserMentionMapFromURLValues(data.expected)
   119  
   120  		require.Equal(t, actualErr, expectedErr)
   121  		require.Equal(t, actualMentionMap, expectedMentionMap)
   122  	}
   123  }
   124  
   125  func TestChannelMentionMapFromURLValues(t *testing.T) {
   126  	fixture := []struct {
   127  		values   url.Values
   128  		expected ChannelMentionMap
   129  		error    bool
   130  	}{
   131  		{
   132  			url.Values{},
   133  			ChannelMentionMap{},
   134  			false,
   135  		},
   136  		{
   137  			url.Values{
   138  				channelMentionsKey:    []string{},
   139  				channelMentionsIdsKey: []string{},
   140  			},
   141  			ChannelMentionMap{},
   142  			false,
   143  		},
   144  		{
   145  			url.Values{
   146  				channelMentionsKey:    []string{"one", "two", "three"},
   147  				channelMentionsIdsKey: []string{"oneId", "twoId", "threeId"},
   148  			},
   149  			ChannelMentionMap{
   150  				"one":   "oneId",
   151  				"two":   "twoId",
   152  				"three": "threeId",
   153  			},
   154  			false,
   155  		},
   156  		{
   157  			url.Values{
   158  				"wrongKey":            []string{"one", "two", "three"},
   159  				channelMentionsIdsKey: []string{"oneId", "twoId", "threeId"},
   160  			},
   161  			nil,
   162  			true,
   163  		},
   164  		{
   165  			url.Values{
   166  				channelMentionsKey: []string{"one", "two", "three"},
   167  				"wrongKey":         []string{"oneId", "twoId", "threeId"},
   168  			},
   169  			nil,
   170  			true,
   171  		},
   172  		{
   173  			url.Values{
   174  				channelMentionsKey:    []string{"one", "two"},
   175  				channelMentionsIdsKey: []string{"justone"},
   176  			},
   177  			nil,
   178  			true,
   179  		},
   180  	}
   181  
   182  	for _, data := range fixture {
   183  		actualMap, actualError := ChannelMentionMapFromURLValues(data.values)
   184  		if data.error {
   185  			require.Error(t, actualError)
   186  			require.Nil(t, actualMap)
   187  		} else {
   188  			require.NoError(t, actualError)
   189  			require.Equal(t, actualMap, data.expected)
   190  		}
   191  	}
   192  }
   193  
   194  func TestChannelMentionMap_ToURLValues(t *testing.T) {
   195  	fixture := []struct {
   196  		mentionMap ChannelMentionMap
   197  		expected   url.Values
   198  	}{
   199  		{
   200  			ChannelMentionMap{},
   201  			url.Values{},
   202  		},
   203  		{
   204  			ChannelMentionMap{"user": "id"},
   205  			url.Values{
   206  				channelMentionsKey:    []string{"user"},
   207  				channelMentionsIdsKey: []string{"id"},
   208  			},
   209  		},
   210  		{
   211  			ChannelMentionMap{"one": "id1", "two": "id2", "three": "id3"},
   212  			url.Values{
   213  				channelMentionsKey:    []string{"one", "two", "three"},
   214  				channelMentionsIdsKey: []string{"id1", "id2", "id3"},
   215  			},
   216  		},
   217  	}
   218  
   219  	for _, data := range fixture {
   220  		actualValues := data.mentionMap.ToURLValues()
   221  
   222  		// require.EqualValues does not work here directly on the url.Values, as
   223  		// the slices in the map values may be in different order; what we need to
   224  		// check is that the pairs are preserved, which can be checked converting
   225  		// back to a map with FromURLValues. We check that the test is well-formed
   226  		// by converting back the expected url.Values too.
   227  		require.Equal(t, len(actualValues), len(data.expected))
   228  
   229  		actualMentionMap, actualErr := ChannelMentionMapFromURLValues(actualValues)
   230  		expectedMentionMap, expectedErr := ChannelMentionMapFromURLValues(data.expected)
   231  
   232  		require.Equal(t, actualErr, expectedErr)
   233  		require.Equal(t, actualMentionMap, expectedMentionMap)
   234  	}
   235  }