From cc82fb33b5cef071a66e7398e3f25789edcb2207 Mon Sep 17 00:00:00 2001 From: OsamaSayegh Date: Tue, 5 Jun 2018 07:43:05 +0300 Subject: [PATCH] REFACTOR: queued posts controller specs to requests --- .../queued_posts_controller_spec.rb | 144 ------------------ spec/requests/queued_posts_controller_spec.rb | 125 +++++++++++++++ 2 files changed, 125 insertions(+), 144 deletions(-) delete mode 100644 spec/controllers/queued_posts_controller_spec.rb create mode 100644 spec/requests/queued_posts_controller_spec.rb diff --git a/spec/controllers/queued_posts_controller_spec.rb b/spec/controllers/queued_posts_controller_spec.rb deleted file mode 100644 index 89620c116dc..00000000000 --- a/spec/controllers/queued_posts_controller_spec.rb +++ /dev/null @@ -1,144 +0,0 @@ -require 'rails_helper' -require_dependency 'queued_posts_controller' -require_dependency 'queued_post' - -describe QueuedPostsController do - context 'without authentication' do - it 'fails' do - get :index, format: :json - expect(response).not_to be_successful - end - end - - context 'as a regular user' do - let!(:user) { log_in(:user) } - it 'fails' do - get :index, format: :json - expect(response).not_to be_successful - end - end - - context 'as an admin' do - let!(:user) { log_in(:moderator) } - - it 'returns the queued posts' do - get :index, format: :json - expect(response).to be_successful - end - end - - describe '#update' do - let!(:user) { log_in(:moderator) } - let(:qp) { Fabricate(:queued_post) } - - context 'not found' do - it 'returns json error' do - qp.destroy! - - put :update, params: { - id: qp.id, queued_post: { state: 'approved' } - }, format: :json - - expect(response.status).to eq(422) - - expect(eval(response.body)).to eq(described_class.new.create_errors_json(I18n.t('queue.not_found'))) - end - end - - context 'approved' do - it 'updates the post to approved' do - - put :update, params: { - id: qp.id, queued_post: { state: 'approved' } - }, format: :json - - expect(response).to be_successful - - qp.reload - expect(qp.state).to eq(QueuedPost.states[:approved]) - end - end - - context 'rejected' do - it 'updates the post to rejected' do - - put :update, params: { - id: qp.id, queued_post: { state: 'rejected' } - }, format: :json - - expect(response).to be_successful - - qp.reload - expect(qp.state).to eq(QueuedPost.states[:rejected]) - end - end - - context 'editing content' do - let(:changes) do - { - raw: 'new raw', - title: 'new title', - category_id: 10, - tags: ['new_tag'] - } - end - - context 'when it is a topic' do - let(:queued_topic) { Fabricate(:queued_topic) } - - before do - put :update, params: { - id: queued_topic.id, queued_post: changes - }, format: :json - - expect(response).to be_successful - end - - it 'updates raw' do - expect(queued_topic.reload.raw).to eq(changes[:raw]) - end - - it 'updates the title' do - expect(queued_topic.reload.post_options['title']).to eq(changes[:title]) - end - - it 'updates the category' do - expect(queued_topic.reload.post_options['category']).to eq(changes[:category_id]) - end - - it 'updates the tags' do - expect(queued_topic.reload.post_options['tags']).to eq(changes[:tags]) - end - end - - context 'when it is a reply' do - let(:queued_reply) { Fabricate(:queued_post) } - - before do - put :update, params: { - id: queued_reply.id, queued_post: changes - }, format: :json - - expect(response).to be_successful - end - - it 'updates raw' do - expect(queued_reply.reload.raw).to eq(changes[:raw]) - end - - it 'does not update the title' do - expect(queued_reply.reload.post_options['title']).to be_nil - end - - it 'does not update the category' do - original_category = queued_reply.post_options['category'] - expect(queued_reply.reload.post_options['category']).to eq(original_category) - end - - it 'does not update the tags' do - expect(queued_reply.reload.post_options['tags']).to be_nil - end - end - end - end -end diff --git a/spec/requests/queued_posts_controller_spec.rb b/spec/requests/queued_posts_controller_spec.rb new file mode 100644 index 00000000000..421c0d9bc70 --- /dev/null +++ b/spec/requests/queued_posts_controller_spec.rb @@ -0,0 +1,125 @@ +require 'rails_helper' +require_dependency 'queued_posts_controller' +require_dependency 'queued_post' + +describe QueuedPostsController do + context 'without authentication' do + it 'fails' do + get "/queued-posts.json" + expect(response).to be_forbidden + end + end + + context 'as a regular user' do + before { sign_in(Fabricate(:user)) } + + it 'fails' do + get "/queued-posts.json" + expect(response).to be_forbidden + end + end + + context 'as an admin' do + before { sign_in(Fabricate(:moderator)) } + + it 'returns the queued posts' do + get "/queued-posts.json" + expect(response).to be_success + end + end + + describe '#update' do + before { sign_in(Fabricate(:moderator)) } + let(:qp) { Fabricate(:queued_post) } + + context 'not found' do + it 'returns json error' do + qp.destroy! + + put "/queued_posts/#{qp.id}.json", params: { + queued_post: { state: 'approved' } + } + + expect(response.status).to eq(422) + + expect(JSON.parse(response.body)["errors"].first).to eq(I18n.t('queue.not_found')) + end + end + + context 'approved' do + it 'updates the post to approved' do + + put "/queued_posts/#{qp.id}.json", params: { + queued_post: { state: 'approved' } + } + + expect(response).to be_success + + qp.reload + expect(qp.state).to eq(QueuedPost.states[:approved]) + end + end + + context 'rejected' do + it 'updates the post to rejected' do + + put "/queued_posts/#{qp.id}.json", params: { + queued_post: { state: 'rejected' } + } + + expect(response).to be_success + + qp.reload + expect(qp.state).to eq(QueuedPost.states[:rejected]) + end + end + + context 'editing content' do + let(:changes) do + { + raw: 'new raw', + title: 'new title', + category_id: 10, + tags: ['new_tag'] + } + end + + context 'when it is a topic' do + let(:queued_topic) { Fabricate(:queued_topic) } + + it 'updates the topic attributes' do + put "/queued_posts/#{queued_topic.id}.json", params: { + queued_post: changes + } + + expect(response).to be_success + queued_topic.reload + + expect(queued_topic.raw).to eq(changes[:raw]) + expect(queued_topic.post_options['title']).to eq(changes[:title]) + expect(queued_topic.post_options['category']).to eq(changes[:category_id]) + expect(queued_topic.post_options['tags']).to eq(changes[:tags]) + end + end + + context 'when it is a reply' do + let(:queued_reply) { Fabricate(:queued_post) } + + it 'updates the reply attributes' do + put "/queued_posts/#{queued_reply.id}.json", params: { + queued_post: changes + } + + original_category = queued_reply.post_options['category'] + expect(response).to be_success + queued_reply.reload + + expect(queued_reply.raw).to eq(changes[:raw]) + expect(queued_reply.post_options['title']).to be_nil + expect(queued_reply.post_options['category']).to eq(original_category) + expect(queued_reply.post_options['tags']).to be_nil + end + end + end + end +end