diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index e717303a5..c5aacaf8b 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -30,6 +30,7 @@ user_2: uid: auth_token: merged_at: + is_allow_newsletter: true user_4: id: 4 @@ -62,6 +63,7 @@ user_4: uid: auth_token: merged_at: + is_allow_newsletter: false user_6: id: 6 @@ -94,6 +96,7 @@ user_6: uid: auth_token: merged_at: + is_allow_newsletter: true user_5: id: 5 @@ -126,6 +129,7 @@ user_5: uid: auth_token: merged_at: + is_allow_newsletter: true user_3: id: 3 @@ -158,6 +162,7 @@ user_3: uid: auth_token: merged_at: + is_allow_newsletter: false user_1: id: 1 @@ -190,6 +195,7 @@ user_1: uid: auth_token: merged_at: + is_allow_newsletter: true user_7: @@ -223,3 +229,4 @@ user_7: uid: auth_token: merged_at: + is_allow_newsletter: false diff --git a/test/integration/exports/members_export_test.rb b/test/integration/exports/members_export_test.rb new file mode 100644 index 000000000..51f359bb6 --- /dev/null +++ b/test/integration/exports/members_export_test.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'test_helper' +require 'rubyXL' + +module Exports; end + +class Exports::MembersExportTest < ActionDispatch::IntegrationTest + setup do + admin = User.with_role(:admin).first + login_as(admin, scope: :user) + end + + test 'export members to Excel' do + # First, we create a new export + get '/api/members/export_members.xlsx' + + # Check response format & status + assert_equal 200, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the export was created correctly + res = json_response(response.body) + e = Export.where(id: res[:export_id]).first + assert_not_nil e, 'Export was not created in database' + + # Run the worker + worker = UsersExportWorker.new + worker.perform(e.id) + + # notification + assert_not_empty Notification.where(attached_object: e) + + # resulting XLSX file + assert FileTest.exist?(e.file), 'XLSX file was not generated' + workbook = RubyXL::Parser.parse(e.file) + + # test worksheet + assert_not_nil workbook[I18n.t('export_members.members')] + + # test data + member = User.members.first + wb = workbook[I18n.t('export_members.members')] + assert_equal member.id, wb.sheet_data[1][0].value + assert_equal (member.is_allow_newsletter ? 1 : nil), wb.sheet_data[1][4].value + + # Clean XLSX file + require 'fileutils' + FileUtils.rm(e.file) + end +end \ No newline at end of file