/spec/controllers/login_controller_spec.rb
Ruby | 124 lines | 80 code | 22 blank | 22 comment | 1 complexity | c3101e8ea8270350faa88d38a54c6e3d MD5 | raw file
- # Copyright (C) 2009 Cognifide
- #
- # This file is part of Taskboard.
- #
- # Taskboard is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # Taskboard is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with Taskboard. If not, see <http://www.gnu.org/licenses/>.
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
- describe LoginController do
- # FIXME login/passwords should be removed from tests
- fixtures :users
-
- before(:each) do
- @test_editor = users(:test_editor)
- @test_editor.password = "editor_password"
-
- @test_viewer = users(:test_viewer)
- @test_viewer.password = "viewer_password"
-
- @controller.instance_eval { flash.extend(DisableFlashSweeping) }
- end
-
- it "should redirect to last reqest after successfull login" do
-
- uri = "http://test.host/taskboard/show"
- post :login, {:login => @test_editor.username, :password => @test_editor.password}, {:original_uri => uri}
- response.should redirect_to(uri)
- session[:user_id].should eql(@test_editor.id)
- end
- it "should store editor role in session if user has edit rights" do
- post :login, {:login => @test_editor.username, :password => @test_editor.password}
- response.should be_redirect
- session[:editor].should be(true)
- end
- it "shouldn't store editor role in session if user doesn't have edit rights" do
- post :login, {:login => @test_viewer.username, :password => @test_viewer.password }
- response.should be_redirect
- session[:editor].should_not be(true)
- end
- it "should show message when login is not correct" do
- post :login, {:login => 'wrong_username', :password => @test_editor.password}
- response.should_not be_redirect
- flash[:notice].should eql("Wrong user name or password!")
- end
- it "should show message when login is not correct" do
- post :login, {:login => 'cognifide', :password => 'qwe1234'}
- response.should_not be_redirect
- flash[:notice].should eql("Wrong user name or password!")
- end
- it "should remove all information from session after logout" do
- post :logout, {}, {:user_id => 1, :editor => true}
- response.should redirect_to :action => "login"
- session[:user_id].should be_nil
- end
- it "should show message when login is empty" do
- post :login, {:login => '', :password => 'password'}
- response.should_not be_redirect
- flash[:notice].should eql("Please fill in both user name and password!")
- end
- it "should show message when password is empty" do
- post :login, {:login => 'somelogin', :password => ''}
- response.should_not be_redirect
- flash[:notice].should eql("Please fill in both user name and password!")
- end
- end
- describe LoginController, "while administrating users" do
- # FIXME: it doesn't work here... don't know why
- before(:each) do
- @controller.instance_eval { flash.extend(DisableFlashSweeping) }
- end
- it "should allow adding new viewer user" do
- user_data = {:username => "newuser", :password => "password", :password_confirmation => "password"}
- user = User.new(user_data)
- User.should_receive(:new).with({"username" => "newuser", "password" => "password", "password_confirmation" => "password"}).and_return(user)
- user.should_receive(:save).and_return(true)
- User.should_receive(:new)
- post :add_user, {:user => user_data}, {:user_id => 1, :editor => true}
- response.should be_success
- # FIXME: it doesn't work here... don't know why
- # flash[:notice].should eql("Added new viewer user newuser")
- end
- it "should allow adding new editor user" do
- user_data = {:username => "newuser", :password => "password", :password_confirmation => "password", :editor => true}
- user = User.new(user_data)
- user.editor?.should be_true
- User.should_receive(:new).with({"username" => "newuser", "password" => "password", "password_confirmation" => "password", "editor" => true}).and_return(user)
- user.should_receive(:save).and_return(true)
- User.should_receive(:new)
- post :add_user, {:user => user_data}, {:user_id => 1, :editor => true}
- response.should be_success
- # FIXME: it doesn't work here... don't know why
- # flash[:notice].should eql("Added new editor user newuser")
- end
-
- it "should list all users" do
- User.should_receive(:find).with(:all)
- get :list_users, {}, {:user_id => 1, :editor => true}
- end
- end