asciinema.org/spec/controllers/users_controller_spec.rb

196 lines
4.0 KiB
Ruby
Raw Normal View History

2012-03-04 22:07:45 +00:00
require 'spec_helper'
describe UsersController do
let(:store) { {} }
before do
allow(controller).to receive(:store) { store }
end
2012-03-06 22:04:35 +00:00
describe '#new' do
2012-03-06 22:04:35 +00:00
before do
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
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
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 }
store[:new_user_email] = 'foo@bar.com'
end
context "when user is persisted" do
2012-03-06 22:04:35 +00:00
before do
allow(user).to receive(:save) { true }
2012-03-10 13:58:10 +00:00
subject
2012-03-06 22:04:35 +00:00
end
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
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
it 'sets the current_user' do
expect(controller).to have_received(:current_user=).with(user)
2012-03-06 22:04:35 +00:00
end
it 'redirects to the "getting started" page with a notice' do
expect(flash[:notice]).to_not be_blank
should redirect_to(docs_path('getting-started'))
end
end
2012-03-10 13:58:10 +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
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
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
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
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 19:51:01 +00:00
let(:username) { user.username }
before do
subject
end
2014-02-12 19:51:01 +00:00
context "when real user username given" do
let(:user) { create(:user) }
it 'renders "show" template' do
should render_template('show')
end
end
2014-02-12 19:51:01 +00:00
context "when dummy user username given" do
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' }
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
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
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
2012-03-04 22:07:45 +00:00
end