MM-16997: Simplified TestUploadFiles (#11707)

* MM-16997: Simplified TestUploadFiles

Tickets:
https://mattermost.atlassian.net/browse/MM-16997
https://mattermost.atlassian.net/browse/MM-16760

- The tests now fully buffer the data vefore uploading it. The prior
  code was much more complex because it was intended to eventually
  mature into generic client code; not a priority, definitely not
  valuable for this test.
- Also improved error handling in TestHookFileWillBeUploaded for
  MM-16760

* PR feedback: typo
Этот коммит содержится в:
Lev
2019-07-26 09:54:04 -07:00
коммит произвёл GitHub
родитель 0a12fef33a
Коммит 52ac87ea32
18 изменённых файлов: 199 добавлений и 305 удалений

Просмотреть файл

@@ -497,6 +497,7 @@ func TestHookFileWillBeUploaded(t *testing.T) {
package main
import (
"fmt"
"io"
"github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/model"
@@ -507,7 +508,10 @@ func TestHookFileWillBeUploaded(t *testing.T) {
}
func (p *MyPlugin) FileWillBeUploaded(c *plugin.Context, info *model.FileInfo, file io.Reader, output io.Writer) (*model.FileInfo, string) {
output.Write([]byte("ignored"))
n, err := output.Write([]byte("ignored"))
if err != nil {
return info, fmt.Sprintf("FAILED to write output file n: %v, err: %v", n, err)
}
info.Name = "ignored"
return info, "rejected"
}
@@ -607,6 +611,7 @@ func TestHookFileWillBeUploaded(t *testing.T) {
import (
"io"
"bytes"
"fmt"
"github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/model"
)
@@ -616,13 +621,17 @@ func TestHookFileWillBeUploaded(t *testing.T) {
}
func (p *MyPlugin) FileWillBeUploaded(c *plugin.Context, info *model.FileInfo, file io.Reader, output io.Writer) (*model.FileInfo, string) {
p.API.LogDebug(info.Name)
var buf bytes.Buffer
buf.ReadFrom(file)
p.API.LogDebug(buf.String())
n, err := buf.ReadFrom(file)
if err != nil {
return info, fmt.Sprintf("FAILED to read input file n: %v, err: %v", n, err)
}
outbuf := bytes.NewBufferString("changedtext")
io.Copy(output, outbuf)
n, err = io.Copy(output, outbuf)
if int(n) != len("changedtext") || err != nil {
return info, fmt.Sprintf("FAILED to write output file n: %v, err: %v", n, err)
}
info.Name = "modifiedinfo"
return info, ""
}