PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cloud/sip-servlets/steamcannon/spec/controllers/users_controller_spec.rb

http://mobicents.googlecode.com/
Ruby | 411 lines | 332 code | 62 blank | 17 comment | 15 complexity | 44170117bbad2929136518cae39b52ca MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. #
  2. # Copyright 2010 Red Hat, Inc.
  3. #
  4. # This is free software; you can redistribute it and/or modify it
  5. # under the terms of the GNU Lesser General Public License as
  6. # published by the Free Software Foundation; either version 3 of
  7. # the License, or (at your option) any later version.
  8. #
  9. # This software is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this software; if not, write to the Free
  16. # Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  17. # 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  18. require 'spec_helper'
  19. describe UsersController do
  20. describe "GET account/new" do
  21. describe "when not logged in" do
  22. before(:each) { logout }
  23. it "should be successful" do
  24. get :new
  25. response.should be_success
  26. end
  27. it "should render the new form" do
  28. get :new
  29. response.should render_template(:new)
  30. end
  31. context 'in invite_only mode' do
  32. before(:each) do
  33. APP_CONFIG[:signup_mode] = 'invite_only'
  34. @account_request = mock_model(AccountRequest, :email => 'blah@example.com',
  35. :organization => nil)
  36. @user = mock_model(User, :email= => nil, :organization= => nil)
  37. end
  38. it "should redirect to login if signup_mode when no token provided" do
  39. get :new
  40. response.should redirect_to(new_user_session_url)
  41. end
  42. it "should execute action if a valid token is provided" do
  43. AccountRequest.should_receive(:find_by_token).with('1234').and_return(@account_request)
  44. User.should_receive(:new).and_return(@user)
  45. get :new, :token => '1234'
  46. end
  47. it "should redirect to login if signup_mode when an invalid token provided" do
  48. get :new, :token => 'bad token'
  49. response.should redirect_to(new_user_session_url)
  50. end
  51. it "should store the account_request in an ivar" do
  52. AccountRequest.should_receive(:find_by_token).with('1234').and_return(@account_request)
  53. get :new, :token => '1234'
  54. assigns[:account_request].should == @account_request
  55. end
  56. it "should copy organization from account_request" do
  57. AccountRequest.should_receive(:find_by_token).with('1234').and_return(@account_request)
  58. User.should_receive(:new).and_return(@user)
  59. organization = mock_model(Organization)
  60. @account_request.should_receive(:organization).and_return(organization)
  61. @user.should_receive(:organization=).with(organization)
  62. get :new, :token => '1234'
  63. end
  64. end
  65. end
  66. describe "when logged in" do
  67. before(:each) { login }
  68. it "should redirect to root page" do
  69. get :new
  70. response.should redirect_to(root_url)
  71. end
  72. end
  73. end
  74. describe "POST account" do
  75. before(:each) do
  76. logout
  77. @user = mock_model(User)
  78. User.stub!(:new).and_return(@user)
  79. @user.stub!(:organization=)
  80. end
  81. describe "with valid params" do
  82. before(:each) do
  83. @user.stub!(:save).and_return(true)
  84. end
  85. it "should create new user" do
  86. User.should_receive(:new)
  87. post :create
  88. end
  89. it "should redirect to root page" do
  90. post :create
  91. response.should redirect_to(root_url)
  92. end
  93. it "should have a flash notice" do
  94. post :create
  95. flash[:notice].should_not be_blank
  96. end
  97. context 'in invite_only mode' do
  98. before(:each) do
  99. APP_CONFIG[:signup_mode] = 'invite_only'
  100. @account_request = mock_model(AccountRequest)
  101. @account_request.stub!(:accept!)
  102. @account_request.stub!(:organization)
  103. end
  104. it "should redirect to login if signup_mode when no token provided" do
  105. post :create
  106. response.should redirect_to(new_user_session_url)
  107. end
  108. it "should execute action if a valid token is provided" do
  109. mock_association = mock("invited")
  110. mock_association.should_receive(:find_by_token).with('1234').and_return(@account_request)
  111. AccountRequest.should_receive(:invited).and_return(mock_association)
  112. User.should_receive(:new)
  113. post :create, :token => '1234'
  114. end
  115. it "should redirect to login if signup_mode when an invalid token provided" do
  116. post :create, :token => 'bad token'
  117. response.should redirect_to(new_user_session_url)
  118. end
  119. it "should store the account_request in an ivar" do
  120. AccountRequest.should_receive(:find_by_token).with('1234').and_return(@account_request)
  121. post :create, :token => '1234'
  122. assigns[:account_request].should == @account_request
  123. end
  124. it "should accept! the account_request" do
  125. AccountRequest.should_receive(:find_by_token).with('1234').and_return(@account_request)
  126. @account_request.should_receive(:accept!)
  127. post :create, :token => '1234'
  128. end
  129. it "should copy organization from account_request" do
  130. AccountRequest.should_receive(:find_by_token).with('1234').and_return(@account_request)
  131. organization = mock_model(Organization)
  132. @account_request.should_receive(:organization).and_return(organization)
  133. @user.should_receive(:organization=).with(organization)
  134. post :create, :token => '1234'
  135. end
  136. end
  137. end
  138. describe "with invalid params" do
  139. before(:each) do
  140. @user.stub!(:save).and_return(false)
  141. end
  142. it "should display registration form" do
  143. post :create
  144. response.should render_template(:new)
  145. end
  146. end
  147. end
  148. describe "GET account" do
  149. before(:each) { login }
  150. it "should be successful" do
  151. get :show
  152. response.should be_success
  153. end
  154. end
  155. describe "GET account/edit" do
  156. before(:each) { login }
  157. it "should be successful" do
  158. get :edit
  159. response.should be_success
  160. end
  161. it "should set an error message in the flash if the user's profile is not complete'" do
  162. @current_user.stub!(:profile_complete?).and_return(false)
  163. get :edit
  164. flash[:error].should_not be_blank
  165. end
  166. it "should NOT set an error message in the flash if the user's profile is not complete'" do
  167. @current_user.stub!(:profile_complete?).and_return(true)
  168. get :edit
  169. flash[:error].should be_blank
  170. end
  171. end
  172. describe "PUT account" do
  173. before(:each) do
  174. login
  175. @current_user.stub!(:cloud_password_dirty=).and_return(true)
  176. end
  177. describe "with valid params" do
  178. before(:each) do
  179. @current_user.stub!(:update_attributes).and_return(true)
  180. end
  181. it "should update the user object's attributes" do
  182. @current_user.should_receive(:update_attributes).and_return(true)
  183. put :update
  184. end
  185. it "should redirect to the account show page" do
  186. put :update
  187. response.should redirect_to(account_url)
  188. end
  189. end
  190. describe "with invalid params" do
  191. before(:each) do
  192. @current_user.stub!(:update_attributes).and_return(false);
  193. end
  194. it "should update the user object's attrributes" do
  195. @current_user.should_receive(:update_attributes).and_return(false)
  196. put :update
  197. end
  198. it "should render the edit form" do
  199. put :update
  200. response.should render_template(:edit)
  201. end
  202. end
  203. end
  204. describe "GET index" do
  205. it "should limit to users visible to the current user" do
  206. User.should_receive(:visible_to_user).with(@current_user).and_return(mock('user_fault', :sorted_by => []))
  207. get :index
  208. end
  209. end
  210. describe "edit/update" do
  211. before(:each) do
  212. @superuser = Factory.build(:superuser)
  213. @account_user = Factory.build(:user)
  214. end
  215. context "with a superuser logged in" do
  216. before(:each) do
  217. login_with_user(@superuser)
  218. User.stub!(:find).and_return(@account_user)
  219. end
  220. it "should allow a superuser to edit another user" do
  221. get :edit, :id => 1
  222. response.should render_template(:edit)
  223. end
  224. it "should allow a superuser to update another user" do
  225. post :update, :id => 1, :user => Factory.attributes_for(:user)
  226. response.should redirect_to(user_path(@account_user))
  227. end
  228. end
  229. context "with a non-superuser logged in" do
  230. before(:each) do
  231. login_with_user(@account_user)
  232. User.stub!(:find).and_return(@superuser)
  233. end
  234. it "should not allow a non-superuser to edit other users" do
  235. get :edit, :id => 1
  236. response.should redirect_to(new_user_session_path)
  237. end
  238. it "should not allow a non-superuser to update another user" do
  239. post :update, :id => 1, :user => Factory.attributes_for(:user)
  240. response.should redirect_to(new_user_session_path)
  241. end
  242. end
  243. end
  244. describe "assume user" do
  245. before(:each) do
  246. @user = mock_model(User, :email => 'email@example.com')
  247. User.stub!(:find).and_return(@user)
  248. end
  249. context "functionality" do
  250. before(:each) do
  251. login({ }, :superuser? => true)
  252. UserSession.stub!(:create)
  253. end
  254. it "should switch the current user to the new user" do
  255. UserSession.should_receive(:create).with(@user)
  256. get :assume_user, :id => 1
  257. end
  258. it "should redirect to the dashboard" do
  259. get :assume_user, :id => 1
  260. response.should redirect_to(root_path)
  261. end
  262. end
  263. context "permissions" do
  264. it "should not allow a regular user access" do
  265. login({ }, :superuser? => false)
  266. get :assume_user, :id => 1
  267. response.should redirect_to(new_user_session_path)
  268. end
  269. it "should allow a superuser to access" do
  270. login({ }, :superuser? => true)
  271. UserSession.should_receive(:create).with(@user)
  272. get :assume_user, :id => 1
  273. end
  274. end
  275. end
  276. describe 'validate_cloud_credentials' do
  277. before(:each) do
  278. @user = login
  279. @client = mock(Cloud::Deltacloud)
  280. @organization = mock(Organization)
  281. @user.stub!(:cloud).and_return(@client)
  282. @user.stub!(:organization).and_return(@organization)
  283. @client.stub!(:attempt).and_return(true)
  284. end
  285. it "should validate" do
  286. @client.should_receive(:attempt).with(:valid_credentials?, false)
  287. get :validate_cloud_credentials
  288. end
  289. context "returned json" do
  290. it "should have a status of :ok if the credentials are valid" do
  291. @client.should_receive(:attempt).with(:valid_credentials?, false).and_return(true)
  292. get :validate_cloud_credentials
  293. JSON.parse(response.body)['status'].should == 'ok'
  294. end
  295. it "should have a status of :error if the credentials are not valid" do
  296. @client.should_receive(:attempt).with(:valid_credentials?, false).and_return(false)
  297. get :validate_cloud_credentials
  298. JSON.parse(response.body)['status'].should == 'error'
  299. end
  300. end
  301. it "should use provided cloud credentials" do
  302. @organization.should_receive(:cloud_password=).with("pw")
  303. @organization.should_receive(:cloud_username=).with("uname")
  304. get :validate_cloud_credentials, :cloud_password => 'pw', :cloud_username => 'uname'
  305. end
  306. end
  307. { "promote" => true, "demote" => false }.each do |action, organization_admin|
  308. describe action do
  309. before(:each) do
  310. @user = mock_model(User, :email => 'email@example.com',
  311. :organization_admin= => nil, :save! => nil)
  312. User.stub!(:find).and_return(@user)
  313. end
  314. context "functionality" do
  315. before(:each) do
  316. login({ }, :organization_admin? => true)
  317. end
  318. it "should set the user's organization admin flag to #{organization_admin}" do
  319. @user.should_receive(:organization_admin=).with(organization_admin)
  320. @user.should_receive(:save!)
  321. post action, :id => 1
  322. end
  323. it "should redirect to the users list" do
  324. post action, :id => 1
  325. response.should redirect_to(users_path)
  326. end
  327. end
  328. context "permissions" do
  329. it "should not allow a regular user access" do
  330. login({ }, :organization_admin? => false)
  331. post action, :id => 1
  332. response.should redirect_to(new_user_session_path)
  333. end
  334. it "should allow an organization admin to access" do
  335. login({ }, :organization_admin? => true)
  336. @user.should_receive(:organization_admin=).with(organization_admin)
  337. post action, :id => 1
  338. end
  339. end
  340. end
  341. end
  342. end