Skip to content
Permalink
Browse files

Tests

  • Loading branch information...
DarthSim committed Nov 6, 2018
1 parent e1f3f42 commit 6b7551213fe0e11b68514b612934b66e4b5a5c29

Some generated files are not rendered by default. Learn more.

@@ -86,7 +86,9 @@ func presetEnvConfig(p presets, name string) {
presetStrings := strings.Split(env, ",")

for _, presetStr := range presetStrings {
parsePreset(p, presetStr)
if err := parsePreset(p, presetStr); err != nil {
log.Fatalln(err)
}
}
}
}
@@ -103,7 +105,9 @@ func presetFileConfig(p presets, filepath string) {

scanner := bufio.NewScanner(f)
for scanner.Scan() {
parsePreset(p, scanner.Text())
if err := parsePreset(p, scanner.Text()); err != nil {
log.Fatalln(err)
}
}

if err := scanner.Err(); err != nil {
@@ -340,7 +344,9 @@ func init() {
}
}

checkPresets(conf.Presets)
if err := checkPresets(conf.Presets); err != nil {
log.Fatalln(err)
}

if conf.WatermarkOpacity <= 0 {
log.Fatalln("Watermark opacity should be greater than 0")
@@ -1,22 +1,38 @@
package main

import (
"encoding/base64"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func TestSignatureFor(t *testing.T) {
oldConf := conf
defer func() { conf = oldConf }()
type CryptTestSuite struct{ MainTestSuite }

func (s *CryptTestSuite) SetupTest() {
s.MainTestSuite.SetupTest()

base64Signature := func(x string) string { return base64.RawURLEncoding.EncodeToString(signatureFor(x)) }
conf.Key = []byte("test-key")
conf.Salt = []byte("test-salt")
assert.Equal(t, "dtLwhdnPPiu_epMl1LrzheLpvHas-4mwvY6L3Z8WwlY", base64Signature("asd"))
assert.Equal(t, "8x1xvzxVqZ3Uz3kEC8gVvBfU0dfU1vKv0Gho8m3Ysgw", base64Signature("qwe"))
}

func (s *CryptTestSuite) TestValidatePath() {
err := validatePath("dtLwhdnPPiu_epMl1LrzheLpvHas-4mwvY6L3Z8WwlY", "asd")
assert.Nil(s.T(), err)
}

func (s *CryptTestSuite) TestValidatePathTruncated() {
conf.SignatureSize = 8
assert.Equal(t, "dtLwhdnPPis", base64Signature("asd"))
assert.Equal(t, "8x1xvzxVqZ0", base64Signature("qwe"))

err := validatePath("dtLwhdnPPis", "asd")
assert.Nil(s.T(), err)
}

func (s *CryptTestSuite) TestValidatePathInvalid() {
err := validatePath("dtLwhdnPPis", "asd")
assert.Error(s.T(), err)
}

func TestCrypt(t *testing.T) {
suite.Run(t, new(CryptTestSuite))
}
@@ -0,0 +1,19 @@
package main

import (
"github.com/stretchr/testify/suite"
)

type MainTestSuite struct {
suite.Suite

oldConf config
}

func (s *MainTestSuite) SetupTest() {
s.oldConf = conf
}

func (s *MainTestSuite) TearDownTest() {
conf = s.oldConf
}
@@ -1,55 +1,56 @@
package main

import (
"log"
"fmt"
"strings"
)

type presets map[string]urlOptions

func parsePreset(p presets, presetStr string) {
func parsePreset(p presets, presetStr string) error {
presetStr = strings.Trim(presetStr, " ")

if len(presetStr) == 0 || strings.HasPrefix(presetStr, "#") {
return
return nil
}

parts := strings.Split(presetStr, "=")

if len(parts) != 2 {
log.Fatalf("Invalid preset string: %s", presetStr)
return
return fmt.Errorf("Invalid preset string: %s", presetStr)
}

name := strings.Trim(parts[0], " ")
if len(name) == 0 {
log.Fatalf("Empty preset name: %s", presetStr)
return
return fmt.Errorf("Empty preset name: %s", presetStr)
}

value := strings.Trim(parts[1], " ")
if len(value) == 0 {
log.Fatalf("Empty preset value: %s", presetStr)
return
return fmt.Errorf("Empty preset value: %s", presetStr)
}

optsStr := strings.Split(value, "/")

opts, rest := parseURLOptions(optsStr)

if len(rest) > 0 {
log.Fatalf("Invalid preset value: %s", presetStr)
return fmt.Errorf("Invalid preset value: %s", presetStr)
}

p[name] = opts

return nil
}

func checkPresets(p presets) {
func checkPresets(p presets) error {
var po processingOptions

for name, opts := range p {
if err := applyProcessingOptions(&po, opts); err != nil {
log.Fatalf("Error in preset `%s`: %s", name, err)
return fmt.Errorf("Error in preset `%s`: %s", name, err)
}
}

return nil
}
@@ -0,0 +1,113 @@
package main

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type PresetsTestSuite struct{ MainTestSuite }

func (s *PresetsTestSuite) TestParsePreset() {
p := make(presets)

err := parsePreset(p, "test=resize:fit:100:200/sharpen:2")

require.Nil(s.T(), err)

assert.Equal(s.T(), urlOptions{
"resize": []string{"fit", "100", "200"},
"sharpen": []string{"2"},
}, p["test"])
}

func (s *PresetsTestSuite) TestParsePresetInvalidString() {
p := make(presets)

presetStr := "resize:fit:100:200/sharpen:2"
err := parsePreset(p, presetStr)

assert.Equal(s.T(), fmt.Errorf("Invalid preset string: %s", presetStr), err)
assert.Empty(s.T(), p)
}

func (s *PresetsTestSuite) TestParsePresetEmptyName() {
p := make(presets)

presetStr := "=resize:fit:100:200/sharpen:2"
err := parsePreset(p, presetStr)

assert.Equal(s.T(), fmt.Errorf("Empty preset name: %s", presetStr), err)
assert.Empty(s.T(), p)
}

func (s *PresetsTestSuite) TestParsePresetEmptyValue() {
p := make(presets)

presetStr := "test="
err := parsePreset(p, presetStr)

assert.Equal(s.T(), fmt.Errorf("Empty preset value: %s", presetStr), err)
assert.Empty(s.T(), p)
}

func (s *PresetsTestSuite) TestParsePresetInvalidValue() {
p := make(presets)

presetStr := "test=resize:fit:100:200/sharpen:2/blur"
err := parsePreset(p, presetStr)

assert.Equal(s.T(), fmt.Errorf("Invalid preset value: %s", presetStr), err)
assert.Empty(s.T(), p)
}

func (s *PresetsTestSuite) TestParsePresetEmptyString() {
p := make(presets)

err := parsePreset(p, " ")

assert.Nil(s.T(), err)
assert.Empty(s.T(), p)
}

func (s *PresetsTestSuite) TestParsePresetComment() {
p := make(presets)

err := parsePreset(p, "# test=resize:fit:100:200/sharpen:2")

assert.Nil(s.T(), err)
assert.Empty(s.T(), p)
}

func (s *PresetsTestSuite) TestCheckPresets() {
p := presets{
"test": urlOptions{
"resize": []string{"fit", "100", "200"},
"sharpen": []string{"2"},
},
}

err := checkPresets(p)

assert.Nil(s.T(), err)
}

func (s *PresetsTestSuite) TestCheckPresetsInvalid() {
p := presets{
"test": urlOptions{
"resize": []string{"fit", "-1", "-2"},
"sharpen": []string{"2"},
},
}

err := checkPresets(p)

assert.Error(s.T(), err)
}

func TestPresets(t *testing.T) {
suite.Run(t, new(PresetsTestSuite))
}
@@ -754,7 +754,7 @@ func parsePathAdvanced(parts []string, headers *processingHeaders) (string, *pro
return url, po, nil
}

func parsePathSimple(parts []string, headers *processingHeaders) (string, *processingOptions, error) {
func parsePathBasic(parts []string, headers *processingHeaders) (string, *processingOptions, error) {
var err error

if len(parts) < 6 {
@@ -823,7 +823,7 @@ func parsePath(ctx context.Context, r *http.Request) (context.Context, error) {
var err error

if _, ok := resizeTypes[parts[1]]; ok {
imageURL, po, err = parsePathSimple(parts[1:], headers)
imageURL, po, err = parsePathBasic(parts[1:], headers)
} else {
imageURL, po, err = parsePathAdvanced(parts[1:], headers)
}

0 comments on commit 6b75512

Please sign in to comment.
You can’t perform that action at this time.