clients_controller_test.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. require 'test_helper'
  2. class ClientsControllerTest < ActionController::TestCase
  3. setup do
  4. @client_a = clients :ircclient_a
  5. @client_b = clients :simclient_a
  6. @last_msg = messages :simclient_user_c_message
  7. @room_b = rooms :room_b
  8. @name = @client_a.name
  9. @secret = @client_a.secret
  10. @room = @client_a.room
  11. end
  12. def post_client client_params
  13. post :create , :client => client_params
  14. end
  15. def post_message client_name , client_secret , message_params
  16. post :message , :client => { :secret => client_secret } , :name => client_name , :message => message_params
  17. end
  18. ## client creation tests ##
  19. test "should not create client with missing channel name" do
  20. assert_no_difference('Client.count') { post_client CLIENT_PARAMS_MISSING_NAME }
  21. assert_response 420
  22. end
  23. test "should not create client with empty channel name" do
  24. assert_no_difference('Client.count') { post_client CLIENT_PARAMS_EMPTY_NAME }
  25. assert_response 420
  26. end
  27. test "should not create client with missing client type" do
  28. assert_no_difference('Client.count') { post_client CLIENT_PARAMS_MISSING_TYPE }
  29. assert_response 420
  30. end
  31. test "should not create client with empty client type" do
  32. assert_no_difference('Client.count') { post_client CLIENT_PARAMS_EMPTY_TYPE }
  33. assert_response 420
  34. end
  35. test "should create client with valid params" do
  36. assert_difference('Client.count') { post_client CLIENT_C_PARAMS_VALID }
  37. assert_not_equal DUPLICATE_CLIENT_SECRET , (JSON.parse response.body)['secret']
  38. assert_not_nil assigns :client
  39. assert_response :success
  40. end
  41. test "should not create client with duplicate channel name" do
  42. assert_no_difference('Client.count') { post_client CLIENT_A_PARAMS_VALID }
  43. assert_equal DUPLICATE_CLIENT_SECRET , (JSON.parse response.body)['secret' ]
  44. assert_equal ROOM_A_SECRET , (JSON.parse response.body)['room_secret']
  45. assert_nil assigns :client
  46. assert_response :success
  47. end
  48. ## rooms fetch tests ##
  49. test "should not fetch rooms with missing client secret" do
  50. post :rooms , :client => { } , :name => @name
  51. assert_response :unauthorized
  52. end
  53. test "should not fetch rooms with unknown channel name" do
  54. post :rooms , :client => { :secret => @secret } , :name => 'name'
  55. assert_response :unauthorized
  56. end
  57. test "should not fetch rooms with empty client secret" do
  58. post :rooms , :client => { :secret => '' } , :name => @name
  59. assert_response :unauthorized
  60. end
  61. test "should not fetch rooms with incorrect client secret" do
  62. post :rooms , :client => { :secret => 'secret' } , :name => @name
  63. assert_response :unauthorized
  64. end
  65. test "should fetch other public rooms with valid params" do
  66. post :rooms , :client => { :secret => @secret } , :name => @name
  67. assert_equal 1 , (JSON.parse response.body)['rooms'].size
  68. assert_not_equal @room .secret , (JSON.parse response.body)['rooms'][0]['secret']
  69. assert_equal @room_b.secret , (JSON.parse response.body)['rooms'][0]['secret']
  70. assert_response :success
  71. end
  72. ## room bridging tests ##
  73. test "should bridge rooms with valid secret and room secret" do
  74. assert_difference('Room.count' , -1) do
  75. post :bridge , :client => { :secret => @secret } , :name => @name , :room_secret => ROOM_B_SECRET
  76. end
  77. assert_equal @room_b.secret , (JSON.parse response.body)['room_secret']
  78. assert_not_nil assigns :client
  79. assert_response :success
  80. end
  81. ## message creation tests ##
  82. test "should not create message with missing nick" do
  83. assert_no_difference('Message.count') { post_message @name , @secret , MESSAGE_PARAMS_MISSING_NICK }
  84. assert_response 420
  85. end
  86. test "should not create message with empty nick" do
  87. assert_no_difference('Message.count') { post_message @name , @secret , MESSAGE_PARAMS_EMPTY_NICK }
  88. assert_response 420
  89. end
  90. test "should not create message with missing text" do
  91. assert_no_difference('Message.count') { post_message @name , @secret , MESSAGE_PARAMS_MISSING_TEXT }
  92. assert_response 420
  93. end
  94. test "should not create message with empty text" do
  95. assert_no_difference('Message.count') { post_message @name , @secret , MESSAGE_PARAMS_EMPTY_TEXT }
  96. assert_response 420
  97. end
  98. test "should not create message with unknown channel name" do
  99. assert_no_difference('Message.count') { post_message 'name' , @secret , MESSAGE_PARAMS_VALID }
  100. assert_response :unauthorized
  101. end
  102. test "should not create message with incorrect client secret" do
  103. assert_no_difference('Message.count') { post_message @name , 'secret' , MESSAGE_PARAMS_VALID }
  104. assert_response :unauthorized
  105. end
  106. test "should create message with valid params" do
  107. post_message @name , @secret , MESSAGE_PARAMS_VALID
  108. assert_response :success
  109. end
  110. ## messages fetch tests ##
  111. test "should not fetch messages with unknown channel name" do
  112. post :messages , :client => { :secret => @secret } , :name => 'name' , :last_id => 1
  113. assert_response :unauthorized
  114. end
  115. test "should not fetch messages with missing client secret" do
  116. post :messages , :client => { } , :name => @name , :last_id => 1
  117. assert_response :unauthorized
  118. end
  119. test "should not fetch messages with empty client secret" do
  120. post :messages , :client => { :secret => '' } , :name => @name , :last_id => 1
  121. assert_response :unauthorized
  122. end
  123. test "should not fetch messages with incorrect client secret" do
  124. post :messages , :client => { :secret => 'secret' } , :name => @name , :last_id => 1
  125. assert_response :unauthorized
  126. end
  127. test "should fetch all messages with missing last message id" do
  128. post :messages , :client => { :secret => @secret } , :name => @name
  129. assert_response 420
  130. end
  131. test "should fetch all messages with empty last message id" do
  132. post :messages , :client => { :secret => @secret } , :name => @name , :last_id => ''
  133. assert_response 420
  134. end
  135. test "should fetch all messages with valid params and floor id" do
  136. post :messages , :client => { :secret => @secret } , :name => @name , :last_id => 0
  137. assert_equal N_MESSAGES_QUEUED + 4 , (JSON.parse response.body)['messages'].size
  138. assert_response :success
  139. end
  140. test "should fetch last message with valid params and last message id - 1" do
  141. post :messages , :client => { :secret => @secret } , :name => @name , :last_id => @last_msg.id - 1
  142. assert_equal 1 , (JSON.parse response.body)['messages'].size
  143. assert_response :success
  144. end
  145. test "should fetch zero messages with valid params and last message id" do
  146. post :messages , :client => { :secret => @secret } , :name => @name , :last_id => @last_msg.id
  147. assert_equal 0 , (JSON.parse response.body)['messages'].size
  148. assert_response :success
  149. end
  150. end