-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [NA] add additional unit tests
- Loading branch information
1 parent
d4e7761
commit 84c9858
Showing
7 changed files
with
175 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package censor | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestConfig_ToString(t *testing.T) { | ||
t.Run("default config", func(t *testing.T) { | ||
// GIVEN a config instance. | ||
cfg := DefaultConfig() | ||
|
||
// WHEN the ToString method is called on the config instance. | ||
got := cfg.ToString() | ||
|
||
// THEN the returned string contains the configuration details. | ||
want := "---------------------------------------------------------------------\n Censor is configured with the following settings:\n---------------------------------------------------------------------\nprint-config-on-init: true\nmask-value: '[CENSORED]'\nexclude-patterns: []\n---------------------------------------------------------------------\n" | ||
require.Equal(t, want, got) | ||
}) | ||
|
||
t.Run("custom config", func(t *testing.T) { | ||
// GIVEN a config instance. | ||
cfg := Config{ | ||
PrintConfigOnInit: false, | ||
MaskValue: "test", | ||
ExcludePatterns: []string{"[0-9]"}, | ||
} | ||
|
||
// WHEN the ToString method is called on the config instance. | ||
got := cfg.ToString() | ||
|
||
// THEN the returned string contains the configuration details. | ||
want := "---------------------------------------------------------------------\n Censor is configured with the following settings:\n---------------------------------------------------------------------\nprint-config-on-init: false\nmask-value: test\nexclude-patterns:\n - '[0-9]'\n---------------------------------------------------------------------\n" | ||
require.Equal(t, want, got) | ||
}) | ||
} | ||
|
||
func TestConfig_FromFile(t *testing.T) { | ||
t.Run("read config from file", func(t *testing.T) { | ||
// GIVEN a path to a configuration file. | ||
path := "testdata/cfg.yml" | ||
|
||
// WHEN the ConfigFromFile function is called with the path. | ||
got, err := ConfigFromFile(path) | ||
|
||
// THEN the config is read from the file and returned. | ||
require.NoError(t, err) | ||
require.Equal(t, Config{ | ||
PrintConfigOnInit: true, | ||
MaskValue: "[CENSORED]", | ||
ExcludePatterns: []string{"\\d", "^\\w$"}, | ||
}, got) | ||
}) | ||
|
||
t.Run("fail to read config from file", func(t *testing.T) { | ||
// GIVEN a path to a non-existing configuration file. | ||
path := "testdata/non-existing.yml" | ||
|
||
// WHEN the ConfigFromFile function is called with the path. | ||
_, err := ConfigFromFile(path) | ||
|
||
// THEN an error is returned. | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("fail to unmarshal config from file", func(t *testing.T) { | ||
// GIVEN a path to a configuration file with invalid content. | ||
path := "testdata/invalid_cfg.yml" | ||
|
||
// WHEN the ConfigFromFile function is called with the path. | ||
_, err := ConfigFromFile(path) | ||
|
||
// THEN an error is returned. | ||
require.Error(t, err) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package censor | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
type S struct { | ||
String1 string `censor:"display"` | ||
String2 string | ||
Array [2]string `censor:"display"` | ||
} | ||
|
||
// GIVEN a default processor instance and a payload. | ||
p := S{ | ||
String1: "value", | ||
String2: "value", | ||
Array: [2]string{"value", "value"}, | ||
} | ||
|
||
// WHEN the Format func is called. | ||
got := Format(p) | ||
|
||
// THEN the returned string contains the masked values. | ||
want := `{"String1":"value","String2":"[CENSORED]","Array":["value","value"]}` | ||
require.JSONEq(t, want, got) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,75 @@ | ||
package censor | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProcessor_Format(t *testing.T) { | ||
type Address struct { | ||
Country string `censor:"display"` | ||
City string | ||
type Nested struct { | ||
String string `censor:"display"` | ||
Float64 float64 | ||
Array [2]string `censor:"display"` | ||
} | ||
|
||
type User struct { | ||
Name string `censor:"display"` | ||
Email string | ||
Map map[string]string `censor:"display"` | ||
Address | ||
type S struct { | ||
String1 string `censor:"display"` | ||
String2 string | ||
Map map[string]string `censor:"display"` | ||
Slice []string | ||
Nested | ||
} | ||
|
||
u := User{ | ||
Name: "John Doe", | ||
Email: "example@gmail.com", | ||
Map: map[string]string{ | ||
"mapKey": "mapVal", | ||
"2": "two", | ||
}, | ||
Address: Address{ | ||
Country: "Ukraine", | ||
City: "Kharkiv", | ||
}, | ||
} | ||
t.Run("with exclude pattern", func(t *testing.T) { | ||
// GIVEN a config instance. | ||
cfg := Config{ | ||
PrintConfigOnInit: true, | ||
MaskValue: "[CENSORED]", | ||
ExcludePatterns: []string{`\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b`}, | ||
} | ||
p, err := NewWithOpts(WithConfig(&cfg)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
m := map[string]User{"1": u} | ||
// WHEN the Format method is called on the processor instance. | ||
payload := S{ | ||
String1: "Hello", | ||
String2: "exampe@gmail.com", | ||
Map: map[string]string{"key": "value"}, | ||
Slice: []string{"one", "two"}, | ||
Nested: Nested{ | ||
String: "Nothing", | ||
Float64: 3.14, | ||
Array: [2]string{"one", "exampe@gmail.com"}, | ||
}, | ||
} | ||
got := p.Format(payload) | ||
|
||
cfg := Config{ | ||
PrintConfigOnInit: true, | ||
MaskValue: "[CENSORED]", | ||
ExcludePatterns: []string{"[0-9]"}, | ||
} | ||
// THEN the returned string contains the configuration details. | ||
want := `{"String1":"Hello","String2":"[CENSORED]","Map":{"key":"value"},"Slice":"[CENSORED]","String":"Nothing","Float64":"[CENSORED]","Array":["one","[CENSORED]"]}` | ||
require.JSONEq(t, want, got) | ||
}) | ||
|
||
p, err := NewWithOpts(WithConfig(&cfg)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Run("with config file option", func(t *testing.T) { | ||
// GIVEN a config instance. | ||
p, err := NewWithOpts(WithConfigPath("testdata/cfg.yml")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// WHEN the Format method is called on the processor instance. | ||
payload := S{ | ||
String1: "Hello", | ||
String2: "exampe@gmail.com", | ||
Map: map[string]string{"key": "value"}, | ||
Slice: []string{"one", "two"}, | ||
} | ||
got := p.Format(payload) | ||
|
||
fmt.Println(p.Format(m)) | ||
// THEN the returned string contains the configuration details. | ||
want := `{"String1":"Hello", "String2":"[CENSORED]", "Map": {"key":"value"}, "Slice":"[CENSORED]", "Array": ["", ""], "Float64":"[CENSORED]", "String":"" }` | ||
require.JSONEq(t, want, got) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
general: | ||
print-config-on-init: true | ||
parser: | ||
use-json-tag-name: true | ||
formatter: | ||
mask-value: '[CENSORED]' | ||
display-pointer-symbol: true | ||
display-struct-name: true | ||
display-map-type: true | ||
exclude-patterns: | ||
- \d | ||
- ^\w$ | ||
print-config-on-init: true | ||
mask-value: '[CENSORED]' | ||
exclude-patterns: | ||
- \d | ||
- ^\w$ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
general: | ||
print-config-on-init: true | ||
parser: | ||
@use-json-tag-name: true | ||
formatter: | ||
mask-value: '[CENSORED]' | ||
display-pointer-symbol: true | ||
display-struct-name: true | ||
display-map-type: true | ||
exclude-patterns: ['\d'] | ||
print-config-on-init: true | ||
@mask-value: '[CENSORED]' | ||
exclude-patterns: | ||
- \d | ||
- ^\w$ |
This file was deleted.
Oops, something went wrong.