(t *testing.T)
| 118 | } |
| 119 | |
| 120 | func TestWaitForDeviceToken(t *testing.T) { |
| 121 | t.Parallel() |
| 122 | |
| 123 | t.Run("success", func(t *testing.T) { |
| 124 | t.Parallel() |
| 125 | expectedToken := TokenResponse{ |
| 126 | AccessToken: "a-real-token", |
| 127 | IDToken: "", |
| 128 | RefreshToken: "the-refresh-token", |
| 129 | Scope: "", |
| 130 | ExpiresIn: 3600, |
| 131 | TokenType: "", |
| 132 | } |
| 133 | var respond atomic.Bool |
| 134 | go func() { |
| 135 | time.Sleep(5 * time.Second) |
| 136 | respond.Store(true) |
| 137 | }() |
| 138 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 139 | assert.Equal(t, "POST", r.Method) |
| 140 | assert.Equal(t, "/oauth/token", r.URL.Path) |
| 141 | assert.Equal(t, r.FormValue("client_id"), "aClientID") |
| 142 | assert.Equal(t, r.FormValue("grant_type"), "urn:ietf:params:oauth:grant-type:device_code") |
| 143 | assert.Equal(t, r.FormValue("device_code"), "aDeviceCode") |
| 144 | |
| 145 | if respond.Load() { |
| 146 | jsonState, err := json.Marshal(expectedToken) |
| 147 | assert.NilError(t, err) |
| 148 | w.Write(jsonState) |
| 149 | } else { |
| 150 | pendingError := "authorization_pending" |
| 151 | jsonResponse, err := json.Marshal(TokenResponse{ |
| 152 | Error: &pendingError, |
| 153 | }) |
| 154 | assert.NilError(t, err) |
| 155 | w.Write(jsonResponse) |
| 156 | } |
| 157 | })) |
| 158 | defer ts.Close() |
| 159 | api := API{ |
| 160 | TenantURL: ts.URL, |
| 161 | ClientID: "aClientID", |
| 162 | Scopes: []string{"bork", "meow"}, |
| 163 | } |
| 164 | state := State{ |
| 165 | DeviceCode: "aDeviceCode", |
| 166 | UserCode: "aUserCode", |
| 167 | Interval: 1, |
| 168 | ExpiresIn: 30, |
| 169 | } |
| 170 | token, err := api.WaitForDeviceToken(context.Background(), state) |
| 171 | assert.NilError(t, err) |
| 172 | |
| 173 | assert.DeepEqual(t, token, expectedToken) |
| 174 | }) |
| 175 | |
| 176 | t.Run("timeout", func(t *testing.T) { |
| 177 | t.Parallel() |
nothing calls this directly
no test coverage detected
searching dependent graphs…