github.com/rajatvaryani/mattermost-server@v5.11.1+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", 501)
   106  	if err := o.IsValid(); err == nil {
   107  		t.Fatal("should be invalid")
   108  	}
   109  
   110  	o.Description = strings.Repeat("1", 500)
   111  	if err := o.IsValid(); err != nil {
   112  		t.Fatal(err)
   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  	o.Username = strings.Repeat("1", 65)
   126  	if err := o.IsValid(); err == nil {
   127  		t.Fatal("should be invalid")
   128  	}
   129  
   130  	o.Username = strings.Repeat("1", 64)
   131  	if err := o.IsValid(); err != nil {
   132  		t.Fatal("should be invalid")
   133  	}
   134  
   135  	o.IconURL = strings.Repeat("1", 1025)
   136  	if err := o.IsValid(); err == nil {
   137  		t.Fatal(err)
   138  	}
   139  
   140  	o.IconURL = strings.Repeat("1", 1024)
   141  	if err := o.IsValid(); err != nil {
   142  		t.Fatal(err)
   143  	}
   144  }
   145  
   146  func TestOutgoingWebhookPayloadToFormValues(t *testing.T) {
   147  	p := &OutgoingWebhookPayload{
   148  		Token:       "Token",
   149  		TeamId:      "TeamId",
   150  		TeamDomain:  "TeamDomain",
   151  		ChannelId:   "ChannelId",
   152  		ChannelName: "ChannelName",
   153  		Timestamp:   123000,
   154  		UserId:      "UserId",
   155  		UserName:    "UserName",
   156  		PostId:      "PostId",
   157  		Text:        "Text",
   158  		TriggerWord: "TriggerWord",
   159  		FileIds:     "FileIds",
   160  	}
   161  	v := url.Values{}
   162  	v.Set("token", "Token")
   163  	v.Set("team_id", "TeamId")
   164  	v.Set("team_domain", "TeamDomain")
   165  	v.Set("channel_id", "ChannelId")
   166  	v.Set("channel_name", "ChannelName")
   167  	v.Set("timestamp", "123")
   168  	v.Set("user_id", "UserId")
   169  	v.Set("user_name", "UserName")
   170  	v.Set("post_id", "PostId")
   171  	v.Set("text", "Text")
   172  	v.Set("trigger_word", "TriggerWord")
   173  	v.Set("file_ids", "FileIds")
   174  	if got, want := p.ToFormValues(), v.Encode(); !reflect.DeepEqual(got, want) {
   175  		t.Fatalf("Got %+v, wanted %+v", got, want)
   176  	}
   177  }
   178  
   179  func TestOutgoingWebhookPreSave(t *testing.T) {
   180  	o := OutgoingWebhook{}
   181  	o.PreSave()
   182  }
   183  
   184  func TestOutgoingWebhookPreUpdate(t *testing.T) {
   185  	o := OutgoingWebhook{}
   186  	o.PreUpdate()
   187  }
   188  
   189  func TestOutgoingWebhookTriggerWordStartsWith(t *testing.T) {
   190  	o := OutgoingWebhook{Id: NewId()}
   191  	o.TriggerWords = append(o.TriggerWords, "foo")
   192  	if !o.TriggerWordStartsWith("foobar") {
   193  		t.Fatal("Should return true")
   194  	}
   195  	if o.TriggerWordStartsWith("barfoo") {
   196  		t.Fatal("Should return false")
   197  	}
   198  }
   199  
   200  func TestOutgoingWebhookResponseJson(t *testing.T) {
   201  	o := OutgoingWebhookResponse{}
   202  	o.Text = NewString("some text")
   203  
   204  	json := o.ToJson()
   205  	ro, _ := OutgoingWebhookResponseFromJson(strings.NewReader(json))
   206  
   207  	if *o.Text != *ro.Text {
   208  		t.Fatal("Text does not match")
   209  	}
   210  }