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

139 lines
3.7 KiB
Ruby

require 'spec_helper'
require 'carrierwave/test/matchers'
describe JamTrack do
include CarrierWave::Test::Matchers
include UsesTempFiles
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 "validations" do
describe "bpm" do
it "1" do
FactoryGirl.build(:jam_track, bpm: 1).valid?.should be_true
end
it "100" do
FactoryGirl.build(:jam_track, bpm: 100).valid?.should be_true
end
it "100.1" do
FactoryGirl.build(:jam_track, bpm: 100.1).valid?.should be_true
end
it "100.12" do
jam_track = FactoryGirl.build(:jam_track, bpm: 100.12)
jam_track.valid?.should be_false
jam_track.errors[:bpm].should == ['is invalid']
end
end
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