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
insoshi / spec / controllers / posts_controller_spec.rb
100644 157 lines (135 sloc) 5.061 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require File.dirname(__FILE__) + '/../spec_helper'
 
describe PostsController do
 
  describe "forum posts" do
    integrate_views
  
    before(:each) do
      @person = login_as(:quentin)
      @forum = forums(:one)
      @topic = topics(:one)
      @post = posts(:forum)
    end
    
    it "should have working pages" do
      with_options :forum_id => @forum, :topic_id => @topic do |page|
        page.get :index
        page.get :new
        page.get :edit, :id => @post
        page.post :create, :post => { }
        page.put :update, :id => @post
        page.delete :destroy, :id => @post
      end
    end
 
    it "should create a forum post" do
      lambda do
        post :create, :forum_id => @forum, :topic_id => @topic,
                      :post => { :body => "The body" }
        topics = forum_topic_url(@forum, @topic, :posts => 2)
        response.should redirect_to(topics)
      end.should change(ForumPost, :count).by(1)
    end
  
    it "should associate a person to a post" do
      with_options :forum_id => @forum, :topic_id => @topic do |page|
        page.post :create, :post => { :body => "The body" }
        assigns(:post).person.should == @person
      end
    end
    
    it "should render the new template on creation failure" do
      post :create, :forum_id => @forum, :topic_id => @topic,
                    :post => { :body => "" }
      response.should render_template("forum_new")
    end
    
    it "should require the right user for editing" do
      person = login_as(:aaron)
      @post.person.should_not == person
      get :edit, :forum_id => @forum, :topic_id => @topic, :id => @post
      response.should redirect_to(home_url)
    end
    
    it "should allow admins to destroy posts" do
      admin!(@person)
      @person.should be_admin
      lambda do
        delete :destroy, :forum_id => @forum, :topic_id => @topic,
                         :id => @post
        response.should redirect_to(forum_topic_url(@forum, @topic))
      end.should change(ForumPost, :count).by(-1)
    end
    
    it "should not allow non-admins to destroy posts" do
      login_as :aaron
      delete :destroy, :forum_id => @forum, :topic_id => @topic,
                       :id => @post
      response.should redirect_to(home_url)
    end
  end
  
  describe "blog posts" do
    integrate_views
  
    before(:each) do
      @person = login_as(:quentin)
      @blog = @person.blog
      @post = posts(:blog_post)
    end
  
    it "should have working pages" do
      with_options :blog_id => @blog do |page|
        page.get :index
        page.get :new
        page.get :show, :id => @post
        page.get :edit, :id => @post
        page.post :create, :post => { }
        page.put :update, :id => @post
        page.delete :destroy, :id => @post
      end
    end
    
    it "should create a blog post" do
      lambda do
        post :create, :blog_id => @blog,
                      :post => { :title => "The post", :body => "The body" }
        response.should redirect_to(blog_post_url(@blog, assigns(:post)))
      end.should change(BlogPost, :count).by(1)
    end
    
    it "should require the right user to show a blog post" do
      person = login_as(:aaron)
      aarons_blog = person.blog
      quentins_post = @post
      get :show, :blog_id => aarons_blog, :id => quentins_post
      response.should be_redirect
    end
    
    it "should require the right user to create a blog post" do
      login_as :aaron
      post :create, :blog_id => @blog,
                    :post => { :title => "The post", :body => "The body" }
      response.should be_redirect
    end
    
    it "should create the right blog post associations" do
      lambda do
        post :create, :blog_id => @blog,
                      :post => { :title => "The post", :body => "The body" }
        assigns(:post).blog.should == @blog
      end
    end
    
    it "should render the new template on creation failure" do
      post :create, :blog_id => @blog, :post => {}
      response.should render_template("blog_new")
    end
    
    it "should require the right user for editing" do
      person = login_as(:aaron)
      @post.blog.person.should_not == person
      get :edit, :blog_id => @blog, :id => @post
      response.should redirect_to(home_url)
    end
    
    it "should require the post being edited to belong to the blog" do
      wrong_blog = blogs(:two)
      wrong_blog.should_not == @blog
      get :edit, :blog_id => wrong_blog, :id => @post
      response.should redirect_to(home_url)
    end
    
    it "should destroy a post" do
      delete :destroy, :blog_id => @blog, :id => @post
      @post.should_not exist_in_database
      response.should redirect_to(blog_url(@blog))
    end
    
    it "should require the right user for destroying" do
      person = login_as(:aaron)
      @post.blog.person.should_not == person
      delete :destroy, :blog_id => @blog, :id => @post
      response.should redirect_to(home_url)
    end
  end
end