(t *testing.T)
| 141 | } |
| 142 | |
| 143 | func TestViewOauthInit(t *testing.T) { |
| 144 | |
| 145 | t.Run("success", func(t *testing.T) { |
| 146 | app := &MockOAuthDatastoreProvider{} |
| 147 | h := oauthHandler{ |
| 148 | Config: app.Config(), |
| 149 | DB: app.DB(), |
| 150 | Store: app.SessionStore(), |
| 151 | EmailKey: []byte{0xd, 0xe, 0xc, 0xa, 0xf, 0xf, 0xb, 0xa, 0xd}, |
| 152 | oauthClient: writeAsOauthClient{ |
| 153 | ClientID: app.Config().WriteAsOauth.ClientID, |
| 154 | ClientSecret: app.Config().WriteAsOauth.ClientSecret, |
| 155 | ExchangeLocation: app.Config().WriteAsOauth.TokenLocation, |
| 156 | InspectLocation: app.Config().WriteAsOauth.InspectLocation, |
| 157 | AuthLocation: app.Config().WriteAsOauth.AuthLocation, |
| 158 | CallbackLocation: "http://localhost/oauth/callback", |
| 159 | HttpClient: nil, |
| 160 | }, |
| 161 | } |
| 162 | req, err := http.NewRequest("GET", "/oauth/client", nil) |
| 163 | assert.NoError(t, err) |
| 164 | rr := httptest.NewRecorder() |
| 165 | err = h.viewOauthInit(nil, rr, req) |
| 166 | assert.NotNil(t, err) |
| 167 | httpErr, ok := err.(impart.HTTPError) |
| 168 | assert.True(t, ok) |
| 169 | assert.Equal(t, http.StatusTemporaryRedirect, httpErr.Status) |
| 170 | assert.NotEmpty(t, httpErr.Message) |
| 171 | locURI, err := url.Parse(httpErr.Message) |
| 172 | assert.NoError(t, err) |
| 173 | assert.Equal(t, "/oauth/login", locURI.Path) |
| 174 | assert.Equal(t, "development", locURI.Query().Get("client_id")) |
| 175 | assert.Equal(t, "http://localhost/oauth/callback", locURI.Query().Get("redirect_uri")) |
| 176 | assert.Equal(t, "code", locURI.Query().Get("response_type")) |
| 177 | assert.NotEmpty(t, locURI.Query().Get("state")) |
| 178 | }) |
| 179 | |
| 180 | t.Run("state failure", func(t *testing.T) { |
| 181 | app := &MockOAuthDatastoreProvider{ |
| 182 | DoDB: func() OAuthDatastore { |
| 183 | return &MockOAuthDatastore{ |
| 184 | DoGenerateOAuthState: func(ctx context.Context, provider, clientID string, attachUserID int64, inviteCode string) (string, error) { |
| 185 | return "", fmt.Errorf("pretend unable to write state error") |
| 186 | }, |
| 187 | } |
| 188 | }, |
| 189 | } |
| 190 | h := oauthHandler{ |
| 191 | Config: app.Config(), |
| 192 | DB: app.DB(), |
| 193 | Store: app.SessionStore(), |
| 194 | EmailKey: []byte{0xd, 0xe, 0xc, 0xa, 0xf, 0xf, 0xb, 0xa, 0xd}, |
| 195 | oauthClient: writeAsOauthClient{ |
| 196 | ClientID: app.Config().WriteAsOauth.ClientID, |
| 197 | ClientSecret: app.Config().WriteAsOauth.ClientSecret, |
| 198 | ExchangeLocation: app.Config().WriteAsOauth.TokenLocation, |
| 199 | InspectLocation: app.Config().WriteAsOauth.InspectLocation, |
| 200 | AuthLocation: app.Config().WriteAsOauth.AuthLocation, |
nothing calls this directly
no test coverage detected