mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-02-06 15:41:09 -05:00
the tests generated by 'rails g scaffold...' are boring and brittle. they don't actually test anything other than the rails framework, and they were causing us all kinds of trouble. we've started to blow them away (and raised a PT chore to remove them from other controllers in due course).
118 lines
3.9 KiB
Ruby
118 lines
3.9 KiB
Ruby
require 'spec_helper'
|
|
|
|
describe PhotosController do
|
|
|
|
login_member
|
|
|
|
|
|
def valid_attributes
|
|
member = FactoryGirl.create(:member)
|
|
{
|
|
"owner_id" => member.id,
|
|
"flickr_photo_id" => 1,
|
|
"title" => "Photo",
|
|
"license_name" => "CC-BY",
|
|
"thumbnail_url" => 'http://example.com/thumb.jpg',
|
|
"fullsize_url" => 'http://example.com/full.jpg',
|
|
"link_url" => 'http://example.com'
|
|
}
|
|
end
|
|
|
|
def valid_session
|
|
{}
|
|
end
|
|
|
|
describe "GET new" do
|
|
it "assigns the flickr auth as @flickr_auth" do
|
|
@member = FactoryGirl.create(:member)
|
|
sign_in @member
|
|
@member.stub(:flickr_photos) { [] }
|
|
controller.stub(:current_member) { @member }
|
|
@auth = FactoryGirl.create(:flickr_authentication, :member => @member)
|
|
get :new, {}
|
|
assigns(:flickr_auth).should be_an_instance_of(Authentication)
|
|
end
|
|
|
|
it "assigns a planting id" do
|
|
get :new, { :planting_id => 5 }
|
|
assigns(:planting_id).should eq "5"
|
|
end
|
|
|
|
end
|
|
|
|
describe "POST create" do
|
|
before(:each) do
|
|
Photo.any_instance.stub(:flickr_metadata).and_return( {
|
|
:title => "A Heartbreaking work of staggering genius",
|
|
:license_name => "CC-BY",
|
|
:license_url => "http://example.com/aybpl",
|
|
:thumbnail_url => "http://example.com/thumb.jpg",
|
|
:fullsize_url => "http://example.com/full.jpg",
|
|
:link_url => "http://example.com"
|
|
})
|
|
end
|
|
|
|
describe "with valid params" do
|
|
|
|
it "attaches the photo to a planting" do
|
|
member = FactoryGirl.create(:member)
|
|
controller.stub(:current_member) { member }
|
|
garden = FactoryGirl.create(:garden, :owner => member)
|
|
planting = FactoryGirl.create(:planting, :garden => garden)
|
|
photo = FactoryGirl.create(:photo, :owner => member)
|
|
post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id },
|
|
:planting_id => planting.id }
|
|
Photo.last.plantings.first.should eq planting
|
|
end
|
|
|
|
it "doesn't attach a photo to a planting twice" do
|
|
member = FactoryGirl.create(:member)
|
|
controller.stub(:current_member) { member }
|
|
garden = FactoryGirl.create(:garden, :owner => member)
|
|
planting = FactoryGirl.create(:planting, :garden => garden)
|
|
photo = FactoryGirl.create(:photo, :owner => member)
|
|
post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id },
|
|
:planting_id => planting.id }
|
|
post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id },
|
|
:planting_id => planting.id }
|
|
Photo.last.plantings.count.should eq 1
|
|
end
|
|
end
|
|
|
|
describe "for the second time" do
|
|
it "does not add a photo twice" do
|
|
expect {
|
|
post :create, {:photo => { :flickr_photo_id => 1 } }
|
|
}.to change(Photo, :count).by(1)
|
|
expect {
|
|
post :create, {:photo => { :flickr_photo_id => 1 } }
|
|
}.to change(Photo, :count).by(0)
|
|
end
|
|
end
|
|
|
|
describe "with matching owners" do
|
|
it "creates the planting/photo link" do
|
|
member = FactoryGirl.create(:member)
|
|
controller.stub(:current_member) { member }
|
|
garden = FactoryGirl.create(:garden, :owner => member)
|
|
planting = FactoryGirl.create(:planting, :garden => garden)
|
|
photo = FactoryGirl.create(:photo, :owner => member)
|
|
post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id },
|
|
:planting_id => planting.id }
|
|
Photo.last.plantings.first.should eq planting
|
|
end
|
|
end
|
|
|
|
describe "with mismatched owners" do
|
|
it "creates the planting/photo link" do
|
|
# members will be auto-created, and different
|
|
planting = FactoryGirl.create(:planting)
|
|
photo = FactoryGirl.create(:photo)
|
|
post :create, {:photo => { :flickr_photo_id => photo.flickr_photo_id },
|
|
:planting_id => planting.id }
|
|
Photo.last.plantings.first.should_not eq planting
|
|
end
|
|
end
|
|
end
|
|
end
|