Add tests to PodcastEpisodesController (#2297)
* Add tests for existing API PodcastEpisodesController * Generate only index route There's no need to generate all routes if they are not implemented
This commit is contained in:
parent
1a0d4667c5
commit
dc2414c174
3 changed files with 42 additions and 7 deletions
|
|
@ -1,22 +1,17 @@
|
|||
module Api
|
||||
module V0
|
||||
class PodcastEpisodesController < ApiController
|
||||
# before_action :set_cache_control_headers, only: [:index, :show]
|
||||
caches_action :index,
|
||||
cache_path: proc { |c| c.params.permit! },
|
||||
expires_in: 10.minutes
|
||||
respond_to :json
|
||||
|
||||
caches_action :show,
|
||||
cache_path: proc { |c| c.params.permit! },
|
||||
expires_in: 10.minutes
|
||||
respond_to :json
|
||||
|
||||
before_action :cors_preflight_check
|
||||
after_action :cors_set_access_control_headers
|
||||
|
||||
def index
|
||||
@page = params[:page]
|
||||
|
||||
if params[:username]
|
||||
@podcast = Podcast.find_by(slug: params[:username]) || not_found
|
||||
@podcast_episodes = @podcast.
|
||||
|
|
|
|||
|
|
@ -67,7 +67,7 @@ Rails.application.routes.draw do
|
|||
end
|
||||
end
|
||||
resources :comments
|
||||
resources :podcast_episodes
|
||||
resources :podcast_episodes, only: [:index]
|
||||
resources :reactions, only: [:create] do
|
||||
collection do
|
||||
post "/onboarding", to: "reactions#onboarding"
|
||||
|
|
|
|||
40
spec/requests/api/v0/podcasts_episodes_spec.rb
Normal file
40
spec/requests/api/v0/podcasts_episodes_spec.rb
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe "Api::V0::PodcastEpisodes", type: :request do
|
||||
let(:podcast) { create(:podcast) }
|
||||
|
||||
describe "GET /api/podcast_episodes" do
|
||||
it "returns json response" do
|
||||
get "/api/podcast_episodes"
|
||||
expect(response.content_type).to eq("application/json")
|
||||
end
|
||||
|
||||
it "returns correct attributes for an episode" do
|
||||
create(:podcast_episode)
|
||||
get "/api/podcast_episodes"
|
||||
expected_attributes = %w[type_of id path image_url title podcast]
|
||||
expect(JSON.parse(response.body).first.keys).to eq(expected_attributes)
|
||||
end
|
||||
|
||||
it "returns episodes in reverse publishing order" do
|
||||
pe1 = create(:podcast_episode, published_at: 1.day.ago)
|
||||
pe2 = create(:podcast_episode, published_at: 1.day.from_now)
|
||||
get "/api/podcast_episodes"
|
||||
expect(JSON.parse(response.body).map { |pe| pe["id"] }).to eq([pe2.id, pe1.id])
|
||||
end
|
||||
|
||||
it "returns only podcasts for a given username" do
|
||||
pe1 = create(:podcast_episode, podcast: podcast)
|
||||
create(:podcast_episode, podcast: create(:podcast))
|
||||
get "/api/podcast_episodes?username=#{podcast.slug}"
|
||||
expect(JSON.parse(response.body).map { |pe| pe["id"] }).to eq([pe1.id])
|
||||
end
|
||||
|
||||
it "returns not found if the username does not exist" do
|
||||
invalid_request = lambda do
|
||||
get "/api/podcast_episodes?username=foobar"
|
||||
end
|
||||
expect(invalid_request).to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue