require 'spec_helper' describe Band do let(:user) { FactoryGirl.create(:user) } let(:user2) { FactoryGirl.create(:user) } let(:fan) { FactoryGirl.create(:fan) } let(:band) { FactoryGirl.create(:band) } let(:band_in_austin) { FactoryGirl.create(:band, country: 'US', state: 'TX', city: 'Austin')} let(:new_band) { FactoryGirl.build(:band) } let(:band_params) { { name: "The Band", biography: "Biography", city: 'Austin', state: 'TX', country: 'US', genres: ['country'] } } describe 'website update' do it 'should have http prefix on website url' do band.website = 'example.com' band.save! expect(band.website).to match(/^http:\/\/example.com$/) end end describe 'band validations' do it "minimum genres" do new_band.save.should be_false new_band.errors[:genres].should == [ValidationMessages::BAND_GENRE_MINIMUM_NOT_MET] end it "maximum genres" do new_band.genres = Genre.limit(4) new_band.save.should be_false new_band.errors[:genres].should == [ValidationMessages::BAND_GENRE_LIMIT_EXCEEDED] end end describe "save" do it "can succeed" do band = Band.save(user, band_params) band.errors.any?.should be_false band.name.should == band_params[:name] band.biography.should == band_params[:biography] band.genres.should == [Genre.find(band_params[:genres][0])] band.city.should == band_params[:city] band.state.should == band_params[:state] band.country.should == band_params[:country] end it "ensures user is a musician" do expect{ Band.save(fan, band_params) }.to raise_error("must be a musician") end it "can update" do band = Band.save(user, band_params) band.errors.any?.should be_false band_params[:id] = band.id band_params[:name] = "changed name" band = Band.save(user, band_params) band.errors.any?.should be_false Band.find(band.id).name.should == band_params[:name] end it "stops non-members from updating" do band = Band.save(user, band_params) band.errors.any?.should be_false band_params[:id] = band.id band_params[:name] = "changed name" expect{ Band.save(user2, band_params) }.to raise_error(ValidationMessages::USER_NOT_BAND_MEMBER_VALIDATION_ERROR) end end describe "validate" do it "can pass" do band = Band.build_band(user, band_params) band.valid?.should be_true end it "can fail" do band_params[:name] = nil band = Band.build_band(user, band_params) band.valid?.should be_false band.errors[:name].should == ["can't be blank"] end end describe "after_maxmind_import" do before(:each) do create_phony_database end after(:all) do create_phony_database end it "updates to non-null after import if matching location is available" do band_in_austin.lat.should be_nil band_in_austin.lng.should be_nil Band.after_maxmind_import(false) band_in_austin.reload band_in_austin.lat.should == 30.2076 band_in_austin.lng.should == -97.8587 end it "updates to non-null after import if matching location is available, with two matching geoip locations" do band_in_austin.lat.should be_nil band_in_austin.lng.should be_nil # change the dallas entry to austin, to make two austin entries GeoIpLocations.connection.execute("UPDATE geoiplocations SET city = 'Austin', region = 'TX', countrycode ='US' WHERE city = 'Dallas' AND region = 'TX' and countrycode = 'US'").check Band.after_maxmind_import(false) band_in_austin.reload # you don't know which GeoIpLocation it'll be. So we need to check both [30.2076, 32.7825].include?(band_in_austin.lat).should be_true [-97.8587, -96.8207].include?(band_in_austin.lng).should be_true end it "updates to null if no matching location available" do band_in_austin.city = 'Blibbity' band_in_austin.save! # change the dallas entry to austin, to make two austin entries GeoIpLocations.connection.execute("UPDATE geoiplocations SET city = 'Austin', region = 'TX', countrycode ='US' WHERE city = 'Dallas' AND region = 'TX' and countrycode = 'US'").check Band.after_maxmind_import(false) band_in_austin.reload band_in_austin.lat.should be_nil band_in_austin.lng.should be_nil end end describe "recent history" do it "should only retrieve recordings with a claimed recording" do user = FactoryGirl.create(:user) band = FactoryGirl.create(:band) band.users << user band.save! claimed_recording = FactoryGirl.create(:claimed_recording, :user => user) claimed_recording.recording.owner = user claimed_recording.recording.band = band claimed_recording.recording.save! recording = FactoryGirl.create(:recording, :owner => user, :band => band) Recording.where(:owner_id => user.id).size.should == 2 Recording.where(:band_id => band.id).size.should == 2 history = band.recent_history(nil, nil) history.size.should == 1 history.first.id.should == claimed_recording.recording.id # exclude the claimed recording history = band.recent_history(nil, claimed_recording.id) history.size.should == 0 end end end