package values
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/cortezaproject/corteza/server/compose/types"
)
func Test_sanitizer_Run(t *testing.T) {
tests := []struct {
name string
kind string
options map[string]interface{}
input string
output string
outref uint64
}{
{
name: "numbers should be trimmed",
kind: "Number",
input: " 42 ",
output: "42",
},
{
name: "object reference should be processed",
kind: "Record",
input: " 133569629112020995 ",
output: "133569629112020995",
outref: 133569629112020995,
},
{
name: "object reference should be numeric",
kind: "Record",
input: " foo ",
output: "",
},
{
name: "user reference should be processed",
kind: "User",
input: " 133569629112020995 ",
output: "133569629112020995",
outref: 133569629112020995,
},
{
name: "user reference should be numeric",
kind: "User",
input: " foo ",
output: "",
},
{
name: "strings should be kept intact",
kind: "String",
input: " The answer ",
output: " The answer ",
},
{
name: "booleans should be converted (t)",
kind: "Bool",
input: "t",
output: "1",
},
{
name: "booleans should be converted (false)",
kind: "Bool",
input: "false",
output: "",
},
{
name: "booleans should be converted (garbage)",
kind: "Bool",
input: "%%#)%)')$)'",
output: "",
},
{
name: "dates should be converted to ISO",
kind: "DateTime",
input: "Mon Jan 2 15:04:05 2006",
output: "2006-01-02T15:04:05Z",
},
{
name: "dates should be converted to UTC",
kind: "DateTime",
input: "2020-03-02T20:20:20+05:00",
output: "2020-03-02T15:20:20Z",
},
{
name: "micro/mili seconds should be cut off",
kind: "DateTime",
input: "2020-03-11T11:20:08.471Z",
output: "2020-03-11T11:20:08Z",
},
{
name: "number space trim",
kind: "Number",
input: " 42 ",
output: "42",
},
{
name: "number negative",
kind: "Number",
input: "-42",
output: "-42",
},
{
name: "number positive",
kind: "Number",
input: "+42",
output: "42",
},
{
name: "number precision",
kind: "Number",
options: map[string]interface{}{"precision": 3},
input: "42.44455",
output: "42.445",
},
{
name: "number precision; bigger precision then provided fracture",
kind: "Number",
options: map[string]interface{}{"precision": 3},
input: "42.4",
output: "42.4",
},
{
name: "number precision; default to 0",
kind: "Number",
options: map[string]interface{}{},
input: "42.4",
output: "42",
},
{
name: "number precision; clamped between [0, 6]",
kind: "Number",
options: map[string]interface{}{"precision": 10},
input: "42.5555555555",
output: "42.555556",
},
{
name: "number precision; round",
kind: "Number",
options: map[string]interface{}{"precision": 0},
input: "41.6",
output: "42",
},
{
name: "number precision; trailing .00",
kind: "Number",
options: map[string]interface{}{"precision": 2},
input: "42.00",
output: "42",
},
{
name: "number precision; trailing .040",
kind: "Number",
options: map[string]interface{}{"precision": 2},
input: "42.040",
output: "42.04",
},
{
name: "string escaping; html",
kind: "String",
options: map[string]interface{}{},
input: "Title here",
output: "Title here",
},
{
name: "string escaping; html a.href with javascript alert",
kind: "String",
options: map[string]interface{}{},
input: `XSS`,
output: "XSS",
},
{
name: "string escaping; a.href with javascript",
kind: "String",
options: map[string]interface{}{},
input: `XSS`,
output: "XSS",
},
{
name: "string escaping; script with script",
kind: "String",
options: map[string]interface{}{},
input: `pt src="https://cortezaproject.org/script.js">`,
output: `pt src="https://cortezaproject.org/script.js">`,
},
{
name: "string escaping; inline styles unchanged",
kind: "String",
options: map[string]interface{}{},
input: `nasty looking content`,
output: `nasty looking content`,
},
{
name: "string escaping; script with a",
kind: "String",
options: map[string]interface{}{},
input: ``,
output: "",
},
{
name: "string escaping; meta with script",
kind: "String",
options: map[string]interface{}{},
input: ``,
output: "",
},
{
name: "string escaping; object",
kind: "String",
options: map[string]interface{}{},
input: ``,
output: "",
},
{
name: "string escaping; base href",
kind: "String",
options: map[string]interface{}{},
input: `