tag_spec.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Tag, type: :request do
  4. describe 'request handling', authenticated_as: :agent do
  5. let(:ticket) { Ticket.first }
  6. let(:agent) { create(:agent, groups: [ticket.group]) }
  7. let(:payload) { { object: ticket.class.name, item: 'bar', o_id: ticket.id } }
  8. context 'tag adding' do
  9. it 'returns created' do
  10. post '/api/v1/tags/add', params: payload
  11. expect(response).to have_http_status(:created)
  12. end
  13. it 'deletes tag' do
  14. post '/api/v1/tags/add', params: payload
  15. expect(described_class.tag_list(payload)).to be_present
  16. end
  17. end
  18. context 'tag removal', authenticated_as: :agent do
  19. before do
  20. described_class.tag_add(**payload, created_by_id: 1)
  21. end
  22. it 'returns ok' do
  23. delete '/api/v1/tags/remove', params: payload
  24. expect(response).to have_http_status(:ok)
  25. end
  26. it 'deletes tag' do
  27. delete '/api/v1/tags/remove', params: payload
  28. expect(described_class.tag_list(payload)).to be_empty
  29. end
  30. end
  31. context 'tag search' do
  32. let!(:tags) do
  33. [
  34. Tag::Item.lookup_by_name_and_create('foobar'),
  35. Tag::Item.lookup_by_name_and_create('xxxxxxxxx_DUMMY_VALUE1'),
  36. Tag::Item.lookup_by_name_and_create('121212121_DUMMY_VALUE2'),
  37. Tag::Item.lookup_by_name_and_create('oxoxoxoxo_DUMMY_VALUE3'),
  38. ]
  39. end
  40. let(:foobar_tag) { tags.first }
  41. shared_examples 'foobar tag found using' do |search_term:|
  42. it "found 1 tag using search term '#{search_term}'" do
  43. get '/api/v1/tag_search', params: { term: search_term }
  44. expect(response).to have_http_status(:ok)
  45. expect(json_response).to contain_exactly('id' => foobar_tag.id, 'value' => foobar_tag.name)
  46. end
  47. end
  48. shared_examples 'no tag found using' do |search_term:|
  49. it "found 0 tags using search term '#{search_term}'" do
  50. get '/api/v1/tag_search', params: { term: search_term }
  51. expect(response).to have_http_status(:ok)
  52. expect(json_response).to be_empty
  53. end
  54. end
  55. shared_examples 'all tags found using' do |search_term:|
  56. it "found all tags using search term '#{search_term}'" do
  57. get '/api/v1/tag_search', params: { term: search_term }
  58. expect(response).to have_http_status(:ok)
  59. expect(json_response.size).to eq(Tag::Item.count)
  60. end
  61. end
  62. describe 'using prefix search' do
  63. include_examples 'foobar tag found using', search_term: 'foobar'
  64. include_examples 'foobar tag found using', search_term: 'foo'
  65. include_examples 'foobar tag found using', search_term: 'f'
  66. end
  67. describe 'using substring search (added via Enhancement #2569 - Enhance tag search to use fulltext search)' do
  68. include_examples 'foobar tag found using', search_term: 'bar'
  69. include_examples 'foobar tag found using', search_term: 'ar'
  70. include_examples 'foobar tag found using', search_term: 'oo'
  71. end
  72. describe 'using wildcard search' do
  73. include_examples 'all tags found using', search_term: ' '
  74. include_examples 'all tags found using', search_term: ' '
  75. end
  76. describe 'using invalid search terms' do
  77. include_examples 'no tag found using', search_term: 'WRONG_VALUE'
  78. include_examples 'no tag found using', search_term: '-'
  79. include_examples 'no tag found using', search_term: 'fooar'
  80. include_examples 'no tag found using', search_term: '1foobar'
  81. include_examples 'no tag found using', search_term: 'foobar2'
  82. end
  83. context 'without search term' do
  84. before do
  85. create_list(:tag, 2, tag_item: tags.last)
  86. create_list(:tag, 1, tag_item: tags.first)
  87. end
  88. it 'most used is on first place without search term' do
  89. get '/api/v1/tag_search', params: { term: '' }
  90. expect(json_response.first['value']).to eq(tags.last.name)
  91. end
  92. end
  93. end
  94. end
  95. describe 'Agents can create new tags even if prohibited by the settings #3501', authenticated_as: :agent do
  96. let(:tag) { SecureRandom.hex(4) }
  97. let(:ticket) { Ticket.first }
  98. let(:agent) { create(:agent, groups: [ticket.group]) }
  99. let(:payload) { { object: ticket.class.name, item: 'bar', o_id: ticket.id } }
  100. before do
  101. Setting.set('tag_new', false)
  102. end
  103. it 'does not add tags to the ticket' do
  104. post '/api/v1/tags/add', params: payload
  105. expect(response).to have_http_status(:forbidden)
  106. end
  107. end
  108. end