forked from Ilhasoft/courier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.go
1056 lines (900 loc) · 32 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package courier
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/buger/jsonparser"
"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/gocommon/uuids"
"github.com/gomodule/redigo/redis"
_ "github.com/lib/pq" // postgres driver
)
//-----------------------------------------------------------------------------
// Mock backend implementation
//-----------------------------------------------------------------------------
// MockBackend is a mocked version of a backend which doesn't require a real database or cache
type MockBackend struct {
channels map[ChannelUUID]Channel
channelsByAddress map[ChannelAddress]Channel
contacts map[urns.URN]Contact
queueMsgs []Msg
errorOnQueue bool
mutex sync.RWMutex
outgoingMsgs []Msg
msgStatuses []MsgStatus
channelEvents []ChannelEvent
channelLogs []*ChannelLog
lastContactName string
sentMsgs map[MsgID]bool
redisPool *redis.Pool
seenExternalIDs []string
}
// NewMockBackend returns a new mock backend suitable for testing
func NewMockBackend() *MockBackend {
redisPool := &redis.Pool{
Wait: true, // makes callers wait for a connection
MaxActive: 5, // only open this many concurrent connections at once
MaxIdle: 2, // only keep up to 2 idle
IdleTimeout: 240 * time.Second, // how long to wait before reaping a connection
Dial: func() (redis.Conn, error) {
conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
return nil, err
}
_, err = conn.Do("SELECT", 0)
return conn, err
},
}
conn := redisPool.Get()
defer conn.Close()
_, err := conn.Do("FLUSHDB")
if err != nil {
log.Fatal(err)
}
return &MockBackend{
channels: make(map[ChannelUUID]Channel),
channelsByAddress: make(map[ChannelAddress]Channel),
contacts: make(map[urns.URN]Contact),
sentMsgs: make(map[MsgID]bool),
redisPool: redisPool,
}
}
// GetLastQueueMsg returns the last message queued to the server
func (mb *MockBackend) GetLastQueueMsg() (Msg, error) {
if len(mb.queueMsgs) == 0 {
return nil, ErrMsgNotFound
}
return mb.queueMsgs[len(mb.queueMsgs)-1], nil
}
// GetLastChannelEvent returns the last event written to the server
func (mb *MockBackend) GetLastChannelEvent() (ChannelEvent, error) {
if len(mb.channelEvents) == 0 {
return nil, errors.New("no channel events")
}
return mb.channelEvents[len(mb.channelEvents)-1], nil
}
// GetLastChannelLog returns the last channel log written to the server
func (mb *MockBackend) GetLastChannelLog() (*ChannelLog, error) {
if len(mb.channelLogs) == 0 {
return nil, errors.New("no channel logs")
}
return mb.channelLogs[len(mb.channelLogs)-1], nil
}
// GetLastMsgStatus returns the last status written to the server
func (mb *MockBackend) GetLastMsgStatus() (MsgStatus, error) {
if len(mb.msgStatuses) == 0 {
return nil, errors.New("no msg statuses")
}
return mb.msgStatuses[len(mb.msgStatuses)-1], nil
}
// GetLastContactName returns the contact name set on the last msg or channel event written
func (mb *MockBackend) GetLastContactName() string {
return mb.lastContactName
}
// DeleteMsgWithExternalID delete a message we receive an event that it should be deleted
func (mb *MockBackend) DeleteMsgWithExternalID(ctx context.Context, channel Channel, externalID string) error {
return nil
}
// NewIncomingMsg creates a new message from the given params
func (mb *MockBackend) NewIncomingMsg(channel Channel, urn urns.URN, text string) Msg {
return &mockMsg{channel: channel, urn: urn, text: text}
}
// NewOutgoingMsg creates a new outgoing message from the given params
func (mb *MockBackend) NewOutgoingMsg(channel Channel, id MsgID, urn urns.URN, text string, highPriority bool, quickReplies []string, topic string, responseToID int64, responseToExternalID string, textLanguage string) Msg {
msgResponseToID := NilMsgID
if responseToID != 0 {
msgResponseToID = NewMsgID(responseToID)
}
return &mockMsg{channel: channel, id: id, urn: urn, text: text, highPriority: highPriority, quickReplies: quickReplies, topic: topic, responseToID: msgResponseToID, responseToExternalID: responseToExternalID, textLanguage: textLanguage}
}
// PushOutgoingMsg is a test method to add a message to our queue of messages to send
func (mb *MockBackend) PushOutgoingMsg(msg Msg) {
mb.mutex.Lock()
defer mb.mutex.Unlock()
mb.outgoingMsgs = append(mb.outgoingMsgs, msg)
}
// PopNextOutgoingMsg returns the next message that should be sent, or nil if there are none to send
func (mb *MockBackend) PopNextOutgoingMsg(ctx context.Context) (Msg, error) {
mb.mutex.Lock()
defer mb.mutex.Unlock()
if len(mb.outgoingMsgs) > 0 {
msg, rest := mb.outgoingMsgs[0], mb.outgoingMsgs[1:]
mb.outgoingMsgs = rest
return msg, nil
}
return nil, nil
}
// WasMsgSent returns whether the passed in msg was already sent
func (mb *MockBackend) WasMsgSent(ctx context.Context, id MsgID) (bool, error) {
mb.mutex.Lock()
defer mb.mutex.Unlock()
return mb.sentMsgs[id], nil
}
func (mb *MockBackend) ClearMsgSent(ctx context.Context, id MsgID) error {
mb.mutex.Lock()
defer mb.mutex.Unlock()
delete(mb.sentMsgs, id)
return nil
}
// IsMsgLoop returns whether the passed in msg is a loop
func (mb *MockBackend) IsMsgLoop(ctx context.Context, msg Msg) (bool, error) {
return false, nil
}
// MarkOutgoingMsgComplete marks the passed msg as having been dealt with
func (mb *MockBackend) MarkOutgoingMsgComplete(ctx context.Context, msg Msg, s MsgStatus) {
mb.mutex.Lock()
defer mb.mutex.Unlock()
mb.sentMsgs[msg.ID()] = true
}
// WriteChannelLogs writes the passed in channel logs to the DB
func (mb *MockBackend) WriteChannelLogs(ctx context.Context, logs []*ChannelLog) error {
mb.mutex.Lock()
defer mb.mutex.Unlock()
for _, log := range logs {
mb.channelLogs = append(mb.channelLogs, log)
}
return nil
}
// SetErrorOnQueue is a mock method which makes the QueueMsg call throw the passed in error on next call
func (mb *MockBackend) SetErrorOnQueue(shouldError bool) {
mb.errorOnQueue = shouldError
}
// WriteMsg queues the passed in message internally
func (mb *MockBackend) WriteMsg(ctx context.Context, m Msg) error {
mock := m.(*mockMsg)
// this msg has already been written (we received it twice), we are a no op
if mock.alreadyWritten {
return nil
}
if mb.errorOnQueue {
return errors.New("unable to queue message")
}
mb.queueMsgs = append(mb.queueMsgs, m)
mb.lastContactName = m.(*mockMsg).contactName
return nil
}
// NewMsgStatusForID creates a new Status object for the given message id
func (mb *MockBackend) NewMsgStatusForID(channel Channel, id MsgID, status MsgStatusValue) MsgStatus {
return &mockMsgStatus{
channel: channel,
id: id,
status: status,
createdOn: time.Now().In(time.UTC),
}
}
// NewMsgStatusForExternalID creates a new Status object for the given external id
func (mb *MockBackend) NewMsgStatusForExternalID(channel Channel, externalID string, status MsgStatusValue) MsgStatus {
return &mockMsgStatus{
channel: channel,
externalID: externalID,
status: status,
createdOn: time.Now().In(time.UTC),
}
}
// WriteMsgStatus writes the status update to our queue
func (mb *MockBackend) WriteMsgStatus(ctx context.Context, status MsgStatus) error {
mb.mutex.Lock()
defer mb.mutex.Unlock()
mb.msgStatuses = append(mb.msgStatuses, status)
return nil
}
// NewChannelEvent creates a new channel event with the passed in parameters
func (mb *MockBackend) NewChannelEvent(channel Channel, eventType ChannelEventType, urn urns.URN) ChannelEvent {
return &mockChannelEvent{
channel: channel,
eventType: eventType,
urn: urn,
}
}
// WriteChannelEvent writes the channel event passed in
func (mb *MockBackend) WriteChannelEvent(ctx context.Context, event ChannelEvent) error {
mb.mutex.Lock()
defer mb.mutex.Unlock()
mb.channelEvents = append(mb.channelEvents, event)
mb.lastContactName = event.(*mockChannelEvent).contactName
return nil
}
// GetChannel returns the channel with the passed in type and channel uuid
func (mb *MockBackend) GetChannel(ctx context.Context, cType ChannelType, uuid ChannelUUID) (Channel, error) {
channel, found := mb.channels[uuid]
if !found {
return nil, ErrChannelNotFound
}
return channel, nil
}
// GetChannelByAddress returns the channel with the passed in type and channel address
func (mb *MockBackend) GetChannelByAddress(ctx context.Context, cType ChannelType, address ChannelAddress) (Channel, error) {
channel, found := mb.channelsByAddress[address]
if !found {
return nil, ErrChannelNotFound
}
return channel, nil
}
// GetContact creates a new contact with the passed in channel and URN
func (mb *MockBackend) GetContact(ctx context.Context, channel Channel, urn urns.URN, auth string, name string) (Contact, error) {
contact, found := mb.contacts[urn]
if !found {
uuid, _ := NewContactUUID(string(uuids.New()))
contact = &mockContact{channel, urn, auth, uuid}
mb.contacts[urn] = contact
}
return contact, nil
}
// AddURNtoContact adds a URN to the passed in contact
func (mb *MockBackend) AddURNtoContact(context context.Context, channel Channel, contact Contact, urn urns.URN) (urns.URN, error) {
mb.contacts[urn] = contact
return urn, nil
}
// RemoveURNFromcontact removes a URN from the passed in contact
func (mb *MockBackend) RemoveURNfromContact(context context.Context, channel Channel, contact Contact, urn urns.URN) (urns.URN, error) {
contact, found := mb.contacts[urn]
if found {
delete(mb.contacts, urn)
}
return urn, nil
}
// AddChannel adds a test channel to the test server
func (mb *MockBackend) AddChannel(channel Channel) {
mb.channels[channel.UUID()] = channel
mb.channelsByAddress[channel.ChannelAddress()] = channel
}
// ClearChannels is a utility function on our mock server to clear all added channels
func (mb *MockBackend) ClearChannels() {
mb.channels = nil
mb.channelsByAddress = nil
}
// Start starts our mock backend
func (mb *MockBackend) Start() error { return nil }
// Stop stops our mock backend
func (mb *MockBackend) Stop() error { return nil }
// Cleanup cleans up any connections that are open
func (mb *MockBackend) Cleanup() error { return nil }
// ClearQueueMsgs clears our mock msg queue
func (mb *MockBackend) ClearQueueMsgs() {
mb.queueMsgs = nil
}
// ClearSeenExternalIDs clears our mock seen external ids
func (mb *MockBackend) ClearSeenExternalIDs() {
mb.seenExternalIDs = nil
}
// LenQueuedMsgs Get the length of queued msgs
func (mb *MockBackend) LenQueuedMsgs() int {
return len(mb.queueMsgs)
}
// CheckExternalIDSeen checks if external ID has been seen in a period
func (mb *MockBackend) CheckExternalIDSeen(msg Msg) Msg {
m := msg.(*mockMsg)
for _, b := range mb.seenExternalIDs {
if b == msg.ExternalID() {
m.alreadyWritten = true
return m
}
}
return m
}
// WriteExternalIDSeen marks a external ID as seen for a period
func (mb *MockBackend) WriteExternalIDSeen(msg Msg) {
mb.seenExternalIDs = append(mb.seenExternalIDs, msg.ExternalID())
}
// Health gives a string representing our health, empty for our mock
func (mb *MockBackend) Health() string {
return ""
}
// Status returns a string describing the status of the service, queue size etc..
func (mb *MockBackend) Status() string {
return ""
}
// Heartbeat is a noop for our mock backend
func (mb *MockBackend) Heartbeat() error {
return nil
}
// RedisPool returns the redisPool for this backend
func (mb *MockBackend) RedisPool() *redis.Pool {
return mb.redisPool
}
func (b *MockBackend) GetRunEventsByMsgUUIDFromDB(ctx context.Context, msgUUID string) ([]RunEvent, error) {
return nil, nil
}
func (b *MockBackend) GetMessage(ctx context.Context, msgUUID string) (Msg, error) {
return nil, nil
}
func buildMockBackend(config *Config) Backend {
return NewMockBackend()
}
func init() {
RegisterBackend("mock", buildMockBackend)
}
//-----------------------------------------------------------------------------
// Mock channel implementation
//-----------------------------------------------------------------------------
// MockChannel implements the Channel interface and is used in our tests
type MockChannel struct {
uuid ChannelUUID
channelType ChannelType
schemes []string
address ChannelAddress
country string
role string
config map[string]interface{}
orgConfig map[string]interface{}
}
// UUID returns the uuid for this channel
func (c *MockChannel) UUID() ChannelUUID { return c.uuid }
// Name returns the name of this channel, we just return our UUID for our mock instances
func (c *MockChannel) Name() string { return fmt.Sprintf("Channel: %s", c.uuid.String()) }
// ChannelType returns the type of this channel
func (c *MockChannel) ChannelType() ChannelType { return c.channelType }
// SetScheme sets the scheme for this channel
func (c *MockChannel) SetScheme(scheme string) { c.schemes = []string{scheme} }
// Schemes returns the schemes for this channel
func (c *MockChannel) Schemes() []string { return c.schemes }
// IsScheme returns whether the passed in scheme is the scheme for this channel
func (c *MockChannel) IsScheme(scheme string) bool {
return len(c.schemes) == 1 && c.schemes[0] == scheme
}
// Address returns the address as a string of this channel
func (c *MockChannel) Address() string { return c.address.String() }
// ChannelAddress returns the address of this channel
func (c *MockChannel) ChannelAddress() ChannelAddress { return c.address }
// Country returns the country this channel is for (if any)
func (c *MockChannel) Country() string { return c.country }
// SetConfig sets the passed in config parameter
func (c *MockChannel) SetConfig(key string, value interface{}) {
c.config[key] = value
}
// CallbackDomain returns the callback domain to use for this channel
func (c *MockChannel) CallbackDomain(fallbackDomain string) string {
value, found := c.config[ConfigCallbackDomain]
if !found {
return fallbackDomain
}
return value.(string)
}
// ConfigForKey returns the config value for the passed in key
func (c *MockChannel) ConfigForKey(key string, defaultValue interface{}) interface{} {
value, found := c.config[key]
if !found {
return defaultValue
}
return value
}
// StringConfigForKey returns the config value for the passed in key
func (c *MockChannel) StringConfigForKey(key string, defaultValue string) string {
val := c.ConfigForKey(key, defaultValue)
str, isStr := val.(string)
if !isStr {
return defaultValue
}
return str
}
// BoolConfigForKey returns the config value for the passed in key
func (c *MockChannel) BoolConfigForKey(key string, defaultValue bool) bool {
val := c.ConfigForKey(key, defaultValue)
b, isBool := val.(bool)
if !isBool {
return defaultValue
}
return b
}
// IntConfigForKey returns the config value for the passed in key
func (c *MockChannel) IntConfigForKey(key string, defaultValue int) int {
val := c.ConfigForKey(key, defaultValue)
// golang unmarshals number literals in JSON into float64s by default
f, isFloat := val.(float64)
if isFloat {
return int(f)
}
// test authors may use literal ints
i, isInt := val.(int)
if isInt {
return i
}
str, isStr := val.(string)
if isStr {
i, err := strconv.Atoi(str)
if err == nil {
return i
}
}
return defaultValue
}
// OrgConfigForKey returns the org config value for the passed in key
func (c *MockChannel) OrgConfigForKey(key string, defaultValue interface{}) interface{} {
value, found := c.orgConfig[key]
if !found {
return defaultValue
}
return value
}
// SetRoles sets the role on the channel
func (c *MockChannel) SetRoles(roles []ChannelRole) {
c.role = fmt.Sprint(roles)
}
// Roles returns the roles of this channel
func (c *MockChannel) Roles() []ChannelRole {
roles := []ChannelRole{}
for _, char := range strings.Split(c.role, "") {
roles = append(roles, ChannelRole(char))
}
return roles
}
// HasRole returns whether the passed in channel supports the passed role
func (c *MockChannel) HasRole(role ChannelRole) bool {
for _, r := range c.Roles() {
if r == role {
return true
}
}
return false
}
// NewMockChannel creates a new mock channel for the passed in type, address, country and config
func NewMockChannel(uuid string, channelType string, address string, country string, config map[string]interface{}) *MockChannel {
cUUID, _ := NewChannelUUID(uuid)
channel := &MockChannel{
uuid: cUUID,
channelType: ChannelType(channelType),
schemes: []string{urns.TelScheme},
address: ChannelAddress(address),
country: country,
config: config,
role: "SR",
orgConfig: map[string]interface{}{},
}
return channel
}
//-----------------------------------------------------------------------------
// Mock msg implementation
//-----------------------------------------------------------------------------
type mockMsg struct {
channel Channel
id MsgID
uuid MsgUUID
text string
attachments []string
externalID string
urn urns.URN
urnAuth string
contactName string
highPriority bool
quickReplies []string
topic string
responseToID MsgID
responseToExternalID string
metadata json.RawMessage
alreadyWritten bool
isResend bool
textLanguage string
receivedOn *time.Time
sentOn *time.Time
wiredOn *time.Time
products []map[string]interface{}
listMessage ListMessage
}
func (m *mockMsg) SessionStatus() string { return "" }
func (m *mockMsg) Channel() Channel { return m.channel }
func (m *mockMsg) ID() MsgID { return m.id }
func (m *mockMsg) EventID() int64 { return int64(m.id) }
func (m *mockMsg) UUID() MsgUUID { return m.uuid }
func (m *mockMsg) Text() string { return m.text }
func (m *mockMsg) Attachments() []string { return m.attachments }
func (m *mockMsg) ExternalID() string { return m.externalID }
func (m *mockMsg) URN() urns.URN { return m.urn }
func (m *mockMsg) URNAuth() string { return m.urnAuth }
func (m *mockMsg) ContactName() string { return m.contactName }
func (m *mockMsg) HighPriority() bool { return m.highPriority }
func (m *mockMsg) QuickReplies() []string { return m.quickReplies }
func (m *mockMsg) Topic() string { return m.topic }
func (m *mockMsg) ResponseToID() MsgID { return m.responseToID }
func (m *mockMsg) ResponseToExternalID() string { return m.responseToExternalID }
func (m *mockMsg) Metadata() json.RawMessage { return m.metadata }
func (m *mockMsg) IsResend() bool { return m.isResend }
func (m *mockMsg) TextLanguage() string { return m.textLanguage }
func (m *mockMsg) ReceivedOn() *time.Time { return m.receivedOn }
func (m *mockMsg) SentOn() *time.Time { return m.sentOn }
func (m *mockMsg) WiredOn() *time.Time { return m.wiredOn }
func (m *mockMsg) WithContactName(name string) Msg { m.contactName = name; return m }
func (m *mockMsg) WithURNAuth(auth string) Msg { m.urnAuth = auth; return m }
func (m *mockMsg) WithReceivedOn(date time.Time) Msg { m.receivedOn = &date; return m }
func (m *mockMsg) WithExternalID(id string) Msg { m.externalID = id; return m }
func (m *mockMsg) WithID(id MsgID) Msg { m.id = id; return m }
func (m *mockMsg) WithUUID(uuid MsgUUID) Msg { m.uuid = uuid; return m }
func (m *mockMsg) WithAttachment(url string) Msg {
m.attachments = append(m.attachments, url)
return m
}
func (m *mockMsg) WithMetadata(metadata json.RawMessage) Msg { m.metadata = metadata; return m }
func (m *mockMsg) Status() MsgStatusValue { return "" }
func (m *mockMsg) Header() string {
if m.metadata == nil {
return ""
}
header, _, _, _ := jsonparser.Get(m.metadata, "header")
return string(header)
}
func (m *mockMsg) Body() string {
if m.metadata == nil {
return ""
}
body, _, _, _ := jsonparser.Get(m.metadata, "body")
return string(body)
}
func (m *mockMsg) Footer() string {
if m.metadata == nil {
return ""
}
footer, _, _, _ := jsonparser.Get(m.metadata, "footer")
return string(footer)
}
func (m *mockMsg) Products() []map[string]interface{} {
if m.products != nil {
return m.products
}
if m.Metadata() == nil {
return nil
}
p, _, _, _ := jsonparser.Get(m.Metadata(), "products")
err := json.Unmarshal(p, &m.products)
if err != nil {
return nil
}
return m.products
}
func (m *mockMsg) Action() string {
if m.metadata == nil {
return ""
}
action, _, _, _ := jsonparser.Get(m.metadata, "action")
return string(action)
}
func (m *mockMsg) SendCatalog() bool {
if m.metadata == nil {
return false
}
byteValue, _, _, _ := jsonparser.Get(m.metadata, "send_catalog")
sendCatalog, err := strconv.ParseBool(string(byteValue))
if err != nil {
return false
}
return sendCatalog
}
func (m *mockMsg) ListMessage() ListMessage {
if m.metadata == nil {
return ListMessage{}
}
var metadata map[string]interface{}
err := json.Unmarshal(m.metadata, &metadata)
if err != nil {
return m.listMessage
}
byteValue, _, _, _ := jsonparser.Get(m.metadata, "interaction_type")
interactionType := string(byteValue)
if interactionType == "list" {
m.listMessage = ListMessage{}
m.listMessage.ButtonText = metadata["list_message"].(map[string]interface{})["button_text"].(string)
listItems := metadata["list_message"].(map[string]interface{})["list_items"].([]interface{})
m.listMessage.ListItems = make([]ListItems, len(listItems))
for i, item := range listItems {
itemMap := item.(map[string]interface{})
m.listMessage.ListItems[i] = ListItems{
Title: itemMap["title"].(string),
UUID: itemMap["uuid"].(string),
}
if itemMap["description"] != nil {
m.listMessage.ListItems[i].Description = itemMap["description"].(string)
}
}
}
return m.listMessage
}
func (m *mockMsg) HeaderType() string {
if m.metadata == nil {
return ""
}
byteValue, _, _, _ := jsonparser.Get(m.metadata, "header_type")
return string(byteValue)
}
func (m *mockMsg) HeaderText() string {
if m.metadata == nil {
return ""
}
byteValue, _, _, _ := jsonparser.Get(m.metadata, "header_text")
return string(byteValue)
}
func (m *mockMsg) InteractionType() string {
if m.metadata == nil {
return ""
}
byteValue, _, _, _ := jsonparser.Get(m.metadata, "interaction_type")
return string(byteValue)
}
func (m *mockMsg) CTAMessage() *CTAMessage {
if m.metadata == nil {
return nil
}
var metadata map[string]interface{}
err := json.Unmarshal(m.metadata, &metadata)
if err != nil {
return nil
}
if metadata == nil {
return nil
}
if interactionType, ok := metadata["interaction_type"].(string); ok && interactionType == "cta_url" {
if ctaMessageData, ok := metadata["cta_message"].(map[string]interface{}); ok {
ctaMessage := &CTAMessage{}
if displayText, ok := ctaMessageData["display_text"].(string); ok {
ctaMessage.DisplayText = displayText
}
if actionURL, ok := ctaMessageData["url"].(string); ok {
ctaMessage.URL = actionURL
}
return ctaMessage
}
}
return nil
}
func (m *mockMsg) FlowMessage() *FlowMessage {
if m.metadata == nil {
return nil
}
var metadata map[string]interface{}
err := json.Unmarshal(m.metadata, &metadata)
if err != nil {
return nil
}
if metadata == nil {
return nil
}
if interactionType, ok := metadata["interaction_type"].(string); ok && interactionType == "flow_msg" {
if flowMessageData, ok := metadata["flow_message"].(map[string]interface{}); ok {
flowMessage := &FlowMessage{}
if flowID, ok := flowMessageData["flow_id"].(string); ok {
flowMessage.FlowID = flowID
}
if flowScreen, ok := flowMessageData["flow_screen"].(string); ok {
flowMessage.FlowScreen = flowScreen
}
if flowData, ok := flowMessageData["flow_data"].(map[string]interface{}); ok {
convertedFlowData := map[string]interface{}{}
for key, value := range flowData {
convertedFlowData[key] = value
}
flowMessage.FlowData = convertedFlowData
}
if flowCTA, ok := flowMessageData["flow_cta"].(string); ok {
flowMessage.FlowCTA = flowCTA
}
if flowMode, ok := flowMessageData["flow_mode"].(string); ok {
flowMessage.FlowMode = flowMode
}
return flowMessage
}
}
return nil
}
func (m *mockMsg) OrderDetailsMessage() *OrderDetailsMessage {
if m.metadata == nil {
return nil
}
var metadata map[string]interface{}
err := json.Unmarshal(m.metadata, &metadata)
if err != nil {
return nil
}
if metadata == nil {
return nil
}
if interactionType, ok := metadata["interaction_type"].(string); ok && interactionType == "order_details" {
if orderDetailsMessageData, ok := metadata["order_details_message"].(map[string]interface{}); ok {
orderDetailsMessage := &OrderDetailsMessage{}
if referenceID, ok := orderDetailsMessageData["reference_id"].(string); ok {
orderDetailsMessage.ReferenceID = referenceID
}
if paymentSettings, ok := orderDetailsMessageData["payment_settings"].(map[string]interface{}); ok {
orderDetailsMessage.PaymentSettings = OrderPaymentSettings{}
if payment_type, ok := paymentSettings["type"].(string); ok {
orderDetailsMessage.PaymentSettings.Type = payment_type
}
if payment_link, ok := paymentSettings["payment_link"].(string); ok {
orderDetailsMessage.PaymentSettings.PaymentLink = payment_link
}
if pix_config, ok := paymentSettings["pix_config"].(map[string]interface{}); ok {
orderDetailsMessage.PaymentSettings.PixConfig = OrderPixConfig{}
if pix_config_key, ok := pix_config["key"].(string); ok {
orderDetailsMessage.PaymentSettings.PixConfig.Key = pix_config_key
}
if pix_config_key_type, ok := pix_config["key_type"].(string); ok {
orderDetailsMessage.PaymentSettings.PixConfig.KeyType = pix_config_key_type
}
if pix_config_merchant_name, ok := pix_config["merchant_name"].(string); ok {
orderDetailsMessage.PaymentSettings.PixConfig.MerchantName = pix_config_merchant_name
}
if pix_config_code, ok := pix_config["code"].(string); ok {
orderDetailsMessage.PaymentSettings.PixConfig.Code = pix_config_code
}
}
}
if totalAmount, ok := orderDetailsMessageData["total_amount"].(float64); ok {
orderDetailsMessage.TotalAmount = int(totalAmount)
}
if orderData, ok := orderDetailsMessageData["order"].(map[string]interface{}); ok {
orderDetailsMessage.Order = Order{}
if itemsData, ok := orderData["items"].([]interface{}); ok {
orderDetailsMessage.Order.Items = make([]OrderItem, len(itemsData))
for i, item := range itemsData {
if itemMap, ok := item.(map[string]interface{}); ok {
itemAmount := itemMap["amount"].(map[string]interface{})
item := OrderItem{
RetailerID: itemMap["retailer_id"].(string),
Name: itemMap["name"].(string),
Quantity: int(itemMap["quantity"].(float64)),
Amount: OrderAmountWithOffset{
Value: int(itemAmount["value"].(float64)),
Offset: int(itemAmount["offset"].(float64)),
},
}
if itemMap["sale_amount"] != nil {
saleAmount := itemMap["sale_amount"].(map[string]interface{})
item.SaleAmount = &OrderAmountWithOffset{
Value: int(saleAmount["value"].(float64)),
Offset: int(saleAmount["offset"].(float64)),
}
}
orderDetailsMessage.Order.Items[i] = item
}
}
}
if subtotal, ok := orderData["subtotal"].(float64); ok {
orderDetailsMessage.Order.Subtotal = int(subtotal)
}
if taxData, ok := orderData["tax"].(map[string]interface{}); ok {
orderDetailsMessage.Order.Tax = OrderAmountWithDescription{}
if value, ok := taxData["value"].(float64); ok {
orderDetailsMessage.Order.Tax.Value = int(value)
}
if description, ok := taxData["description"].(string); ok {
orderDetailsMessage.Order.Tax.Description = description
}
}
if shippingData, ok := orderData["shipping"].(map[string]interface{}); ok {
orderDetailsMessage.Order.Shipping = OrderAmountWithDescription{}
if value, ok := shippingData["value"].(float64); ok {
orderDetailsMessage.Order.Shipping.Value = int(value)
}
if description, ok := shippingData["description"].(string); ok {
orderDetailsMessage.Order.Shipping.Description = description
}
}
if discountData, ok := orderData["discount"].(map[string]interface{}); ok {
orderDetailsMessage.Order.Discount = OrderDiscount{}
if value, ok := discountData["value"].(float64); ok {
orderDetailsMessage.Order.Discount.Value = int(value)
}
if description, ok := discountData["description"].(string); ok {
orderDetailsMessage.Order.Discount.Description = description
}
if programName, ok := discountData["program_name"].(string); ok {
orderDetailsMessage.Order.Discount.ProgramName = programName
}
}
}
return orderDetailsMessage
}
}
return nil
}
//-----------------------------------------------------------------------------
// Mock status implementation
//-----------------------------------------------------------------------------
type mockMsgStatus struct {
channel Channel
id MsgID
oldURN urns.URN
newURN urns.URN
externalID string
status MsgStatusValue
createdOn time.Time
logs []*ChannelLog
}
func (m *mockMsgStatus) ChannelUUID() ChannelUUID { return m.channel.UUID() }
func (m *mockMsgStatus) ID() MsgID { return m.id }
func (m *mockMsgStatus) EventID() int64 { return int64(m.id) }
func (m *mockMsgStatus) SetUpdatedURN(old, new urns.URN) error {
m.oldURN = old
m.newURN = new
return nil
}
func (m *mockMsgStatus) UpdatedURN() (urns.URN, urns.URN) {
return m.oldURN, m.newURN
}
func (m *mockMsgStatus) HasUpdatedURN() bool {
if m.oldURN != urns.NilURN && m.newURN != urns.NilURN {
return true
}
return false
}
func (m *mockMsgStatus) ExternalID() string { return m.externalID }
func (m *mockMsgStatus) SetExternalID(id string) { m.externalID = id }
func (m *mockMsgStatus) Status() MsgStatusValue { return m.status }
func (m *mockMsgStatus) SetStatus(status MsgStatusValue) { m.status = status }
func (m *mockMsgStatus) Logs() []*ChannelLog { return m.logs }
func (m *mockMsgStatus) AddLog(log *ChannelLog) { m.logs = append(m.logs, log) }
//-----------------------------------------------------------------------------
// Mock channel event implementation
//-----------------------------------------------------------------------------
type mockChannelEvent struct {
channel Channel