| 1 |
package httphandler_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"net/url" |
| 6 |
"testing" |
| 7 |
"testing/fstest" |
| 8 |
|
| 9 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 10 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types" |
| 11 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/crypto" |
| 12 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/httphandler" |
| 13 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2" |
| 14 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/providers/generic" |
| 15 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testlogger" |
| 16 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testsuite" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
) |
| 19 |
|
| 20 |
func TestAssets(t *testing.T) { |
| 21 |
t.Parallel() |
| 22 |
|
| 23 |
logger := testlogger.New() |
| 24 |
|
| 25 |
conf := config.Defaults |
| 26 |
conf.OAuth2.Issuer = types.URL{URL: &url.URL{Scheme: config.SchemeHTTP, Host: "localhost"}} |
| 27 |
conf.OAuth2.Endpoints.Discovery = conf.OAuth2.Issuer |
| 28 |
conf.OAuth2.Endpoints.Auth = conf.OAuth2.Issuer |
| 29 |
conf.OAuth2.Endpoints.Token = conf.OAuth2.Issuer |
| 30 |
conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: config.SchemeHTTP, Host: "localhost:9000"}} |
| 31 |
|
| 32 |
provider, err := generic.NewProvider(t.Context(), &conf, http.DefaultClient) |
| 33 |
require.NoError(t, err) |
| 34 |
|
| 35 |
stateCrypto, err := crypto.New(conf.HTTP.Secret.String()) |
| 36 |
require.NoError(t, err) |
| 37 |
oAuth2Client, err := oauth2.New( |
| 38 |
t.Context(), |
| 39 |
logger.Logger(), |
| 40 |
&conf, |
| 41 |
http.DefaultClient, |
| 42 |
testsuite.NewFakeStorage(), |
| 43 |
stateCrypto, |
| 44 |
provider, |
| 45 |
testsuite.NewFakeOpenVPNClient(), |
| 46 |
) |
| 47 |
require.NoError(t, err) |
| 48 |
|
| 49 |
handler := httphandler.New(&conf, oAuth2Client) |
| 50 |
|
| 51 |
require.HTTPStatusCode(t, handler.ServeHTTP, http.MethodGet, "/", nil, http.StatusNotFound) |
| 52 |
require.HTTPSuccess(t, handler.ServeHTTP, http.MethodGet, "/assets/favicon.svg", nil) |
| 53 |
require.HTTPSuccess(t, handler.ServeHTTP, http.MethodGet, "/assets/i18n/de.json", nil) |
| 54 |
} |
| 55 |
|
| 56 |
func TestCustomAssets(t *testing.T) { |
| 57 |
t.Parallel() |
| 58 |
|
| 59 |
logger := testlogger.New() |
| 60 |
|
| 61 |
conf := config.Defaults |
| 62 |
conf.OAuth2.Issuer = types.URL{URL: &url.URL{Scheme: config.SchemeHTTP, Host: "localhost"}} |
| 63 |
conf.OAuth2.Endpoints.Discovery = conf.OAuth2.Issuer |
| 64 |
conf.OAuth2.Endpoints.Auth = conf.OAuth2.Issuer |
| 65 |
conf.OAuth2.Endpoints.Token = conf.OAuth2.Issuer |
| 66 |
conf.HTTP.BaseURL = types.URL{URL: &url.URL{Scheme: config.SchemeHTTP, Host: "localhost:9000"}} |
| 67 |
conf.HTTP.BaseURL.Path = "/custom" |
| 68 |
|
| 69 |
provider, err := generic.NewProvider(t.Context(), &conf, http.DefaultClient) |
| 70 |
require.NoError(t, err) |
| 71 |
|
| 72 |
stateCrypto, err := crypto.New(conf.HTTP.Secret.String()) |
| 73 |
require.NoError(t, err) |
| 74 |
oAuth2Client, err := oauth2.New( |
| 75 |
t.Context(), |
| 76 |
logger.Logger(), |
| 77 |
&conf, |
| 78 |
http.DefaultClient, |
| 79 |
testsuite.NewFakeStorage(), |
| 80 |
stateCrypto, |
| 81 |
provider, |
| 82 |
testsuite.NewFakeOpenVPNClient(), |
| 83 |
) |
| 84 |
require.NoError(t, err) |
| 85 |
|
| 86 |
conf.HTTP.AssetPath = types.FS{ |
| 87 |
FS: fstest.MapFS{ |
| 88 |
"index.txt": &fstest.MapFile{ |
| 89 |
Data: []byte("index"), |
| 90 |
}, |
| 91 |
}, |
| 92 |
} |
| 93 |
|
| 94 |
handler := httphandler.New(&conf, oAuth2Client) |
| 95 |
|
| 96 |
require.HTTPBodyContains(t, handler.ServeHTTP, http.MethodGet, "/custom/assets/index.txt", nil, "index") |
| 97 |
} |