text_module_spec.rb 3.8 KB

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