text_module_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Text Module', type: :request do
  4. before do
  5. # XSS processing may run into a timeout on slow CI systems, so turn the timeout off for the test.
  6. stub_const("#{HtmlSanitizer}::PROCESSING_TIMEOUT", nil)
  7. end
  8. let(:admin) do
  9. create(:admin)
  10. end
  11. let(:agent) do
  12. create(:agent)
  13. end
  14. let(:customer) do
  15. create(:customer)
  16. end
  17. describe 'request handling' do
  18. it 'does csv example - customer no access' do
  19. authenticated_as(customer)
  20. get '/api/v1/text_modules/import_example', as: :json
  21. expect(response).to have_http_status(:forbidden)
  22. expect(json_response['error']).to eq('Not authorized (user)!')
  23. end
  24. it 'does csv example - admin access' do
  25. TextModule.load('en-en')
  26. authenticated_as(admin)
  27. get '/api/v1/text_modules/import_example', as: :json
  28. expect(response).to have_http_status(:ok)
  29. rows = CSV.parse(response.body)
  30. header = rows.shift
  31. expect(header[0]).to eq('id')
  32. expect(header[1]).to eq('name')
  33. expect(header[2]).to eq('keywords')
  34. expect(header[3]).to eq('content')
  35. expect(header[4]).to eq('note')
  36. expect(header[5]).to eq('active')
  37. expect(header).not_to include('organization')
  38. expect(header).not_to include('priority')
  39. expect(header).not_to include('state')
  40. expect(header).not_to include('owner')
  41. expect(header).not_to include('customer')
  42. end
  43. it 'does csv import - admin access' do
  44. # invalid file
  45. csv_file = fixture_file_upload('csv_import/text_module/simple_col_not_existing.csv', 'text/csv')
  46. authenticated_as(admin)
  47. post '/api/v1/text_modules/import', params: { try: true, file: csv_file, col_sep: ';' }
  48. expect(response).to have_http_status(:ok)
  49. expect(json_response).to be_a(Hash)
  50. expect(json_response['try']).to be_truthy
  51. expect(json_response['records']).to be_empty
  52. expect(json_response['result']).to eq('failed')
  53. expect(json_response['errors'].count).to eq(2)
  54. expect(json_response['errors'][0]).to eq("Line 1: Unable to create record - unknown attribute 'keywords2' for TextModule.")
  55. expect(json_response['errors'][1]).to eq("Line 2: Unable to create record - unknown attribute 'keywords2' for TextModule.")
  56. # valid file try
  57. csv_file = fixture_file_upload('csv_import/text_module/simple.csv', 'text/csv')
  58. post '/api/v1/text_modules/import?try=true', params: { file: csv_file, col_sep: ';' }
  59. expect(response).to have_http_status(:ok)
  60. expect(json_response).to be_a(Hash)
  61. expect(json_response['try']).to be_truthy
  62. expect(json_response['records'].count).to eq(2)
  63. expect(json_response['result']).to eq('success')
  64. expect(TextModule.find_by(name: 'some name1')).to be_nil
  65. expect(TextModule.find_by(name: 'some name2')).to be_nil
  66. # valid file
  67. csv_file = fixture_file_upload('csv_import/text_module/simple.csv', 'text/csv')
  68. post '/api/v1/text_modules/import', params: { file: csv_file, col_sep: ';' }
  69. expect(response).to have_http_status(:ok)
  70. expect(json_response).to be_a(Hash)
  71. expect(json_response['try']).to be(false)
  72. expect(json_response['records'].count).to eq(2)
  73. expect(json_response['result']).to eq('success')
  74. text_module1 = TextModule.find_by(name: 'some name1')
  75. expect(text_module1).to be_truthy
  76. expect(text_module1.name).to eq('some name1')
  77. expect(text_module1.keywords).to eq('keyword1')
  78. expect(text_module1.content).to eq('some<br>content1')
  79. expect(text_module1.active).to be_truthy
  80. text_module2 = TextModule.find_by(name: 'some name2')
  81. expect(text_module2).to be_truthy
  82. expect(text_module2.name).to eq('some name2')
  83. expect(text_module2.keywords).to eq('keyword2')
  84. expect(text_module2.content).to eq('some content<br>test123')
  85. expect(text_module2.active).to be_truthy
  86. end
  87. end
  88. end