Skip to content

Commit 11fc3cd

Browse files
Merge pull request #16 from shashimalcse/shashimalcse-patch-0003
2 parents 5a392ea + 42efe1f commit 11fc3cd

File tree

6 files changed

+692
-0
lines changed

6 files changed

+692
-0
lines changed

Diff for: .gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,12 @@ go.sum
3030
# OS generated files
3131
.DS_Store
3232

33+
# builds
3334
openmcpauthproxy
35+
36+
# test out files
37+
coverage.out
38+
coverage.html
39+
40+
# IDE files
41+
.vscode

Diff for: Makefile

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Makefile for open-mcp-auth-proxy
2+
3+
# Variables
4+
BINARY_NAME := openmcpauthproxy
5+
GO := go
6+
GOFMT := gofmt
7+
GOVET := go vet
8+
GOTEST := go test
9+
GOLINT := golangci-lint
10+
GOCOV := go tool cover
11+
BUILD_DIR := build
12+
13+
# Source files
14+
SRC := $(shell find . -name "*.go" -not -path "./vendor/*")
15+
PKGS := $(shell go list ./... | grep -v /vendor/)
16+
17+
# Set build options
18+
BUILD_OPTS := -v
19+
20+
# Set test options
21+
TEST_OPTS := -v -race
22+
23+
.PHONY: all build clean test fmt lint vet coverage help
24+
25+
# Default target
26+
all: lint test build
27+
28+
# Build the application
29+
build:
30+
@echo "Building $(BINARY_NAME)..."
31+
@mkdir -p $(BUILD_DIR)
32+
$(GO) build $(BUILD_OPTS) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/proxy
33+
34+
# Clean build artifacts
35+
clean:
36+
@echo "Cleaning build artifacts..."
37+
@rm -rf $(BUILD_DIR)
38+
@rm -f coverage.out
39+
40+
# Run tests
41+
test:
42+
@echo "Running tests..."
43+
$(GOTEST) $(TEST_OPTS) ./...
44+
45+
# Run tests with coverage report
46+
coverage:
47+
@echo "Running tests with coverage..."
48+
@$(GOTEST) -coverprofile=coverage.out ./...
49+
@$(GOCOV) -func=coverage.out
50+
@$(GOCOV) -html=coverage.out -o coverage.html
51+
@echo "Coverage report generated in coverage.html"
52+
53+
# Run gofmt
54+
fmt:
55+
@echo "Running gofmt..."
56+
@$(GOFMT) -w -s $(SRC)
57+
58+
# Run go vet
59+
vet:
60+
@echo "Running go vet..."
61+
@$(GOVET) ./...
62+
63+
# Show help
64+
help:
65+
@echo "Available targets:"
66+
@echo " all : Run lint, test, and build"
67+
@echo " build : Build the application"
68+
@echo " clean : Clean build artifacts"
69+
@echo " test : Run tests"
70+
@echo " coverage : Run tests with coverage report"
71+
@echo " fmt : Run gofmt"
72+
@echo " vet : Run go vet"
73+
@echo " help : Show this help message"

Diff for: internal/authz/default_test.go

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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

Comments
 (0)