Add authentication and User model
* Add User model * Add SessionsController * Add password authentication on User * Request authentication for all actions except sign in * Add some helpers for ApplicationController * Update features to work with mandatory authentication
This commit is contained in:
21
spec/controllers/application_controller_spec.rb
Normal file
21
spec/controllers/application_controller_spec.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe ApplicationController do
|
||||
let(:user) { Factory.create(:user) }
|
||||
|
||||
describe '#current_user=' do
|
||||
it 'stores the user id in the session as :user_id' do
|
||||
controller.current_user = user
|
||||
session[:user_id].should == user.id
|
||||
end
|
||||
end
|
||||
|
||||
describe '#current_user' do
|
||||
context 'when session[:user_id] is set' do
|
||||
it 'returns the User instance from the session' do
|
||||
session[:user_id] = user.id
|
||||
controller.current_user.should == user
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -1,6 +1,10 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe HomeController do
|
||||
before do
|
||||
controller.current_user = Factory.create(:user)
|
||||
end
|
||||
|
||||
describe 'GET index' do
|
||||
it 'assigns all playlists as @playlists' do
|
||||
playlist = Factory.create(:playlist)
|
||||
|
@@ -1,6 +1,10 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe PlaylistsController do
|
||||
before do
|
||||
controller.current_user = Factory.create(:user)
|
||||
end
|
||||
|
||||
describe 'GET index' do
|
||||
it 'assigns all playlists as @playlists' do
|
||||
playlist = Factory.create(:playlist)
|
||||
|
40
spec/controllers/sessions_controller_spec.rb
Normal file
40
spec/controllers/sessions_controller_spec.rb
Normal file
@@ -0,0 +1,40 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe SessionsController do
|
||||
describe 'GET new' do
|
||||
it 'responds successfully' do
|
||||
response.should be_success
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST create' do
|
||||
context 'when the user submit invalid credentials' do
|
||||
it 'renders the new template' do
|
||||
User.stub(:authenticate).and_return(false)
|
||||
post :create,
|
||||
:session => Factory.attributes_for(:user, :password => 'WRONG')
|
||||
response.should render_template('new')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the user submit valid credentials' do
|
||||
let(:user) { Factory.create(:user) }
|
||||
before do
|
||||
User.stub(:authenticate).and_return(user)
|
||||
end
|
||||
|
||||
it 'signs the user in' do
|
||||
post :create, :session => Factory.attributes_for(:user)
|
||||
controller.current_user.should == user
|
||||
end
|
||||
|
||||
it 'redirects to the home page' do
|
||||
post :create, :session => Factory.attributes_for(:user)
|
||||
response.should redirect_to(:root)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'DELETE destroy' do
|
||||
end
|
||||
end
|
@@ -1,6 +1,10 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe TracksController do
|
||||
before do
|
||||
controller.current_user = Factory.create(:user)
|
||||
end
|
||||
|
||||
describe 'GET show' do
|
||||
it 'assigns the requested track as @track' do
|
||||
track = Factory.create(:track)
|
||||
|
@@ -8,4 +8,9 @@ FactoryGirl.define do
|
||||
mime_type 'audio/ogg'
|
||||
sha256 '94a5486a69a7261da350c57f9e5a1eaa789e08752cfc56a1989976a6ad82f7a8'
|
||||
end
|
||||
|
||||
factory :user do
|
||||
email 'alice@example.net'
|
||||
password '733tP4s5'
|
||||
end
|
||||
end
|
||||
|
44
spec/models/user_spec.rb
Normal file
44
spec/models/user_spec.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe User do
|
||||
subject { user }
|
||||
let(:user) { Factory.build(:user) }
|
||||
|
||||
context 'with valid attributes' do
|
||||
it { should be_valid }
|
||||
end
|
||||
|
||||
context 'when email empty' do
|
||||
before do
|
||||
user.email = ''
|
||||
end
|
||||
|
||||
it { should_not be_valid }
|
||||
end
|
||||
|
||||
context 'when password empty' do
|
||||
before do
|
||||
user.password = ''
|
||||
end
|
||||
|
||||
it { should_not be_valid }
|
||||
end
|
||||
|
||||
describe '.authenticate' do
|
||||
let (:user) { Factory.create(:user) }
|
||||
|
||||
it 'returns the user with valid credentials' do
|
||||
User.authenticate(
|
||||
user.email,
|
||||
user.password
|
||||
).should == user
|
||||
end
|
||||
|
||||
it 'returns false with invalid credentials' do
|
||||
User.authenticate(
|
||||
user.email,
|
||||
'WRONG'
|
||||
).should be_false
|
||||
end
|
||||
end
|
||||
end
|
26
spec/views/sessions/new.html.haml_spec.rb
Normal file
26
spec/views/sessions/new.html.haml_spec.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe 'sessions/new.html.haml' do
|
||||
it 'renders a form to sign in' do
|
||||
render
|
||||
rendered.should \
|
||||
have_selector("form[method=post][action='#{sessions_path}']")
|
||||
rendered.should have_selector('input[type=submit]')
|
||||
end
|
||||
|
||||
it 'renders a text field with a label for the mail address' do
|
||||
render
|
||||
rendered.should \
|
||||
have_selector("input[type=text][name='session[email]']")
|
||||
rendered.should \
|
||||
have_selector('label[for=session_email]', :text => 'Email')
|
||||
end
|
||||
|
||||
it 'renders a password field with a label for the password' do
|
||||
render
|
||||
rendered.should \
|
||||
have_selector("input[type=password][name='session[password]']")
|
||||
rendered.should \
|
||||
have_selector('label[for=session_password]', :text => 'Password')
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user