cirandas.net

ref: master

test/functional/chat_controller_test.rb


  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
require_relative "../test_helper"

class ChatControllerTest < ActionController::TestCase

  def setup
    env = Environment.default
    env.enable('xmpp_chat')
    env.save!
    #TODO Maybe someday we should have a real testing ejabberd server
    RubyBOSH.stubs(:initialize_session).returns(['fake-jid@example.org', 'fake-sid', 'fake-rid'])
    @person = create_user('testuser').person
  end

  should 'cant view chat when not logged in' do
    get :start_session
    assert_response 302
  end

  should 'can view chat when logged in' do
    login_as 'testuser'

    get :start_session
    assert_response :success
  end

  should 'get default avatar' do
    login_as 'testuser'

    get :avatar, :id => 'testuser'

    assert_response :redirect
  end

  should 'get avatar from community' do
    community = fast_create(Community)
    login_as 'testuser'

    get :avatar, :id => community.identifier

    assert_equal 'image/png', @response.content_type
    assert @response.body.index('PNG')
  end

  begin
    require 'ruby_bosh'
    should 'try to start xmpp session' do
      login_as 'testuser'

      RubyBOSH.expects(:initialize_session).raises("Error trying to connect...")

      get :start_session
      assert_response 500
      assert_template 'start_session_error'
    end
  rescue LoadError
    puts 'W: could not load RubyBOSH; skipping some chat tests'
    should 'skip the above test if the chat dependencies are not installed' do
      print '*'
    end
  end

  should 'render not found if chat feature disabled' do
    login_as 'testuser'

    env = Environment.default
    env.disable('xmpp_chat')
    env.save!

    get :start_session

    assert_response 404
    assert_template 'not_found'
  end

  should 'not update presence status from non-ajax requests' do
    @person.user.expects(:update).never
    @controller.stubs(:current_user).returns(@person.user)
    get :update_presence_status
    assert_template nil
  end

  should 'update presence status from ajax requests' do
    @person.user.expects(:update).once
    @controller.stubs(:current_user).returns(@person.user)
    @request.stubs(:xhr?).returns(true)
    get :update_presence_status
    assert_template nil
  end

  should 'update presence status time from ajax requests' do
    @controller.stubs(:current_user).returns(@person.user)
    @request.stubs(:xhr?).returns(true)
    chat_status_at = @person.user.chat_status_at
    get :update_presence_status
    assert_not_equal chat_status_at, @person.user.chat_status_at
  end

  should 'forbid to register a message without to' do
    @controller.stubs(:current_user).returns(@person.user)
    @request.stubs(:xhr?).returns(true)

    post :save_message, {:body =>'Hello!'}
    assert_equal 3, ActiveSupport::JSON.decode(@response.body)['status']
  end

  should 'forbid to register a message without body' do
    @controller.stubs(:current_user).returns(@person.user)
    @request.stubs(:xhr?).returns(true)

    post :save_message, {:to =>'mary'}
    assert_equal 3, ActiveSupport::JSON.decode(@response.body)['status']
  end

  should 'forbid user to register a message to a stranger' do
    @controller.stubs(:current_user).returns(@person.user)
    @request.stubs(:xhr?).returns(true)

    post :save_message, {:to =>'random', :body => 'Hello, stranger!'}
    assert_equal 3, ActiveSupport::JSON.decode(@response.body)['status']
  end

  should 'register a message to a friend' do
    @controller.stubs(:current_user).returns(@person.user)
    friend = create_user('friend').person
    @person.add_friend friend
    @request.stubs(:xhr?).returns(true)

    assert_difference 'ChatMessage.count', 1 do
      post :save_message, {:to => friend.identifier, :body => 'Hey! How is it going?'}
      assert ActiveSupport::JSON.decode(@response.body)['status'] == 0
    end
  end

  should 'register a message to a group' do
    @controller.stubs(:current_user).returns(@person.user)
    group = fast_create(Organization)
    group.add_member(@person)
    @request.stubs(:xhr?).returns(true)

    assert_difference 'ChatMessage.count', 1 do
      post :save_message, {:to => group.identifier, :body => 'Hey! How is it going?'}
      assert ActiveSupport::JSON.decode(@response.body)['status'] == 0
    end
  end

  should 'toggle chat status' do
    login_as 'testuser'

    get :start_session
    assert_nil session[:chat][:status]

    get :toggle
    assert_equal 'opened', session[:chat][:status]

    get :toggle
    assert_equal 'closed', session[:chat][:status]

    get :toggle
    assert_equal 'opened', session[:chat][:status]
  end

  should 'set tab' do
    login_as 'testuser'
    get :start_session

    post :tab, :tab_id => 'my_tab'
    assert_equal 'my_tab', session[:chat][:tab_id]
  end

  should 'join room' do
    login_as 'testuser'
    get :start_session

    post :join, :room_id => 'room1'
    assert_equivalent ['room1'], session[:chat][:rooms]

    post :join, :room_id => 'room2'
    assert_equivalent ['room1', 'room2'], session[:chat][:rooms]

    post :join, :room_id => 'room1'
    assert_equivalent ['room1', 'room2'], session[:chat][:rooms]
  end

  should 'leave room' do
    login_as 'testuser'
    get :start_session
    session[:chat][:rooms] = ['room1', 'room2']

    post :leave, :room_id => 'room2'
    assert_equivalent ['room1'], session[:chat][:rooms]

    post :leave, :room_id => 'room1'
    assert_equivalent [], session[:chat][:rooms]

    post :leave, :room_id => 'room1'
    assert_equivalent [], session[:chat][:rooms]
  end

  should 'fetch chat session as json' do
    login_as 'testuser'
    get :start_session
    my_chat = {:status => 'opened', :rooms => ['room1', 'room2'], :tab_id => 'room1'}
    session[:chat] = my_chat

    get :my_session
    assert_equal @response.body, my_chat.to_json
  end

end