-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathmedia_test.go
179 lines (168 loc) · 9.45 KB
/
media_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
package handlers_test
import (
"context"
"testing"
"github.com/nyaruka/courier"
"github.com/nyaruka/courier/handlers"
"github.com/nyaruka/courier/test"
"github.com/stretchr/testify/assert"
)
func TestResolveAttachments(t *testing.T) {
ctx := context.Background()
mb := test.NewMockBackend()
imageJPG := test.NewMockMedia("test.jpg", "image/jpeg", "http://mock.com/1234/test.jpg", 1024*1024, 640, 480, 0, nil)
audioM4A := test.NewMockMedia("test.m4a", "audio/mp4", "http://mock.com/2345/test.m4a", 1024*1024, 0, 0, 200, nil)
audioMP3 := test.NewMockMedia("test.mp3", "audio/mp3", "http://mock.com/3456/test.mp3", 1024*1024, 0, 0, 200, []courier.Media{audioM4A})
thumbJPG := test.NewMockMedia("test.jpg", "image/jpeg", "http://mock.com/4567/test.jpg", 1024*1024, 640, 480, 0, nil)
videoMP4 := test.NewMockMedia("test.mp4", "video/mp4", "http://mock.com/5678/test.mp4", 1024*1024, 0, 0, 1000, []courier.Media{thumbJPG})
videoMOV := test.NewMockMedia("test.mov", "video/quicktime", "http://mock.com/6789/test.mov", 100*1024*1024, 0, 0, 2000, nil)
mb.MockMedia(imageJPG)
mb.MockMedia(audioMP3)
mb.MockMedia(videoMP4)
mb.MockMedia(videoMOV)
tcs := []struct {
attachments []string
mediaSupport map[handlers.MediaType]handlers.MediaTypeSupport
allowURLOnly bool
resolved []*handlers.Attachment
errors []*courier.ChannelError
err string
}{
{ // 0: user entered image URL
attachments: []string{"image:https://example.com/image%201.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/png"}}}, // ignored
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeImage, Name: "image 1.jpg", ContentType: "image", URL: "https://example.com/image%201.jpg"},
},
},
{ // 1: user entered audio URL which isn't properly escaped
attachments: []string{"image:https://example.com/audio 1.m4a"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"audio/mp3"}}}, // ignored
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeImage, Name: "audio 1.m4a", ContentType: "image", URL: "https://example.com/audio%201.m4a"},
},
},
{ // 2: user entered image URL, URL only attachments not allowed
attachments: []string{"image:https://example.com/image.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/png"}}}, // ignored
allowURLOnly: false,
resolved: []*handlers.Attachment{},
errors: []*courier.ChannelError{courier.ErrorMediaUnresolveable("image")},
},
{ // 3: resolveable uploaded image URL
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/png", "image/jpeg"}}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeImage, Name: "test.jpg", ContentType: "image/jpeg", URL: "http://mock.com/1234/test.jpg", Media: imageJPG, Thumbnail: nil},
},
},
{ // 4: unresolveable uploaded image URL
attachments: []string{"image/jpeg:http://mock.com/9876/gone.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeImage, Name: "gone.jpg", ContentType: "image/jpeg", URL: "http://mock.com/9876/gone.jpg", Media: nil, Thumbnail: nil},
},
},
{ // 5: unresolveable uploaded image URL, URL only attachments not allowed
attachments: []string{"image/jpeg:http://mock.com/9876/gone.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}}},
allowURLOnly: false,
resolved: []*handlers.Attachment{},
errors: []*courier.ChannelError{courier.ErrorMediaUnresolveable("image/jpeg")},
},
{ // 6: resolveable uploaded image URL, type not in supported types
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/png"}}},
allowURLOnly: true,
resolved: []*handlers.Attachment{},
errors: []*courier.ChannelError{courier.ErrorMediaUnresolveable("image/jpeg")},
},
{ // 7: resolveable uploaded audio URL, type in supported types
attachments: []string{"audio/mp3:http://mock.com/3456/test.mp3"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeAudio: {Types: []string{"audio/mp3", "audio/mp4"}}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeAudio, Name: "test.mp3", ContentType: "audio/mp3", URL: "http://mock.com/3456/test.mp3", Media: audioMP3, Thumbnail: nil},
},
},
{ // 8: resolveable uploaded audio URL, type not in supported types, but has alternate
attachments: []string{"audio/mp3:http://mock.com/3456/test.mp3"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeAudio: {Types: []string{"audio/mp4"}}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeAudio, Name: "test.m4a", ContentType: "audio/mp4", URL: "http://mock.com/2345/test.m4a", Media: audioM4A, Thumbnail: nil},
},
},
{ // 9: resolveable uploaded video URL, has thumbnail
attachments: []string{"video/mp4:http://mock.com/5678/test.mp4"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeVideo: {Types: []string{"video/mp4", "video/quicktime"}}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeVideo, Name: "test.mp4", ContentType: "video/mp4", URL: "http://mock.com/5678/test.mp4", Media: videoMP4, Thumbnail: thumbJPG},
},
},
{ // 10: resolveable uploaded video URL, no thumbnail
attachments: []string{"video/quicktime:http://mock.com/6789/test.mov"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeVideo: {Types: []string{"video/mp4", "video/quicktime"}}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeVideo, Name: "test.mov", ContentType: "video/quicktime", URL: "http://mock.com/6789/test.mov", Media: videoMOV, Thumbnail: nil},
},
},
{ // 11: resolveable uploaded video URL, too big
attachments: []string{"video/quicktime:http://mock.com/6789/test.mov"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeVideo: {Types: []string{"video/quicktime"}, MaxBytes: 10 * 1024 * 1024}},
allowURLOnly: true,
resolved: []*handlers.Attachment{},
errors: []*courier.ChannelError{courier.ErrorMediaUnresolveable("video/quicktime")},
},
{ // 12: invalid attachment format
attachments: []string{"image"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{},
err: "invalid attachment format: image",
},
{ // 13: invalid attachment format (missing content type)
attachments: []string{"http://mock.com/1234/test.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{},
err: "invalid attachment format: http://mock.com/1234/test.jpg",
},
{ // 14: resolveable uploaded image URL with matching dimensions
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 1000, MaxHeight: 1000}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeImage, Name: "test.jpg", ContentType: "image/jpeg", URL: "http://mock.com/1234/test.jpg", Media: imageJPG, Thumbnail: nil},
},
},
{ // 15: resolveable uploaded image URL without matching dimensions
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 100, MaxHeight: 100}},
allowURLOnly: true,
resolved: []*handlers.Attachment{},
errors: []*courier.ChannelError{courier.ErrorMediaUnresolveable("image/jpeg")},
},
{ // 16: resolveable uploaded image URL without matching dimensions by specific content type precendence
attachments: []string{"image/jpeg:http://mock.com/1234/test.jpg"},
mediaSupport: map[handlers.MediaType]handlers.MediaTypeSupport{handlers.MediaTypeImage: {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 100, MaxHeight: 100}, handlers.MediaType("image/jpeg"): {Types: []string{"image/jpeg", "image/png"}, MaxWidth: 1000, MaxHeight: 1000}},
allowURLOnly: true,
resolved: []*handlers.Attachment{
{Type: handlers.MediaTypeImage, Name: "test.jpg", ContentType: "image/jpeg", URL: "http://mock.com/1234/test.jpg", Media: imageJPG, Thumbnail: nil},
},
},
}
for i, tc := range tcs {
clog := courier.NewChannelLog(courier.ChannelLogTypeMsgSend, nil, nil)
resolved, err := handlers.ResolveAttachments(ctx, mb, tc.attachments, tc.mediaSupport, tc.allowURLOnly, clog)
if tc.err != "" {
assert.EqualError(t, err, tc.err, "expected error for test %d", i)
} else {
assert.NoError(t, err, "unexpected error for test %d", i)
assert.Equal(t, tc.resolved, resolved, "resolved mismatch for test %d", i)
assert.Equal(t, tc.errors, clog.Errors(), "errors mismatch for test %d", i)
}
}
}