tags_spec.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Tags' do
  4. let(:role) { UserRole.find_by(name: 'Admin') }
  5. let(:user) { Fabricate(:user, role: role) }
  6. let(:scopes) { 'admin:read admin:write' }
  7. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  8. let(:tag) { Fabricate(:tag) }
  9. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  10. describe 'GET /api/v1/admin/tags' do
  11. subject do
  12. get '/api/v1/admin/tags', headers: headers, params: params
  13. end
  14. let(:params) { {} }
  15. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  16. it_behaves_like 'forbidden for wrong role', ''
  17. it 'returns http success' do
  18. subject
  19. expect(response).to have_http_status(200)
  20. end
  21. context 'when there are no tags' do
  22. it 'returns an empty list' do
  23. subject
  24. expect(body_as_json).to be_empty
  25. end
  26. end
  27. context 'when there are tagss' do
  28. let!(:tags) do
  29. [
  30. Fabricate(:tag),
  31. Fabricate(:tag),
  32. Fabricate(:tag),
  33. Fabricate(:tag),
  34. ]
  35. end
  36. it 'returns the expected tags' do
  37. subject
  38. tags.each do |tag|
  39. expect(body_as_json.find { |item| item[:id] == tag.id.to_s && item[:name] == tag.name }).to_not be_nil
  40. end
  41. end
  42. context 'with limit param' do
  43. let(:params) { { limit: 2 } }
  44. it 'returns only the requested number of tags' do
  45. subject
  46. expect(body_as_json.size).to eq(params[:limit])
  47. end
  48. end
  49. end
  50. end
  51. describe 'GET /api/v1/admin/tags/:id' do
  52. subject do
  53. get "/api/v1/admin/tags/#{tag.id}", headers: headers
  54. end
  55. let!(:tag) { Fabricate(:tag) }
  56. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  57. it_behaves_like 'forbidden for wrong role', ''
  58. it 'returns http success' do
  59. subject
  60. expect(response).to have_http_status(200)
  61. end
  62. it 'returns expected tag content' do
  63. subject
  64. expect(body_as_json[:id].to_i).to eq(tag.id)
  65. expect(body_as_json[:name]).to eq(tag.name)
  66. end
  67. context 'when the requested tag does not exist' do
  68. it 'returns http not found' do
  69. get '/api/v1/admin/tags/-1', headers: headers
  70. expect(response).to have_http_status(404)
  71. end
  72. end
  73. end
  74. describe 'PUT /api/v1/admin/tags/:id' do
  75. subject do
  76. put "/api/v1/admin/tags/#{tag.id}", headers: headers, params: params
  77. end
  78. let!(:tag) { Fabricate(:tag) }
  79. let(:params) { { display_name: tag.name.upcase } }
  80. it_behaves_like 'forbidden for wrong scope', 'write:statuses'
  81. it_behaves_like 'forbidden for wrong scope', 'admin:read'
  82. it_behaves_like 'forbidden for wrong role', ''
  83. it 'returns http success' do
  84. subject
  85. expect(response).to have_http_status(200)
  86. end
  87. it 'returns updated tag' do
  88. subject
  89. expect(body_as_json[:id].to_i).to eq(tag.id)
  90. expect(body_as_json[:name]).to eq(tag.name.upcase)
  91. end
  92. context 'when the updated display name is invalid' do
  93. let(:params) { { display_name: tag.name + tag.id.to_s } }
  94. it 'returns http unprocessable content' do
  95. subject
  96. expect(response).to have_http_status(422)
  97. end
  98. end
  99. context 'when the requested tag does not exist' do
  100. it 'returns http not found' do
  101. get '/api/v1/admin/tags/-1', headers: headers
  102. expect(response).to have_http_status(404)
  103. end
  104. end
  105. end
  106. end