Skip to content

Commit

Permalink
fix(CE): enable catalog validation only for ai models (#355)
Browse files Browse the repository at this point in the history
* Resolve conflict in cherry-pick of 6de5e956cb5cd7d54544cfb096802d8e00e15b5f and change the commit message

* fix(CE): resolve conflicts

* fix(CE): remove duplicate

* fix(CE): fix spec

---------

Co-authored-by: datafloyd <[email protected]>
Co-authored-by: afthab vp <[email protected]>
  • Loading branch information
3 people authored Sep 4, 2024
1 parent d11a780 commit fb86473
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
3 changes: 2 additions & 1 deletion server/app/controllers/api/v1/connectors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ConnectorsController < ApplicationController
# TODO: Enable this once we have query validation implemented for all the connectors
# before_action :validate_query, only: %i[query_source]
# TODO: Enable this for ai_ml sources
# before_action :validate_catalog, only: %i[query_source]
before_action :validate_catalog, only: %i[query_source]
after_action :event_logger

def index
Expand Down Expand Up @@ -126,6 +126,7 @@ def set_connector
end

def validate_catalog
return unless @connector.ai_model?
return if @connector.catalog.present?

render_error(
Expand Down
4 changes: 4 additions & 0 deletions server/app/models/connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,8 @@ def set_category
rescue StandardError => e
Rails.logger.error("Failed to set category for connector ##{id}: #{e.message}")
end

def ai_model?
connector_category == "AI Model"
end
end
5 changes: 5 additions & 0 deletions server/spec/models/connector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@
expect(result).to include(ai_ml_connector)
expect(result).not_to include(non_ai_ml_connector)
end

it "check whether connector is ai model or not" do
expect(ai_ml_connector.ai_model?).to eq(true)
expect(non_ai_ml_connector.ai_model?).to eq(false)
end
end

describe ".data" do
Expand Down
46 changes: 33 additions & 13 deletions server/spec/requests/api/v1/connectors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,19 +481,39 @@
expect(response_hash[:data]).to eq([record1.record.data, record2.record.data])
end

# it "returns an error message for missing catalog" do
# catalog = connector.catalog
# catalog.connector_id = connectors.second.id
# catalog.save

# allow(Connectors::QuerySource).to receive(:call).and_return(double(:context, success?: true,
# records: [record1, record2]))
# post "/api/v1/connectors/#{connector.id}/query_source", params: request_body.to_json, headers:
# { "Content-Type": "application/json" }.merge(auth_headers(user, workspace.id))
# expect(response).to have_http_status(:unprocessable_entity)
# response_hash = JSON.parse(response.body).with_indifferent_access
# expect(response_hash.dig(:errors, 0, :detail)).to eq("Catalog is not present for the connector")
# end
it "returns an error message for missing catalog for ai connectors" do
catalog = connector.catalog
catalog.connector_id = connectors.second.id
catalog.save
# rubocop:disable Rails/SkipsModelValidations
connector.update_column(:connector_category, "AI Model")
# rubocop:enable Rails/SkipsModelValidations

allow(Connectors::QuerySource).to receive(:call).and_return(double(:context, success?: true,
records: [record1, record2]))
post "/api/v1/connectors/#{connector.id}/query_source", params: request_body.to_json, headers:
{ "Content-Type": "application/json" }.merge(auth_headers(user, workspace.id))
expect(response).to have_http_status(:unprocessable_entity)
response_hash = JSON.parse(response.body).with_indifferent_access
expect(response_hash.dig(:errors, 0, :detail)).to eq("Catalog is not present for the connector")
end

it "should not return error message for missing catalog for data connectors" do
catalog = connector.catalog
catalog.connector_id = connectors.second.id
# rubocop:disable Rails/SkipsModelValidations
connector.update_column(:connector_category, "Data Warehouse")
# rubocop:enable Rails/SkipsModelValidations
catalog.save

allow(Connectors::QuerySource).to receive(:call).and_return(double(:context, success?: true,
records: [record1, record2]))
post "/api/v1/connectors/#{connector.id}/query_source", params: request_body.to_json, headers:
{ "Content-Type": "application/json" }.merge(auth_headers(user, workspace.id))
expect(response).to have_http_status(:ok)
response_hash = JSON.parse(response.body).with_indifferent_access
expect(response_hash[:data]).to eq([record1.record.data, record2.record.data])
end

it "returns success status for a valid query for viewer role" do
workspace.workspace_users.first.update(role: viewer_role)
Expand Down

0 comments on commit fb86473

Please sign in to comment.