Skip to content

Commit 927199f

Browse files
committed
Add tests for retrieve errors
1 parent 8322f7f commit 927199f

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

deviceauth.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,18 @@ func retrieveDeviceAuth(ctx context.Context, c *Config, v url.Values) (*DeviceAu
134134
retrieveError.ErrorDescription = vals.Get("error_description")
135135
retrieveError.ErrorURI = vals.Get("error_uri")
136136
default:
137-
json.Unmarshal(body, &retrieveError) // no error checks
137+
var tj struct {
138+
// https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
139+
ErrorCode string `json:"error"`
140+
ErrorDescription string `json:"error_description"`
141+
ErrorURI string `json:"error_uri"`
142+
}
143+
if json.Unmarshal(body, &tj) != nil {
144+
return nil, retrieveError
145+
}
146+
retrieveError.ErrorCode = tj.ErrorCode
147+
retrieveError.ErrorDescription = tj.ErrorDescription
148+
retrieveError.ErrorURI = tj.ErrorURI
138149
}
139150

140151
return nil, retrieveError

deviceauth_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"net/http"
8+
"net/http/httptest"
79
"strings"
810
"testing"
911
"time"
@@ -101,3 +103,49 @@ func ExampleConfig_DeviceAuth() {
101103
}
102104
fmt.Println(token)
103105
}
106+
107+
func TestDeviceAuthTokenRetrieveErrorJSON(t *testing.T) {
108+
for _, responseFun := range []func(w http.ResponseWriter){
109+
func(w http.ResponseWriter) {
110+
w.Header().Set("Content-type", "application/x-www-form-urlencoded")
111+
// "The authorization server responds with an HTTP 400 (Bad Request)" https://www.rfc-editor.org/rfc/rfc6749#section-5.2
112+
w.WriteHeader(http.StatusBadRequest)
113+
w.Write([]byte(`error=invalid_grant&error_description=sometext`))
114+
},
115+
func(w http.ResponseWriter) {
116+
w.Header().Set("Content-type", "application/json")
117+
// "The authorization server responds with an HTTP 400 (Bad Request)" https://www.rfc-editor.org/rfc/rfc6749#section-5.2
118+
w.WriteHeader(http.StatusBadRequest)
119+
w.Write([]byte(`{"error": "invalid_grant", "error_description": "sometext"}`))
120+
},
121+
} {
122+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
123+
if r.URL.String() != "/device" {
124+
t.Errorf("Unexpected device auth request URL, %v is found.", r.URL)
125+
}
126+
responseFun(w)
127+
}))
128+
defer ts.Close()
129+
conf := newConf(ts.URL)
130+
_, err := conf.DeviceAuth(context.Background())
131+
if err == nil {
132+
t.Fatalf("got no error, expected one")
133+
}
134+
re, ok := err.(*RetrieveError)
135+
if !ok {
136+
t.Fatalf("got %T error, expected *RetrieveError; error was: %v", err, err)
137+
}
138+
expected := `oauth2: "invalid_grant" "sometext"`
139+
if errStr := err.Error(); errStr != expected {
140+
t.Fatalf("got %#v, expected %#v", errStr, expected)
141+
}
142+
expected = "invalid_grant"
143+
if re.ErrorCode != expected {
144+
t.Fatalf("got %#v, expected %#v", re.ErrorCode, expected)
145+
}
146+
expected = "sometext"
147+
if re.ErrorDescription != expected {
148+
t.Fatalf("got %#v, expected %#v", re.ErrorDescription, expected)
149+
}
150+
}
151+
}

oauth2_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func newConf(url string) *Config {
3131
RedirectURL: "REDIRECT_URL",
3232
Scopes: []string{"scope1", "scope2"},
3333
Endpoint: Endpoint{
34-
AuthURL: url + "/auth",
35-
TokenURL: url + "/token",
34+
AuthURL: url + "/auth",
35+
DeviceAuthURL: url + "/device",
36+
TokenURL: url + "/token",
3637
},
3738
}
3839
}

0 commit comments

Comments
 (0)