|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestBasicAuth(t *testing.T) { |
| 13 | + // Start mcp-front with basic auth config |
| 14 | + startMCPFront(t, "config/config.basic-auth-test.json", |
| 15 | + "ADMIN_PASSWORD=adminpass123", |
| 16 | + "USER_PASSWORD=userpass456", |
| 17 | + ) |
| 18 | + |
| 19 | + // Wait for startup |
| 20 | + waitForMCPFront(t) |
| 21 | + |
| 22 | + t.Run("valid credentials", func(t *testing.T) { |
| 23 | + req, err := http.NewRequest("GET", "http://localhost:8080/postgres/sse", nil) |
| 24 | + require.NoError(t, err) |
| 25 | + req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:adminpass123"))) |
| 26 | + req.Header.Set("Accept", "text/event-stream") |
| 27 | + |
| 28 | + client := &http.Client{} |
| 29 | + resp, err := client.Do(req) |
| 30 | + require.NoError(t, err) |
| 31 | + defer resp.Body.Close() |
| 32 | + |
| 33 | + // Should get 200 OK with SSE stream when auth passes |
| 34 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 35 | + assert.Equal(t, "text/event-stream", resp.Header.Get("Content-Type")) |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("invalid password", func(t *testing.T) { |
| 39 | + req, err := http.NewRequest("GET", "http://localhost:8080/postgres/sse", nil) |
| 40 | + require.NoError(t, err) |
| 41 | + req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:wrongpass"))) |
| 42 | + |
| 43 | + client := &http.Client{} |
| 44 | + resp, err := client.Do(req) |
| 45 | + require.NoError(t, err) |
| 46 | + defer resp.Body.Close() |
| 47 | + |
| 48 | + assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("unknown user", func(t *testing.T) { |
| 52 | + req, err := http.NewRequest("GET", "http://localhost:8080/postgres/sse", nil) |
| 53 | + require.NoError(t, err) |
| 54 | + req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("unknown:adminpass123"))) |
| 55 | + |
| 56 | + client := &http.Client{} |
| 57 | + resp, err := client.Do(req) |
| 58 | + require.NoError(t, err) |
| 59 | + defer resp.Body.Close() |
| 60 | + |
| 61 | + assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) |
| 62 | + }) |
| 63 | + |
| 64 | + t.Run("access MCP endpoint with basic auth", func(t *testing.T) { |
| 65 | + // Test accessing a protected MCP endpoint |
| 66 | + req, err := http.NewRequest("GET", "http://localhost:8080/postgres/sse", nil) |
| 67 | + require.NoError(t, err) |
| 68 | + req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("user:userpass456"))) |
| 69 | + req.Header.Set("Accept", "text/event-stream") |
| 70 | + |
| 71 | + client := &http.Client{} |
| 72 | + resp, err := client.Do(req) |
| 73 | + require.NoError(t, err) |
| 74 | + defer resp.Body.Close() |
| 75 | + |
| 76 | + // Should get 200 OK with SSE stream when auth passes |
| 77 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 78 | + assert.Equal(t, "text/event-stream", resp.Header.Get("Content-Type")) |
| 79 | + }) |
| 80 | + |
| 81 | + t.Run("bearer token with basic auth configured", func(t *testing.T) { |
| 82 | + // Server expects basic auth, bearer tokens should fail |
| 83 | + req, err := http.NewRequest("GET", "http://localhost:8080/postgres/sse", nil) |
| 84 | + require.NoError(t, err) |
| 85 | + req.Header.Set("Authorization", "Bearer sometoken") |
| 86 | + |
| 87 | + client := &http.Client{} |
| 88 | + resp, err := client.Do(req) |
| 89 | + require.NoError(t, err) |
| 90 | + defer resp.Body.Close() |
| 91 | + |
| 92 | + assert.Equal(t, http.StatusUnauthorized, resp.StatusCode) |
| 93 | + }) |
| 94 | +} |
0 commit comments