jam-cloud/web/spec/controllers/api_jam_tracks_controller_s...

275 lines
9.2 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe ApiJamTracksController do
render_views
include CarrierWave::Test::Matchers
before(:all) do
@original_storage = JamTrackTrackUploader.storage = :fog
@original_storage_right = JamTrackRightUploader.storage = :fog
@s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)
end
after(:all) do
JamTrackTrackUploader.storage = @original_storage
JamTrackRightUploader.storage = @original_storage_right
end
before(:each) do
JamTrack.destroy_all
@user = FactoryGirl.create(:user)
@jam_track = FactoryGirl.create(:jam_track)
@jam_track_unavailable = FactoryGirl.create(:jam_track, :status=>'Staging')
controller.current_user = @user
end
describe "admin" do
before(:each) do
@admin = FactoryGirl.create(:admin)
controller.current_user = @admin
end
it "can see unavailable" do
get :index
response.should be_success
json = JSON.parse(response.body)
json["next"].should be_nil
json["jamtracks"].length.should == 2
# Create another unavailable track and see:
jam_track2 = FactoryGirl.create(:jam_track, :status => 'Staging')
get :index
response.should be_success
json = JSON.parse(response.body)
json["next"].should be_nil
json["jamtracks"].length.should == 3
end
end # describe "admin"
describe "download functionality" do
it "lists available tracks" do
get :index
response.should be_success
json = JSON.parse(response.body)
json["next"].should be_nil
json["jamtracks"].length.should == 1
jam_track2 = FactoryGirl.create(:jam_track)
get :index
response.should be_success
json = JSON.parse(response.body)
json["next"].should be_nil
json["jamtracks"].length.should == 2
end
it "lists owned tracks" do
get :downloads
response.should be_success
json = JSON.parse(response.body)
json['downloads'].should have(0).items
right = JamTrackRight.create(:user=>@user, :jam_track=>@jam_track)
get :downloads
response.should be_success
json = JSON.parse(response.body)
json['downloads'].should have(1).items
end
it "finds a download" do
#get "/download/#{right.id}/"
right = JamTrackRight.create(:user=>@user, :jam_track=>@jam_track)
get :download, { :format=>'json', :id=>@jam_track.id }
response.should be_success
response.status.should == 202
response.body.should =~ /not available.*/
end
2015-01-09 02:35:39 +00:00
end
2015-01-09 02:35:39 +00:00
describe "purchased" do
it "can return empty" do
get :purchased
response.should be_success
json = JSON.parse(response.body)
json['jamtracks'].length.should eq(0)
json['next'].should eq(nil)
end
it "can return single item" do
# purchase the item for the user
right = FactoryGirl.create(:jam_track_right, jam_track: @jam_track, user: @user)
get :purchased
response.should be_success
json = JSON.parse(response.body)
json['jamtracks'].length.should eq(1)
2015-01-27 03:22:44 +00:00
json['jamtracks'][0]['jam_track_right_id'].should eq(right.id)
2015-01-09 02:35:39 +00:00
json['next'].should be_nil
end
end
describe "with a JamTrack" do
before(:each) do
JamTrackRight.destroy_all
# Create a working JamTrack for these tests. The integrity
# of this process is checked in other tests:
@ogg_path = File.join('spec', 'files', 'on.ogg')
@jam_track = FactoryGirl.create(:jam_track) #jam_track_track.jam_track
jam_track_track = @jam_track.jam_track_tracks.first
# 48 kHz:
2015-02-26 23:00:53 +00:00
uploader = JamTrackTrackUploader.new(jam_track_track, :url_48)
uploader.store!(File.open(@ogg_path, 'rb'))
# 44 kHz:
uploader = JamTrackTrackUploader.new(jam_track_track, :url_44)
uploader.store!(File.open(File.join('spec', 'files', 'off.ogg'), 'rb'))
#jam_track_track.url.store!(File.open(ogg_path, "rb"))
jam_track_track.save!
jam_track_track.reload
ResqueSpec.reset!
end
it "download depends on rights" do
get :download, :id=>@jam_track.id
response.status.should == 403
right = JamTrackRight.create(:user=>@user, :jam_track=>@jam_track)
get :download, :id=>@jam_track.id
response.status.should == 202
right.download_count.should eq(0)
2015-01-05 21:19:20 +00:00
right.private_key.should be_nil
qname = "#{ResqueSpec.queue_name(JamRuby::JamTracksBuilder)}"
#puts "ResqueSpec.peek(qname)#{ResqueSpec.peek(qname)}"
JamTracksBuilder.should have_queued(right.id,nil).in(:jam_tracks_builder)
expect(ResqueSpec.peek(qname).present?).to eq(true)
ResqueSpec.perform_next(qname)
JamTracksBuilder.should_not have_queued(right.id,nil).in(:jam_tracks_builder)
right.reload
right.private_key.should_not be_nil
right.download_count.should eq(0)
get :download, :id=>@jam_track.id
response.status.should == 302
response.location.should =~ /.*#{Regexp.escape(right.filename)}.*/
right.reload
right.download_count.should eq(1)
notifications = Notification.where(:jam_track_right_id=>right.id)
notifications.count.should == 1
end
it "supports multiple bitrates" do
2015-03-09 14:44:12 +00:00
get :download, :id=>@jam_track.id, :sample_rate=>44
response.status.should == 403
right = JamTrackRight.create(:user=>@user, :jam_track=>@jam_track)
2015-03-09 14:44:12 +00:00
get :download, :id=>@jam_track.id, :sample_rate=>44
response.status.should == 202
right.download_count.should eq(0)
right.private_key.should be_nil
qname = "#{ResqueSpec.queue_name(JamRuby::JamTracksBuilder)}"
#puts "ResqueSpec.peek(qname)#{ResqueSpec.peek(qname)}"
2015-03-09 14:44:12 +00:00
JamTracksBuilder.should have_queued(right.id,44).in(:jam_tracks_builder)
expect(ResqueSpec.peek(qname).present?).to eq(true)
ResqueSpec.perform_next(qname)
2015-03-09 14:44:12 +00:00
JamTracksBuilder.should_not have_queued(right.id, 44).in(:jam_tracks_builder)
right.reload
2015-01-05 21:19:20 +00:00
right.private_key.should_not be_nil
right.download_count.should eq(0)
2015-03-09 14:44:12 +00:00
get :download, :id=>@jam_track.id, :sample_rate=>44
response.status.should == 302
response.location.should =~ /.*#{Regexp.escape(right.filename)}.*/
right.reload
right.download_count.should eq(1)
notifications = Notification.where(:jam_track_right_id=>right.id)
notifications.count.should == 1
end
end
2015-01-05 22:53:43 +00:00
describe "keys" do
it "empty" do
2015-01-23 20:30:52 +00:00
get :keys, jamtracks: {tracks: []}
2015-01-05 22:53:43 +00:00
response.status.should == 200
json = JSON.parse(response.body)
json.length == 0
end
it "track with no rights" do
2015-01-23 20:30:52 +00:00
get :keys, jamtracks: { tracks: [@jam_track.id] }
2015-01-05 22:53:43 +00:00
response.status.should == 200
json = JSON.parse(response.body)
json.length.should == 1
2015-01-23 20:30:52 +00:00
json[0]['id'].should == @jam_track.id.to_s
2015-01-05 22:53:43 +00:00
json[0]['private'].should be_nil
json[0]['error'].should == 'not_purchased'
2015-01-05 22:53:43 +00:00
end
it "track with no key" do
right = FactoryGirl.create(:jam_track_right, user: @user, private_key: nil, jam_track: @jam_track)
2015-01-23 20:30:52 +00:00
get :keys, jamtracks: { tracks: [@jam_track.id] }
2015-01-05 22:53:43 +00:00
response.status.should == 200
json = JSON.parse(response.body)
json.length.should == 1
2015-01-23 20:30:52 +00:00
json[0]['id'].should == @jam_track.id.to_s
2015-01-05 22:53:43 +00:00
json[0]['private'].should be_nil
json[0]['error'].should == 'no_key'
end
it "track with key" do
right = FactoryGirl.create(:jam_track_right, user: @user, private_key: 'abc', jam_track: @jam_track)
2015-01-23 20:30:52 +00:00
get :keys, jamtracks: { tracks: [@jam_track.id] }
2015-01-05 22:53:43 +00:00
response.status.should == 200
json = JSON.parse(response.body)
json.length.should == 1
2015-01-23 20:30:52 +00:00
json[0]['id'].should == @jam_track.id.to_s
2015-01-05 22:53:43 +00:00
json[0]['private'].should eq('abc')
json[0]['error'].should be_nil
end
it "non-owning user asking for a real track" do
right = FactoryGirl.create(:jam_track_right, user: FactoryGirl.create(:user), private_key: 'abc', jam_track: @jam_track)
2015-01-23 20:30:52 +00:00
get :keys, jamtracks: { tracks: [@jam_track.id] }
2015-01-05 22:53:43 +00:00
response.status.should == 200
json = JSON.parse(response.body)
2015-01-23 20:30:52 +00:00
json[0]['id'].should == @jam_track.id.to_s
2015-01-05 22:53:43 +00:00
json[0]['private'].should be_nil
json[0]['error'].should == 'not_purchased'
end
end
describe "enqueue" do
it "success" do
right = FactoryGirl.create(:jam_track_right, user: @user, signed: false)
right.signing_queued_at.should be_nil
post :enqueue, {:format=>'json', :id=>right.jam_track.id}
response.should be_success
right.reload
right.signing_queued_at.should_not be_nil
end
end
describe "show_jam_track_right" do
it "success" do
right = FactoryGirl.create(:jam_track_right, user: @user)
get :show_jam_track_right, {:id=>right.jam_track.id}
response.should be_success
json = JSON.parse(response.body)
json['signing_state'].should eq('QUIET')
json['error_count'].should eq(0)
end
end
end