|
| 1 | +package authz |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/wso2/open-mcp-auth-proxy/internal/config" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNewDefaultProvider(t *testing.T) { |
| 13 | + cfg := &config.Config{} |
| 14 | + provider := NewDefaultProvider(cfg) |
| 15 | + |
| 16 | + if provider == nil { |
| 17 | + t.Fatal("Expected non-nil provider") |
| 18 | + } |
| 19 | + |
| 20 | + // Ensure it implements the Provider interface |
| 21 | + var _ Provider = provider |
| 22 | +} |
| 23 | + |
| 24 | +func TestDefaultProviderWellKnownHandler(t *testing.T) { |
| 25 | + // Create a config with a custom well-known response |
| 26 | + cfg := &config.Config{ |
| 27 | + Default: config.DefaultConfig{ |
| 28 | + Path: map[string]config.PathConfig{ |
| 29 | + "/.well-known/oauth-authorization-server": { |
| 30 | + Response: &config.ResponseConfig{ |
| 31 | + Issuer: "https://test-issuer.com", |
| 32 | + JwksURI: "https://test-issuer.com/jwks", |
| 33 | + ResponseTypesSupported: []string{"code"}, |
| 34 | + GrantTypesSupported: []string{"authorization_code"}, |
| 35 | + CodeChallengeMethodsSupported: []string{"S256"}, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + provider := NewDefaultProvider(cfg) |
| 43 | + handler := provider.WellKnownHandler() |
| 44 | + |
| 45 | + // Create a test request |
| 46 | + req := httptest.NewRequest("GET", "/.well-known/oauth-authorization-server", nil) |
| 47 | + req.Host = "test-host.com" |
| 48 | + req.Header.Set("X-Forwarded-Proto", "https") |
| 49 | + |
| 50 | + // Create a response recorder |
| 51 | + w := httptest.NewRecorder() |
| 52 | + |
| 53 | + // Call the handler |
| 54 | + handler(w, req) |
| 55 | + |
| 56 | + // Check response status |
| 57 | + if w.Code != http.StatusOK { |
| 58 | + t.Errorf("Expected status OK, got %v", w.Code) |
| 59 | + } |
| 60 | + |
| 61 | + // Verify content type |
| 62 | + contentType := w.Header().Get("Content-Type") |
| 63 | + if contentType != "application/json" { |
| 64 | + t.Errorf("Expected Content-Type: application/json, got %s", contentType) |
| 65 | + } |
| 66 | + |
| 67 | + // Decode and check the response body |
| 68 | + var response map[string]interface{} |
| 69 | + if err := json.NewDecoder(w.Body).Decode(&response); err != nil { |
| 70 | + t.Fatalf("Failed to decode response JSON: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + // Check expected values |
| 74 | + if response["issuer"] != "https://test-issuer.com" { |
| 75 | + t.Errorf("Expected issuer=https://test-issuer.com, got %v", response["issuer"]) |
| 76 | + } |
| 77 | + if response["jwks_uri"] != "https://test-issuer.com/jwks" { |
| 78 | + t.Errorf("Expected jwks_uri=https://test-issuer.com/jwks, got %v", response["jwks_uri"]) |
| 79 | + } |
| 80 | + if response["authorization_endpoint"] != "https://test-host.com/authorize" { |
| 81 | + t.Errorf("Expected authorization_endpoint=https://test-host.com/authorize, got %v", response["authorization_endpoint"]) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestDefaultProviderHandleOPTIONS(t *testing.T) { |
| 86 | + provider := NewDefaultProvider(&config.Config{}) |
| 87 | + handler := provider.WellKnownHandler() |
| 88 | + |
| 89 | + // Create OPTIONS request |
| 90 | + req := httptest.NewRequest("OPTIONS", "/.well-known/oauth-authorization-server", nil) |
| 91 | + w := httptest.NewRecorder() |
| 92 | + |
| 93 | + // Call the handler |
| 94 | + handler(w, req) |
| 95 | + |
| 96 | + // Check response |
| 97 | + if w.Code != http.StatusNoContent { |
| 98 | + t.Errorf("Expected status NoContent for OPTIONS request, got %v", w.Code) |
| 99 | + } |
| 100 | + |
| 101 | + // Check CORS headers |
| 102 | + if w.Header().Get("Access-Control-Allow-Origin") != "*" { |
| 103 | + t.Errorf("Expected Access-Control-Allow-Origin: *, got %s", w.Header().Get("Access-Control-Allow-Origin")) |
| 104 | + } |
| 105 | + if w.Header().Get("Access-Control-Allow-Methods") != "GET, OPTIONS" { |
| 106 | + t.Errorf("Expected Access-Control-Allow-Methods: GET, OPTIONS, got %s", w.Header().Get("Access-Control-Allow-Methods")) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestDefaultProviderInvalidMethod(t *testing.T) { |
| 111 | + provider := NewDefaultProvider(&config.Config{}) |
| 112 | + handler := provider.WellKnownHandler() |
| 113 | + |
| 114 | + // Create POST request (which should be rejected) |
| 115 | + req := httptest.NewRequest("POST", "/.well-known/oauth-authorization-server", nil) |
| 116 | + w := httptest.NewRecorder() |
| 117 | + |
| 118 | + // Call the handler |
| 119 | + handler(w, req) |
| 120 | + |
| 121 | + // Check response |
| 122 | + if w.Code != http.StatusMethodNotAllowed { |
| 123 | + t.Errorf("Expected status MethodNotAllowed for POST request, got %v", w.Code) |
| 124 | + } |
| 125 | +} |
0 commit comments