tag_spec.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright (C) 2012-2022 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(:agent) { create(:agent) }
  6. context 'tag adding' do
  7. it 'returns created' do
  8. post '/api/v1/tags/add', params: { object: 'Foo', item: 'bar', o_id: 1 }
  9. expect(response).to have_http_status(:created)
  10. end
  11. it 'deletes tag' do
  12. post '/api/v1/tags/add', params: { object: 'Foo', item: 'bar', o_id: 1 }
  13. expect(described_class.tag_list({ object: 'Foo', item: 'bar', o_id: 1 })).to be_present
  14. end
  15. end
  16. context 'tag removal', authenticated_as: :agent do
  17. before do
  18. described_class.tag_add(object: 'Foo', item: 'bar', o_id: 1, created_by_id: 1)
  19. end
  20. it 'returns ok' do
  21. delete '/api/v1/tags/remove', params: { object: 'Foo', item: 'bar', o_id: 1 }
  22. expect(response).to have_http_status(:ok)
  23. end
  24. it 'deletes tag' do
  25. delete '/api/v1/tags/remove', params: { object: 'Foo', item: 'bar', o_id: 1 }
  26. expect(described_class.tag_list({ object: 'Foo', item: 'bar', o_id: 1 })).to be_empty
  27. end
  28. end
  29. context 'tag search' do
  30. let!(:tags) do
  31. [
  32. Tag::Item.lookup_by_name_and_create('foobar'),
  33. Tag::Item.lookup_by_name_and_create('xxxxxxxxx_DUMMY_VALUE1'),
  34. Tag::Item.lookup_by_name_and_create('121212121_DUMMY_VALUE2'),
  35. Tag::Item.lookup_by_name_and_create('oxoxoxoxo_DUMMY_VALUE3'),
  36. ]
  37. end
  38. let(:foobar_tag) { tags.first }
  39. shared_examples 'foobar tag found using' do |search_term:|
  40. it "found 1 tag using search term '#{search_term}'" do
  41. get '/api/v1/tag_search', params: { term: search_term }
  42. expect(response).to have_http_status(:ok)
  43. expect(json_response).to contain_exactly('id' => foobar_tag.id, 'value' => foobar_tag.name)
  44. end
  45. end
  46. shared_examples 'no tag found using' do |search_term:|
  47. it "found 0 tags using search term '#{search_term}'" do
  48. get '/api/v1/tag_search', params: { term: search_term }
  49. expect(response).to have_http_status(:ok)
  50. expect(json_response).to contain_exactly
  51. end
  52. end
  53. shared_examples 'all tags found using' do |search_term:|
  54. it "found all tags using search term '#{search_term}'" do
  55. get '/api/v1/tag_search', params: { term: search_term }
  56. expect(response).to have_http_status(:ok)
  57. expect(json_response.size).to eq(Tag::Item.count)
  58. end
  59. end
  60. describe 'using prefix search' do
  61. include_examples 'foobar tag found using', search_term: 'foobar'
  62. include_examples 'foobar tag found using', search_term: 'foo'
  63. include_examples 'foobar tag found using', search_term: 'f'
  64. end
  65. describe 'using substring search (added via Enhancement #2569 - Enhance tag search to use fulltext search)' do
  66. include_examples 'foobar tag found using', search_term: 'bar'
  67. include_examples 'foobar tag found using', search_term: 'ar'
  68. include_examples 'foobar tag found using', search_term: 'oo'
  69. end
  70. describe 'using wildcard search' do
  71. include_examples 'all tags found using', search_term: ' '
  72. include_examples 'all tags found using', search_term: ' '
  73. end
  74. describe 'using invalid search terms' do
  75. include_examples 'no tag found using', search_term: 'WRONG_VALUE'
  76. include_examples 'no tag found using', search_term: '-'
  77. include_examples 'no tag found using', search_term: 'fooar'
  78. include_examples 'no tag found using', search_term: '1foobar'
  79. include_examples 'no tag found using', search_term: 'foobar2'
  80. end
  81. context 'without search term' do
  82. before do
  83. create_list(:tag, 2, tag_item: tags.last)
  84. create_list(:tag, 1, tag_item: tags.first)
  85. end
  86. it 'most used is on first place without search term' do
  87. get '/api/v1/tag_search', params: { term: '' }
  88. expect(json_response.first['value']).to eq(tags.last.name)
  89. end
  90. end
  91. end
  92. end
  93. end