Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion server/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ApplicationController < ActionController::API
include Pundit::Authorization
before_action :authenticate_user!
before_action :validate_contract
before_action :ensure_eula_accepted
around_action :handle_with_exception
after_action :verify_authorized

Expand All @@ -27,7 +28,10 @@ def authenticate_user!
def current_workspace
workspace_id = request.headers["Workspace-Id"]
@current_workspace = current_user.workspaces.find_by(id: workspace_id)
@current_workspace || raise(StandardError, "Workspace not found")
@current_workspace || raise(
StandardError,
"Workspace not found: workspace_id=#{workspace_id.inspect}, user_id=#{current_user&.id}, path=#{request.path}"
)
end

def current_organization
Expand Down Expand Up @@ -67,6 +71,23 @@ def render_error(message:, status:, details: nil)
def event_logger
metadata = {}
metadata[:connector_name] = @connector.connector_name if @connector.present?
<<<<<<< HEAD
_track_event("#{params[:controller]}##{params[:action]}", {}.merge(metadata))
=======
# _track_event("#{params[:controller]}##{params[:action]}", {}.merge(metadata))
end

def eula_required?
current_organization&.eulas&.enabled&.exists?
rescue StandardError
false
end

def render_eula_error
render_error(
message: "You must accept the EULA before accessing the API.",
status: :forbidden
)
>>>>>>> 29bfd20fe (chore(CE): add detail error in workspace (#1792))
end
end
52 changes: 48 additions & 4 deletions server/spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,58 @@
request.headers["Workspace-Id"] = "invalid_id"
end

it 'raises a StandardError with message "Workspace not found"' do
expect { controller.send(:current_workspace) }.to raise_error(StandardError, "Workspace not found")
it 'raises a StandardError with message containing "Workspace not found"' do
expect { controller.send(:current_workspace) }.to raise_error(StandardError, /Workspace not found/)
end
end

context "when Workspace-Id header is not present" do
it 'raises a StandardError with message "Workspace not found"' do
expect { controller.send(:current_workspace) }.to raise_error(StandardError, "Workspace not found")
it 'raises a StandardError with message containing "Workspace not found"' do
expect { controller.send(:current_workspace) }.to raise_error(StandardError, /Workspace not found/)
end
end

context "when Workspace-Id header is present but invalid" do
before do
request.headers["Workspace-Id"] = "invalid_id"
end

it "includes workspace_id, user_id, and path in the error message" do
expect { controller.send(:current_workspace) }.to raise_error(StandardError) do |error|
expect(error.message).to include("workspace_id=")
expect(error.message).to include("user_id=#{user.id}")
expect(error.message).to include("path=")
end
end
end
end

describe "#eula_required?" do
context "when current_workspace raises an error" do
it "returns false" do
allow(controller).to receive(:current_workspace).and_raise(StandardError, "Workspace not found")
expect(controller.send(:eula_required?)).to be false
end
end

context "when organization has no eulas" do
before do
request.headers["Workspace-Id"] = workspace.id
allow(workspace.organization).to receive_message_chain(:eulas, :enabled, :exists?).and_return(false)
allow(controller).to receive(:current_organization).and_return(workspace.organization)
end

it "returns false" do
expect(controller.send(:eula_required?)).to be false
end
end
end

describe "#ensure_eula_accepted" do
context "when workspace is not found" do
it "does not raise and allows the request to continue" do
allow(controller).to receive(:current_workspace).and_raise(StandardError, "Workspace not found")
expect { controller.send(:ensure_eula_accepted) }.not_to raise_error
end
end
end
Expand Down
Loading