@@ -2,6 +2,7 @@ package todoist
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "io"
78 "net/http"
@@ -141,3 +142,85 @@ func TestTodoist_VerificationEndpoint(t *testing.T) {
141142 t .Fatal ("expected result to be verified" )
142143 }
143144}
145+
146+ func TestTodoist_VerificationResponses (t * testing.T ) {
147+ d := Scanner {}
148+ input := fmt .Sprintf ("%s token = '%s'" , keyword , validPattern )
149+
150+ tests := []struct {
151+ name string
152+ response * http.Response
153+ responseErr error
154+ wantVerified bool
155+ wantVerificationErr bool
156+ }{
157+ {
158+ name : "valid token" ,
159+ response : & http.Response {
160+ StatusCode : http .StatusOK ,
161+ Body : io .NopCloser (strings .NewReader ("{}" )),
162+ Header : make (http.Header ),
163+ },
164+ wantVerified : true ,
165+ },
166+ {
167+ name : "unauthorized token" ,
168+ response : & http.Response {
169+ StatusCode : http .StatusUnauthorized ,
170+ Body : io .NopCloser (strings .NewReader ("{}" )),
171+ Header : make (http.Header ),
172+ },
173+ },
174+ {
175+ name : "forbidden token" ,
176+ response : & http.Response {
177+ StatusCode : http .StatusForbidden ,
178+ Body : io .NopCloser (strings .NewReader ("{}" )),
179+ Header : make (http.Header ),
180+ },
181+ },
182+ {
183+ name : "client error" ,
184+ responseErr : errors .New ("network failure" ),
185+ wantVerificationErr : true ,
186+ },
187+ {
188+ name : "unexpected status" ,
189+ response : & http.Response {
190+ StatusCode : http .StatusInternalServerError ,
191+ Body : io .NopCloser (strings .NewReader ("{}" )),
192+ Header : make (http.Header ),
193+ },
194+ wantVerificationErr : true ,
195+ },
196+ }
197+
198+ for _ , tt := range tests {
199+ t .Run (tt .name , func (t * testing.T ) {
200+ prevClient := client
201+ t .Cleanup (func () {
202+ client = prevClient
203+ })
204+
205+ client = & http.Client {
206+ Transport : roundTripFunc (func (req * http.Request ) (* http.Response , error ) {
207+ return tt .response , tt .responseErr
208+ }),
209+ }
210+
211+ results , err := d .FromData (context .Background (), true , []byte (input ))
212+ if err != nil {
213+ t .Fatalf ("FromData returned error: %v" , err )
214+ }
215+ if len (results ) != 1 {
216+ t .Fatalf ("expected 1 result, got %d" , len (results ))
217+ }
218+ if results [0 ].Verified != tt .wantVerified {
219+ t .Fatalf ("Verified = %v, want %v" , results [0 ].Verified , tt .wantVerified )
220+ }
221+ if (results [0 ].VerificationError () != nil ) != tt .wantVerificationErr {
222+ t .Fatalf ("wantVerificationError = %v, verification error = %v" , tt .wantVerificationErr , results [0 ].VerificationError ())
223+ }
224+ })
225+ }
226+ }
0 commit comments