Passed
Pull Request — master (#30)
by Stefano
02:32
created

cmd_test.TestScanWithUserAgentFlag   A

Complexity

Conditions 3

Size

Total Lines 38
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 27
nop 1
dl 0
loc 38
rs 9.232
c 0
b 0
f 0
1
package cmd_test
2
3
import (
4
	"bytes"
5
	"io/ioutil"
6
	"net/http"
7
	"net/http/httptest"
8
	"os"
9
	"strings"
10
	"sync/atomic"
11
	"testing"
12
13
	"github.com/spf13/cobra"
14
	"github.com/stefanoj3/dirstalk/pkg/cmd"
15
	"github.com/stefanoj3/dirstalk/pkg/common/test"
16
	"github.com/stretchr/testify/assert"
17
)
18
19
func TestRootCommand(t *testing.T) {
20
	logger, _ := test.NewLogger()
21
22
	c, err := cmd.NewRootCommand(logger)
23
	assert.NoError(t, err)
24
	assert.NotNil(t, c)
25
26
	_, out, err := executeCommandC(c)
27
	assert.NoError(t, err)
28
29
	// ensure the summary is printed
30
	assert.Contains(t, out, "dirstalk is a tool that attempts")
31
	assert.Contains(t, out, "Usage")
32
	assert.Contains(t, out, "dictionary.generate")
33
	assert.Contains(t, out, "scan")
34
}
35
36
func TestScanCommand(t *testing.T) {
37
	logger, _ := test.NewLogger()
38
39
	c, err := cmd.NewRootCommand(logger)
40
	assert.NoError(t, err)
41
	assert.NotNil(t, c)
42
43
	var calls int32
44
	srv := httptest.NewServer(
45
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
46
			atomic.AddInt32(&calls, 1)
47
			w.WriteHeader(http.StatusNotFound)
48
		}),
49
	)
50
	defer srv.Close()
51
52
	_, _, err = executeCommandC(c, "scan", srv.URL, "--dictionary", "testdata/dict.txt")
53
	assert.NoError(t, err)
54
55
	assert.Equal(t, int32(3), calls)
56
}
57
58
func TestScanWithRemoteDictionary(t *testing.T) {
59
	logger, _ := test.NewLogger()
60
61
	c, err := cmd.NewRootCommand(logger)
62
	assert.NoError(t, err)
63
	assert.NotNil(t, c)
64
65
	dictionaryServer := httptest.NewServer(
66
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
67
			dict := `home
68
home/index.php
69
blabla
70
`
71
			w.WriteHeader(http.StatusOK)
72
			_, _ = w.Write([]byte(dict))
73
		}),
74
	)
75
	defer dictionaryServer.Close()
76
77
	var calls int32
78
	srv := httptest.NewServer(
79
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
80
			atomic.AddInt32(&calls, 1)
81
			w.WriteHeader(http.StatusNotFound)
82
		}),
83
	)
84
	defer srv.Close()
85
86
	_, _, err = executeCommandC(c, "scan", srv.URL, "--dictionary", dictionaryServer.URL)
87
	assert.NoError(t, err)
88
89
	assert.Equal(t, int32(3), calls)
90
}
91
92
func TestScanWithUserAgentFlag(t *testing.T) {
93
	const testUserAgent = "my_test_user_agent"
94
95
	logger, _ := test.NewLogger()
96
97
	c, err := cmd.NewRootCommand(logger)
98
	assert.NoError(t, err)
99
	assert.NotNil(t, c)
100
101
	var callsWithMatchingUserAgent int32
102
	var callsWithNonMatchingUserAgent int32
103
104
	srv := httptest.NewServer(
105
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
106
			if r.Header.Get("User-Agent") == testUserAgent {
107
				atomic.AddInt32(&callsWithMatchingUserAgent, 1)
108
			} else {
109
				atomic.AddInt32(&callsWithNonMatchingUserAgent, 1)
110
			}
111
112
			w.WriteHeader(http.StatusNotFound)
113
		}),
114
	)
115
	defer srv.Close()
116
117
	_, _, err = executeCommandC(
118
		c,
119
		"scan",
120
		srv.URL,
121
		"--user-agent",
122
		testUserAgent,
123
		"--dictionary",
124
		"testdata/dict.txt",
125
	)
126
	assert.NoError(t, err)
127
128
	assert.Equal(t, int32(3), callsWithMatchingUserAgent)
129
	assert.Equal(t, int32(0), callsWithNonMatchingUserAgent)
130
}
131
132
func TestDictionaryGenerateCommand(t *testing.T) {
133
	logger, _ := test.NewLogger()
134
135
	c, err := cmd.NewRootCommand(logger)
136
	assert.NoError(t, err)
137
	assert.NotNil(t, c)
138
139
	testFilePath := "testdata/" + test.RandStringRunes(10)
140
	defer removeTestFile(testFilePath)
141
	_, _, err = executeCommandC(c, "dictionary.generate", ".", "-o", testFilePath)
142
	assert.NoError(t, err)
143
144
	content, err := ioutil.ReadFile(testFilePath)
145
	assert.NoError(t, err)
146
147
	// Ensure the command ran and produced some of the expected output
148
	// it is not in the scope of this test to ensure the correct output
149
	assert.Contains(t, string(content), "root_integration_test.go")
150
}
151
152
func TestVersionCommand(t *testing.T) {
153
	logger, buf := test.NewLogger()
154
155
	c, err := cmd.NewRootCommand(logger)
156
	assert.NoError(t, err)
157
	assert.NotNil(t, c)
158
159
	_, _, err = executeCommandC(c, "version")
160
	assert.NoError(t, err)
161
162
	// Ensure the command ran and produced some of the expected output
163
	// it is not in the scope of this test to ensure the correct output
164
	assert.Contains(t, buf.String(), "Version: ")
165
}
166
167
func executeCommandC(root *cobra.Command, args ...string) (c *cobra.Command, output string, err error) {
168
	buf := new(bytes.Buffer)
169
	root.SetOutput(buf)
170
171
	a := []string{""}
172
	os.Args = append(a, args...)
173
174
	c, err = root.ExecuteC()
175
176
	return c, buf.String(), err
177
}
178
179
func removeTestFile(path string) {
180
	if !strings.Contains(path, "testdata") {
181
		return
182
	}
183
184
	_ = os.Remove(path)
185
}
186