text_module_spec.rb 3.7 KB

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