report_spec.rb 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. require 'rails_helper'
  2. RSpec.describe 'Report', type: :request, searchindex: true do
  3. let!(:admin) do
  4. create(:admin)
  5. end
  6. let!(:year) do
  7. DateTime.now.utc.year
  8. end
  9. let!(:month) do
  10. DateTime.now.utc.month
  11. end
  12. let!(:week) do
  13. DateTime.now.utc.strftime('%U').to_i
  14. end
  15. let!(:day) do
  16. DateTime.now.utc.day
  17. end
  18. let!(:today) do
  19. Time.zone.parse('2019-03-15T08:00:00Z')
  20. end
  21. let!(:backends) do
  22. {
  23. 'count::created': true,
  24. 'count::closed': true,
  25. 'count::backlog': true,
  26. 'create_channels::phone_in': true,
  27. 'create_channels::phone_out': true,
  28. 'create_channels::email_in': true,
  29. 'create_channels::email_out': true,
  30. 'create_channels::web_in': true,
  31. 'create_channels::twitter_in': true,
  32. 'create_channels::twitter_out': true,
  33. 'communication::phone_in': true,
  34. 'communication::phone_out': true,
  35. 'communication::email_in': true,
  36. 'communication::email_out': true,
  37. 'communication::web_in': true,
  38. 'communication::twitter_in': true,
  39. 'communication::twitter_out': true
  40. }
  41. end
  42. before do
  43. configure_elasticsearch do
  44. travel 1.minute
  45. travel_to today.midday
  46. Ticket.destroy_all
  47. create(:ticket, title: 'ticket for report #1', created_at: today.midday)
  48. create(:ticket, title: 'ticket for report #2', created_at: today.midday + 2.hours)
  49. create(:ticket, title: 'ticket for report #3', created_at: today.midday + 2.hours)
  50. create(:ticket, title: 'ticket for report #4', created_at: today.midday + 10.hours, state: Ticket::State.lookup(name: 'closed') )
  51. create(:ticket, title: 'ticket for report #5', created_at: today.midday + 11.hours)
  52. create(:ticket, title: 'ticket for report #6', created_at: today.midday - 11.hours)
  53. create(:ticket, title: 'ticket for report #7', created_at: Time.zone.parse('2019-02-28T23:30:00Z'))
  54. create(:ticket, title: 'ticket for report #8', created_at: Time.zone.parse('2019-03-01T00:30:00Z'))
  55. create(:ticket, title: 'ticket for report #9', created_at: Time.zone.parse('2019-03-31T23:30:00Z'))
  56. create(:ticket, title: 'ticket for report #10', created_at: Time.zone.parse('2019-04-01T00:30:00Z'))
  57. rebuild_searchindex
  58. # execute background jobs
  59. Scheduler.worker(true)
  60. sleep 6
  61. end
  62. end
  63. describe 'request handling' do
  64. it 'does report example - admin access' do
  65. authenticated_as(admin)
  66. get "/api/v1/reports/sets?sheet=true;metric=count;year=#{year};month=#{month};week=#{week};day=#{day};timeRange=year;profile_id=1;downloadBackendSelected=count::created", params: {}, as: :json
  67. expect(response).to have_http_status(:ok)
  68. assert(@response['Content-Disposition'])
  69. expect(@response['Content-Disposition']).to eq('attachment; filename="tickets--all--Created.xls"')
  70. expect(@response['Content-Type']).to eq('application/vnd.ms-excel')
  71. end
  72. it 'does report example - deliver result' do
  73. skip('No ES configured') if !SearchIndexBackend.enabled?
  74. authenticated_as(admin)
  75. # 2019-03-15 - day interval
  76. params = {
  77. metric: 'count',
  78. year: today.year,
  79. month: today.month,
  80. day: today.day,
  81. timeRange: 'day',
  82. profiles: {
  83. 1 => true
  84. },
  85. backends: backends
  86. }
  87. post '/api/v1/reports/generate', params: params, as: :json
  88. expect(response).to have_http_status(:ok)
  89. expect(json_response['data']['count::created']).to eq([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1])
  90. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  91. expect(json_response['data']['count::backlog']).to eq([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1])
  92. Setting.set('timezone_default', 'Europe/Berlin')
  93. post '/api/v1/reports/generate', params: params, as: :json
  94. expect(response).to have_http_status(:ok)
  95. expect(json_response['data']['count::created']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1])
  96. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  97. expect(json_response['data']['count::backlog']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1])
  98. Setting.set('timezone_default', 'America/Chicago')
  99. post '/api/v1/reports/generate', params: params, as: :json
  100. expect(response).to have_http_status(:ok)
  101. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0])
  102. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  103. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0])
  104. Setting.set('timezone_default', 'Australia/Melbourne')
  105. post '/api/v1/reports/generate', params: params, as: :json
  106. expect(response).to have_http_status(:ok)
  107. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  108. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  109. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  110. # 2019-03 - month interval
  111. Setting.set('timezone_default', '')
  112. params = {
  113. metric: 'count',
  114. year: today.year,
  115. month: today.month,
  116. timeRange: 'month',
  117. profiles: {
  118. 1 => true
  119. },
  120. backends: backends
  121. }
  122. post '/api/v1/reports/generate', params: params, as: :json
  123. expect(response).to have_http_status(:ok)
  124. expect(json_response['data']['count::created']).to eq([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  125. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  126. expect(json_response['data']['count::backlog']).to eq([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  127. Setting.set('timezone_default', 'Europe/Berlin')
  128. post '/api/v1/reports/generate', params: params, as: :json
  129. expect(response).to have_http_status(:ok)
  130. expect(json_response['data']['count::created']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  131. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  132. expect(json_response['data']['count::backlog']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  133. Setting.set('timezone_default', 'America/Chicago')
  134. post '/api/v1/reports/generate', params: params, as: :json
  135. expect(response).to have_http_status(:ok)
  136. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2])
  137. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  138. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2])
  139. Setting.set('timezone_default', 'Australia/Melbourne')
  140. post '/api/v1/reports/generate', params: params, as: :json
  141. expect(json_response['data']['count::created']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  142. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  143. expect(json_response['data']['count::backlog']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  144. # 2019-02 - month interval
  145. Setting.set('timezone_default', '')
  146. params = {
  147. metric: 'count',
  148. year: today.year,
  149. month: today.month - 1,
  150. timeRange: 'month',
  151. profiles: {
  152. 1 => true
  153. },
  154. backends: backends
  155. }
  156. post '/api/v1/reports/generate', params: params, as: :json
  157. expect(response).to have_http_status(:ok)
  158. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  159. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  160. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
  161. Setting.set('timezone_default', 'Europe/Berlin')
  162. post '/api/v1/reports/generate', params: params, as: :json
  163. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  164. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  165. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  166. Setting.set('timezone_default', 'America/Chicago')
  167. post '/api/v1/reports/generate', params: params, as: :json
  168. expect(response).to have_http_status(:ok)
  169. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2])
  170. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  171. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2])
  172. Setting.set('timezone_default', 'Australia/Melbourne')
  173. post '/api/v1/reports/generate', params: params, as: :json
  174. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  175. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  176. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  177. # 2019-04 - month interval
  178. Setting.set('timezone_default', '')
  179. params = {
  180. metric: 'count',
  181. year: today.year,
  182. month: today.month + 1,
  183. timeRange: 'month',
  184. profiles: {
  185. 1 => true
  186. },
  187. backends: backends
  188. }
  189. post '/api/v1/reports/generate', params: params, as: :json
  190. expect(response).to have_http_status(:ok)
  191. expect(json_response['data']['count::created']).to eq([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  192. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  193. expect(json_response['data']['count::backlog']).to eq([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  194. Setting.set('timezone_default', 'Europe/Berlin')
  195. post '/api/v1/reports/generate', params: params, as: :json
  196. expect(json_response['data']['count::created']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  197. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  198. expect(json_response['data']['count::backlog']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  199. Setting.set('timezone_default', 'America/Chicago')
  200. post '/api/v1/reports/generate', params: params, as: :json
  201. expect(response).to have_http_status(:ok)
  202. expect(json_response['data']['count::created']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  203. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  204. expect(json_response['data']['count::backlog']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  205. Setting.set('timezone_default', 'Australia/Melbourne')
  206. post '/api/v1/reports/generate', params: params, as: :json
  207. expect(json_response['data']['count::created']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  208. expect(json_response['data']['count::closed']).to eq([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  209. expect(json_response['data']['count::backlog']).to eq([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  210. # 2019 - year interval
  211. Setting.set('timezone_default', '')
  212. params = {
  213. metric: 'count',
  214. year: today.year,
  215. timeRange: 'year',
  216. profiles: {
  217. 1 => true
  218. },
  219. backends: backends
  220. }
  221. post '/api/v1/reports/generate', params: params, as: :json
  222. expect(response).to have_http_status(:ok)
  223. expect(json_response['data']['count::created']).to eq([0, 1, 8, 1, 0, 0, 0, 0, 0, 0, 0, 0])
  224. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  225. expect(json_response['data']['count::backlog']).to eq([0, 1, 7, 1, 0, 0, 0, 0, 0, 0, 0, 0])
  226. Setting.set('timezone_default', 'Europe/Berlin')
  227. post '/api/v1/reports/generate', params: params, as: :json
  228. expect(response).to have_http_status(:ok)
  229. expect(json_response['data']['count::created']).to eq([0, 0, 8, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  230. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  231. expect(json_response['data']['count::backlog']).to eq([0, 0, 7, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  232. Setting.set('timezone_default', 'America/Chicago')
  233. post '/api/v1/reports/generate', params: params, as: :json
  234. expect(response).to have_http_status(:ok)
  235. expect(json_response['data']['count::created']).to eq([0, 2, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  236. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  237. expect(json_response['data']['count::backlog']).to eq([0, 2, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  238. Setting.set('timezone_default', 'Australia/Melbourne')
  239. post '/api/v1/reports/generate', params: params, as: :json
  240. expect(response).to have_http_status(:ok)
  241. expect(json_response['data']['count::created']).to eq([0, 0, 8, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  242. expect(json_response['data']['count::closed']).to eq([0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0])
  243. expect(json_response['data']['count::backlog']).to eq([0, 0, 7, 2, 0, 0, 0, 0, 0, 0, 0, 0])
  244. end
  245. end
  246. end