public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Search Repo:
Michael Hartl (author)
Fri May 16 12:30:40 -0700 2008
commit  e1fd8b8e440c9f3ab34161d4e87de78e956c1012
tree    bfe11afff13499c63ff4e63347234d93fa20d275
parent  e58dfbc1ddbfd2327ee4031c4b8a0f29061e4107
insoshi / spec / controllers / photos_controller_spec.rb
100644 102 lines (85 sloc) 3.201 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require File.dirname(__FILE__) + '/../spec_helper'
 
describe PhotosController do
 
  describe "when not logged in" do
      
    it "should protect the index page" do
      get :index
      response.should redirect_to(login_url)
    end
  end
 
  describe "when logged in" do
    integrate_views
    
    before(:each) do
      @person = login_as(:quentin)
      @primary, @secondary = [mock_photo(:primary => true), mock_photo]
      photos = [@primary, @secondary]
      photos.each { |p| p.stub!(:person).and_return(@person) }
      @photo = @primary
      @person.stub!(:photos).and_return([@primary, @secondary])
    end
  
    it "should have an index page" do
      get :index
      response.should be_success
      response.should render_template("index")
    end
    
    it "should have a new photo page" do
      get :new
      response.should be_success
      response.should render_template("new")
    end
 
    it "should have an edit photo page" do
      Photo.should_receive(:find).and_return(@photo)
      get :edit, :id => @photo
      response.should be_success
      response.should render_template("edit")
    end
    
    it "should create photo" do
      image = uploaded_file("rails.png")
      num_thumbnails = 3
      lambda do
        post :create, :photo => { :uploaded_data => image }
      end.should change(Photo, :count).by(num_thumbnails + 1)
    end
    
    it "should handle empty photo upload" do
      lambda do
        post :create, :photo => { :uploaded_data => nil }
        response.should render_template("new")
      end.should_not change(Photo, :count)
    end
    
    it "should handle cancellation and doesn't report about problem" do
      post :create, :commit => "Cancel"
      response.should redirect_to(edit_person_url(@person))
      flash[:error].should be_nil
    end
    
    it "should handle nil photo parameter" do
      post :create, :photo => nil
      response.should redirect_to(edit_person_url(@person))
      flash[:error].should_not be_nil
    end
    
    it "should mark a photo as primary" do
      # We check that the secondary photo is made primary, while the old
      # primary photo is marked non-primary.
      Photo.should_receive(:find).and_return(@secondary)
      @secondary.should_receive(:update_attributes).with(:primary => true).
        and_return(true)
      @primary.should_receive(:update_attributes!).with(:primary => false)
      put :update, :photo => @secondary
    end
    
    it "should destroy a photo" do
      Photo.should_receive(:find).and_return(@secondary)
      @secondary.should_receive(:destroy).and_return(true)
      delete :destroy, :id => @secondary
    end
    
    it "should mark another photo as primary if destroying primary" do
      Photo.should_receive(:find).and_return(@primary)
      @primary.should_receive(:destroy).and_return(true)
      @secondary.should_receive(:update_attributes!).with(:primary => true)
      delete :destroy, :id => @primary
    end
    
    it "should require the correct user to edit" do
      login_as :aaron
      Photo.should_receive(:find).and_return(@photo)
      get :edit, :id => @photo
      response.should redirect_to(home_url)
    end
  end
end