From a7a929ddbc0c1d1d45b7ded6d03181044e901a4a Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 1 Oct 2019 14:32:30 +0200 Subject: [PATCH] integration unit test for imports + fixed import result colors in UI + fixed test fixtures --- app/assets/stylesheets/app.colors.scss | 1 + .../admin/members/import_result.html | 4 +- app/services/members/import_service.rb | 2 +- db/schema.rb | 2 +- test/fixtures/files/members.csv | 6 ++ test/fixtures/imports.yml | 95 ++++++++++++++++++- test/integration/members/import_test.rb | 71 ++++++++++++++ test/integration/wallets_test.rb | 12 +-- test/test_helper.rb | 8 +- 9 files changed, 185 insertions(+), 16 deletions(-) create mode 100644 test/fixtures/files/members.csv create mode 100644 test/integration/members/import_test.rb diff --git a/app/assets/stylesheets/app.colors.scss b/app/assets/stylesheets/app.colors.scss index 7637f3c10..c7a66e439 100644 --- a/app/assets/stylesheets/app.colors.scss +++ b/app/assets/stylesheets/app.colors.scss @@ -37,6 +37,7 @@ .text-blue { color: $blue; } .text-muted { color: $text-muted; } .text-danger, .red { color: $red !important; } +.text-red-only { color: $red !important; } .text-purple { color: $violet !important; } .text-japonica { color: $japonica !important; } .text-beige { color: $beige !important; } diff --git a/app/assets/templates/admin/members/import_result.html b/app/assets/templates/admin/members/import_result.html index 55fa8d6cb..16e7ae9e3 100644 --- a/app/assets/templates/admin/members/import_result.html +++ b/app/assets/templates/admin/members/import_result.html @@ -46,7 +46,7 @@ {{ 'members_import_result.success' }} - + @@ -54,7 +54,7 @@ {{ 'members_import_result.failed' }} -
+
{{ 'members_import_result.error_details' }}{{resultRow}}
diff --git a/app/services/members/import_service.rb b/app/services/members/import_service.rb index ed1fca779..cc0441a53 100644 --- a/app/services/members/import_service.rb +++ b/app/services/members/import_service.rb @@ -22,7 +22,7 @@ class Members::ImportService user = User.new(params) service = Members::MembersService.new(user) res = service.create(import.user, params) - log << { user: nil, status: 'create', result: res } + log << { user: user.id, status: 'create', result: res } end log << user.errors.to_hash unless user.errors.to_hash.empty? rescue StandardError => e diff --git a/db/schema.rb b/db/schema.rb index 2c91f1ca4..3a3ac9fb4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -15,8 +15,8 @@ ActiveRecord::Schema.define(version: 20190924140726) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - enable_extension "pg_trgm" enable_extension "unaccent" + enable_extension "pg_trgm" create_table "abuses", force: :cascade do |t| t.integer "signaled_id" diff --git a/test/fixtures/files/members.csv b/test/fixtures/files/members.csv new file mode 100644 index 000000000..da887eb74 --- /dev/null +++ b/test/fixtures/files/members.csv @@ -0,0 +1,6 @@ +id;gender;first_name;last_name;username;email;password;birthdate;address;phone;group;tags;trainings;website;job;interests;softwares;allow_contact;allow_newsletter;organization_name;organization_address;facebook;twitter;googleplus;viadeo;linkedin;instagram;youtube;vimeo;dailymotion;github;echosciences;pinterest;lastfm;flickr +;male;victor;hugo;vhugo;victor.hugo@wanadoo.fr;;1802-02-26;140 Grande Rue - 25000 Besançon;0381614214;standard;1;1,2,5;http://www.victor-hugo.info;Poète;Hacking, DIY;SolidWorks, Inkspace;yes;yes;;;http://www.facebook.com/victor-hugo;;;;;;;;;http://github.com/vhugo;;;; +;female;louise;michel;lmichel;louise.michel@gresille.org;;1830-03-29;8 rue de l'église - 52240 Vroncourt-la-Côte;0324491826;standard;2;5;https://rebellyon.info;Institutrice;militantisme;AutoCAD, LibreCAD;yes;yes;;;;;;;;;;;;http://github.com/louisemichel;https://www.echosciences-grenoble.fr/membres/louise-michel;;; +;male;ambroise;croizat;acroizat;acroizat@msn.com;;1801-01-28;cité des Maisonnettes - 73260 Notre-Dame-de-Briançon;0473147852;standard;;5;;Ouvrier;métallurgie;;no;no;;;;;;;;;https://www.youtube.com/user/croizat;;;;;;; +;female;rirette;maîtrejean;;;;1887-08-14;19330 Saint-Mexant;0555124789;standard;;;;;;;;yes;no +2;;;;;jean.dupond@yahoo.fr diff --git a/test/fixtures/imports.yml b/test/fixtures/imports.yml index 70de5c697..1e358f500 100644 --- a/test/fixtures/imports.yml +++ b/test/fixtures/imports.yml @@ -1,9 +1,102 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: - author_id: 1 + user_id: 1 attachment: 'users.csv' update_field: 'id' + category: 'members' + results: | + --- + - :row: + id: + gender: male + first_name: jean + last_name: dupont + username: jdupont + email: jean.dupont@gmail.com + password: + birthdate: '1970-01-01' + address: 12 bvd Libération - 75000 Paris + phone: '0123456789' + group: standard + tags: '1,2' + trainings: '1' + website: http://www.example.com + job: Charpentier + interests: Ping-pong + softwares: AutoCAD + allow_contact: 'yes' + allow_newsletter: 'no' + organization_name: + organization_address: + facebook: http://www.facebook.com/jdupont + twitter: + googleplus: + viadeo: + linkedin: + instagram: + youtube: + vimeo: + dailymotion: + github: http://github.com/example + echosciences: + pinterest: + lastfm: + flickr: + - :user: + :status: create + :result: false + - :username: + - n'est pas disponible + - :row: + id: '43' + gender: + first_name: + last_name: + username: + email: + password: "********" + birthdate: + address: + phone: + group: + tags: + trainings: + website: + job: + interests: + softwares: + allow_contact: + allow_newsletter: + organization_name: + organization_address: + facebook: + twitter: + googleplus: + viadeo: + linkedin: + instagram: + youtube: + vimeo: + dailymotion: + github: + echosciences: + pinterest: + lastfm: + flickr: + - :user: + :status: create + :result: false + - :email: + - doit être rempli(e) + :profile.first_name: + - doit être rempli(e) + :profile.last_name: + - doit être rempli(e) + :profile.phone: + - n'est pas un nombre + :username: + - doit être rempli(e) created_at: 2019-09-24 15:06:22.151882000 Z updated_at: 2019-09-24 15:06:22.151882000 Z diff --git a/test/integration/members/import_test.rb b/test/integration/members/import_test.rb new file mode 100644 index 000000000..76c7d6855 --- /dev/null +++ b/test/integration/members/import_test.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +class ImportTest < ActionDispatch::IntegrationTest + # Called before every test method runs. Can be used + # to set up fixture information. + def setup + @admin = User.find_by(username: 'admin') + login_as(@admin, scope: :user) + end + + test 'bulk import members through CSV' do + + bulk_csv = fixture_file_upload('files/members.csv', 'text/csv') + post '/api/imports/members', + { + import_members: bulk_csv, + update_field: 'id' + }, default_headers + + assert_equal 201, response.status, response.body + assert_equal Mime::JSON, response.content_type + + # Check that the returned import was created + import = json_response(response.body) + assert_not_nil import[:id], 'no returned id for the import' + db_import = Import.find(import[:id]) + assert_not_nil db_import + + # run the import synchronously + worker = MembersImportWorker.new + worker.perform(import[:id]) + + # check the results were written + db_import.reload + assert_not_empty db_import.results, 'import results were not written' + res = db_import.results_hash + assert_not_nil res[0][:row], 'import result does not contains the imported data row' + assert_not_nil res[1][:status], 'import result does not contains the operation applied to the row (create/update)' + assert_not_nil res[1][:result], 'import result does not contains the result of the operation' + + # check the results match the expectations + assert_not_nil res[1][:user], 'wrong user: victor hugo is expected to have been created in database' + assert_equal 'create', res[1][:status], 'wrong operation: victor hugo should have been created' + assert res[1][:result], 'wrong result: operation should have succeeded' + assert_equal 1, User.where(id: res[1][:user]).count, 'victor hugo was not found in database' + + assert_not_nil res[3][:user], 'wrong user: louise michel is expected to have been created in database' + assert_equal 'create', res[3][:status], 'wrong operation: louise michel should have been created' + assert res[3][:result], 'wrong result: operation should have succeeded' + assert_equal 1, User.where(id: res[3][:user]).count, 'louise michel was not found in database' + + assert_not_nil res[5][:user], 'wrong user: ambroise croizat is expected to have been created in database' + assert_equal 'create', res[5][:status], 'wrong operation: ambroise croizat should have been created' + assert res[5][:result], 'wrong result: operation should have succeeded' + assert_equal 1, User.where(id: res[5][:user]).count, 'ambroise croizat was not found in database' + + assert_nil res[7][:user], 'wrong user: rirette maitrejean is not expected to have been created in database' + assert_equal 'create', res[7][:status], 'wrong operation: rirette maitrejean should have been created' + assert_not res[7][:result], 'wrong result: operation should have failed' + assert_equal 0, Profile.where(last_name: res[6][:row]['last_name']).count, 'rirette maitrejean was found in database' + + assert_match /can't be blank/, res[8][:email].to_json + assert_match /can't be blank/, res[8][:username].to_json + + assert_not_nil res[10][:user], 'wrong user: jean dupont is expected to exists in database' + assert_equal 'update', res[10][:status], 'wrong operation: jean dupont should have been updated' + assert res[10][:result], 'wrong result: operation should have succeeded' + assert_equal res[9][:row]['email'], User.find(res[10][:user]).email, 'jean dupont email was not updated' + + end +end diff --git a/test/integration/wallets_test.rb b/test/integration/wallets_test.rb index 7e0e3791b..06ca437c9 100644 --- a/test/integration/wallets_test.rb +++ b/test/integration/wallets_test.rb @@ -64,9 +64,7 @@ class WalletsTest < ActionDispatch::IntegrationTest amount = 10.5 expected_amount = w.amount + amount put "/api/wallet/#{w.id}/credit", - { amount: amount - } assert_equal 200, response.status assert_equal Mime::JSON, response.content_type @@ -88,12 +86,10 @@ class WalletsTest < ActionDispatch::IntegrationTest avoir_date = Time.now.end_of_day expected_amount = w.amount + amount put "/api/wallet/#{w.id}/credit", - { - amount: amount, - avoir: true, - avoir_date: avoir_date, - avoir_description: 'Some text' - } + amount: amount, + avoir: true, + avoir_date: avoir_date, + avoir_description: 'Some text' assert_equal 200, response.status assert_equal Mime::JSON, response.content_type diff --git a/test/test_helper.rb b/test/test_helper.rb index 8204e3c44..6081adc5e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,6 +10,8 @@ require 'vcr' require 'sidekiq/testing' require 'minitest/reporters' +include ActionDispatch::TestProcess + VCR.configure do |config| config.cassette_library_dir = 'test/vcr_cassettes' config.hook_into :webmock @@ -92,9 +94,9 @@ class ActiveSupport::TestCase ht_amount = parse_amount_from_invoice_line(line) if line.include? I18n.t('invoices.including_total_excluding_taxes') end - if Setting.find_by(name: 'invoice_VAT-active').value == 'true' - vat_service = VatHistoryService.new - vat_rate = vat_service.invoice_vat(invoice) + vat_service = VatHistoryService.new + vat_rate = vat_service.invoice_vat(invoice) + if vat_rate.positive? computed_ht = sprintf('%.2f', (invoice.total / (vat_rate / 100.00 + 1)) / 100.00).to_f assert_equal computed_ht, ht_amount, 'Total excluding taxes rendered in the PDF file is not computed correctly'