github.com/dschalla/mattermost-server@v4.8.1-rc1+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  
    11  func TestCommandResponseJson(t *testing.T) {
    12  	o := CommandResponse{Text: "test"}
    13  	json := o.ToJson()
    14  	ro := CommandResponseFromJson(strings.NewReader(json))
    15  
    16  	if o.Text != ro.Text {
    17  		t.Fatal("Ids do not match")
    18  	}
    19  }
    20  
    21  func TestCommandResponseFromHTTPBody(t *testing.T) {
    22  	for _, test := range []struct {
    23  		ContentType  string
    24  		Body         string
    25  		ExpectedText string
    26  	}{
    27  		{"", "foo", "foo"},
    28  		{"text/plain", "foo", "foo"},
    29  		{"application/json", `{"text": "foo"}`, "foo"},
    30  		{"application/json; charset=utf-8", `{"text": "foo"}`, "foo"},
    31  	} {
    32  		response := CommandResponseFromHTTPBody(test.ContentType, strings.NewReader(test.Body))
    33  		if response.Text != test.ExpectedText {
    34  			t.Fatal()
    35  		}
    36  	}
    37  }
    38  
    39  func TestCommandResponseFromPlainText(t *testing.T) {
    40  	response := CommandResponseFromPlainText("foo")
    41  	if response.Text != "foo" {
    42  		t.Fatal("text should be foo")
    43  	}
    44  }
    45  
    46  func TestCommandResponseFromJson(t *testing.T) {
    47  	json := `{
    48  		"response_type": "ephemeral",
    49  		"text": "response text",
    50  		"username": "response username",
    51  		"icon_url": "response icon url",
    52  		"goto_location": "response goto location",
    53  		"attachments": [{
    54  			"text": "attachment 1 text",
    55  			"pretext": "attachment 1 pretext"
    56  		},{
    57  			"text": "attachment 2 text",
    58  			"fields": [{
    59  				"title": "field 1",
    60  				"value": "value 1",
    61  				"short": true
    62  			},{
    63  				"title": "field 2",
    64  				"value": [],
    65  				"short": false
    66  			}]
    67  		}]
    68  	}`
    69  
    70  	response := CommandResponseFromJson(strings.NewReader(json))
    71  
    72  	if response == nil {
    73  		t.Fatal("should've received non-nil CommandResponse")
    74  	}
    75  
    76  	if response.ResponseType != "ephemeral" {
    77  		t.Fatal("should've received correct response type")
    78  	} else if response.Text != "response text" {
    79  		t.Fatal("should've received correct response text")
    80  	} else if response.Username != "response username" {
    81  		t.Fatal("should've received correct response username")
    82  	} else if response.IconURL != "response icon url" {
    83  		t.Fatal("should've received correct response icon url")
    84  	} else if response.GotoLocation != "response goto location" {
    85  		t.Fatal("should've received correct response goto location")
    86  	}
    87  
    88  	attachments := response.Attachments
    89  	if len(attachments) != 2 {
    90  		t.Fatal("should've received 2 attachments")
    91  	} else if attachments[0].Text != "attachment 1 text" {
    92  		t.Fatal("should've received correct first attachment text")
    93  	} else if attachments[0].Pretext != "attachment 1 pretext" {
    94  		t.Fatal("should've received correct first attachment pretext")
    95  	} else if attachments[1].Text != "attachment 2 text" {
    96  		t.Fatal("should've received correct second attachment text")
    97  	}
    98  
    99  	fields := attachments[1].Fields
   100  	if len(fields) != 2 {
   101  		t.Fatal("should've received 2 fields")
   102  	} else if fields[0].Value.(string) != "value 1" {
   103  		t.Fatal("should've received correct first attachment value")
   104  	} else if _, ok := fields[1].Value.(string); !ok {
   105  		t.Fatal("should've received second attachment value parsed as a string")
   106  	} else if fields[1].Value.(string) != "[]" {
   107  		t.Fatal("should've received correct second attachment value")
   108  	}
   109  }
   110  
   111  func TestCommandResponseNullArrayItems(t *testing.T) {
   112  	payload := `{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`
   113  	cr := CommandResponseFromJson(strings.NewReader(payload))
   114  	if cr == nil {
   115  		t.Fatal("CommandResponse should not be nil")
   116  	}
   117  	if len(cr.Attachments) != 1 {
   118  		t.Fatalf("expected one attachment")
   119  	}
   120  	if len(cr.Attachments[0].Fields) != 1 {
   121  		t.Fatalf("expected one field")
   122  	}
   123  }