| 1 |
//nolint:testpackage |
| 2 |
package oauth2 |
| 3 |
|
| 4 |
import ( |
| 5 |
"context" |
| 6 |
"errors" |
| 7 |
"log/slog" |
| 8 |
"net/http" |
| 9 |
"net/http/httptest" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 13 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state" |
| 14 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/tokenstorage" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
) |
| 17 |
|
| 18 |
func TestAcceptOAuth2ClientDoesNotStoreRefreshStateOnAcceptError(t *testing.T) { |
| 19 |
t.Parallel() |
| 20 |
|
| 21 |
ctx := context.Background() |
| 22 |
conf := config.Defaults |
| 23 |
conf.OAuth2.Refresh.Enabled = true |
| 24 |
conf.OAuth2.Refresh.ValidateUser = false |
| 25 |
|
| 26 |
storage := newInMemoryStorage(t) |
| 27 |
client := Client{ |
| 28 |
conf: &conf, |
| 29 |
openvpn: failingOpenVPNClient{}, |
| 30 |
storage: storage, |
| 31 |
} |
| 32 |
|
| 33 |
recorder := httptest.NewRecorder() |
| 34 |
client.acceptOAuth2Client(ctx, recorder, codeExchangeRequest{ |
| 35 |
logger: slog.New(slog.DiscardHandler), |
| 36 |
clientID: "1", |
| 37 |
session: state.State{Client: state.ClientIdentifier{CID: 1, KID: 2}}, |
| 38 |
}, "missing") |
| 39 |
|
| 40 |
require.Equal(t, http.StatusInternalServerError, recorder.Code) |
| 41 |
|
| 42 |
_, err := storage.Get(ctx, "1") |
| 43 |
require.ErrorIs(t, err, tokenstorage.ErrNotExists) |
| 44 |
} |
| 45 |
|
| 46 |
type failingOpenVPNClient struct{} |
| 47 |
|
| 48 |
func (failingOpenVPNClient) AcceptClient( |
| 49 |
context.Context, |
| 50 |
*slog.Logger, |
| 51 |
state.ClientIdentifier, |
| 52 |
string, |
| 53 |
...string, |
| 54 |
) error { |
| 55 |
return errors.New("client configuration not found") |
| 56 |
} |
| 57 |
|
| 58 |
func (failingOpenVPNClient) DenyClient(context.Context, *slog.Logger, state.ClientIdentifier, string) { |
| 59 |
} |