github.com/keys-pub/mattermost-server@v4.10.10+incompatible/model/command_response_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestCommandResponseFromHTTPBody(t *testing.T) {
    14  	for _, test := range []struct {
    15  		ContentType  string
    16  		Body         string
    17  		ExpectedText string
    18  	}{
    19  		{"", "foo", "foo"},
    20  		{"text/plain", "foo", "foo"},
    21  		{"application/json", `{"text": "foo"}`, "foo"},
    22  		{"application/json; charset=utf-8", `{"text": "foo"}`, "foo"},
    23  	} {
    24  		response, err := CommandResponseFromHTTPBody(test.ContentType, strings.NewReader(test.Body))
    25  		assert.NoError(t, err)
    26  		assert.Equal(t, test.ExpectedText, response.Text)
    27  	}
    28  }
    29  
    30  func TestCommandResponseFromPlainText(t *testing.T) {
    31  	response := CommandResponseFromPlainText("foo")
    32  	assert.Equal(t, "foo", response.Text)
    33  }
    34  
    35  func TestCommandResponseFromJson(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	sToP := func(s string) *string {
    39  		return &s
    40  	}
    41  
    42  	testCases := []struct {
    43  		Description             string
    44  		Json                    string
    45  		ExpectedCommandResponse *CommandResponse
    46  		ExpectedError           *string
    47  	}{
    48  		{
    49  			"empty response",
    50  			"",
    51  			nil,
    52  			sToP("parsing error at line 1, character 1: unexpected end of JSON input"),
    53  		},
    54  		{
    55  			"malformed response",
    56  			`{"text": }`,
    57  			nil,
    58  			sToP("parsing error at line 1, character 11: invalid character '}' looking for beginning of value"),
    59  		},
    60  		{
    61  			"invalid response",
    62  			`{"text": "test", "response_type": 5}`,
    63  			nil,
    64  			sToP("parsing error at line 1, character 36: json: cannot unmarshal number into Go struct field CommandResponse.response_type of type string"),
    65  		},
    66  		{
    67  			"ephemeral response",
    68  			`{
    69  				"response_type": "ephemeral",
    70  				"text": "response text",
    71  				"username": "response username",
    72  				"icon_url": "response icon url",
    73  				"goto_location": "response goto location",
    74  				"attachments": [{
    75  					"text": "attachment 1 text",
    76  					"pretext": "attachment 1 pretext"
    77  				},{
    78  					"text": "attachment 2 text",
    79  					"fields": [{
    80  						"title": "field 1",
    81  						"value": "value 1",
    82  						"short": true
    83  					},{
    84  						"title": "field 2",
    85  						"value": [],
    86  						"short": false
    87  					}]
    88  				}]
    89  			}`,
    90  			&CommandResponse{
    91  				ResponseType: "ephemeral",
    92  				Text:         "response text",
    93  				Username:     "response username",
    94  				IconURL:      "response icon url",
    95  				GotoLocation: "response goto location",
    96  				Attachments: []*SlackAttachment{
    97  					{
    98  						Text:    "attachment 1 text",
    99  						Pretext: "attachment 1 pretext",
   100  					},
   101  					{
   102  						Text: "attachment 2 text",
   103  						Fields: []*SlackAttachmentField{
   104  							{
   105  								Title: "field 1",
   106  								Value: "value 1",
   107  								Short: true,
   108  							},
   109  							{
   110  								Title: "field 2",
   111  								Value: "[]",
   112  								Short: false,
   113  							},
   114  						},
   115  					},
   116  				},
   117  			},
   118  			nil,
   119  		},
   120  		{
   121  			"null array items",
   122  			`{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`,
   123  			&CommandResponse{
   124  				Attachments: []*SlackAttachment{
   125  					{
   126  						Fields: []*SlackAttachmentField{
   127  							{
   128  								Title: "foo",
   129  								Value: "bar",
   130  								Short: true,
   131  							},
   132  						},
   133  					},
   134  				},
   135  			},
   136  			nil,
   137  		},
   138  	}
   139  
   140  	for _, testCase := range testCases {
   141  		testCase := testCase
   142  		t.Run(testCase.Description, func(t *testing.T) {
   143  			t.Parallel()
   144  
   145  			response, err := CommandResponseFromJson(strings.NewReader(testCase.Json))
   146  			if testCase.ExpectedError != nil {
   147  				assert.EqualError(t, err, *testCase.ExpectedError)
   148  				assert.Nil(t, response)
   149  			} else {
   150  				assert.NoError(t, err)
   151  				if assert.NotNil(t, response) {
   152  					assert.Equal(t, testCase.ExpectedCommandResponse, response)
   153  				}
   154  			}
   155  		})
   156  	}
   157  }