|
| 1 | +package messagebird |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +var mmsMessageObject []byte = []byte(`{ |
| 9 | + "id": "6d9e7100b1f9406c81a3c303c30ccf05", |
| 10 | + "href": "https://rest.messagebird.com/mms/6d9e7100b1f9406c81a3c303c30ccf05", |
| 11 | + "direction": "mt", |
| 12 | + "originator": "TestName", |
| 13 | + "subject": "SBJCT", |
| 14 | + "body": "Hello World", |
| 15 | + "mediaUrls": [], |
| 16 | + "reference": null, |
| 17 | + "scheduledDatetime": null, |
| 18 | + "createdDatetime": "2017-10-20T12:50:28+00:00", |
| 19 | + "recipients": { |
| 20 | + "totalCount": 1, |
| 21 | + "totalSentCount": 1, |
| 22 | + "totalDeliveredCount": 0, |
| 23 | + "totalDeliveryFailedCount": 0, |
| 24 | + "items": [ |
| 25 | + { |
| 26 | + "recipient": 31612345678, |
| 27 | + "status": "sent", |
| 28 | + "statusDatetime": "2017-10-20T12:50:28+00:00" |
| 29 | + } |
| 30 | + ] |
| 31 | + } |
| 32 | +}`) |
| 33 | + |
| 34 | +func TestNewMmsMessage(t *testing.T) { |
| 35 | + SetServerResponse(200, mmsMessageObject) |
| 36 | + |
| 37 | + mmsMessage, err := mbClient.NewMmsMessage("TestName", []string{"31612345678"}, "Hello World", nil, nil) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("Didn't expect error while creating a new MMS message: %s", err) |
| 40 | + } |
| 41 | + |
| 42 | + if mmsMessage.Id != "6d9e7100b1f9406c81a3c303c30ccf05" { |
| 43 | + t.Errorf("Unexpected mmsMessage id: %s", mmsMessage.Id) |
| 44 | + } |
| 45 | + |
| 46 | + if mmsMessage.HRef != "https://rest.messagebird.com/mms/6d9e7100b1f9406c81a3c303c30ccf05" { |
| 47 | + t.Errorf("Unexpected mmsMessage href: %s", mmsMessage.HRef) |
| 48 | + } |
| 49 | + |
| 50 | + if mmsMessage.Direction != "mt" { |
| 51 | + t.Errorf("Unexpected mmsMessage direction: %s", mmsMessage.Direction) |
| 52 | + } |
| 53 | + |
| 54 | + if mmsMessage.Originator != "TestName" { |
| 55 | + t.Errorf("Unexpected mmsMessage originator: %s", mmsMessage.Originator) |
| 56 | + } |
| 57 | + |
| 58 | + if mmsMessage.Body != "Hello World" { |
| 59 | + t.Errorf("Unexpected mmsMessage body: %s", mmsMessage.Body) |
| 60 | + } |
| 61 | + |
| 62 | + if mmsMessage.Reference != "" { |
| 63 | + t.Errorf("Unexpected mmsMessage reference: %s", mmsMessage.Reference) |
| 64 | + } |
| 65 | + |
| 66 | + if mmsMessage.ScheduledDatetime != nil { |
| 67 | + t.Errorf("Unexpected mmsMessage scheduled datetime: %s", mmsMessage.ScheduledDatetime) |
| 68 | + } |
| 69 | + |
| 70 | + if mmsMessage.CreatedDatetime == nil || mmsMessage.CreatedDatetime.Format(time.RFC3339) != "2017-10-20T12:50:28Z" { |
| 71 | + t.Errorf("Unexpected mmsMessage created datetime: %s", mmsMessage.CreatedDatetime) |
| 72 | + } |
| 73 | + |
| 74 | + if mmsMessage.Recipients.TotalCount != 1 { |
| 75 | + t.Fatalf("Unexpected number of total count: %d", mmsMessage.Recipients.TotalCount) |
| 76 | + } |
| 77 | + |
| 78 | + if mmsMessage.Recipients.TotalSentCount != 1 { |
| 79 | + t.Errorf("Unexpected number of total sent count: %d", mmsMessage.Recipients.TotalSentCount) |
| 80 | + } |
| 81 | + |
| 82 | + if mmsMessage.Recipients.Items[0].Recipient != 31612345678 { |
| 83 | + t.Errorf("Unexpected mmsMessage recipient: %d", mmsMessage.Recipients.Items[0].Recipient) |
| 84 | + } |
| 85 | + |
| 86 | + if mmsMessage.Recipients.Items[0].Status != "sent" { |
| 87 | + t.Errorf("Unexpected mmsMessage recipient status: %s", mmsMessage.Recipients.Items[0].Status) |
| 88 | + } |
| 89 | + |
| 90 | + if mmsMessage.Recipients.Items[0].StatusDatetime == nil || mmsMessage.Recipients.Items[0].StatusDatetime.Format(time.RFC3339) != "2017-10-20T12:50:28Z" { |
| 91 | + t.Errorf("Unexpected datetime status for mmsMessage recipient: %s", mmsMessage.Recipients.Items[0].StatusDatetime.Format(time.RFC3339)) |
| 92 | + } |
| 93 | + |
| 94 | + if len(mmsMessage.Errors) != 0 { |
| 95 | + t.Errorf("Unexpected number of errors in mmsMessage: %d", len(mmsMessage.Errors)) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestNewMmsMessageError(t *testing.T) { |
| 100 | + SetServerResponse(405, accessKeyErrorObject) |
| 101 | + |
| 102 | + message, err := mbClient.NewMmsMessage("TestName", []string{"31612345678"}, "Hello World", nil, nil) |
| 103 | + if err != ErrResponse { |
| 104 | + t.Fatalf("Expected ErrResponse to be returned, instead I got %s", err) |
| 105 | + } |
| 106 | + |
| 107 | + if len(message.Errors) != 1 { |
| 108 | + t.Fatalf("Unexpected number of errors: %d", len(message.Errors)) |
| 109 | + } |
| 110 | + |
| 111 | + if message.Errors[0].Code != 2 { |
| 112 | + t.Errorf("Unexpected error code: %d", message.Errors[0].Code) |
| 113 | + } |
| 114 | + |
| 115 | + if message.Errors[0].Parameter != "access_key" { |
| 116 | + t.Errorf("Unexpected error parameter %s", message.Errors[0].Parameter) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestNewMmsMessageWithParams(t *testing.T) { |
| 121 | + SetServerResponse(200, mmsMessageObjectWithParams) |
| 122 | + |
| 123 | + params := &MmsMessageParams{ |
| 124 | + Subject: "Test-Subject", |
| 125 | + Reference: "Test-Reference", |
| 126 | + } |
| 127 | + |
| 128 | + mmsMessage, err := mbClient.NewMmsMessage("TestName", []string{"31612345678"}, "", []string{"http://w3.org/1.gif", "http://w3.org/2.gif"}, params) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("Didn't expect error while creating a new MMS message: %s", err) |
| 131 | + } |
| 132 | + |
| 133 | + if mmsMessage.Subject != "Test-Subject" { |
| 134 | + t.Errorf("Unexpected message subject: %s", mmsMessage.Subject) |
| 135 | + } |
| 136 | + if mmsMessage.Reference != "Test-Reference" { |
| 137 | + t.Errorf("Unexpected message reference: %s", mmsMessage.Reference) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +var mmsMessageObjectWithParams []byte = []byte(`{ |
| 142 | + "id": "6d9e7100b1f9406c81a3c303c30ccf05", |
| 143 | + "href": "https://rest.messagebird.com/mms/6d9e7100b1f9406c81a3c303c30ccf05", |
| 144 | + "direction": "mt", |
| 145 | + "originator": "TestName", |
| 146 | + "subject": "Test-Subject", |
| 147 | + "body": "Hello World", |
| 148 | + "mediaUrls": [], |
| 149 | + "reference": "Test-Reference", |
| 150 | + "scheduledDatetime": null, |
| 151 | + "createdDatetime": "2017-10-20T12:50:28+00:00", |
| 152 | + "recipients": { |
| 153 | + "totalCount": 1, |
| 154 | + "totalSentCount": 1, |
| 155 | + "totalDeliveredCount": 0, |
| 156 | + "totalDeliveryFailedCount": 0, |
| 157 | + "items": [ |
| 158 | + { |
| 159 | + "recipient": 31612345678, |
| 160 | + "status": "sent", |
| 161 | + "statusDatetime": "2017-10-20T12:50:28+00:00" |
| 162 | + } |
| 163 | + ] |
| 164 | + } |
| 165 | +}`) |
0 commit comments