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:
insoshi / spec / controllers / messages_controller_spec.rb
100644 146 lines (120 sloc) 4.438 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
require File.dirname(__FILE__) + '/../spec_helper'
 
describe MessagesController do
 
  before(:each) do
    @emails = ActionMailer::Base.deliveries
    @emails.clear
 
    @person = login_as(:quentin)
    @other_person = people(:aaron)
    @message = @person.received_messages.first
  end
 
  describe "pages" do
    integrate_views
    
    it "should have a working index" do
      working_page(:index, :received_messages)
    end
 
    it "should have working sent messages" do
      working_page(:sent, :sent_messages)
    end
 
    it "should have working trashed messages" do
      working_page(:trash, :trashed_messages)
    end
    
    it "should have a working show page" do
      get :show, :id => @message
      response.should be_success
      response.should render_template("show")
    end
    
    it "should have a working new page" do
      get :new, :person_id => @person
      response.should be_success
      response.should render_template("new")
    end
    
    it "should have a working reply page" do
      proper_reply_behavior(@message.recipient)
    end
    
    it "should have a working reply page for the recipient" do
      # This spec tests replying to your *own* message, as at
      # the bottom of a thread.
      proper_reply_behavior(@message.sender)
    end
 
    it "should reply correctly when logged in as the sender" do
      login_as @message.sender
      get :reply, :id => @message
      assigns(:recipient).should == @message.recipient
    end
    
    it "should allow create cancellation" do
      post :create, :commit => "Cancel"
      response.should redirect_to(messages_url)
    end
    
    it "should handle invalid reply creation" do
      login_as(:kelly)
      post :create, :parent_id => @message, :person_id => @person
      response.should redirect_to(home_url)
    end
    
    it "should create a message" do
      lambda do
        post :create, :message => { :subject => "The subject",
                                    :content => "Hey there!" },
                      :person_id => @other_person
      end.should change(Message, :count).by(1)
    end
    
    it "should send a message receipt email" do
      lambda do
        post :create, :message => { :subject => "The subject",
                                    :content => "Hey there!" },
                      :person_id => @other_person
      end.should change(@emails, :length).by(1)
    end
    
    it "should handle replies as recipient" do
      handle_replies(@message, @message.recipient, @message.sender)
    end
    
    it "should handle replies as sender" do
      handle_replies(@message, @message.sender, @message.recipient)
    end
    
    it "should trash messages" do
      delete :destroy, :id => @message
      assigns(:message).should be_trashed(@message.recipient)
      assigns(:message).should_not be_trashed(@message.sender)
    end
    
    it "should untrash messages" do
      delete :destroy, :id => @message
      put :undestroy, :id => @message
      assigns(:message).should_not be_trashed(@message.recipient)
    end
    
    it "should require login" do
      logout
      get :index
      response.should redirect_to(login_url)
    end
  end
  
  
  private
 
  def working_page(page, message_type)
    get page
    response.should be_success
    response.should render_template("index")
    assigns(:messages).should == @person.send(message_type)
  end
  
  def handle_replies(message, recipient, sender)
    login_as(recipient)
    lambda do
      post :create, :message => { :subject => "The subject",
                                  :content => "This is a reply",
                                  :parent => message },
                    :person_id => sender
      assigns(:message).should be_reply
    end.should change(Message, :count).by(1)
  end
  
  def proper_reply_behavior(person)
    login_as person
    get :reply, :id => @message
    response.should be_success
    # Check that the hidden parent_id tag is there, with the right value.
    response.should have_tag("input[id=?][name=?][type=?][value=?]",
                             "message_parent_id",
                             "message[parent_id]",
                             "hidden",
                             @message.id)
    response.should render_template("new")
    assigns(:message).parent.should == @message
    assigns(:recipient).should == @message.other_person(person)
  end
end