11package mattermost
22
33import (
4+ "fmt"
45 "net/url"
56 "os"
67 "testing"
@@ -111,6 +112,42 @@ var _ = Describe("the mattermost service", func() {
111112 })
112113 })
113114 })
115+ When ("generating a config object" , func () {
116+ It ("should not set icon" , func () {
117+ slackURL , _ := url .Parse ("mattermost://AAAAAAAAA/BBBBBBBBB" )
118+ config , configError := CreateConfigFromURL (slackURL )
119+
120+ Expect (configError ).NotTo (HaveOccurred ())
121+ Expect (config .Icon ).To (BeEmpty ())
122+ })
123+ It ("should set icon" , func () {
124+ slackURL , _ := url .Parse ("mattermost://AAAAAAAAA/BBBBBBBBB?icon=test" )
125+ config , configError := CreateConfigFromURL (slackURL )
126+
127+ Expect (configError ).NotTo (HaveOccurred ())
128+ Expect (config .Icon ).To (BeIdenticalTo ("test" ))
129+ })
130+ })
131+ Describe ("creating the payload" , func () {
132+ Describe ("the icon fields" , func () {
133+ payload := JSON {}
134+ It ("should set IconURL when the configured icon looks like an URL" , func () {
135+ payload .SetIcon ("https://example.com/logo.png" )
136+ Expect (payload .IconURL ).To (Equal ("https://example.com/logo.png" ))
137+ Expect (payload .IconEmoji ).To (BeEmpty ())
138+ })
139+ It ("should set IconEmoji when the configured icon does not look like an URL" , func () {
140+ payload .SetIcon ("tanabata_tree" )
141+ Expect (payload .IconEmoji ).To (Equal ("tanabata_tree" ))
142+ Expect (payload .IconURL ).To (BeEmpty ())
143+ })
144+ It ("should clear both fields when icon is empty" , func () {
145+ payload .SetIcon ("" )
146+ Expect (payload .IconEmoji ).To (BeEmpty ())
147+ Expect (payload .IconURL ).To (BeEmpty ())
148+ })
149+ })
150+ })
114151 Describe ("Sending messages" , func () {
115152 When ("sending a message completely without parameters" , func () {
116153 mattermostURL , _ := url .Parse ("mattermost://mattermost.my-domain.com/thisshouldbeanapitoken" )
@@ -159,6 +196,51 @@ var _ = Describe("the mattermost service", func() {
159196 })
160197 })
161198
199+ Describe ("creating configurations" , func () {
200+ When ("given a url with channel field" , func () {
201+ It ("should not throw an error" , func () {
202+ serviceURL := testutils .URLMust (`mattermost://user@mockserver/atoken/achannel` )
203+ Expect ((& Config {}).SetURL (serviceURL )).To (Succeed ())
204+ })
205+ })
206+ When ("given a url with title prop" , func () {
207+ It ("should not throw an error" , func () {
208+ serviceURL := testutils .URLMust (`mattermost://user@mockserver/atoken?icon=https%3A%2F%2Fexample%2Fsomething.png` )
209+ Expect ((& Config {}).SetURL (serviceURL )).To (Succeed ())
210+ })
211+ })
212+ When ("given a url with all fields and props" , func () {
213+ It ("should not throw an error" , func () {
214+ serviceURL := testutils .URLMust (`mattermost://user@mockserver/atoken/achannel?icon=https%3A%2F%2Fexample%2Fsomething.png` )
215+ Expect ((& Config {}).SetURL (serviceURL )).To (Succeed ())
216+ })
217+ })
218+ When ("given a url with invalid props" , func () {
219+ It ("should return an error" , func () {
220+ serviceURL := testutils .URLMust (`matrix://user@mockserver/atoken?foo=bar` )
221+ Expect ((& Config {}).SetURL (serviceURL )).To (HaveOccurred ())
222+ })
223+ })
224+ When ("parsing the configuration URL" , func () {
225+ It ("should be identical after de-/serialization" , func () {
226+ testURL := "mattermost://user@mockserver/atoken/achannel?icon=something"
227+
228+ url , err := url .Parse (testURL )
229+ Expect (err ).NotTo (HaveOccurred (), "parsing" )
230+
231+ config := & Config {}
232+ err = config .SetURL (url )
233+ Expect (err ).NotTo (HaveOccurred (), "verifying" )
234+
235+ outputURL := config .GetURL ()
236+ fmt .Println (outputURL .String (), testURL )
237+
238+ Expect (outputURL .String ()).To (Equal (testURL ))
239+
240+ })
241+ })
242+ })
243+
162244 Describe ("sending the payload" , func () {
163245 var err error
164246 BeforeEach (func () {
@@ -182,6 +264,31 @@ var _ = Describe("the mattermost service", func() {
182264 err = service .Send ("Message" , nil )
183265 Expect (err ).NotTo (HaveOccurred ())
184266 })
267+ })
268+
269+ Describe ("the basic service API" , func () {
270+ Describe ("the service config" , func () {
271+ It ("should implement basic service config API methods correctly" , func () {
272+ testutils .TestConfigGetInvalidQueryValue (& Config {})
273+
274+ testutils .TestConfigSetDefaultValues (& Config {})
185275
276+ testutils .TestConfigGetEnumsCount (& Config {}, 0 )
277+ testutils .TestConfigGetFieldsCount (& Config {}, 4 )
278+ })
279+ })
280+ Describe ("the service instance" , func () {
281+ BeforeEach (func () {
282+ httpmock .Activate ()
283+ })
284+ AfterEach (func () {
285+ httpmock .DeactivateAndReset ()
286+ })
287+ It ("should implement basic service API methods correctly" , func () {
288+ serviceURL := testutils .URLMust ("mattermost://mockhost/mocktoken" )
289+ Expect (service .Initialize (serviceURL , testutils .TestLogger ())).To (Succeed ())
290+ testutils .TestServiceSetInvalidParamValue (service , "foo" , "bar" )
291+ })
292+ })
186293 })
187294})
0 commit comments