2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2018-09-19 13:54:49 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Text Module', type: :request do
|
|
|
|
|
2022-02-11 14:25:39 +00:00
|
|
|
before do
|
|
|
|
# XSS processing may run into a timeout on slow CI systems, so turn the timeout off for the test.
|
|
|
|
stub_const("#{HtmlSanitizer}::PROCESSING_TIMEOUT", nil)
|
|
|
|
end
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:admin) do
|
|
|
|
create(:admin)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:agent) do
|
|
|
|
create(:agent)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
2020-06-19 09:17:18 +00:00
|
|
|
let(:customer) do
|
|
|
|
create(:customer)
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'request handling' do
|
|
|
|
|
|
|
|
it 'does csv example - customer no access' do
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(customer)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/text_modules/import_example', as: :json
|
2021-02-04 08:28:41 +00:00
|
|
|
expect(response).to have_http_status(:forbidden)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response['error']).to eq('Not authorized (user)!')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does csv example - admin access' do
|
|
|
|
TextModule.load('en-en')
|
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin)
|
2018-09-19 13:54:49 +00:00
|
|
|
get '/api/v1/text_modules/import_example', as: :json
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
rows = CSV.parse(@response.body)
|
|
|
|
header = rows.shift
|
|
|
|
|
|
|
|
expect(header[0]).to eq('id')
|
|
|
|
expect(header[1]).to eq('name')
|
|
|
|
expect(header[2]).to eq('keywords')
|
|
|
|
expect(header[3]).to eq('content')
|
|
|
|
expect(header[4]).to eq('note')
|
|
|
|
expect(header[5]).to eq('active')
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(header).not_to include('organization')
|
|
|
|
expect(header).not_to include('priority')
|
|
|
|
expect(header).not_to include('state')
|
|
|
|
expect(header).not_to include('owner')
|
|
|
|
expect(header).not_to include('customer')
|
2018-09-19 13:54:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does csv import - admin access' do
|
|
|
|
|
|
|
|
# invalid file
|
2019-03-14 01:37:35 +00:00
|
|
|
csv_file = fixture_file_upload('csv_import/text_module/simple_col_not_existing.csv', 'text/csv')
|
2018-09-19 13:54:49 +00:00
|
|
|
|
2020-06-19 09:17:18 +00:00
|
|
|
authenticated_as(admin)
|
2018-09-19 13:54:49 +00:00
|
|
|
post '/api/v1/text_modules/import', params: { try: true, file: csv_file, col_sep: ';' }
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
|
|
|
|
expect(json_response['try']).to be_truthy
|
2019-10-21 10:44:52 +00:00
|
|
|
expect(json_response['records']).to be_empty
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response['result']).to eq('failed')
|
|
|
|
expect(json_response['errors'].count).to eq(2)
|
2018-11-06 05:42:52 +00:00
|
|
|
expect(json_response['errors'][0]).to eq("Line 1: Unable to create record - unknown attribute 'keywords2' for TextModule.")
|
|
|
|
expect(json_response['errors'][1]).to eq("Line 2: Unable to create record - unknown attribute 'keywords2' for TextModule.")
|
2018-09-19 13:54:49 +00:00
|
|
|
|
|
|
|
# valid file try
|
2019-03-14 01:37:35 +00:00
|
|
|
csv_file = fixture_file_upload('csv_import/text_module/simple.csv', 'text/csv')
|
2018-09-19 13:54:49 +00:00
|
|
|
post '/api/v1/text_modules/import?try=true', params: { file: csv_file, col_sep: ';' }
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
|
|
|
|
expect(json_response['try']).to be_truthy
|
|
|
|
expect(json_response['records'].count).to eq(2)
|
|
|
|
expect(json_response['result']).to eq('success')
|
|
|
|
|
|
|
|
expect(TextModule.find_by(name: 'some name1')).to be_nil
|
|
|
|
expect(TextModule.find_by(name: 'some name2')).to be_nil
|
|
|
|
|
|
|
|
# valid file
|
2019-03-14 01:37:35 +00:00
|
|
|
csv_file = fixture_file_upload('csv_import/text_module/simple.csv', 'text/csv')
|
2018-09-19 13:54:49 +00:00
|
|
|
post '/api/v1/text_modules/import', params: { file: csv_file, col_sep: ';' }
|
2019-04-15 01:41:17 +00:00
|
|
|
expect(response).to have_http_status(:ok)
|
2018-09-19 13:54:49 +00:00
|
|
|
expect(json_response).to be_a_kind_of(Hash)
|
|
|
|
|
|
|
|
expect(json_response['try']).to eq(false)
|
|
|
|
expect(json_response['records'].count).to eq(2)
|
|
|
|
expect(json_response['result']).to eq('success')
|
|
|
|
|
|
|
|
text_module1 = TextModule.find_by(name: 'some name1')
|
|
|
|
expect(text_module1).to be_truthy
|
|
|
|
expect(text_module1.name).to eq('some name1')
|
|
|
|
expect(text_module1.keywords).to eq('keyword1')
|
|
|
|
expect(text_module1.content).to eq('some<br>content1')
|
|
|
|
expect(text_module1.active).to be_truthy
|
|
|
|
text_module2 = TextModule.find_by(name: 'some name2')
|
|
|
|
expect(text_module2).to be_truthy
|
|
|
|
expect(text_module2.name).to eq('some name2')
|
|
|
|
expect(text_module2.keywords).to eq('keyword2')
|
|
|
|
expect(text_module2.content).to eq('some content<br>test123')
|
|
|
|
expect(text_module2.active).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|