Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

7.0 KB
Raw Blame
package store_test

import (
	"context"
	"testing"
	"time"

	"github.com/bytebase/bytebase/backend/common/testcontainer"

	"github.com/stretchr/testify/require"

	storepb "github.com/bytebase/bytebase/backend/generated-go/store"
	"github.com/bytebase/bytebase/backend/store"

	_ "github.com/bytebase/bytebase/backend/plugin/db/pg"
)

// TestOAuth2WorkspaceBinding exercises the workspace round-trip on
// oauth2_authorization_code and oauth2_refresh_token via the real store.
// This is the persistence half of the consent → token workspace binding
// flow (the in-memory propagation through handleAuthorizePost /
// handleAuthorizationCodeGrant / issueTokens / GenerateOAuth2AccessToken
// is covered by the unit tests in backend/api/oauth2/token_test.go and
// the helper-package unit tests below).
//
// Covers:
//   - Non-empty workspace persists across write/read
//   - Legacy empty-workspace path: a code or refresh token with no workspace
//     (pre-3.18.2 row) survives the migration's nullable column and reads
//     back as "" so the handler's fallback chain to client.Workspace /
//     singleton can take over.
func TestOAuth2WorkspaceBinding(t *testing.T) {
	t.Parallel()
	ctx := context.Background()
	db, s, _ := testcontainer.NewMetadataDB(t)

	// Seed the minimal fixtures needed by the FK constraints on the OAuth2
	// tables: a workspace and a principal.
	_, err := db.ExecContext(ctx, `
		INSERT INTO workspace (resource_id) VALUES ('ws-test');
		INSERT INTO principal (name, email, password_hash) VALUES ('demo', '[email protected]', 'unused');
		INSERT INTO oauth2_client (client_id, workspace, client_secret_hash, config)
		VALUES ('client-A', NULL, 'unused-hash', '{}'::jsonb);
	`)
	require.NoError(t, err)

	t.Run("auth code round-trips workspace bound at consent time", func(t *testing.T) {
		t.Parallel()
		_, err := s.CreateOAuth2AuthorizationCode(ctx, &store.OAuth2AuthorizationCodeMessage{
			Code:      "code-with-ws",
			ClientID:  "client-A",
			UserEmail: "[email protected]",
			Workspace: "ws-test",
			Config:    &storepb.OAuth2AuthorizationCodeConfig{RedirectUri: "http://localhost/cb"},
			ExpiresAt: time.Now().Add(10 * time.Minute),
		})
		require.NoError(t, err)

		got, err := s.GetOAuth2AuthorizationCode(ctx, "client-A", "code-with-ws")
		require.NoError(t, err)
		require.NotNil(t, got)
		require.Equal(t, "ws-test", got.Workspace,
			"workspace written at consent time must round-trip through the DB into the issued code")
	})

	t.Run("auth code with empty workspace stays empty for legacy fallback", func(t *testing.T) {
		t.Parallel()
		// Simulates a pre-3.18.2 auth code created before the workspace
		// column existed. The migration added the column as nullable, so
		// existing rows have NULL and Get returns "".
		_, err := s.CreateOAuth2AuthorizationCode(ctx, &store.OAuth2AuthorizationCodeMessage{
			Code:      "code-no-ws",
			ClientID:  "client-A",
			UserEmail: "[email protected]",
			Workspace: "", // explicitly empty
			Config:    &storepb.OAuth2AuthorizationCodeConfig{RedirectUri: "http://localhost/cb"},
			ExpiresAt: time.Now().Add(10 * time.Minute),
		})
		require.NoError(t, err)

		got, err := s.GetOAuth2AuthorizationCode(ctx, "client-A", "code-no-ws")
		require.NoError(t, err)
		require.NotNil(t, got)
		require.Empty(t, got.Workspace,
			"empty workspace must persist as NULL and read back as \"\" so the handler's fallback chain (client.Workspace → singleton) can run")
	})

	t.Run("refresh token preserves workspace across refresh", func(t *testing.T) {
		t.Parallel()
		_, err := s.CreateOAuth2RefreshToken(ctx, &store.OAuth2RefreshTokenMessage{
			TokenHash: "rt-hash-1",
			ClientID:  "client-A",
			UserEmail: "[email protected]",
			Workspace: "ws-test",
			ExpiresAt: time.Now().Add(30 * 24 * time.Hour),
		})
		require.NoError(t, err)

		got, err := s.GetOAuth2RefreshToken(ctx, "client-A", "rt-hash-1")
		require.NoError(t, err)
		require.NotNil(t, got)
		require.Equal(t, "ws-test", got.Workspace,
			"refresh token must carry the workspace binding so a /token refresh re-issues for the same workspace")
	})

	t.Run("refresh token with empty workspace stays empty", func(t *testing.T) {
		t.Parallel()
		_, err := s.CreateOAuth2RefreshToken(ctx, &store.OAuth2RefreshTokenMessage{
			TokenHash: "rt-hash-2",
			ClientID:  "client-A",
			UserEmail: "[email protected]",
			Workspace: "",
			ExpiresAt: time.Now().Add(30 * 24 * time.Hour),
		})
		require.NoError(t, err)

		got, err := s.GetOAuth2RefreshToken(ctx, "client-A", "rt-hash-2")
		require.NoError(t, err)
		require.NotNil(t, got)
		require.Empty(t, got.Workspace)
	})

	// The refresh token's config payload (3.22.1) is the durable half of the token
	// boundary: a refresh reads it back to re-issue the same grant, so a dropped
	// value would silently widen or unbind the session.
	t.Run("refresh token config round-trips resource and scope", func(t *testing.T) {
		t.Parallel()
		_, err := s.CreateOAuth2RefreshToken(ctx, &store.OAuth2RefreshTokenMessage{
			TokenHash: "rt-hash-3",
			ClientID:  "client-A",
			UserEmail: "[email protected]",
			Workspace: "ws-test",
			Config: &storepb.OAuth2RefreshTokenConfig{
				Resource: "https://bb.example.com/mcp",
				Scope:    "mcp:read-only",
			},
			ExpiresAt: time.Now().Add(30 * 24 * time.Hour),
		})
		require.NoError(t, err)

		got, err := s.GetOAuth2RefreshToken(ctx, "client-A", "rt-hash-3")
		require.NoError(t, err)
		require.NotNil(t, got)
		require.Equal(t, "https://bb.example.com/mcp", got.Config.GetResource())
		require.Equal(t, "mcp:read-only", got.Config.GetScope())
	})

	t.Run("refresh token with no config stays empty", func(t *testing.T) {
		t.Parallel()
		// Both parameters are optional, and rows written before 3.22.1 default to
		// '{}' — either way the handler must see "" and carry "" forward.
		_, err := s.CreateOAuth2RefreshToken(ctx, &store.OAuth2RefreshTokenMessage{
			TokenHash: "rt-hash-4",
			ClientID:  "client-A",
			UserEmail: "[email protected]",
			Workspace: "ws-test",
			ExpiresAt: time.Now().Add(30 * 24 * time.Hour),
		})
		require.NoError(t, err)

		got, err := s.GetOAuth2RefreshToken(ctx, "client-A", "rt-hash-4")
		require.NoError(t, err)
		require.NotNil(t, got)
		require.Empty(t, got.Config.GetResource())
		require.Empty(t, got.Config.GetScope())
	})

	t.Run("auth code config round-trips resource and scope", func(t *testing.T) {
		t.Parallel()
		_, err := s.CreateOAuth2AuthorizationCode(ctx, &store.OAuth2AuthorizationCodeMessage{
			Code:      "code-with-resource",
			ClientID:  "client-A",
			UserEmail: "[email protected]",
			Workspace: "ws-test",
			Config: &storepb.OAuth2AuthorizationCodeConfig{
				RedirectUri: "http://localhost/cb",
				Resource:    "https://bb.example.com/mcp",
				Scope:       "mcp:read-write",
			},
			ExpiresAt: time.Now().Add(10 * time.Minute),
		})
		require.NoError(t, err)

		got, err := s.GetOAuth2AuthorizationCode(ctx, "client-A", "code-with-resource")
		require.NoError(t, err)
		require.NotNil(t, got)
		require.Equal(t, "https://bb.example.com/mcp", got.Config.Resource)
		require.Equal(t, "mcp:read-write", got.Config.Scope)
	})
}