text_module_controller_test.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. require 'test_helper'
  2. require 'rake'
  3. class TextModuleControllerTest < ActionDispatch::IntegrationTest
  4. setup do
  5. # set accept header
  6. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  7. # create agent
  8. roles = Role.where(name: %w[Admin Agent])
  9. groups = Group.all
  10. UserInfo.current_user_id = 1
  11. @admin = User.create!(
  12. login: 'rest-admin',
  13. firstname: 'Rest',
  14. lastname: 'Agent',
  15. email: 'rest-admin@example.com',
  16. password: 'adminpw',
  17. active: true,
  18. roles: roles,
  19. groups: groups,
  20. )
  21. # create agent
  22. roles = Role.where(name: 'Agent')
  23. @agent = User.create!(
  24. login: 'rest-agent@example.com',
  25. firstname: 'Rest',
  26. lastname: 'Agent',
  27. email: 'rest-agent@example.com',
  28. password: 'agentpw',
  29. active: true,
  30. roles: roles,
  31. groups: groups,
  32. )
  33. # create customer without org
  34. roles = Role.where(name: 'Customer')
  35. @customer_without_org = User.create!(
  36. login: 'rest-customer1@example.com',
  37. firstname: 'Rest',
  38. lastname: 'Customer1',
  39. email: 'rest-customer1@example.com',
  40. password: 'customer1pw',
  41. active: true,
  42. roles: roles,
  43. )
  44. # create customer
  45. @customer_with_org = User.create!(
  46. login: 'rest-customer2@example.com',
  47. firstname: 'Rest',
  48. lastname: 'Customer2',
  49. email: 'rest-customer2@example.com',
  50. password: 'customer2pw',
  51. active: true,
  52. roles: roles,
  53. )
  54. UserInfo.current_user_id = nil
  55. end
  56. test '05.01 csv example - customer no access' do
  57. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-customer1@example.com', 'customer1pw')
  58. get '/api/v1/text_modules/import_example', params: {}, headers: @headers.merge('Authorization' => credentials)
  59. assert_response(401)
  60. result = JSON.parse(@response.body)
  61. assert_equal('Not authorized (user)!', result['error'])
  62. end
  63. test '05.02 csv example - admin access' do
  64. TextModule.load('en-en')
  65. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  66. get '/api/v1/text_modules/import_example', params: {}, headers: @headers.merge('Authorization' => credentials)
  67. assert_response(200)
  68. rows = CSV.parse(@response.body)
  69. header = rows.shift
  70. assert_equal('id', header[0])
  71. assert_equal('name', header[1])
  72. assert_equal('keywords', header[2])
  73. assert_equal('content', header[3])
  74. assert_equal('note', header[4])
  75. assert_equal('active', header[5])
  76. assert_not(header.include?('organization'))
  77. assert_not(header.include?('priority'))
  78. assert_not(header.include?('state'))
  79. assert_not(header.include?('owner'))
  80. assert_not(header.include?('customer'))
  81. end
  82. test '05.03 csv import - admin access' do
  83. credentials = ActionController::HttpAuthentication::Basic.encode_credentials('rest-admin@example.com', 'adminpw')
  84. # invalid file
  85. csv_file_path = Rails.root.join('test', 'data', 'csv', 'text_module_simple_col_not_existing.csv')
  86. csv_file = ::Rack::Test::UploadedFile.new(csv_file_path, 'text/csv')
  87. post '/api/v1/text_modules/import?try=true', params: { file: csv_file, col_sep: ';' }, headers: { 'Authorization' => credentials }
  88. assert_response(200)
  89. result = JSON.parse(@response.body)
  90. assert_equal(Hash, result.class)
  91. assert_equal(true, result['try'])
  92. assert_equal(2, result['records'].count)
  93. assert_equal('failed', result['result'])
  94. assert_equal(2, result['errors'].count)
  95. assert_equal("Line 1: unknown attribute 'keywords2' for TextModule.", result['errors'][0])
  96. assert_equal("Line 2: unknown attribute 'keywords2' for TextModule.", result['errors'][1])
  97. # valid file try
  98. csv_file_path = Rails.root.join('test', 'data', 'csv', 'text_module_simple.csv')
  99. csv_file = ::Rack::Test::UploadedFile.new(csv_file_path, 'text/csv')
  100. post '/api/v1/text_modules/import?try=true', params: { file: csv_file, col_sep: ';' }, headers: { 'Authorization' => credentials }
  101. assert_response(200)
  102. result = JSON.parse(@response.body)
  103. assert_equal(Hash, result.class)
  104. assert_equal(true, result['try'])
  105. assert_equal(2, result['records'].count)
  106. assert_equal('success', result['result'])
  107. assert_nil(TextModule.find_by(name: 'some name1'))
  108. assert_nil(TextModule.find_by(name: 'some name2'))
  109. # valid file
  110. csv_file_path = Rails.root.join('test', 'data', 'csv', 'text_module_simple.csv')
  111. csv_file = ::Rack::Test::UploadedFile.new(csv_file_path, 'text/csv')
  112. post '/api/v1/text_modules/import', params: { file: csv_file, col_sep: ';' }, headers: { 'Authorization' => credentials }
  113. assert_response(200)
  114. result = JSON.parse(@response.body)
  115. assert_equal(Hash, result.class)
  116. assert_equal(false, result['try'])
  117. assert_equal(2, result['records'].count)
  118. assert_equal('success', result['result'])
  119. text_module1 = TextModule.find_by(name: 'some name1')
  120. assert(text_module1)
  121. assert_equal(text_module1.name, 'some name1')
  122. assert_equal(text_module1.keywords, 'keyword1')
  123. assert_equal(text_module1.content, 'some<br>content1')
  124. assert_equal(text_module1.active, true)
  125. text_module2 = TextModule.find_by(name: 'some name2')
  126. assert(text_module2)
  127. assert_equal(text_module2.name, 'some name2')
  128. assert_equal(text_module2.keywords, 'keyword2')
  129. assert_equal(text_module2.content, 'some content<br>test123')
  130. assert_equal(text_module2.active, true)
  131. end
  132. end