(t *testing.T)
| 13 | ) |
| 14 | |
| 15 | func TestGetDeviceCode(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | |
| 18 | t.Run("success", func(t *testing.T) { |
| 19 | t.Parallel() |
| 20 | var clientID, audience, scope, path string |
| 21 | expectedState := State{ |
| 22 | DeviceCode: "aDeviceCode", |
| 23 | UserCode: "aUserCode", |
| 24 | VerificationURI: "aVerificationURI", |
| 25 | ExpiresIn: 60, |
| 26 | } |
| 27 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 28 | r.ParseForm() |
| 29 | clientID = r.FormValue("client_id") |
| 30 | audience = r.FormValue("audience") |
| 31 | scope = r.FormValue("scope") |
| 32 | path = r.URL.Path |
| 33 | |
| 34 | jsonState, err := json.Marshal(expectedState) |
| 35 | assert.NilError(t, err) |
| 36 | |
| 37 | _, _ = w.Write(jsonState) |
| 38 | })) |
| 39 | defer ts.Close() |
| 40 | api := API{ |
| 41 | TenantURL: ts.URL, |
| 42 | ClientID: "aClientID", |
| 43 | Scopes: []string{"bork", "meow"}, |
| 44 | } |
| 45 | |
| 46 | state, err := api.GetDeviceCode(context.Background(), "anAudience") |
| 47 | assert.NilError(t, err) |
| 48 | |
| 49 | assert.DeepEqual(t, expectedState, state) |
| 50 | assert.Equal(t, clientID, "aClientID") |
| 51 | assert.Equal(t, audience, "anAudience") |
| 52 | assert.Equal(t, scope, "bork meow") |
| 53 | assert.Equal(t, path, "/oauth/device/code") |
| 54 | }) |
| 55 | |
| 56 | t.Run("error w/ description", func(t *testing.T) { |
| 57 | t.Parallel() |
| 58 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 59 | jsonState, err := json.Marshal(TokenResponse{ |
| 60 | ErrorDescription: "invalid audience", |
| 61 | }) |
| 62 | assert.NilError(t, err) |
| 63 | |
| 64 | w.WriteHeader(http.StatusBadRequest) |
| 65 | _, _ = w.Write(jsonState) |
| 66 | })) |
| 67 | defer ts.Close() |
| 68 | api := API{ |
| 69 | TenantURL: ts.URL, |
| 70 | ClientID: "aClientID", |
| 71 | Scopes: []string{"bork", "meow"}, |
| 72 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…