11package talismanrc
22
33import (
4+ "regexp"
45 "talisman/detector/severity"
56 "testing"
67
8+ "github.com/spf13/afero"
79 "github.com/stretchr/testify/assert"
810)
911
12+ func TestLoadingFromFile (t * testing.T ) {
13+ fs := afero .NewMemMapFs ()
14+ file , err := afero .TempFile (fs , "" , DefaultRCFileName )
15+ assert .NoError (t , err , "Problem setting up test .talismanrc?" )
16+ talismanRCFile := file .Name ()
17+ SetFs__ (fs )
18+
19+ t .Run ("Creates an empty TalismanRC if .talismanrc file doesn't exist" , func (t * testing.T ) {
20+ SetRcFilename__ ("not-a-file" )
21+ emptyRC , err := Load ()
22+ assert .NoError (t , err , "Should not error if there is a problem reading the file" )
23+ assert .Equal (t , & TalismanRC {Version : DefaultRCVersion }, emptyRC )
24+ })
25+
26+ t .Run ("Loads all valid TalismanRC fields" , func (t * testing.T ) {
27+ SetRcFilename__ (talismanRCFile )
28+ err = afero .WriteFile (fs , talismanRCFile , []byte (fullyConfiguredTalismanRC ), 0666 )
29+ assert .NoError (t , err , "Problem setting up test .talismanrc?" )
30+
31+ talismanRCFromFile , _ := Load ()
32+ expectedTalismanRC := & TalismanRC {
33+ FileIgnoreConfig : []FileIgnoreConfig {
34+ {FileName : "existing.pem" , Checksum : "123444ddssa75333b25b6275f97680604add51b84eb8f4a3b9dcbbc652e6f27ac" }},
35+ ScopeConfig : []ScopeConfig {{"go" }},
36+ AllowedPatterns : []* Pattern {
37+ {regexp .MustCompile ("this-is-okay" )},
38+ {regexp .MustCompile ("key={listOfThings.id}" )}},
39+ CustomPatterns : []PatternString {"this-isn't-okay" },
40+ Threshold : severity .Medium ,
41+ CustomSeverities : []CustomSeverityConfig {
42+ {Detector : "HexContent" , Severity : severity .Low }},
43+ Experimental : ExperimentalConfig {Base64EntropyThreshold : 4.7 },
44+ Version : "1.0" ,
45+ }
46+ assert .Equal (t , expectedTalismanRC , talismanRCFromFile )
47+ })
48+
49+ SetRcFilename__ (DefaultRCFileName )
50+ }
51+
1052func TestUnmarshalsValidYaml (t * testing.T ) {
11- t .Run ("talismanrc should not fail as long as the yaml structure is correct" , func (t * testing.T ) {
53+ t .Run ("Should not fail as long as the yaml structure is correct" , func (t * testing.T ) {
1254 fileContents := []byte (`
1355---
1456fileignoreconfig:
@@ -23,7 +65,7 @@ custom_patterns:
2365 assert .Equal (t , 1 , len (rc .CustomPatterns ))
2466 })
2567
26- t .Run ("talismanrc.For(mode) should read multiple entries in rc file correctly" , func (t * testing.T ) {
68+ t .Run ("Should read multiple entries in rc file correctly" , func (t * testing.T ) {
2769 fileContent := []byte (`
2870fileignoreconfig:
2971- filename: testfile_1.yml
@@ -43,6 +85,22 @@ fileignoreconfig:
4385 assert .Equal (t , rc .FileIgnoreConfig [2 ].GetFileName (), "testfile_3.yml" )
4486 assert .True (t , rc .FileIgnoreConfig [2 ].ChecksumMatches ("file3_checksum" ))
4587 })
88+
89+ t .Run ("Should read severity level" , func (t * testing.T ) {
90+ talismanRCContents := []byte ("threshold: high" )
91+ persistedTalismanrc , _ := newPersistedRC (talismanRCContents )
92+ assert .Equal (t , persistedTalismanrc .Threshold , severity .High )
93+ })
94+
95+ t .Run ("Should read custom severities" , func (t * testing.T ) {
96+ talismanRCContents := []byte (`
97+ custom_severities:
98+ - detector: Base64Content
99+ severity: low
100+ ` )
101+ talismanRC , _ := newPersistedRC (talismanRCContents )
102+ assert .Equal (t , talismanRC .CustomSeverities , []CustomSeverityConfig {{Detector : "Base64Content" , Severity : severity .Low }})
103+ })
46104}
47105
48106func TestShouldIgnoreUnformattedFiles (t * testing.T ) {
@@ -53,9 +111,3 @@ func TestShouldIgnoreUnformattedFiles(t *testing.T) {
53111 assert .Equal (t , & TalismanRC {Version : "1.0" }, talismanRC , "Expected commented line '%s' to result in an empty TalismanRC" )
54112 }
55113}
56-
57- func TestShouldConvertThresholdToValue (t * testing.T ) {
58- talismanRCContents := []byte ("threshold: high" )
59- persistedTalismanrc , _ := newPersistedRC (talismanRCContents )
60- assert .Equal (t , persistedTalismanrc .Threshold , severity .High )
61- }
0 commit comments