2012-03-04 22:07:45 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe UsersController do
|
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
let(:store) { {} }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:store) { store }
|
|
|
|
end
|
2012-03-06 22:04:35 +00:00
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
describe '#new' do
|
2012-03-06 22:04:35 +00:00
|
|
|
before do
|
2013-10-19 18:59:39 +00:00
|
|
|
store[:new_user_email] = 'foo@bar.com'
|
|
|
|
|
|
|
|
get :new
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns user with a stored email' do
|
|
|
|
expect(assigns(:user).email).to eq('foo@bar.com')
|
2012-03-06 22:04:35 +00:00
|
|
|
end
|
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
it 'renders new template' do
|
|
|
|
should render_template('new')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#create" do
|
|
|
|
let!(:user) { stub_model(User) }
|
|
|
|
|
2014-02-12 19:51:01 +00:00
|
|
|
subject { post :create, user: { username: 'jola' } }
|
2012-03-10 13:58:10 +00:00
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
before do
|
|
|
|
allow(controller).to receive(:current_user=)
|
2014-02-12 19:51:01 +00:00
|
|
|
allow(User).to receive(:new).with('username' => 'jola') { user }
|
2013-10-19 18:59:39 +00:00
|
|
|
store[:new_user_email] = 'foo@bar.com'
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when user is persisted" do
|
2012-03-06 22:04:35 +00:00
|
|
|
before do
|
2013-10-19 18:59:39 +00:00
|
|
|
allow(user).to receive(:save) { true }
|
2012-03-10 13:58:10 +00:00
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
subject
|
2012-03-06 22:04:35 +00:00
|
|
|
end
|
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
it 'removes the email from the store' do
|
|
|
|
expect(store.key?(:new_user_email)).to be(false)
|
|
|
|
end
|
2012-03-10 13:58:10 +00:00
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
it 'assigns the stored email to the user' do
|
|
|
|
expect(user.email).to eq('foo@bar.com')
|
2012-03-10 13:58:10 +00:00
|
|
|
end
|
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
it 'sets the current_user' do
|
|
|
|
expect(controller).to have_received(:current_user=).with(user)
|
2012-03-06 22:04:35 +00:00
|
|
|
end
|
|
|
|
|
2013-10-22 15:31:15 +00:00
|
|
|
it 'redirects to the "getting started" page with a notice' do
|
2013-10-19 18:59:39 +00:00
|
|
|
expect(flash[:notice]).to_not be_blank
|
2013-11-18 17:04:06 +00:00
|
|
|
should redirect_to(docs_path('getting-started'))
|
2013-10-19 18:59:39 +00:00
|
|
|
end
|
|
|
|
end
|
2012-03-10 13:58:10 +00:00
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
context "when user isn't persisted" do
|
|
|
|
before do
|
|
|
|
allow(user).to receive(:save) { false }
|
|
|
|
|
|
|
|
subject
|
2012-03-10 13:58:10 +00:00
|
|
|
end
|
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
it "doesn't remove the email from the store" do
|
|
|
|
expect(store.key?(:new_user_email)).to be(true)
|
2012-03-06 22:04:35 +00:00
|
|
|
end
|
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
it "doesn't set the current_user" do
|
|
|
|
expect(controller).to_not have_received(:current_user=)
|
|
|
|
end
|
2012-03-06 22:04:35 +00:00
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
it 'assigns user with a stored email' do
|
|
|
|
expect(assigns(:user).email).to eq('foo@bar.com')
|
2012-03-06 22:04:35 +00:00
|
|
|
end
|
|
|
|
|
2013-10-19 18:59:39 +00:00
|
|
|
it 'renders "new" template' do
|
|
|
|
should render_template('new')
|
2012-03-06 22:04:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-04-09 21:53:38 +00:00
|
|
|
|
|
|
|
describe '#show' do
|
2014-02-12 19:51:01 +00:00
|
|
|
subject { get :show, username: username }
|
2014-02-12 18:51:25 +00:00
|
|
|
|
2014-02-12 19:51:01 +00:00
|
|
|
let(:username) { user.username }
|
2014-02-12 18:51:25 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2014-02-12 19:51:01 +00:00
|
|
|
context "when real user username given" do
|
2014-02-12 18:51:25 +00:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
2014-03-16 17:34:37 +00:00
|
|
|
it 'renders "show" template' do
|
2014-02-12 18:51:25 +00:00
|
|
|
should render_template('show')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-12 19:51:01 +00:00
|
|
|
context "when dummy user username given" do
|
2014-02-12 18:51:25 +00:00
|
|
|
let(:user) { create(:dummy_user) }
|
|
|
|
|
|
|
|
it "responds with 404" do
|
|
|
|
expect(subject).to be_not_found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-12 19:51:01 +00:00
|
|
|
context "when fictional username given" do
|
|
|
|
let(:username) { 'nononono-no' }
|
2014-02-12 18:51:25 +00:00
|
|
|
|
|
|
|
it "responds with 404" do
|
|
|
|
expect(subject).to be_not_found
|
|
|
|
end
|
|
|
|
end
|
2012-04-09 21:53:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#edit' do
|
2014-03-16 17:34:37 +00:00
|
|
|
subject { get :edit }
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
login_as(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is successful" do
|
|
|
|
subject
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders "edit" template' do
|
|
|
|
subject
|
|
|
|
expect(response).to render_template(:edit)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for guest user" do
|
|
|
|
before do
|
|
|
|
logout
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects to login page" do
|
|
|
|
subject
|
|
|
|
expect(response).to redirect_to(login_path)
|
|
|
|
end
|
|
|
|
end
|
2012-04-09 21:53:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#update' do
|
2014-03-16 17:34:37 +00:00
|
|
|
subject { put :update, user: { username: 'batman' } }
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
login_as(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects to profile" do
|
|
|
|
subject
|
|
|
|
expect(response).to redirect_to(profile_path('batman'))
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when update fails" do
|
|
|
|
before do
|
|
|
|
allow(user).to receive(:update_attributes) { false }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "responds with 422 status code" do
|
|
|
|
subject
|
|
|
|
expect(response.status).to eq(422)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'renders "edit" template' do
|
|
|
|
subject
|
|
|
|
expect(response).to render_template(:edit)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "for guest user" do
|
|
|
|
before do
|
|
|
|
logout
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects to login page" do
|
|
|
|
subject
|
|
|
|
expect(response).to redirect_to(login_path)
|
|
|
|
end
|
|
|
|
end
|
2012-04-09 21:53:38 +00:00
|
|
|
end
|
2013-10-19 18:59:39 +00:00
|
|
|
|
2012-03-04 22:07:45 +00:00
|
|
|
end
|