asciinema.org/spec/controllers/sessions_controller_spec.rb

80 lines
1.7 KiB
Ruby
Raw Normal View History

2014-08-30 17:38:47 +00:00
require 'rails_helper'
describe SessionsController do
2014-10-12 18:36:31 +00:00
describe "#create" do
subject { get :create, token: 'the-to-ken' }
2014-10-12 18:36:31 +00:00
let(:login_service) { double(:login_service) }
before do
2014-10-12 18:36:31 +00:00
allow(controller).to receive(:login_service) { login_service }
allow(login_service).to receive(:validate).with('the-to-ken') { user }
end
2014-10-12 18:36:31 +00:00
context "when token is successfully validated" do
let(:user) { stub_model(User) }
before do
allow(controller).to receive(:current_user=)
2012-03-05 23:25:21 +00:00
subject
end
2012-03-05 23:25:21 +00:00
2014-10-12 18:36:31 +00:00
it "sets the current_user" do
expect(controller).to have_received(:current_user=).with(user)
end
2014-10-17 15:28:22 +00:00
it "sets a notice" do
expect(flash[:notice]).to_not be_blank
2014-10-17 15:28:22 +00:00
end
context "when user has username" do
let(:user) { User.new(username: "foobar") }
it "redirects to user's profile" do
should redirect_to(public_profile_path(username: "foobar"))
end
end
context "when user has no username" do
let(:user) { User.new }
it "redirects to new username page" do
should redirect_to(new_username_path)
end
end
end
2014-10-12 18:36:31 +00:00
context "when token is not validated" do
let(:user) { nil }
before do
subject
end
2012-03-10 13:58:10 +00:00
2014-10-12 18:36:31 +00:00
it "displays error" do
should render_template('error')
end
end
end
describe "#destroy" do
before do
allow(controller).to receive(:current_user=)
2012-02-26 19:09:07 +00:00
get :destroy
end
2014-10-12 18:36:31 +00:00
it "sets current_user to nil" do
expect(controller).to have_received(:current_user=).with(nil)
end
it "redirects to root_path with a notice" do
expect(flash[:notice]).to_not be_blank
should redirect_to(root_path)
end
end
end