235 lines
7.6 KiB
Ruby
235 lines
7.6 KiB
Ruby
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
|
|
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, :available=>false)
|
|
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, :available=>false)
|
|
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
|
|
end
|
|
|
|
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)
|
|
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
|
|
|
|
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)
|
|
right.private_key.should be_nil
|
|
|
|
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.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
|
|
end
|
|
|
|
describe "keys" do
|
|
it "empty" do
|
|
get :keys, jamtracks: {tracks: []}
|
|
response.status.should == 200
|
|
json = JSON.parse(response.body)
|
|
json.length == 0
|
|
end
|
|
|
|
it "track with no rights" do
|
|
get :keys, jamtracks: { tracks: [@jam_track.id] }
|
|
response.status.should == 200
|
|
json = JSON.parse(response.body)
|
|
json.length.should == 1
|
|
json[0]['id'].should == @jam_track.id.to_s
|
|
json[0]['private'].should be_nil
|
|
json[0]['error'].should == 'not_purchased'
|
|
end
|
|
|
|
it "track with no key" do
|
|
|
|
right = FactoryGirl.create(:jam_track_right, user: @user, private_key: nil, jam_track: @jam_track)
|
|
|
|
get :keys, jamtracks: { tracks: [@jam_track.id] }
|
|
response.status.should == 200
|
|
json = JSON.parse(response.body)
|
|
json.length.should == 1
|
|
json[0]['id'].should == @jam_track.id.to_s
|
|
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)
|
|
get :keys, jamtracks: { tracks: [@jam_track.id] }
|
|
response.status.should == 200
|
|
json = JSON.parse(response.body)
|
|
json.length.should == 1
|
|
json[0]['id'].should == @jam_track.id.to_s
|
|
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)
|
|
get :keys, jamtracks: { tracks: [@jam_track.id] }
|
|
response.status.should == 200
|
|
json = JSON.parse(response.body)
|
|
json[0]['id'].should == @jam_track.id.to_s
|
|
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
|