diff --git a/app/models/synchronization/adapter.rb b/app/models/synchronization/adapter.rb index 8111766687..8df1739872 100644 --- a/app/models/synchronization/adapter.rb +++ b/app/models/synchronization/adapter.rb @@ -80,7 +80,7 @@ module CartoDB end def fix_oid(table_name) - user_table = user.tables.where(name: table_name).first + user_table = Carto::UserTable.find(user.tables.where(name: table_name).first.id) user_table.sync_table_id user_table.save @@ -114,7 +114,7 @@ module CartoDB end def setup_table(table_name, geo_type) - table = user.tables.where(name: table_name).first.service + table = Carto::UserTable.find(user.tables.where(name: table_name).first.id).service table.force_schema = true diff --git a/db/fake_data/guess_country.csv b/db/fake_data/guess_country.csv new file mode 100644 index 0000000000..ab213acebc --- /dev/null +++ b/db/fake_data/guess_country.csv @@ -0,0 +1,11 @@ +Country,Amount +Spain,1 +France,2 +China,3 +Japan,4 +UK,5 +Holland,8 +Korea,6 +The Netherlands,7 +Portugal,9 +Brazil,10 diff --git a/services/importer/spec/unit/downloader_spec.rb b/services/importer/spec/unit/downloader_spec.rb index 5b4b0ada94..dac8fcd45f 100644 --- a/services/importer/spec/unit/downloader_spec.rb +++ b/services/importer/spec/unit/downloader_spec.rb @@ -382,37 +382,6 @@ describe Downloader do end end - def stub_download(url:, filepath:, headers: {}, content_disposition: true) - Typhoeus.stub(url).and_return(response_for(filepath, headers, content_disposition: content_disposition)) - end - - def stub_failed_download(options) - url = options.fetch(:url) - filepath = options.fetch(:filepath) - headers = options.fetch(:headers, {}) - - Typhoeus.stub(url).and_return(failed_response_for(filepath, headers)) - end - - def response_for(filepath, headers = {}, content_disposition: true) - response = Typhoeus::Response.new( - code: 200, - body: File.new(filepath).read.to_s, - headers: headers.merge(headers_for(filepath, content_disposition: content_disposition)) - ) - response - end - - def failed_response_for(filepath, headers={}) - Typhoeus::Response.new(code: 404, body: nil, headers: {}) - end - - def headers_for(filepath, content_disposition: true) - return {} unless content_disposition - filename = filepath.split('/').last - { "Content-Disposition" => "attachment; filename=#{filename}" } - end - def path_to(filename) File.join(File.dirname(__FILE__), '..', 'fixtures', filename) end diff --git a/spec/helpers/file_server_helper.rb b/spec/helpers/file_server_helper.rb index da16fa8a38..67e744067f 100644 --- a/spec/helpers/file_server_helper.rb +++ b/spec/helpers/file_server_helper.rb @@ -42,4 +42,35 @@ module FileServerHelper raise "No ports available on machine." end + + def stub_download(url:, filepath:, headers: {}, content_disposition: true) + Typhoeus.stub(url).and_return(response_for(filepath, headers, content_disposition: content_disposition)) + end + + def stub_failed_download(options) + url = options.fetch(:url) + filepath = options.fetch(:filepath) + headers = options.fetch(:headers, {}) + + Typhoeus.stub(url).and_return(failed_response_for(filepath, headers)) + end + + def response_for(filepath, headers = {}, content_disposition: true) + response = Typhoeus::Response.new( + code: 200, + body: File.new(filepath).read.to_s, + headers: headers.merge(headers_for(filepath, content_disposition: content_disposition)) + ) + response + end + + def failed_response_for(filepath, headers={}) + Typhoeus::Response.new(code: 404, body: nil, headers: {}) + end + + def headers_for(filepath, content_disposition: true) + return {} unless content_disposition + filename = filepath.split('/').last + { "Content-Disposition" => "attachment; filename=#{filename}" } + end end diff --git a/spec/models/synchronization/member_spec.rb b/spec/models/synchronization/member_spec.rb index e5b94ebfd8..25b317ac64 100644 --- a/spec/models/synchronization/member_spec.rb +++ b/spec/models/synchronization/member_spec.rb @@ -6,6 +6,7 @@ require_relative '../../../services/data-repository/backend/sequel' require_relative '../../../services/data-repository/repository' require_relative '../../../app/models/synchronization/member' require 'helpers/unique_names_helper' +require 'helpers/file_server_helper' include UniqueNamesHelper include CartoDB @@ -51,34 +52,67 @@ describe Synchronization::Member do end end - describe "External sources" do + describe "synchronizations" do + before(:all) do + @user_1 = create_user(sync_tables_enabled: true) + @user_2 = create_user(sync_tables_enabled: true) + end + before(:each) do - @user_1 = FactoryGirl.create(:valid_user) - @user_2 = FactoryGirl.create(:valid_user) + bypass_named_maps + Cartodb.config[:metrics] = {} end - after(:each) do + after(:all) do @user_1.destroy @user_2.destroy end - it "Authorizes to sync always if from an external source" do - member = Synchronization::Member.new(random_attributes({user_id: @user_1.id})).store - member.fetch + describe 'external sources' do + it "Authorizes to sync always if from an external source" do + member = Synchronization::Member.new(random_attributes({user_id: @user_1.id})).store + member.fetch + + member.expects(:from_external_source?) + .returns(true) + + @user_1.sync_tables_enabled = true + @user_2.sync_tables_enabled = true + + member.authorize?(@user_1).should eq true + member.authorize?(@user_2).should eq false + + @user_1.sync_tables_enabled = false + @user_2.sync_tables_enabled = false + + member.authorize?(@user_1).should eq true + end + end + + describe "synchronization" do + it 'syncs' do + # TODO: this is the minimum test valid to reproduce #11889, it's not a complete sync test + CartoDB::Logger.stubs(:error).never - member.expects(:from_external_source?) - .returns(true) + url = 'https://wadus.com/guess_country.csv' - @user_1.sync_tables_enabled = true - @user_2.sync_tables_enabled = true + path = fake_data_path('guess_country.csv') + stub_download(url: url, filepath: path, content_disposition: false) - member.authorize?(@user_1).should eq true - member.authorize?(@user_2).should eq false + attrs = random_attributes(user_id: @user_1.id).merge(service_item_id: url, url: url, name: 'guess_country') + member = Synchronization::Member.new(attrs).store - @user_1.sync_tables_enabled = false - @user_2.sync_tables_enabled = false + DataImport.create( + user_id: @user_1.id, + data_source: fake_data_path('guess_country.csv'), + synchronization_id: member.id, + service_name: 'public_url', + service_item_id: url, + updated_at: Time.now + ).run_import! - member.authorize?(@user_1).should eq true + member.run + end end end @@ -87,7 +121,7 @@ describe Synchronization::Member do def random_attributes(attributes={}) random = unique_integer { - name: attributes.fetch(:name, "name #{random}"), + name: attributes.fetch(:name, "name#{random}"), interval: attributes.fetch(:interval, 15 * 60 + random), state: attributes.fetch(:state, 'enabled'), user_id: attributes.fetch(:user_id, nil) diff --git a/spec/support/data/guess_country.csv b/spec/support/data/guess_country.csv new file mode 100644 index 0000000000..ab213acebc --- /dev/null +++ b/spec/support/data/guess_country.csv @@ -0,0 +1,11 @@ +Country,Amount +Spain,1 +France,2 +China,3 +Japan,4 +UK,5 +Holland,8 +Korea,6 +The Netherlands,7 +Portugal,9 +Brazil,10 diff --git a/spec/support/factory_girl.rb b/spec/support/factory_girl.rb index aa22e62c72..b1b128c862 100644 --- a/spec/support/factory_girl.rb +++ b/spec/support/factory_girl.rb @@ -1,6 +1 @@ # This makes factory_girl compatible with Sequel -class Sequel::Model - def save! - save(:validate=>false) - end -end