forked from thoughtworks/talisman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacceptance_test.go
115 lines (95 loc) · 3.52 KB
/
acceptance_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
git "github.com/thoughtworks/talisman/git_testing"
)
func TestNotHavingAnyOutgoingChangesShouldNotFail(t *testing.T) {
withNewTmpGitRepo(func(gitPath string) {
git.SetupBaselineFiles(gitPath, "simple-file")
assert.Equal(t, 0, runTalisman(gitPath), "Expected run() to return 0 if no input is available on stdin. This happens when there are no outgoing changes")
})
}
func TestAddingSimpleFileShouldExitZero(t *testing.T) {
withNewTmpGitRepo(func(gitPath string) {
git.SetupBaselineFiles(gitPath, "simple-file")
exitStatus := runTalisman(gitPath)
assert.Equal(t, 0, exitStatus, "Expected run() to return 0 and pass as no suspicious files are in the repo")
})
}
func TestAddingSecretKeyShouldExitOne(t *testing.T) {
withNewTmpGitRepo(func(gitPath string) {
git.SetupBaselineFiles(gitPath, "simple-file")
git.CreateFileWithContents(gitPath, "private.pem", "secret")
git.AddAndcommit(gitPath, "*", "add private key")
exitStatus := runTalisman(gitPath)
assert.Equal(t, 1, exitStatus, "Expected run() to return 1 and fail as pem file was present in the repo")
})
}
func TestAddingSecretKeyAsFileContentShouldExitOne(t *testing.T) {
const awsAccessKeyIDExample string = "accessKey=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
withNewTmpGitRepo(func(gitPath string) {
git.SetupBaselineFiles(gitPath, "simple-file")
git.CreateFileWithContents(gitPath, "contains_keys.properties", awsAccessKeyIDExample)
git.AddAndcommit(gitPath, "*", "add private key as content")
exitStatus := runTalisman(gitPath)
assert.Equal(t, 1, exitStatus, "Expected run() to return 1 and fail as pem file was present in the repo")
})
}
func TestAddingSecretKeyShouldExitZeroIfPEMFilesAreIgnored(t *testing.T) {
withNewTmpGitRepo(func(gitPath string) {
git.SetupBaselineFiles(gitPath, "simple-file")
git.CreateFileWithContents(gitPath, "private.pem", "secret")
git.CreateFileWithContents(gitPath, ".talismanignore", "*.pem")
git.AddAndcommit(gitPath, "*", "add private key")
exitStatus := runTalisman(gitPath)
assert.Equal(t, 0, exitStatus, "Expected run() to return 0 and pass as pem file was ignored")
})
}
func TestStagingSecretKeyShouldExitOneWhenPreCommitFlagIsSet(t *testing.T) {
withNewTmpGitRepo(func(gitPath string) {
git.SetupBaselineFiles(gitPath, "simple-file")
git.CreateFileWithContents(gitPath, "private.pem", "secret")
git.Add(gitPath, "*")
options := Options{
debug: false,
githook: "pre-commit",
}
exitStatus := runTalismanWithOptions(gitPath, options)
assert.Equal(t, 1, exitStatus, "Expected run() to return 1 and fail as pem file was present in the repo")
})
}
func runTalisman(gitPath string) int {
options := Options{
debug: false,
githook: "pre-push",
}
return runTalismanWithOptions(gitPath, options)
}
func runTalismanWithOptions(gitPath string, options Options) int {
os.Chdir(gitPath)
return run(mockStdIn(git.EarliestCommit(gitPath), git.LatestCommit(gitPath)), options)
}
func withNewTmpGitRepo(gitOp func(gitPath string)) {
WithNewTmpDirNamed("talisman-acceptance-test", func(gitPath string) {
git.Init(gitPath)
gitOp(gitPath)
})
}
type DirOp func(dirName string)
func WithNewTmpDirNamed(dirName string, dop DirOp) {
path, err := ioutil.TempDir(os.TempDir(), dirName)
if err != nil {
panic(err)
}
defer os.RemoveAll(path)
dop(path)
}
func mockStdIn(oldSha string, newSha string) io.Reader {
return strings.NewReader(fmt.Sprintf("master %s master %s\n", newSha, oldSha))
}