time_accounting_spec.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Ticket::Article API > Time Accounting', :aggregate_failures, type: :request do
  4. let(:ticket) { create(:ticket) }
  5. let(:agent) { create(:agent, groups: Group.all) }
  6. describe 'GET /api/v1/ticket_articles' do
  7. let(:article) { create(:ticket_article, ticket: ticket) }
  8. let(:accounted_time) { create(:ticket_time_accounting, ticket: ticket, ticket_article: article, time_unit: 42) }
  9. before do
  10. article && accounted_time
  11. authenticated_as(agent)
  12. get "/api/v1/ticket_articles/#{article.id}?expand=true"
  13. end
  14. context 'when no time was accounted' do
  15. let(:accounted_time) { nil }
  16. it 'returns nil' do
  17. expect(response).to have_http_status(:ok)
  18. expect(json_response['time_unit']).to be_nil
  19. end
  20. end
  21. context 'when time was accounted' do
  22. it 'returns the accounted time' do
  23. expect(response).to have_http_status(:ok)
  24. expect(json_response['time_unit']).to eq(accounted_time.time_unit.to_s)
  25. end
  26. end
  27. end
  28. describe 'PUT /api/v1/tickets/:id' do
  29. let(:params) do
  30. {
  31. article: article
  32. }
  33. end
  34. let(:article) do
  35. {
  36. body: 'Some example body.',
  37. time_unit: time_unit,
  38. accounted_time_type_id: accounted_time_type_id
  39. }
  40. end
  41. let(:time_unit) { 42 }
  42. let(:accounted_time_type_id) { nil }
  43. let(:time_accounting_enabled) { true }
  44. before do
  45. Setting.set('time_accounting', time_accounting_enabled)
  46. ticket
  47. authenticated_as(agent)
  48. put "/api/v1/tickets/#{ticket.id}", params: params, as: :json
  49. end
  50. context 'when time accounting disabled' do
  51. let(:time_accounting_enabled) { false }
  52. it 'does not create ticket article' do
  53. expect(response).to have_http_status(:forbidden)
  54. end
  55. end
  56. context 'without accounted time type' do
  57. it 'created accounted time entry for article' do
  58. expect(response).to have_http_status(:ok)
  59. expect(Ticket::TimeAccounting.last.time_unit).to eq(42)
  60. end
  61. end
  62. context 'with accounted time type' do
  63. let(:accounted_time_type) { create(:ticket_time_accounting_type) }
  64. let(:accounted_time_type_id) { accounted_time_type.id }
  65. it 'created accounted time entry for article' do
  66. expect(response).to have_http_status(:ok)
  67. expect(Ticket::TimeAccounting.last.time_unit).to eq(42)
  68. expect(Ticket::TimeAccounting.last.type.id).to eq(accounted_time_type_id)
  69. end
  70. context 'with type name' do
  71. let(:article) do
  72. {
  73. body: 'Some example body.',
  74. time_unit: time_unit,
  75. accounted_time_type: accounted_time_type.name
  76. }
  77. end
  78. it 'created accounted time entry for article' do
  79. expect(response).to have_http_status(:ok)
  80. expect(Ticket::TimeAccounting.last.time_unit).to eq(42)
  81. expect(Ticket::TimeAccounting.last.type.id).to eq(accounted_time_type_id)
  82. end
  83. end
  84. end
  85. end
  86. end