@@ -3,6 +3,9 @@ package todoist
33import (
44 "context"
55 "fmt"
6+ "io"
7+ "net/http"
8+ "strings"
69 "testing"
710
811 "github.com/google/go-cmp/cmp"
@@ -11,6 +14,12 @@ import (
1114 "github.com/trufflesecurity/trufflehog/v3/pkg/engine/ahocorasick"
1215)
1316
17+ type roundTripFunc func (* http.Request ) (* http.Response , error )
18+
19+ func (f roundTripFunc ) RoundTrip (req * http.Request ) (* http.Response , error ) {
20+ return f (req )
21+ }
22+
1423var (
1524 validPattern = "qpgv7z8amkp4ln55znaacezm9jy35wcayy6bya2r"
1625 invalidPattern = "qpgv7z8amkp4ln55znaa?ezm9jy35wcayy6bya2r"
@@ -89,3 +98,46 @@ func TestTodoist_Pattern(t *testing.T) {
8998 })
9099 }
91100}
101+
102+ func TestTodoist_VerificationEndpoint (t * testing.T ) {
103+ d := Scanner {}
104+ input := fmt .Sprintf ("%s token = '%s'" , keyword , validPattern )
105+
106+ prevClient := client
107+ t .Cleanup (func () {
108+ client = prevClient
109+ })
110+
111+ called := false
112+ client = & http.Client {
113+ Transport : roundTripFunc (func (req * http.Request ) (* http.Response , error ) {
114+ called = true
115+ if req .URL .String () != "https://api.todoist.com/api/v1/projects" {
116+ t .Fatalf ("unexpected verification URL: %s" , req .URL .String ())
117+ }
118+ if req .Header .Get ("Authorization" ) == "" {
119+ t .Fatal ("missing Authorization header" )
120+ }
121+
122+ return & http.Response {
123+ StatusCode : http .StatusOK ,
124+ Body : io .NopCloser (strings .NewReader ("{}" )),
125+ Header : make (http.Header ),
126+ }, nil
127+ }),
128+ }
129+
130+ results , err := d .FromData (context .Background (), true , []byte (input ))
131+ if err != nil {
132+ t .Fatalf ("FromData returned error: %v" , err )
133+ }
134+ if ! called {
135+ t .Fatal ("verification HTTP request was not made" )
136+ }
137+ if len (results ) != 1 {
138+ t .Fatalf ("expected 1 result, got %d" , len (results ))
139+ }
140+ if ! results [0 ].Verified {
141+ t .Fatal ("expected result to be verified" )
142+ }
143+ }
0 commit comments