github.com/wgh-/mattermost-server@v4.8.0-rc2+incompatible/model/outgoing_webhook_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  	"reflect"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestOutgoingWebhookJson(t *testing.T) {
    14  	o := OutgoingWebhook{Id: NewId()}
    15  	json := o.ToJson()
    16  	ro := OutgoingWebhookFromJson(strings.NewReader(json))
    17  
    18  	if o.Id != ro.Id {
    19  		t.Fatal("Ids do not match")
    20  	}
    21  }
    22  
    23  func TestOutgoingWebhookIsValid(t *testing.T) {
    24  	o := OutgoingWebhook{}
    25  
    26  	if err := o.IsValid(); err == nil {
    27  		t.Fatal("should be invalid")
    28  	}
    29  
    30  	o.Id = NewId()
    31  	if err := o.IsValid(); err == nil {
    32  		t.Fatal("should be invalid")
    33  	}
    34  
    35  	o.CreateAt = GetMillis()
    36  	if err := o.IsValid(); err == nil {
    37  		t.Fatal("should be invalid")
    38  	}
    39  
    40  	o.UpdateAt = GetMillis()
    41  	if err := o.IsValid(); err == nil {
    42  		t.Fatal("should be invalid")
    43  	}
    44  
    45  	o.CreatorId = "123"
    46  	if err := o.IsValid(); err == nil {
    47  		t.Fatal("should be invalid")
    48  	}
    49  
    50  	o.CreatorId = NewId()
    51  	if err := o.IsValid(); err == nil {
    52  		t.Fatal("should be invalid")
    53  	}
    54  
    55  	o.Token = "123"
    56  	if err := o.IsValid(); err == nil {
    57  		t.Fatal("should be invalid")
    58  	}
    59  
    60  	o.Token = NewId()
    61  	if err := o.IsValid(); err == nil {
    62  		t.Fatal("should be invalid")
    63  	}
    64  
    65  	o.ChannelId = "123"
    66  	if err := o.IsValid(); err == nil {
    67  		t.Fatal("should be invalid")
    68  	}
    69  
    70  	o.ChannelId = NewId()
    71  	if err := o.IsValid(); err == nil {
    72  		t.Fatal("should be invalid")
    73  	}
    74  
    75  	o.TeamId = "123"
    76  	if err := o.IsValid(); err == nil {
    77  		t.Fatal("should be invalid")
    78  	}
    79  
    80  	o.TeamId = NewId()
    81  	if err := o.IsValid(); err == nil {
    82  		t.Fatal("should be invalid")
    83  	}
    84  
    85  	o.CallbackURLs = []string{"nowhere.com/"}
    86  	if err := o.IsValid(); err == nil {
    87  		t.Fatal("should be invalid")
    88  	}
    89  
    90  	o.CallbackURLs = []string{"http://nowhere.com/"}
    91  	if err := o.IsValid(); err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	o.DisplayName = strings.Repeat("1", 65)
    96  	if err := o.IsValid(); err == nil {
    97  		t.Fatal("should be invalid")
    98  	}
    99  
   100  	o.DisplayName = strings.Repeat("1", 64)
   101  	if err := o.IsValid(); err != nil {
   102  		t.Fatal(err)
   103  	}
   104  
   105  	o.Description = strings.Repeat("1", 129)
   106  	if err := o.IsValid(); err == nil {
   107  		t.Fatal("should be invalid")
   108  	}
   109  
   110  	o.Description = strings.Repeat("1", 128)
   111  	if err := o.IsValid(); err != nil {
   112  		t.Fatal("should be invalid")
   113  	}
   114  
   115  	o.ContentType = strings.Repeat("1", 129)
   116  	if err := o.IsValid(); err == nil {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	o.ContentType = strings.Repeat("1", 128)
   121  	if err := o.IsValid(); err != nil {
   122  		t.Fatal(err)
   123  	}
   124  }
   125  
   126  func TestOutgoingWebhookPayloadToFormValues(t *testing.T) {
   127  	p := &OutgoingWebhookPayload{
   128  		Token:       "Token",
   129  		TeamId:      "TeamId",
   130  		TeamDomain:  "TeamDomain",
   131  		ChannelId:   "ChannelId",
   132  		ChannelName: "ChannelName",
   133  		Timestamp:   123000,
   134  		UserId:      "UserId",
   135  		UserName:    "UserName",
   136  		PostId:      "PostId",
   137  		Text:        "Text",
   138  		TriggerWord: "TriggerWord",
   139  		FileIds:     "FileIds",
   140  	}
   141  	v := url.Values{}
   142  	v.Set("token", "Token")
   143  	v.Set("team_id", "TeamId")
   144  	v.Set("team_domain", "TeamDomain")
   145  	v.Set("channel_id", "ChannelId")
   146  	v.Set("channel_name", "ChannelName")
   147  	v.Set("timestamp", "123")
   148  	v.Set("user_id", "UserId")
   149  	v.Set("user_name", "UserName")
   150  	v.Set("post_id", "PostId")
   151  	v.Set("text", "Text")
   152  	v.Set("trigger_word", "TriggerWord")
   153  	v.Set("file_ids", "FileIds")
   154  	if got, want := p.ToFormValues(), v.Encode(); !reflect.DeepEqual(got, want) {
   155  		t.Fatalf("Got %+v, wanted %+v", got, want)
   156  	}
   157  }
   158  
   159  func TestOutgoingWebhookPreSave(t *testing.T) {
   160  	o := OutgoingWebhook{}
   161  	o.PreSave()
   162  }
   163  
   164  func TestOutgoingWebhookPreUpdate(t *testing.T) {
   165  	o := OutgoingWebhook{}
   166  	o.PreUpdate()
   167  }
   168  
   169  func TestOutgoingWebhookTriggerWordStartsWith(t *testing.T) {
   170  	o := OutgoingWebhook{Id: NewId()}
   171  	o.TriggerWords = append(o.TriggerWords, "foo")
   172  	if !o.TriggerWordStartsWith("foobar") {
   173  		t.Fatal("Should return true")
   174  	}
   175  	if o.TriggerWordStartsWith("barfoo") {
   176  		t.Fatal("Should return false")
   177  	}
   178  }
   179  
   180  func TestOutgoingWebhookResponseJson(t *testing.T) {
   181  	o := OutgoingWebhookResponse{}
   182  	o.Text = NewString("some text")
   183  
   184  	json := o.ToJson()
   185  	ro := OutgoingWebhookResponseFromJson(strings.NewReader(json))
   186  
   187  	if *o.Text != *ro.Text {
   188  		t.Fatal("Text does not match")
   189  	}
   190  }