mirror of
https://github.com/Luzifer/ots.git
synced 2025-08-02 03:06:12 -04:00
Implement attachment checking in CLI (#141)
This commit is contained in:
parent
34275baa2f
commit
9a530e1c66
16 changed files with 374 additions and 17 deletions
100
pkg/client/sanity_test.go
Normal file
100
pkg/client/sanity_test.go
Normal file
|
@ -0,0 +1,100 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Luzifer/ots/pkg/customization"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type custMockClient struct {
|
||||
Response *customization.Customize
|
||||
}
|
||||
|
||||
func (c custMockClient) Do(r *http.Request) (*http.Response, error) {
|
||||
m := http.NewServeMux()
|
||||
m.HandleFunc(r.URL.Path, func(w http.ResponseWriter, _ *http.Request) {
|
||||
if c.Response == nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
d, _ := c.Response.ToJSON()
|
||||
_, _ = w.Write([]byte(d))
|
||||
})
|
||||
|
||||
w := httptest.NewRecorder()
|
||||
m.ServeHTTP(w, r)
|
||||
|
||||
return w.Result(), nil
|
||||
}
|
||||
|
||||
func TestSanityCheck(t *testing.T) {
|
||||
var (
|
||||
err error
|
||||
m = custMockClient{&customization.Customize{
|
||||
AcceptedFileTypes: "text/*,image/png,.gif",
|
||||
DisableFileAttachment: true,
|
||||
MaxAttachmentSizeTotal: 64,
|
||||
}}
|
||||
u = "http://localhost/"
|
||||
)
|
||||
|
||||
HTTPClient = &m
|
||||
defer func() { HTTPClient = http.DefaultClient }()
|
||||
|
||||
s := Secret{Secret: "ohai"}
|
||||
|
||||
// no attachments & attachments disabled
|
||||
err = SanityCheck(u, s)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// attachments & attachmetns disabled
|
||||
s.Attachments = []SecretAttachment{
|
||||
{Name: "myfile.webm", Type: "video/webm", Content: []byte{0x0}},
|
||||
}
|
||||
|
||||
err = SanityCheck(u, s)
|
||||
assert.ErrorIs(t, err, ErrAttachmentsDisabled)
|
||||
|
||||
// disallowed attachment
|
||||
m.Response.DisableFileAttachment = false
|
||||
err = SanityCheck(u, s)
|
||||
assert.ErrorIs(t, err, ErrAttachmentTypeNotAllowed)
|
||||
|
||||
// attachment allowed by extension
|
||||
s.Attachments = []SecretAttachment{
|
||||
{Name: "doesthiswork.gif", Type: "image/gif", Content: []byte{0x0}},
|
||||
}
|
||||
err = SanityCheck(u, s)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// attachment allowed by mime type
|
||||
s.Attachments = []SecretAttachment{
|
||||
{Name: "doesthiswork.png", Type: "image/png", Content: []byte{0x0}},
|
||||
}
|
||||
err = SanityCheck(u, s)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// attachment allowed by mime type wildcard
|
||||
s.Attachments = []SecretAttachment{
|
||||
{Name: "doesthiswork.md", Type: "text/markdown", Content: []byte{0x0}},
|
||||
}
|
||||
err = SanityCheck(u, s)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// attachment too large
|
||||
s.Attachments = []SecretAttachment{
|
||||
{Name: "doesthiswork.md", Type: "text/markdown", Content: bytes.Repeat([]byte{0x0}, 128)},
|
||||
}
|
||||
err = SanityCheck(u, s)
|
||||
assert.ErrorIs(t, err, ErrAttachmentsTooLarge)
|
||||
|
||||
// check without settings API on instance
|
||||
m.Response = nil
|
||||
err = SanityCheck(u, s)
|
||||
assert.NoError(t, err)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue