github.com/rajatvaryani/mattermost-server@v5.11.1+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  	testCases := []struct {
    39  		Description             string
    40  		Json                    string
    41  		ExpectedCommandResponse *CommandResponse
    42  		ShouldError             bool
    43  	}{
    44  		{
    45  			"empty response",
    46  			"",
    47  			nil,
    48  			true,
    49  		},
    50  		{
    51  			"malformed response",
    52  			`{"text": }`,
    53  			nil,
    54  			true,
    55  		},
    56  		{
    57  			"invalid response",
    58  			`{"text": "test", "response_type": 5}`,
    59  			nil,
    60  			true,
    61  		},
    62  		{
    63  			"ephemeral response",
    64  			`{
    65  				"response_type": "ephemeral",
    66  				"text": "response text",
    67  				"username": "response username",
    68  				"channel_id": "response channel id",
    69  				"icon_url": "response icon url",
    70  				"goto_location": "response goto location",
    71  				"attachments": [{
    72  					"text": "attachment 1 text",
    73  					"pretext": "attachment 1 pretext"
    74  				},{
    75  					"text": "attachment 2 text",
    76  					"fields": [{
    77  						"title": "field 1",
    78  						"value": "value 1",
    79  						"short": true
    80  					},{
    81  						"title": "field 2",
    82  						"value": [],
    83  						"short": false
    84  					}]
    85  				}]
    86  			}`,
    87  			&CommandResponse{
    88  				ResponseType: "ephemeral",
    89  				Text:         "response text",
    90  				Username:     "response username",
    91  				ChannelId:    "response channel id",
    92  				IconURL:      "response icon url",
    93  				GotoLocation: "response goto location",
    94  				Attachments: []*SlackAttachment{
    95  					{
    96  						Text:    "attachment 1 text",
    97  						Pretext: "attachment 1 pretext",
    98  					},
    99  					{
   100  						Text: "attachment 2 text",
   101  						Fields: []*SlackAttachmentField{
   102  							{
   103  								Title: "field 1",
   104  								Value: "value 1",
   105  								Short: true,
   106  							},
   107  							{
   108  								Title: "field 2",
   109  								Value: "[]",
   110  								Short: false,
   111  							},
   112  						},
   113  					},
   114  				},
   115  			},
   116  			false,
   117  		},
   118  		{
   119  			"null array items",
   120  			`{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`,
   121  			&CommandResponse{
   122  				Attachments: []*SlackAttachment{
   123  					{
   124  						Fields: []*SlackAttachmentField{
   125  							{
   126  								Title: "foo",
   127  								Value: "bar",
   128  								Short: true,
   129  							},
   130  						},
   131  					},
   132  				},
   133  			},
   134  			false,
   135  		},
   136  		{
   137  			"multiple responses returned",
   138  			`
   139  			{
   140  				"text": "message 1",
   141  				"extra_responses": [
   142  					{"text": "message 2"}
   143  				]
   144  			}
   145  			`,
   146  			&CommandResponse{
   147  				Text: "message 1",
   148  				ExtraResponses: []*CommandResponse{
   149  					&CommandResponse{
   150  						Text: "message 2",
   151  					},
   152  				},
   153  			},
   154  			false,
   155  		},
   156  		{
   157  			"multiple responses returned, with attachments",
   158  			`
   159  			{
   160  				"text": "message 1",
   161  				"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}]}],
   162  				"extra_responses": [
   163  					{
   164  						"text": "message 2",
   165  						"attachments":[{"fields":[{"title":"foo 2","value":"bar 2","short":false}]}]
   166  					}
   167  				]
   168  			}`,
   169  			&CommandResponse{
   170  				Text: "message 1",
   171  				Attachments: []*SlackAttachment{
   172  					{
   173  						Fields: []*SlackAttachmentField{
   174  							{
   175  								Title: "foo",
   176  								Value: "bar",
   177  								Short: true,
   178  							},
   179  						},
   180  					},
   181  				},
   182  				ExtraResponses: []*CommandResponse{
   183  					&CommandResponse{
   184  						Text: "message 2",
   185  						Attachments: []*SlackAttachment{
   186  							{
   187  								Fields: []*SlackAttachmentField{
   188  									{
   189  										Title: "foo 2",
   190  										Value: "bar 2",
   191  										Short: false,
   192  									},
   193  								},
   194  							},
   195  						},
   196  					},
   197  				},
   198  			},
   199  			false,
   200  		},
   201  	}
   202  
   203  	for _, testCase := range testCases {
   204  		testCase := testCase
   205  		t.Run(testCase.Description, func(t *testing.T) {
   206  			t.Parallel()
   207  
   208  			response, err := CommandResponseFromJson(strings.NewReader(testCase.Json))
   209  			if testCase.ShouldError {
   210  				assert.Nil(t, response)
   211  			} else {
   212  				assert.NoError(t, err)
   213  				if assert.NotNil(t, response) {
   214  					assert.Equal(t, testCase.ExpectedCommandResponse, response)
   215  				}
   216  			}
   217  		})
   218  	}
   219  }