(t *testing.T)
| 14 | ) |
| 15 | |
| 16 | func TestWaitUntilUserLogsIn(t *testing.T) { |
| 17 | state := State{ |
| 18 | "1234", |
| 19 | "12345", |
| 20 | "https://example.com/12345", |
| 21 | 1000, |
| 22 | 1, |
| 23 | } |
| 24 | |
| 25 | t.Run("successfully waits and handles response", func(t *testing.T) { |
| 26 | counter := 0 |
| 27 | tokenResponse := `{ |
| 28 | "access_token": "Zm9v.eyJhdWQiOiBbImh0dHBzOi8vYXV0aDAtY2xpLXRlc3QudXMuYXV0aDAuY29tL2FwaS92Mi8iXX0", |
| 29 | "id_token": "id-token-here", |
| 30 | "scope": "scope-here", |
| 31 | "token_type": "token-type-here", |
| 32 | "expires_in": 1000 |
| 33 | }` |
| 34 | ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 35 | w.WriteHeader(http.StatusOK) |
| 36 | if counter < 1 { |
| 37 | _, err := io.WriteString(w, `{ |
| 38 | "error": "authorization_pending", |
| 39 | "error_description": "still pending auth" |
| 40 | }`) |
| 41 | require.NoError(t, err) |
| 42 | } else { |
| 43 | _, err := io.WriteString(w, tokenResponse) |
| 44 | require.NoError(t, err) |
| 45 | } |
| 46 | counter++ |
| 47 | })) |
| 48 | |
| 49 | defer ts.Close() |
| 50 | |
| 51 | parsedURL, err := url.Parse(ts.URL) |
| 52 | assert.NoError(t, err) |
| 53 | u := url.URL{Scheme: "https", Host: parsedURL.Host, Path: "/oauth/token"} |
| 54 | credentials.OauthTokenEndpoint = u.String() |
| 55 | |
| 56 | result, err := WaitUntilUserLogsIn(context.Background(), ts.Client(), state) |
| 57 | |
| 58 | assert.NoError(t, err) |
| 59 | assert.Equal(t, "auth0-cli-test", result.Tenant) |
| 60 | assert.Equal(t, "auth0-cli-test.us.auth0.com", result.Domain) |
| 61 | }) |
| 62 | |
| 63 | testCases := []struct { |
| 64 | name string |
| 65 | httpStatus int |
| 66 | response string |
| 67 | expect string |
| 68 | }{ |
| 69 | { |
| 70 | name: "handle malformed JSON", |
| 71 | httpStatus: http.StatusOK, |
| 72 | response: "foo", |
| 73 | expect: "cannot decode response: invalid character 'o' in literal false (expecting 'a')", |
nothing calls this directly
no test coverage detected