|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | +require 'rack/test' |
| 5 | + |
| 6 | +describe Web do |
| 7 | + include Rack::Test::Methods |
| 8 | + |
| 9 | + def app |
| 10 | + Web::FeedbacksAPI |
| 11 | + end |
| 12 | + |
| 13 | + let(:user) { create(:user) } |
| 14 | + let(:token) { user.generate_jwt } |
| 15 | + |
| 16 | + describe 'POST /web/feedbacks' do |
| 17 | + context 'with valid parameters' do |
| 18 | + it 'creates a feedback' do |
| 19 | + post '/web/feedbacks', { content: 'Great post!', post_id: 1 }, 'HTTP_AUTHORIZATION' => "Bearer #{token}" |
| 20 | + |
| 21 | + expect(last_response.status).to eq(200) |
| 22 | + json = JSON.parse(last_response.body) |
| 23 | + expect(json['success']).to eq(true) |
| 24 | + expect(json['message']).to eq('Feedback created') |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + context 'with invalid parameters' do |
| 29 | + it 'returns an error message' do |
| 30 | + post '/web/feedbacks', { content: '', post_id: 1 }, 'HTTP_AUTHORIZATION' => "Bearer #{token}" |
| 31 | + |
| 32 | + expect(last_response.status).to eq(400) |
| 33 | + end |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe 'GET /web/feedbacks/:id' do |
| 38 | + let(:feedback) { create(:feedback, user_id: user.id) } |
| 39 | + |
| 40 | + context 'with a valid feedback ID' do |
| 41 | + it 'returns the details of the feedback' do |
| 42 | + get "/web/feedbacks/#{feedback.id}", {}, 'HTTP_AUTHORIZATION' => "Bearer #{token}" |
| 43 | + |
| 44 | + expect(last_response.status).to eq(200) |
| 45 | + json = JSON.parse(last_response.body) |
| 46 | + expect(json['success']).to eq(true) |
| 47 | + expect(json['feedback']['id']).to eq(feedback.id) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + context 'with an invalid feedback ID' do |
| 52 | + it 'returns an error message' do |
| 53 | + get '/web/feedbacks/999', {}, 'HTTP_AUTHORIZATION' => "Bearer #{token}" |
| 54 | + |
| 55 | + expect(last_response.status).to eq(400) |
| 56 | + json = JSON.parse(last_response.body) |
| 57 | + expect(json['success']).to eq(false) |
| 58 | + expect(json['message']).to eq('Feedback not found') |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe 'DELETE /web/feedbacks/:id' do |
| 64 | + let(:feedback) { create(:feedback, user: user) } |
| 65 | + |
| 66 | + context 'with a valid feedback ID' do |
| 67 | + it 'deletes the feedback' do |
| 68 | + delete "/web/feedbacks/#{feedback.id}", {}, 'HTTP_AUTHORIZATION' => "Bearer #{token}" |
| 69 | + |
| 70 | + expect(last_response.status).to eq(200) |
| 71 | + json = JSON.parse(last_response.body) |
| 72 | + expect(json['success']).to eq(true) |
| 73 | + expect(json['message']).to eq('Feedback deleted') |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context 'with an invalid feedback ID' do |
| 78 | + it 'returns an error message' do |
| 79 | + delete '/web/feedbacks/999', {}, 'HTTP_AUTHORIZATION' => "Bearer #{token}" |
| 80 | + |
| 81 | + expect(last_response.status).to eq(400) |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + describe 'PUT /web/feedbacks/:id' do |
| 87 | + let(:feedback) { create(:feedback, user: user) } |
| 88 | + |
| 89 | + context 'with valid parameters' do |
| 90 | + it 'updates the feedback' do |
| 91 | + put "/web/feedbacks/#{feedback.id}", { new_content: 'Updated content' }, 'HTTP_AUTHORIZATION' => "Bearer #{token}" |
| 92 | + |
| 93 | + expect(last_response.status).to eq(200) |
| 94 | + json = JSON.parse(last_response.body) |
| 95 | + expect(json['success']).to eq(true) |
| 96 | + expect(json['message']).to eq('Feedback updated') |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + context 'with invalid parameters' do |
| 101 | + it 'returns an error message' do |
| 102 | + put "/web/feedbacks/#{feedback.id}", { new_content: '' }, 'HTTP_AUTHORIZATION' => "Bearer #{token}" |
| 103 | + |
| 104 | + expect(last_response.status).to eq(400) |
| 105 | + end |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments