aboutsummaryrefslogtreecommitdiffstats
path: root/spec/controllers/comment_controller_spec.rb
blob: 93752537c611757882ab99322484c2131c85e7d1 (plain)
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
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe CommentController, "when commenting on a request" do
    integrate_views

    it "should give an error and render 'new' template when body text is just some whitespace" do
        post :new, :url_title => info_requests(:naughty_chicken_request).url_title,
            :comment => { :body => "   " },
            :type => 'request', :submitted_comment => 1, :preview => 1
        assigns[:comment].errors[:body].should_not be_nil
        response.should render_template('new')
    end

    it "should show preview when input is good" do
        post :new, :url_title => info_requests(:naughty_chicken_request).url_title,
            :comment => { :body => "A good question, but why not also ask about nice chickens?" },
            :type => 'request', :submitted_comment => 1, :preview => 1
        response.should render_template('preview')
    end

    it "should redirect to sign in page when input is good and nobody is logged in" do
        params = { :url_title => info_requests(:naughty_chicken_request).url_title,
            :comment => { :body => "A good question, but why not also ask about nice chickens?" },
            :type => 'request', :submitted_comment => 1, :preview => 0
        }
        post :new, params
        post_redirect = PostRedirect.get_last_post_redirect
        response.should redirect_to(:controller => 'user', :action => 'signin', :token => post_redirect.token)
        # post_redirect.post_params.should == params # XXX get this working. there's a : vs '' problem amongst others
    end

    it "should create the comment, and redirect to request page when input is good and somebody is logged in" do
        session[:user_id] = users(:bob_smith_user).id
        post :new, :url_title => info_requests(:naughty_chicken_request).url_title,
            :comment => { :body => "A good question, but why not also ask about nice chickens?" },
            :type => 'request', :submitted_comment => 1, :preview => 0

        comment_array = Comment.find(:all, :conditions => ["body = ?", "A good question, but why not also ask about nice chickens?"])
        comment_array.size.should == 1
        comment = comment_array[0]

        ActionMailer::Base.deliveries.size.should == 0

        response.should redirect_to(:controller => 'request', :action => 'show', :url_title => info_requests(:naughty_chicken_request).url_title)
    end

    it "should give an error if the same request is submitted twice" do
        session[:user_id] = users(:silly_name_user).id

        post :new, :url_title => info_requests(:fancy_dog_request).url_title,
            :comment => { :body => comments(:silly_comment).body },
            :type => 'request', :submitted_comment => 1, :preview => 0

        response.should render_template('new')
    end

end