outgoing_webhook_test.go 4 KB
Newer Older
1
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
JoramWilander's avatar
JoramWilander committed
2 3 4 5 6
// See License.txt for license information.

package model

import (
7 8
	"net/url"
	"reflect"
JoramWilander's avatar
JoramWilander committed
9 10 11 12
	"strings"
	"testing"
)

13 14
func TestOutgoingWebhookJson(t *testing.T) {
	o := OutgoingWebhook{Id: NewId()}
JoramWilander's avatar
JoramWilander committed
15
	json := o.ToJson()
16
	ro := OutgoingWebhookFromJson(strings.NewReader(json))
JoramWilander's avatar
JoramWilander committed
17 18 19 20 21 22

	if o.Id != ro.Id {
		t.Fatal("Ids do not match")
	}
}

23 24
func TestOutgoingWebhookIsValid(t *testing.T) {
	o := OutgoingWebhook{}
JoramWilander's avatar
JoramWilander committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Id = NewId()
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.CreateAt = GetMillis()
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.UpdateAt = GetMillis()
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

45
	o.CreatorId = "123"
JoramWilander's avatar
JoramWilander committed
46 47 48 49
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

50 51 52 53 54 55 56 57 58 59 60
	o.CreatorId = NewId()
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Token = "123"
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Token = NewId()
JoramWilander's avatar
JoramWilander committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.ChannelId = "123"
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.ChannelId = NewId()
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.TeamId = "123"
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.TeamId = NewId()
81 82 83 84
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

85 86 87 88 89
	o.CallbackURLs = []string{"nowhere.com/"}
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

90
	o.CallbackURLs = []string{"http://nowhere.com/"}
JoramWilander's avatar
JoramWilander committed
91 92 93
	if err := o.IsValid(); err != nil {
		t.Fatal(err)
	}
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

	o.DisplayName = strings.Repeat("1", 65)
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.DisplayName = strings.Repeat("1", 64)
	if err := o.IsValid(); err != nil {
		t.Fatal(err)
	}

	o.Description = strings.Repeat("1", 129)
	if err := o.IsValid(); err == nil {
		t.Fatal("should be invalid")
	}

	o.Description = strings.Repeat("1", 128)
	if err := o.IsValid(); err != nil {
112 113 114 115 116
		t.Fatal("should be invalid")
	}

	o.ContentType = strings.Repeat("1", 129)
	if err := o.IsValid(); err == nil {
117 118
		t.Fatal(err)
	}
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

	o.ContentType = strings.Repeat("1", 128)
	if err := o.IsValid(); err != nil {
		t.Fatal(err)
	}
}

func TestOutgoingWebhookPayloadToFormValues(t *testing.T) {
	p := &OutgoingWebhookPayload{
		Token:       "Token",
		TeamId:      "TeamId",
		TeamDomain:  "TeamDomain",
		ChannelId:   "ChannelId",
		ChannelName: "ChannelName",
		Timestamp:   123000,
		UserId:      "UserId",
		UserName:    "UserName",
		PostId:      "PostId",
		Text:        "Text",
		TriggerWord: "TriggerWord",
139
		FileIds:     "FileIds",
140 141 142 143 144 145 146 147 148 149 150 151 152
	}
	v := url.Values{}
	v.Set("token", "Token")
	v.Set("team_id", "TeamId")
	v.Set("team_domain", "TeamDomain")
	v.Set("channel_id", "ChannelId")
	v.Set("channel_name", "ChannelName")
	v.Set("timestamp", "123")
	v.Set("user_id", "UserId")
	v.Set("user_name", "UserName")
	v.Set("post_id", "PostId")
	v.Set("text", "Text")
	v.Set("trigger_word", "TriggerWord")
153
	v.Set("file_ids", "FileIds")
154 155 156
	if got, want := p.ToFormValues(), v.Encode(); !reflect.DeepEqual(got, want) {
		t.Fatalf("Got %+v, wanted %+v", got, want)
	}
JoramWilander's avatar
JoramWilander committed
157 158
}

159 160
func TestOutgoingWebhookPreSave(t *testing.T) {
	o := OutgoingWebhook{}
JoramWilander's avatar
JoramWilander committed
161 162 163
	o.PreSave()
}

164 165
func TestOutgoingWebhookPreUpdate(t *testing.T) {
	o := OutgoingWebhook{}
JoramWilander's avatar
JoramWilander committed
166 167
	o.PreUpdate()
}
168 169 170 171 172 173 174 175 176 177 178

func TestOutgoingWebhookTriggerWordStartsWith(t *testing.T) {
	o := OutgoingWebhook{Id: NewId()}
	o.TriggerWords = append(o.TriggerWords, "foo")
	if !o.TriggerWordStartsWith("foobar") {
		t.Fatal("Should return true")
	}
	if o.TriggerWordStartsWith("barfoo") {
		t.Fatal("Should return false")
	}
}
179 180 181

func TestOutgoingWebhookResponseJson(t *testing.T) {
	o := OutgoingWebhookResponse{}
182
	o.Text = NewString("some text")
183 184 185 186 187 188 189 190

	json := o.ToJson()
	ro := OutgoingWebhookResponseFromJson(strings.NewReader(json))

	if *o.Text != *ro.Text {
		t.Fatal("Text does not match")
	}
}