require 'spec_helper' describe ApiRecordingsController do render_views before(:each) do @user = FactoryGirl.create(:user) @instrument = FactoryGirl.create(:instrument, :description => 'a great instrument') @music_session = FactoryGirl.create(:music_session, :creator => @user, :musician_access => true) @connection = FactoryGirl.create(:connection, :user => @user, :music_session => @music_session) @track = FactoryGirl.create(:track, :connection => @connection, :instrument => @instrument) controller.current_user = @user end describe "start" do it "should work" do post :start, { :format => 'json', :music_session_id => @music_session.id } response.should be_success response_body = JSON.parse(response.body) response_body['id'].should_not be_nil recording = Recording.find(response_body['id']) end it "should not allow multiple starts" do post :start, { :format => 'json', :music_session_id => @music_session.id } post :start, { :format => 'json', :music_session_id => @music_session.id } response.status.should == 422 response_body = JSON.parse(response.body) response_body["errors"]["music_session"][0].should == ValidationMessages::ALREADY_BEING_RECORDED end it "should not allow start while playback ongoing" do recording = Recording.start(@music_session, @user) recording.stop recording.reload claimed_recording = recording.claim(@user, "name", "description", Genre.first, true, true) @music_session.claimed_recording_start(@user, claimed_recording) @music_session.errors.any?.should be_false post :start, { :format => 'json', :music_session_id => @music_session.id } response.status.should == 422 response_body = JSON.parse(response.body) response_body["errors"]["music_session"][0].should == ValidationMessages::ALREADY_PLAYBACK_RECORDING end it "should not allow start by somebody not in the music session" do user2 = FactoryGirl.create(:user) controller.current_user = user2 post :start, { :format => 'json', :music_session_id => @music_session.id } response.status.should == 403 end end describe "get" do it "should work" do post :start, { :format => 'json', :music_session_id => @music_session.id } response.should be_success response_body = JSON.parse(response.body) response_body['id'].should_not be_nil recordingId = response_body['id'] get :show, {:format => 'json', :id => recordingId} response.should be_success response_body = JSON.parse(response.body) response_body['id'].should == recordingId end end describe "stop" do it "should work" do post :start, { :format => 'json', :music_session_id => @music_session.id } response_body = JSON.parse(response.body) recording = Recording.find(response_body['id']) post :stop, { :format => 'json', :id => recording.id } response.should be_success response_body = JSON.parse(response.body) response_body['id'].should_not be_nil Recording.find(response_body['id']).id.should == recording.id end it "should not allow stop on a session not being recorded" do post :start, { :format => 'json', :music_session_id => @music_session.id } response_body = JSON.parse(response.body) recording = Recording.find(response_body['id']) post :stop, { :format => 'json', :id => recording.id } post :stop, { :format => 'json', :id => recording.id } response.status.should == 422 response_body = JSON.parse(response.body) end it "should not allow stop on a session requested by a different member" do post :start, { :format => 'json', :music_session_id => @music_session.id } response_body = JSON.parse(response.body) recording = Recording.find(response_body['id']) user2 = FactoryGirl.create(:user) controller.current_user = user2 post :stop, { :format => 'json', :id => recording.id } response.status.should == 403 end end describe "download" do it "should only allow a user to download a track if they have claimed the recording" do post :start, { :format => 'json', :music_session_id => @music_session.id } response_body = JSON.parse(response.body) recording = Recording.find(response_body['id']) post :stop, { :format => 'json', :id => recording.id } response.should be_success end end end