|
| 1 | +package controller_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http/httptest" |
| 7 | + "os" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | + "github.com/steveiliop56/tinyauth/internal/bootstrap" |
| 12 | + "github.com/steveiliop56/tinyauth/internal/config" |
| 13 | + "github.com/steveiliop56/tinyauth/internal/controller" |
| 14 | + "github.com/steveiliop56/tinyauth/internal/repository" |
| 15 | + "github.com/steveiliop56/tinyauth/internal/service" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +func TestWellKnownController(t *testing.T) { |
| 20 | + oidcServiceCfg := service.OIDCServiceConfig{ |
| 21 | + Clients: map[string]config.OIDCClientConfig{ |
| 22 | + "test": { |
| 23 | + ClientID: "some-client-id", |
| 24 | + ClientSecret: "some-client-secret", |
| 25 | + TrustedRedirectURIs: []string{"https://test.example.com/callback"}, |
| 26 | + Name: "Test Client", |
| 27 | + }, |
| 28 | + }, |
| 29 | + PrivateKeyPath: "/tmp/tinyauth_testing_key.pem", |
| 30 | + PublicKeyPath: "/tmp/tinyauth_testing_key.pub", |
| 31 | + Issuer: "https://tinyauth.example.com", |
| 32 | + SessionExpiry: 500, |
| 33 | + } |
| 34 | + |
| 35 | + type testCase struct { |
| 36 | + description string |
| 37 | + run func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) |
| 38 | + } |
| 39 | + |
| 40 | + tests := []testCase{ |
| 41 | + { |
| 42 | + description: "Ensure well-known endpoint returns correct OIDC configuration", |
| 43 | + run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) { |
| 44 | + req := httptest.NewRequest("GET", "/.well-known/openid-configuration", nil) |
| 45 | + router.ServeHTTP(recorder, req) |
| 46 | + |
| 47 | + assert.Equal(t, 200, recorder.Code) |
| 48 | + |
| 49 | + res := controller.OpenIDConnectConfiguration{} |
| 50 | + err := json.Unmarshal(recorder.Body.Bytes(), &res) |
| 51 | + assert.NoError(t, err) |
| 52 | + |
| 53 | + expected := controller.OpenIDConnectConfiguration{ |
| 54 | + Issuer: oidcServiceCfg.Issuer, |
| 55 | + AuthorizationEndpoint: fmt.Sprintf("%s/authorize", oidcServiceCfg.Issuer), |
| 56 | + TokenEndpoint: fmt.Sprintf("%s/api/oidc/token", oidcServiceCfg.Issuer), |
| 57 | + UserinfoEndpoint: fmt.Sprintf("%s/api/oidc/userinfo", oidcServiceCfg.Issuer), |
| 58 | + JwksUri: fmt.Sprintf("%s/.well-known/jwks.json", oidcServiceCfg.Issuer), |
| 59 | + ScopesSupported: service.SupportedScopes, |
| 60 | + ResponseTypesSupported: service.SupportedResponseTypes, |
| 61 | + GrantTypesSupported: service.SupportedGrantTypes, |
| 62 | + SubjectTypesSupported: []string{"pairwise"}, |
| 63 | + IDTokenSigningAlgValuesSupported: []string{"RS256"}, |
| 64 | + TokenEndpointAuthMethodsSupported: []string{"client_secret_basic", "client_secret_post"}, |
| 65 | + ClaimsSupported: []string{"sub", "updated_at", "name", "preferred_username", "email", "email_verified", "groups"}, |
| 66 | + ServiceDocumentation: "https://tinyauth.app/docs/guides/oidc", |
| 67 | + } |
| 68 | + |
| 69 | + assert.Equal(t, res, expected) |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + description: "Ensure well-known endpoint returns correct JWKS", |
| 74 | + run: func(t *testing.T, router *gin.Engine, recorder *httptest.ResponseRecorder) { |
| 75 | + req := httptest.NewRequest("GET", "/.well-known/jwks.json", nil) |
| 76 | + router.ServeHTTP(recorder, req) |
| 77 | + |
| 78 | + assert.Equal(t, 200, recorder.Code) |
| 79 | + |
| 80 | + decodedBody := make(map[string]any) |
| 81 | + err := json.Unmarshal(recorder.Body.Bytes(), &decodedBody) |
| 82 | + assert.NoError(t, err) |
| 83 | + |
| 84 | + keys, ok := decodedBody["keys"].([]any) |
| 85 | + assert.True(t, ok) |
| 86 | + assert.Len(t, keys, 1) |
| 87 | + |
| 88 | + keyData, ok := keys[0].(map[string]any) |
| 89 | + assert.True(t, ok) |
| 90 | + assert.Equal(t, "RSA", keyData["kty"]) |
| 91 | + assert.Equal(t, "sig", keyData["use"]) |
| 92 | + assert.Equal(t, "RS256", keyData["alg"]) |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + app := bootstrap.NewBootstrapApp(config.Config{}) |
| 98 | + |
| 99 | + db, err := app.SetupDatabase("/tmp/tinyauth_test.db") |
| 100 | + assert.NoError(t, err) |
| 101 | + |
| 102 | + queries := repository.New(db) |
| 103 | + |
| 104 | + oidcService := service.NewOIDCService(oidcServiceCfg, queries) |
| 105 | + err = oidcService.Init() |
| 106 | + assert.NoError(t, err) |
| 107 | + |
| 108 | + for _, test := range tests { |
| 109 | + t.Run(test.description, func(t *testing.T) { |
| 110 | + router := gin.Default() |
| 111 | + gin.SetMode(gin.TestMode) |
| 112 | + |
| 113 | + recorder := httptest.NewRecorder() |
| 114 | + |
| 115 | + wellKnownController := controller.NewWellKnownController(controller.WellKnownControllerConfig{}, oidcService, router) |
| 116 | + wellKnownController.SetupRoutes() |
| 117 | + |
| 118 | + test.run(t, router, recorder) |
| 119 | + }) |
| 120 | + } |
| 121 | + |
| 122 | + err = db.Close() |
| 123 | + assert.NoError(t, err) |
| 124 | + |
| 125 | + err = os.Remove("/tmp/tinyauth_test.db") |
| 126 | + assert.NoError(t, err) |
| 127 | +} |
0 commit comments