This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
generated from mattermost/mattermost-plugin-starter-template
/
plugin_test.go
116 lines (90 loc) · 2.76 KB
/
plugin_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
package main
import (
"bufio"
"bytes"
"image"
"io/ioutil"
"testing"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin/plugintest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"gopkg.in/auyer/steganography.v2"
)
func TestFileWillBeUpload(t *testing.T) {
t.Run("original image is corrupt", func(t *testing.T) {
setupAPI := func() *plugintest.API {
api := &plugintest.API{}
api.On("LogInfo", mock.Anything).Maybe()
return api
}
api := setupAPI()
api.On("LogWarn", mock.AnythingOfType("string"))
defer api.AssertExpectations(t)
p := &Plugin{}
p.API = api
fi := &model.FileInfo{
Extension: "JPG",
}
r := bytes.NewReader([]byte{})
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
fi, reason := p.FileWillBeUploaded(nil, fi, r, w)
assert.Equal(t, reason, "Original image is corrupt: image: unknown format")
assert.Nil(t, fi)
})
t.Run("png watermark", func(t *testing.T) {
setupAPI := func() *plugintest.API {
api := &plugintest.API{}
return api
}
api := setupAPI()
defer api.AssertExpectations(t)
p := &Plugin{}
p.API = api
p.configuration = &configuration{WaterMark: "Test Watermark"}
data, err := ioutil.ReadFile("../assets/test.png")
assert.Nil(t, err)
fi := &model.FileInfo{
Extension: "PNG",
}
r := bytes.NewReader(data)
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
fi, reason := p.FileWillBeUploaded(nil, fi, r, w)
assert.Equal(t, reason, "")
assert.Nil(t, fi)
img, _, err := image.Decode(bytes.NewReader(buf.Bytes())) // decoding to golang's image.Image
assert.Nil(t, err)
sizeOfMessage := steganography.GetMessageSizeFromImage(img) // retrieving message size to decode in the next line
msg := steganography.Decode(sizeOfMessage, img)
assert.Equal(t, string(msg), "Test Watermark")
})
t.Run("jpg watermark", func(t *testing.T) {
setupAPI := func() *plugintest.API {
api := &plugintest.API{}
return api
}
api := setupAPI()
defer api.AssertExpectations(t)
p := &Plugin{}
p.API = api
p.configuration = &configuration{WaterMark: "Test Watermark"}
data, err := ioutil.ReadFile("../assets/test.jpg")
assert.Nil(t, err)
fi := &model.FileInfo{
Extension: "JPG",
}
r := bytes.NewReader(data)
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
fi, reason := p.FileWillBeUploaded(nil, fi, r, w)
assert.Equal(t, reason, "")
assert.Nil(t, fi)
img, _, err := image.Decode(bytes.NewReader(buf.Bytes())) // decoding to golang's image.Image
assert.Nil(t, err)
sizeOfMessage := steganography.GetMessageSizeFromImage(img) // retrieving message size to decode in the next line
msg := steganography.Decode(sizeOfMessage, img)
assert.Equal(t, string(msg), "Test Watermark")
})
}