github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/slack_compatibility_test.go (about)

     1  // Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestSlackCompatibleBool_UnmarshalJSON_True(t *testing.T) {
    14  	cases := []struct {
    15  		name    string
    16  		payload string
    17  	}{
    18  		{name: "literal", payload: `{"title": "Foo", "value": "Bar", "short": true}`},
    19  		{name: "stringLower", payload: `{"title": "Foo", "value": "Bar", "short": "true"}`},
    20  		{name: "stringMixed", payload: `{"title": "Foo", "value": "Bar", "short": "True"}`},
    21  		{name: "stringUpper", payload: `{"title": "Foo", "value": "Bar", "short": "TRUE"}`},
    22  	}
    23  
    24  	for _, tt := range cases {
    25  		t.Run(tt.name, func(t *testing.T) {
    26  			field := &SlackAttachmentField{}
    27  
    28  			err := json.Unmarshal([]byte(tt.payload), field)
    29  
    30  			require.NoError(t, err)
    31  			require.True(t, bool(field.Short))
    32  		})
    33  	}
    34  }
    35  
    36  func TestSlackCompatibleBool_UnmarshalJSON_False(t *testing.T) {
    37  	cases := []struct {
    38  		name    string
    39  		payload string
    40  	}{
    41  		{name: "literal", payload: `{"title": "Foo", "value": "Bar", "short": false}`},
    42  		{name: "stringLower", payload: `{"title": "Foo", "value": "Bar", "short": "false"}`},
    43  		{name: "stringMixed", payload: `{"title": "Foo", "value": "Bar", "short": "False"}`},
    44  		{name: "stringUpper", payload: `{"title": "Foo", "value": "Bar", "short": "FALSE"}`},
    45  	}
    46  
    47  	for _, tt := range cases {
    48  		t.Run(tt.name, func(t *testing.T) {
    49  			field := &SlackAttachmentField{}
    50  
    51  			err := json.Unmarshal([]byte(tt.payload), field)
    52  
    53  			require.NoError(t, err)
    54  			require.False(t, bool(field.Short))
    55  		})
    56  	}
    57  }