(t *testing.T)
| 111 | } |
| 112 | |
| 113 | func TestGetDeviceCode(t *testing.T) { |
| 114 | t.Run("successfully retrieve state from response", func(t *testing.T) { |
| 115 | ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 116 | w.Header().Set("Content-Type", "application/json") |
| 117 | _, err := io.WriteString(w, `{ |
| 118 | "device_code": "device-code-here", |
| 119 | "user_code": "user-code-here", |
| 120 | "verification_uri_complete": "verification-uri-here", |
| 121 | "expires_in": 1000, |
| 122 | "interval": 1 |
| 123 | }`) |
| 124 | require.NoError(t, err) |
| 125 | })) |
| 126 | |
| 127 | defer ts.Close() |
| 128 | |
| 129 | parsedURL, err := url.Parse(ts.URL) |
| 130 | assert.NoError(t, err) |
| 131 | u := url.URL{Scheme: "https", Host: parsedURL.Host, Path: "/oauth/device/code"} |
| 132 | credentials.DeviceCodeEndpoint = u.String() |
| 133 | |
| 134 | state, err := GetDeviceCode(context.Background(), ts.Client(), []string{}, "") |
| 135 | |
| 136 | assert.NoError(t, err) |
| 137 | assert.Equal(t, "device-code-here", state.DeviceCode) |
| 138 | assert.Equal(t, "user-code-here", state.UserCode) |
| 139 | assert.Equal(t, "verification-uri-here", state.VerificationURI) |
| 140 | assert.Equal(t, 1000, state.ExpiresIn) |
| 141 | assert.Equal(t, 1, state.Interval) |
| 142 | assert.Equal(t, time.Duration(4000000000), state.IntervalDuration()) |
| 143 | }) |
| 144 | |
| 145 | testCases := []struct { |
| 146 | name string |
| 147 | httpStatus int |
| 148 | response string |
| 149 | expect string |
| 150 | }{ |
| 151 | { |
| 152 | name: "handle HTTP status errors", |
| 153 | httpStatus: http.StatusNotFound, |
| 154 | response: "Test response return", |
| 155 | expect: "received a 404 response: Test response return", |
| 156 | }, |
| 157 | { |
| 158 | name: "handle bad JSON response", |
| 159 | httpStatus: http.StatusOK, |
| 160 | response: "foo", |
| 161 | expect: "failed to decode the response: invalid character 'o' in literal false (expecting 'a')", |
| 162 | }, |
| 163 | } |
| 164 | |
| 165 | for _, testCase := range testCases { |
| 166 | t.Run(testCase.name, func(t *testing.T) { |
| 167 | ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 168 | w.WriteHeader(testCase.httpStatus) |
| 169 | if testCase.response != "" { |
| 170 | _, err := io.WriteString(w, testCase.response) |
nothing calls this directly
no test coverage detected