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

89 lines
3.1 KiB
Ruby

require 'spec_helper'
describe ApiJamTracksController do
include CarrierWave::Test::Matchers
before(:all) do
original_storage = JamTrackTrackUploader.storage = :fog
original_storage = JamTrackRightUploader.storage = :fog
end
after(:all) do
JamTrackTrackUploader.storage = @original_storage
JamTrackRightUploader.storage = @original_storage
end
before(:each) do
@user = FactoryGirl.create(:user)
@jam_track = FactoryGirl.create(:jam_track)
controller.current_user = @user
end
describe "download" do
it "list download" do
right = JamTrackRight.create(:user=>@user, :jam_track=>@jam_track)
get :list_downloads
response.should be_success
json = JSON.parse(response.body)
json['downloads'].should have(1).items
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
uploader = JamTrackTrackUploader.new(jam_track_track, :url)
uploader.store!(File.open(@ogg_path, '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
s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)
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)
JamTracksBuilder.should have_queued(right.id).in(:jam_tracks_builder)
qname = "#{ResqueSpec.queue_name(JamRuby::JamTracksBuilder)}"
expect(ResqueSpec.peek(qname).present?).to eq(true)
ResqueSpec.perform_next(qname)
JamTracksBuilder.should_not have_queued(right.id).in(:jam_tracks_builder)
right.reload
right.download_count.should eq(0)
get :download, :id => @jam_track.id
response.status.should == 302
response.location.should =~ /.*#{Regexp.escape(right.filename)}.*/
#response.should redirect_to(/.*#{Regexp.escape(right.filename)}.*/)
#response.should redirect_to("%r{.*#{Regexp.escape(right.filename)}.*}")
#right.reload
#s3.exists?(response.location).should be_true
#puts "s3.length (response.location): #{s3.length (response.location)}"
#s3.length (response.location).should > File.size?(@ogg_path)
right.reload
right.download_count.should eq(1)
notifications = Notification.where(:jam_track_right_id => right.id)
notifications.count.should == 1
puts "notifications.first.inspect: #{notifications.first.inspect}"
end
end
end