votes_spec.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'API V1 Polls Votes' do
  4. let(:user) { Fabricate(:user) }
  5. let(:scopes) { 'write:statuses' }
  6. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'POST /api/v1/polls/:poll_id/votes' do
  9. let(:poll) { Fabricate(:poll) }
  10. let(:params) { { choices: %w(1) } }
  11. before do
  12. post "/api/v1/polls/#{poll.id}/votes", params: params, headers: headers
  13. end
  14. it 'creates a vote', :aggregate_failures do
  15. expect(response).to have_http_status(200)
  16. expect(response.content_type)
  17. .to start_with('application/json')
  18. expect(vote).to_not be_nil
  19. expect(vote.choice).to eq 1
  20. expect(poll.reload.cached_tallies).to eq [0, 1]
  21. end
  22. context 'when the required choices param is not provided' do
  23. let(:params) { {} }
  24. it 'returns http bad request' do
  25. expect(response).to have_http_status(400)
  26. expect(response.content_type)
  27. .to start_with('application/json')
  28. end
  29. end
  30. private
  31. def vote
  32. poll.votes.where(account: user.account).first
  33. end
  34. end
  35. end