Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
brianvoe committed Jun 21, 2024
2 parents 9d25135 + 356c601 commit 05bf36a
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 19 deletions.
22 changes: 11 additions & 11 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,29 @@ func password(f *Faker, lower bool, upper bool, numeric bool, special bool, spac
items := make([]any, 0)
weights := make([]float32, 0)
if lower {
items = append(items, "lower")
items = append(items, "l")
weights = append(weights, 4)
}
if upper {
items = append(items, "upper")
items = append(items, "u")
weights = append(weights, 4)
}
if numeric {
items = append(items, "numeric")
items = append(items, "n")
weights = append(weights, 3)
}
if special {
items = append(items, "special")
items = append(items, "e")
weights = append(weights, 2)
}
if space {
items = append(items, "space")
items = append(items, "a")
weights = append(weights, 1)
}

// If no items are selected then default to lower, upper, numeric
if len(items) == 0 {
items = append(items, "lower", "upper", "numeric")
items = append(items, "l", "u", "n")
weights = append(weights, 4, 4, 3)
}

Expand All @@ -70,15 +70,15 @@ func password(f *Faker, lower bool, upper bool, numeric bool, special bool, spac
weight, _ := weighted(f, items, weights)

switch weight.(string) {
case "lower":
case "l":
b[i] = lowerStr[f.Int64()%int64(len(lowerStr))]
case "upper":
case "u":
b[i] = upperStr[f.Int64()%int64(len(upperStr))]
case "numeric":
case "n":
b[i] = numericStr[f.Int64()%int64(len(numericStr))]
case "special":
case "e":
b[i] = specialSafeStr[f.Int64()%int64(len(specialSafeStr))]
case "space":
case "a":
b[i] = spaceStr[f.Int64()%int64(len(spaceStr))]
}
}
Expand Down
8 changes: 4 additions & 4 deletions auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func ExamplePassword() {
// Output: cfelntbponnbbzrhswobuwlxajeeclrx
// KYEKNGUUNKUYSFBUFFTGDKUVCVYKPONP
// 43622637275953627791234759581343
// !?*-_-?#@!*?@?-&@!*!*?_!#$@@@**@
// .DTHJ&@oF#d.L5F65 N.-#U5xWX F0DI
// @.__-._-!-!_..!-_*_*__-@*.__.__!
// -DTHJ@.oF@d@L5F65 N-.@U5xWX F0DI
// foZnB
}

Expand All @@ -78,8 +78,8 @@ func ExampleFaker_Password() {
// Output: cfelntbponnbbzrhswobuwlxajeeclrx
// KYEKNGUUNKUYSFBUFFTGDKUVCVYKPONP
// 43622637275953627791234759581343
// !?*-_-?#@!*?@?-&@!*!*?_!#$@@@**@
// .DTHJ&@oF#d.L5F65 N.-#U5xWX F0DI
// @.__-._-!-!_..!-_*_*__-@*.__.__!
// -DTHJ@.oF@d@L5F65 N-.@U5xWX F0DI
// foZnB
}

Expand Down
2 changes: 1 addition & 1 deletion faker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Example() {
// Credit Card: 6282690620525711
// Hacker Phrase: Try to bundle the PNG firewall, maybe it will deconstruct the open-source bandwidth!
// Job Title: Assistant
// Password: Nyf8p8ka1Kvgn**@3H&$w7j01yM1vkc2
// Password: Nyf8p8ka1Kvgn...3H*.w7j01yM1vkc2
}

func ExampleNew() {
Expand Down
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const lowerStr = "abcdefghijklmnopqrstuvwxyz"
const upperStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numericStr = "0123456789"
const specialStr = "@#$%&?|!(){}<>=*+-_:;,."
const specialSafeStr = "@#$&?!-_*."
const specialSafeStr = "!@.-_*" // https://github.com/1Password/spg/pull/22
const spaceStr = " "
const allStr = lowerStr + upperStr + numericStr + specialStr + spaceStr
const vowels = "aeiou"
Expand Down
14 changes: 13 additions & 1 deletion json.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,19 @@ func addFileJSONLookup() {
// encoding/json.RawMessage is a special case of []byte
// it cannot be handled as a reflect.Array/reflect.Slice
// because it needs additional structure in the output
func rJsonRawMessage(f *Faker, v reflect.Value) error {
func rJsonRawMessage(f *Faker, v reflect.Value, tag string) error {
if tag != "" {
err := rCustom(f, v, tag)
if err == nil {
jsonData := v.Bytes()
if !json.Valid(jsonData) {
fName, _ := parseNameAndParamsFromTag(tag)
return errors.New("custom function " + fName + " returned invalid json data: " + string(jsonData))
}
}
return err
}

b, err := f.JSON(nil)
if err != nil {
return err
Expand Down
78 changes: 78 additions & 0 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,84 @@ func TestJSONRawMessageWithTag(t *testing.T) {
}
}

func TestJSONRawMessageWithCustomFuncTag(t *testing.T) {
AddFuncLookup("customjsontest", Info{
Display: "CustomJSONTest",
Category: "file",
Example: `{"ErTA":"bale","FQJUIGrmnRBfuGlb":"over","HTJJPnEKGS":"please say that again","HvLvfsQRGbK":"whenever one turns around","KKbMlbxquDmwwvRWVlPmwRAeAw":"Voluptatem eaque quia facilis quo."}`,
Output: "[]byte",
ContentType: "application/json",
Params: []Param{},
Generate: func(f *Faker, m *MapParams, info *Info) (any, error) {
length := f.Number(5, 15)
dataSet := []string{"word", "phrase", "loremipsumsentence", "{hackeradjective}-{hackernoun}", "float64", "bool"}
resultMap := make(map[string]any)
for i := 0; i < length; i++ {
key := f.LetterN(uint(f.Number(3, 30)))
val, err := f.Generate(RandomString(dataSet))
if err != nil {
return nil, err
}
resultMap[key] = val
}
marshal, err := json.Marshal(resultMap)
if err != nil {
panic(err)
}
return marshal, nil
},
})

type J struct {
Field json.RawMessage `json:"field" fake:"customjsontest"`
}

Seed(11)

var objs []J
Slice(&objs)

_, err := json.Marshal(objs)
if err != nil {
t.Fatal(err)
}
}

func TestJSONRawMessageWithInvalidCustomFuncTag(t *testing.T) {
AddFuncLookup("invalidjsontest", Info{
Display: "InvalidJSONTest",
Category: "file",
Example: `[181 251 51 164 185 142 21 3 33]`,
Output: "[]byte",
Params: []Param{},
Generate: func(f *Faker, m *MapParams, info *Info) (any, error) {
var result []byte
Slice(&result)
return result, nil
},
})

type J struct {
Field json.RawMessage `json:"field" fake:"invalidjsontest"`
}

Seed(11)

var objs []J
Slice(&objs)

_, err := json.Marshal(objs)
if err != nil {
t.Fatal(err)
}

var obj J
err = Struct(&obj)
if err == nil {
t.Fatal("'Struct(&obj)' was supposed to return an error but didn't")
}
}

func TestJSONNumber(t *testing.T) {
type J struct {
Field json.Number `json:"field"`
Expand Down
2 changes: 1 addition & 1 deletion struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func r(f *Faker, t reflect.Type, v reflect.Value, tag string, size int) error {

switch t.Name() {
case "RawMessage":
return rJsonRawMessage(f, v)
return rJsonRawMessage(f, v, tag)
case "Number":
return rJsonNumber(f, v, tag)
default:
Expand Down

0 comments on commit 05bf36a

Please sign in to comment.