PageRenderTime 64ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/controllers/login_controller_spec.rb

https://github.com/liquid/taskboard
Ruby | 124 lines | 80 code | 22 blank | 22 comment | 1 complexity | c3101e8ea8270350faa88d38a54c6e3d MD5 | raw file
  1. # Copyright (C) 2009 Cognifide
  2. #
  3. # This file is part of Taskboard.
  4. #
  5. # Taskboard is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Taskboard is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Taskboard. If not, see <http://www.gnu.org/licenses/>.
  17. require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
  18. describe LoginController do
  19. # FIXME login/passwords should be removed from tests
  20. fixtures :users
  21. before(:each) do
  22. @test_editor = users(:test_editor)
  23. @test_editor.password = "editor_password"
  24. @test_viewer = users(:test_viewer)
  25. @test_viewer.password = "viewer_password"
  26. @controller.instance_eval { flash.extend(DisableFlashSweeping) }
  27. end
  28. it "should redirect to last reqest after successfull login" do
  29. uri = "http://test.host/taskboard/show"
  30. post :login, {:login => @test_editor.username, :password => @test_editor.password}, {:original_uri => uri}
  31. response.should redirect_to(uri)
  32. session[:user_id].should eql(@test_editor.id)
  33. end
  34. it "should store editor role in session if user has edit rights" do
  35. post :login, {:login => @test_editor.username, :password => @test_editor.password}
  36. response.should be_redirect
  37. session[:editor].should be(true)
  38. end
  39. it "shouldn't store editor role in session if user doesn't have edit rights" do
  40. post :login, {:login => @test_viewer.username, :password => @test_viewer.password }
  41. response.should be_redirect
  42. session[:editor].should_not be(true)
  43. end
  44. it "should show message when login is not correct" do
  45. post :login, {:login => 'wrong_username', :password => @test_editor.password}
  46. response.should_not be_redirect
  47. flash[:notice].should eql("Wrong user name or password!")
  48. end
  49. it "should show message when login is not correct" do
  50. post :login, {:login => 'cognifide', :password => 'qwe1234'}
  51. response.should_not be_redirect
  52. flash[:notice].should eql("Wrong user name or password!")
  53. end
  54. it "should remove all information from session after logout" do
  55. post :logout, {}, {:user_id => 1, :editor => true}
  56. response.should redirect_to :action => "login"
  57. session[:user_id].should be_nil
  58. end
  59. it "should show message when login is empty" do
  60. post :login, {:login => '', :password => 'password'}
  61. response.should_not be_redirect
  62. flash[:notice].should eql("Please fill in both user name and password!")
  63. end
  64. it "should show message when password is empty" do
  65. post :login, {:login => 'somelogin', :password => ''}
  66. response.should_not be_redirect
  67. flash[:notice].should eql("Please fill in both user name and password!")
  68. end
  69. end
  70. describe LoginController, "while administrating users" do
  71. # FIXME: it doesn't work here... don't know why
  72. before(:each) do
  73. @controller.instance_eval { flash.extend(DisableFlashSweeping) }
  74. end
  75. it "should allow adding new viewer user" do
  76. user_data = {:username => "newuser", :password => "password", :password_confirmation => "password"}
  77. user = User.new(user_data)
  78. User.should_receive(:new).with({"username" => "newuser", "password" => "password", "password_confirmation" => "password"}).and_return(user)
  79. user.should_receive(:save).and_return(true)
  80. User.should_receive(:new)
  81. post :add_user, {:user => user_data}, {:user_id => 1, :editor => true}
  82. response.should be_success
  83. # FIXME: it doesn't work here... don't know why
  84. # flash[:notice].should eql("Added new viewer user newuser")
  85. end
  86. it "should allow adding new editor user" do
  87. user_data = {:username => "newuser", :password => "password", :password_confirmation => "password", :editor => true}
  88. user = User.new(user_data)
  89. user.editor?.should be_true
  90. User.should_receive(:new).with({"username" => "newuser", "password" => "password", "password_confirmation" => "password", "editor" => true}).and_return(user)
  91. user.should_receive(:save).and_return(true)
  92. User.should_receive(:new)
  93. post :add_user, {:user => user_data}, {:user_id => 1, :editor => true}
  94. response.should be_success
  95. # FIXME: it doesn't work here... don't know why
  96. # flash[:notice].should eql("Added new editor user newuser")
  97. end
  98. it "should list all users" do
  99. User.should_receive(:find).with(:all)
  100. get :list_users, {}, {:user_id => 1, :editor => true}
  101. end
  102. end