jam-cloud/ruby/spec/jam_ruby/models/jam_track_spec.rb

161 lines
4.4 KiB
Ruby

require 'spec_helper'
require 'carrierwave/test/matchers'
describe JamTrack do
include CarrierWave::Test::Matchers
include UsesTempFiles
let(:user) {FactoryGirl.create(:user)}
it "created" do
jam_track = FactoryGirl.create(:jam_track)
jam_track.licensor.should_not be_nil
jam_track.licensor.jam_tracks.should == [jam_track]
end
describe "index" do
it "empty query" do
query, pager = JamTrack.index({}, user)
query.size.should == 0
end
it "sorts by name" do
jam_track1 = FactoryGirl.create(:jam_track_with_tracks, name: 'a')
jam_track2 = FactoryGirl.create(:jam_track_with_tracks, name: 'b')
query, pager = JamTrack.index({}, user)
query.size.should == 2
query[0].should eq(jam_track1)
query[1].should eq(jam_track2)
# flip order by mucking with name
jam_track1.name = 'c'
jam_track1.save!
query, pager = JamTrack.index({}, user)
query[0].should eq(jam_track2)
query[1].should eq(jam_track1)
end
it "supports showing purchased only" do
jam_track1 = FactoryGirl.create(:jam_track_with_tracks, name: 'a')
# no results yet
query, pager = JamTrack.index({show_purchased_only:true}, user)
query.size.should == 0
# but after the user buys it, it is returned
FactoryGirl.create(:jam_track_right, jam_track: jam_track1, user: user)
query, pager = JamTrack.index({show_purchased_only:true}, user)
query.size.should == 1
query[0].should eq(jam_track1)
end
end
describe "validations" do
describe "price" do
it "0.99" do
FactoryGirl.build(:jam_track, price: 0.99).valid?.should be_true
end
it "1" do
FactoryGirl.build(:jam_track, price: 1).valid?.should be_true
end
it "100" do
FactoryGirl.build(:jam_track, price: 100).valid?.should be_true
end
it "100.1" do
FactoryGirl.build(:jam_track, price: 100.1).valid?.should be_true
end
it "100.12" do
FactoryGirl.build(:jam_track, price: 100.12).valid?.should be_true
end
it "100.123" do
jam_track = FactoryGirl.build(:jam_track, price: 100.123)
jam_track.valid?.should be_false
jam_track.errors[:price].should == ['is invalid']
end
end
describe "reproduction_royalty_amount" do
it "0.99" do
FactoryGirl.build(:jam_track, reproduction_royalty_amount: 0.99).valid?.should be_true
end
it "1" do
FactoryGirl.build(:jam_track, reproduction_royalty_amount: 1).valid?.should be_true
end
it "100" do
FactoryGirl.build(:jam_track, reproduction_royalty_amount: 100).valid?.should be_true
end
it "100.1" do
FactoryGirl.build(:jam_track, reproduction_royalty_amount: 100.1).valid?.should be_true
end
it "100.12" do
FactoryGirl.build(:jam_track, reproduction_royalty_amount: 100.12).valid?.should be_true
end
it "100.123" do
FactoryGirl.build(:jam_track, reproduction_royalty_amount: 100.123).valid?.should be_true
end
it "100.1234" do
jam_track = FactoryGirl.build(:jam_track, reproduction_royalty_amount: 100.1234)
jam_track.valid?.should be_false
jam_track.errors[:reproduction_royalty_amount].should == ['is invalid']
end
end
end
describe "upload/download" do
JKA_NAME = 'blah.jkz'
in_directory_with_file(JKA_NAME)
before(:all) do
original_storage = JamTrackUploader.storage = :fog
end
after(:all) do
JamTrackUploader.storage = @original_storage
end
before(:each) do
content_for_file('abc')
end
it "uploads to s3 with correct name, and then downloads via signed URL" do
jam_track = FactoryGirl.create(:jam_track)
uploader = JamTrackUploader.new(jam_track, :url)
uploader.store!(File.open(JKA_NAME)) # uploads file
jam_track.save!
# verify that the uploader stores the correct path
jam_track[:url].should == jam_track.store_dir + '/' + jam_track.filename
# verify it's on S3
s3 = S3Manager.new(APP_CONFIG.aws_bucket, APP_CONFIG.aws_access_key_id, APP_CONFIG.aws_secret_access_key)
s3.exists?(jam_track[:url]).should be_true
s3.length(jam_track[:url]).should == 'abc'.length
# download it via signed URL, and check contents
url = jam_track.sign_url
downloaded_contents = open(url).read
downloaded_contents.should == 'abc'
end
end
end