Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

finished with sessions

  • Loading branch information...
commit 08e781637f4f7ac681de0041902134d9ff897831 1 parent d59fd60
@kuloshius authored
Showing with 316 additions and 258 deletions.
  1. +3 −0  .autotest
  2. +0 −1  .rspec
  3. +9 −5 Gemfile
  4. +31 −11 Gemfile.lock
  5. +2 −7 app/controllers/application_controller.rb
  6. +5 −1 app/controllers/sessions_controller.rb
  7. +4 −11 app/controllers/users_controller.rb
  8. +1 −1  app/helpers/application_helper.rb
  9. +9 −0 app/helpers/sessions_helper.rb
  10. +3 −4 app/models/user.rb
  11. +0 −4 app/views/pages/about.html.erb
  12. +0 −4 app/views/pages/contact.html.erb
  13. +0 −3  app/views/pages/help.html.erb
  14. +0 −3  app/views/pages/home.html.erb
  15. +2 −2 app/views/sessions/new.html.erb
  16. +0 −5 config/application.rb
  17. +0 −1  config/environments/test.rb
  18. +0 −8 config/initializers/setup_mail.rb
  19. +0 −39 config/locales/devise.en.yml
  20. +5 −6 config/routes.rb
  21. +0 −87 spec/controllers/pages_controller_spec.rb
  22. +73 −1 spec/controllers/sessions_controller_spec.rb
  23. +39 −4 spec/controllers/users_controller_spec.rb
  24. +3 −3 spec/factories.rb
  25. +0 −15 spec/helpers/sessions_helper_spec.rb
  26. +0 −15 spec/helpers/users_helper_spec.rb
  27. +1 −3 spec/models/account_spec.rb
  28. +114 −1 spec/models/user_spec.rb
  29. +12 −3 spec/spec_helper.rb
  30. +0 −5 spec/views/sessions/new.html.erb_spec.rb
  31. +0 −5 spec/views/users/new.html.erb_spec.rb
View
3  .autotest
@@ -2,5 +2,8 @@ Autotest.add_hook :initialize do |autotest|
autotest.add_mapping(%r%^spec/(requests)/.*rb$%) do|filename, _|
filename
end
+# autotest.add_mapping(/^spec\/requests\/.*_spec\.rb$/) do
+# autotest.files_matching(/^spec\/requests\/.*_spec\.rb$/)
+# end
end
View
1  .rspec
@@ -1,3 +1,2 @@
--colour
--drb
-
View
14 Gemfile
@@ -1,17 +1,21 @@
source 'http://rubygems.org'
-gem 'rails'
+gem 'rails', '3.0.9'
gem 'mysql2', '0.2.11'
gem 'jquery-rails'
gem 'bcrypt-ruby', :require => 'bcrypt'
group :development do
- gem 'rspec-rails'
- gem 'faker'
+ gem 'rspec-rails', '2.6.1'
+ gem 'faker', '0.3.1'
+ gem 'annotate-models', '1.0.4'
+ gem 'ruby-debug19'
end
group :test do
- gem 'rspec'
+ gem 'rspec-rails', '2.6.1'
gem 'spork', '~> 0.9.0.rc'
- gem 'webrat'
+ gem 'webrat', '0.7.1'
+ gem 'factory_girl_rails', '1.0'
+ gem 'ruby-debug19'
end
View
42 Gemfile.lock
@@ -28,18 +28,26 @@ GEM
activemodel (= 3.0.9)
activesupport (= 3.0.9)
activesupport (3.0.9)
+ annotate-models (1.0.4)
+ archive-tar-minitar (0.5.2)
arel (2.0.10)
bcrypt-ruby (2.1.4)
builder (2.1.2)
+ columnize (0.3.3)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
- faker (0.9.5)
- i18n (~> 0.4)
+ factory_girl (1.3.3)
+ factory_girl_rails (1.0)
+ factory_girl (~> 1.3)
+ rails (>= 3.0.0.beta4)
+ faker (0.3.1)
i18n (0.5.0)
jquery-rails (1.0.12)
railties (~> 3.0)
thor (~> 0.14)
+ linecache19 (0.5.12)
+ ruby_core_source (>= 0.1.4)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -47,7 +55,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.16)
mysql2 (0.2.11)
- nokogiri (1.4.6)
+ nokogiri (1.5.0)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
@@ -69,7 +77,7 @@ GEM
rdoc (~> 3.4)
thor (~> 0.14.4)
rake (0.9.2)
- rdoc (3.7)
+ rdoc (3.8)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
@@ -83,12 +91,22 @@ GEM
activesupport (~> 3.0)
railties (~> 3.0)
rspec (~> 2.6.0)
- spork (0.9.0.rc8)
+ ruby-debug-base19 (0.11.25)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby_core_source (>= 0.1.4)
+ ruby-debug19 (0.11.6)
+ columnize (>= 0.3.1)
+ linecache19 (>= 0.5.11)
+ ruby-debug-base19 (>= 0.11.19)
+ ruby_core_source (0.1.5)
+ archive-tar-minitar (>= 0.5.2)
+ spork (0.9.0.rc9)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.29)
- webrat (0.7.3)
+ webrat (0.7.1)
nokogiri (>= 1.2.0)
rack (>= 1.0)
rack-test (>= 0.5.3)
@@ -97,12 +115,14 @@ PLATFORMS
ruby
DEPENDENCIES
+ annotate-models (= 1.0.4)
bcrypt-ruby
- faker
+ factory_girl_rails (= 1.0)
+ faker (= 0.3.1)
jquery-rails
mysql2 (= 0.2.11)
- rails
- rspec
- rspec-rails
+ rails (= 3.0.9)
+ rspec-rails (= 2.6.1)
+ ruby-debug19
spork (~> 0.9.0.rc)
- webrat
+ webrat (= 0.7.1)
View
9 app/controllers/application_controller.rb
@@ -1,10 +1,5 @@
class ApplicationController < ActionController::Base
protect_from_forgery
- helper_method :current_user
-
- private
-
- def current_user
- @current_user ||= User.find(session[:user_id]) if session[:user_id]
- end
+ include SessionsHelper
+ before_filter :authenticate
end
View
6 app/controllers/sessions_controller.rb
@@ -1,13 +1,17 @@
class SessionsController < ApplicationController
+ skip_before_filter :authenticate, :only => [:new, :create]
+
def new
+ @title = "Sign in"
end
def create
user = User.authenticate(params[:email], params[:password])
if user
session[:user_id] = user.id
- redirect_to root_url, :notice => "Logged in!"
+ redirect_to user, :notice => "Logged in!"
else
+ @title = "Sign in"
flash.now.alert = "Invalid email or password"
render "new"
end
View
15 app/controllers/users_controller.rb
@@ -1,15 +1,8 @@
class UsersController < ApplicationController
- def new
- @user = User.new
- end
-
- def create
- @user = User.new(params[:user])
- if @user.save?
- redirect_to root_url, :notice => "Signed up!"
- else
- render "new"
- end
+ def show
+ @title = "SLP | Home"
+ @user = current_user
+ @company = Company.new
end
end
View
2  app/helpers/application_helper.rb
@@ -11,5 +11,5 @@ def title
def app_name
"SLP"
end
-
+
end
View
9 app/helpers/sessions_helper.rb
@@ -1,2 +1,11 @@
module SessionsHelper
+
+ def current_user
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
+ end
+
+ def authenticate
+ redirect_to(root_path) unless current_user
+ end
+
end
View
7 app/models/user.rb
@@ -12,15 +12,14 @@
#
class User < ActiveRecord::Base
- attr_accessible :email, :password
+ attr_accessible :email, :password, :password_confirmation
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
- validates_presence_of :password, :on => :create
- validates_presence_of :email
- validates_uniqueness_of :email
+ validates_presence_of :password, :password_confirmation, :email
+ validates_uniqueness_of :email, :case_sensitive => false
def self.authenticate(email, password)
user = find_by_email(email)
View
4 app/views/pages/about.html.erb
@@ -1,4 +0,0 @@
-<p>
- <%= app_name %> is a project to make lifes easier for one's operating in waste industry
- with <a href="http://rubyonrails.org/">Ruby on Rails</a>.
-</p>
View
4 app/views/pages/contact.html.erb
@@ -1,4 +0,0 @@
-<p>
- Suggestions? Bugs?
- <a href="mailto:tomas@ebox.lt"> Let me know!</a>
-</p>
View
3  app/views/pages/help.html.erb
@@ -1,3 +0,0 @@
-<p>
- This is help page. If you need any help with <%= app_name %>, <a href="http://www.google.com">click here</a>
-</p>
View
3  app/views/pages/home.html.erb
@@ -1,3 +0,0 @@
-<p>
- This is the home page.
-</p>
View
4 app/views/sessions/new.html.erb
@@ -1,5 +1,5 @@
<h1>Log in</h1>
-<% form_tag sessions_path do %>
+<%= form_tag sessions_path do %>
<p>
<%= label_tag :email %><br />
<%= text_field_tag :email, params[:email] %>
@@ -9,4 +9,4 @@
<%= password_field_tag :password %>
</p>
<p class="button"><%= submit_tag "Log in" %>
-<% end -%>
+<% end %>
View
5 config/application.rb
@@ -8,10 +8,5 @@ module Grays
class Application < Rails::Application
config.encoding = "utf-8"
config.filter_parameters += [:password]
- if Rails.env.test?
- initializer :after => :initialize_dependency_mechanism do
- ActiveSupport::Dependencies.mechanism = :load
- end
- end
end
end
View
1  config/environments/test.rb
@@ -32,5 +32,4 @@
# Print deprecation notices to the stderr
config.active_support.deprecation = :stderr
- config.action_mailer.default_url_options = {:host => 'localhost:3000'}
end
View
8 config/initializers/setup_mail.rb
@@ -1,8 +0,0 @@
-ActionMailer::Base.smtp_settings = {
- :address => "smtp.bit.lt",
- :port => '25',
- :domain => "mydomain.com",
- :authentication => :plain,
- :user_name => "tomas@ebox.lt",
- :password => "tomopastas"
-}
View
39 config/locales/devise.en.yml
@@ -1,39 +0,0 @@
-en:
- errors:
- messages:
- not_found: "not found"
- already_confirmed: "was already confirmed"
- not_locked: "was not locked"
-
- devise:
- failure:
- unauthenticated: 'You need to sign in or sign up before continuing.'
- unconfirmed: 'You have to confirm your account before continuing.'
- locked: 'Your account is locked.'
- invalid: 'Invalid email or password.'
- invalid_token: 'Invalid authentication token.'
- timeout: 'Your session expired, please sign in again to continue.'
- inactive: 'Your account was not activated yet.'
- sessions:
- signed_in: 'Signed in successfully.'
- signed_out: 'Signed out successfully.'
- passwords:
- send_instructions: 'You will receive an email with instructions about how to reset your password in a few minutes.'
- updated: 'Your password was changed successfully. You are now signed in.'
- confirmations:
- send_instructions: 'You will receive an email with instructions about how to confirm your account in a few minutes.'
- confirmed: 'Your account was successfully confirmed. You are now signed in.'
- registrations:
- signed_up: 'You have signed up successfully. If enabled, a confirmation was sent to your e-mail.'
- updated: 'You updated your account successfully.'
- destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.'
- unlocks:
- send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.'
- unlocked: 'Your account was successfully unlocked. You are now signed in.'
- mailer:
- confirmation_instructions:
- subject: 'Confirmation instructions'
- reset_password_instructions:
- subject: 'Reset password instructions'
- unlock_instructions:
- subject: 'Unlock Instructions'
View
11 config/routes.rb
@@ -1,13 +1,12 @@
Grays::Application.routes.draw do
+ get "users/show"
+
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
root :to => "sessions#new"
- resources :sessions
-
-
- match '/contact', :to => 'pages#contact'
- match '/about', :to => 'pages#about'
- match '/help', :to => 'pages#help'
+ resources :sessions, :only => [:new, :create, :destroy]
+ resources :users, :only => [:show]
+
View
87 spec/controllers/pages_controller_spec.rb
@@ -1,87 +0,0 @@
-require 'spec_helper'
-
-describe PagesController do
- #this line is for rendering views inside controller. Without it rspec would test only controller actions. This ensures that if the test passes, the page is really there
- render_views
-
- describe "when logged in" do
-
- before(:each) do
- @base_title = "SLP | "
- end
-
- describe "GET 'home'" do
- it "should be successful" do
- get 'home'
- response.should be_success
- end
-
- it "should have the right title" do
- get 'home'
- response.should have_selector("title", :content => @base_title + "Home")
- end
- end
-
- describe "GET 'contact'" do
- it "should be successful" do
- get 'contact'
- response.should be_success
- end
-
- it "should have the right title" do
- get 'contact'
- response.should have_selector("title", :content => @base_title + "Contact")
- end
- end
-
- describe "GET 'about'" do
- it "should be successful" do
- get 'about'
- response.should be_success
- end
-
- it "should have the right title" do
- get 'about'
- response.should have_selector("title", :content => @base_title + "About")
- end
- end
-
- describe "GET 'help'" do
- it "should be successful" do
- get 'help'
- response.should be_success
- end
-
- it "should have the right title" do
- get 'help'
- response.should have_selector("title", :content => @base_title + "Help")
- end
- end
- after(:each) do
- response.should have_selector("div#framecontentTop")
- response.should have_selector("div#framecontentLeft")
- response.should have_selector("div#framecontentRight")
- end
- end
-
- describe "when not logged in" do
-
- it "should always redirect to login page" do
- get 'help'
- response.should redirect_to(new_user_session_path)
- get 'about'
- response.should redirect_to(new_user_session_path)
- get 'home'
- response.should redirect_to(new_user_session_path)
- get 'contact'
- response.should redirect_to(new_user_session_path)
- end
-
- it "should not have top, left and right menu's" do
- get 'help'
- response.should_not have_selector("div#framecontentTop")
- response.should_not have_selector("div#framecontentLeft")
- response.should_not have_selector("div#framecontentRigh")
- end
- end
-end
View
74 spec/controllers/sessions_controller_spec.rb
@@ -1,12 +1,84 @@
require 'spec_helper'
describe SessionsController do
+ render_views
describe "GET 'new'" do
+
it "should be successful" do
- get 'new'
+ get :new
response.should be_success
end
+
+ it "should have the right title" do
+ get :new
+ response.should have_selector('title', :content => 'Sign in')
+ end
+
+ end
+
+ describe "POST 'create'" do
+
+ context "with wrong credentials" do
+
+ before(:each) do
+ @user = Factory(:user)
+ @attr = {:email => @user.email, :password => "wrongpassword"}
+ end
+
+ it "should re-render the new page" do
+ post :create, :session => @attr
+ response.should render_template('new')
+ end
+
+ it "should have the right title" do
+ post :create, :session => @attr
+ response.should have_selector('title', :content => 'Sign in')
+ end
+
+ it "should have an error message" do
+ post :create, :session => @attr
+ flash.now[:alert] =~ /invalid/i
+ end
+
+ end
+
+ context "with correct credentials" do
+
+ before(:each) do
+ @user = Factory(:user)
+ @attr = {:email => @user.email, :password => @user.password}
+ end
+
+ it "should sign the user in" do
+ post :create, :email => @user.email, :password => @user.password
+ session[:user_id].should == @user.id
+ controller.current_user.should == @user
+ end
+
+ it "should redirect to the user show page" do
+ post :create, :email => @user.email, :password => @user.password
+ response.should redirect_to(user_path(@user))
+ end
+
+ end
+
+ end
+
+ describe "DELETE 'destroy'" do
+ it "should sign a user out when user is signed in" do
+ @user = Factory(:user)
+ post :create, :email => @user.email, :password => @user.password
+ delete :destroy
+ controller.current_user.should be_nil
+ response.should redirect_to(root_path)
+ end
+
+ it "should redirect to root_path when user is not signed in" do
+ controller.current_user.should be_nil
+ delete :destroy
+ response.should redirect_to(root_path)
+ end
end
end
View
43 spec/controllers/users_controller_spec.rb
@@ -1,12 +1,47 @@
require 'spec_helper'
describe UsersController do
+ render_views
- describe "GET 'new'" do
- it "should be successful" do
- get 'new'
- response.should be_success
+ describe "GET 'show'" do
+
+ context "when signed in" do
+
+ before(:each) do
+ test_sign_in(Factory(:user))
+ end
+
+ it "user show should be successful" do
+ get 'show'
+ response.should be_success
+ end
+
+ it "should have the right title" do
+ get 'show'
+ response.should have_selector('title', :content => "Home")
+ end
+
+ it "should have the company name which the user belongs to" do
+ get 'show'
+ response.should have_selector('h1', :content => "Grays Ltd.")
+ end
+
+ it "should show the welcome message for current user" do
+ get 'show'
+ response.should have_selector('div', :content => "Welcome admin@grays.com!")
+ end
+
end
+
+ context "when not signed in" do
+
+ it "should redirect to root_path" do
+ get 'show'
+ response.should redirect_to(root_path)
+ end
+
+ end
+
end
end
View
6 spec/factories.rb
@@ -1,5 +1,5 @@
Factory.define :user do |u|
- u.email 'admin@grays.com'
- u.password 'asdasd'
- u.confirmed_at Time.now
+ u.email "admin@grays.com"
+ u.password "asdasd"
+ u.password_confirmation "asdasd"
end
View
15 spec/helpers/sessions_helper_spec.rb
@@ -1,15 +0,0 @@
-require 'spec_helper'
-
-# Specs in this file have access to a helper object that includes
-# the SessionsHelper. For example:
-#
-# describe SessionsHelper do
-# describe "string concat" do
-# it "concats two strings with spaces" do
-# helper.concat_strings("this","that").should == "this that"
-# end
-# end
-# end
-describe SessionsHelper do
- pending "add some examples to (or delete) #{__FILE__}"
-end
View
15 spec/helpers/users_helper_spec.rb
@@ -1,15 +0,0 @@
-require 'spec_helper'
-
-# Specs in this file have access to a helper object that includes
-# the UsersHelper. For example:
-#
-# describe UsersHelper do
-# describe "string concat" do
-# it "concats two strings with spaces" do
-# helper.concat_strings("this","that").should == "this that"
-# end
-# end
-# end
-describe UsersHelper do
- pending "add some examples to (or delete) #{__FILE__}"
-end
View
4 spec/models/account_spec.rb
@@ -1,5 +1,3 @@
require 'spec_helper'
-describe Account do
- pending "add some examples to (or delete) #{__FILE__}"
-end
+
View
115 spec/models/user_spec.rb
@@ -1,5 +1,118 @@
require 'spec_helper'
describe User do
- pending "add some examples to (or delete) #{__FILE__}"
+
+ before(:each) do
+ @attr = {
+ :email => 'user@example.com',
+ :password => 'asdasd',
+ :password_confirmation => 'asdasd'
+ }
+ end
+
+ it "should create a new instance given a valid attributes" do
+ User.create!(@attr)
+ end
+
+ describe "emails" do
+
+ it "should require email address" do
+ no_email_user = User.new(@attr.merge(:email => ""))
+ no_email_user.should_not be_valid
+ end
+
+ it "should reject duplicate email addresses" do
+ User.create!(@attr)
+ user_with_duplicate_email = User.new(@attr)
+ user_with_duplicate_email.should_not be_valid
+ end
+
+ it "should reject email addresses identical up to case" do
+ upcased_email = @attr[:email].upcase
+ User.create!(@attr.merge(:email => upcased_email))
+ user_with_duplicate_email = User.new(@attr)
+ user_with_duplicate_email.should_not be_valid
+ end
+
+ end
+
+ describe "passwords" do
+
+ before(:each) do
+ @user = User.new(@attr)
+ end
+
+ it "should have a password attribute" do
+ @user.should respond_to(:password)
+ end
+
+ it "should respond to password_confirmation attribute" do
+ @user.should respond_to(:password_confirmation)
+ end
+ end
+
+ describe "password validations" do
+
+ it "should require a password" do
+ User.new(@attr.merge(:password => "", :password_confirmation => "")).should_not be_valid
+ end
+
+ it "should require a matchin password confirmation" do
+ User.new(@attr.merge(:password_confirmation => "invalid")).should_not be_valid
+ end
+
+ end
+
+ describe "password encryption" do
+
+ before(:each) do
+ @user = User.create!(@attr)
+ end
+
+ it "should respond to password_hash attribute" do
+ @user.should respond_to(:password_hash)
+ end
+
+ it "should set the password_hash attribute" do
+ @user.password_hash.should_not be_blank
+ end
+
+ it "should respond to password_salt attribute" do
+ @user.should respond_to(:password_salt)
+ end
+
+ it "should set the password_salt attribute" do
+ @user.password_salt.should_not be_blank
+ end
+
+ describe "#encrypt_password" do
+
+ it "should exist" do
+ @user.should respond_to(:encrypt_password)
+ end
+
+ end
+
+ describe ".authenticate" do
+
+ it "should exist" do
+ User.should respond_to(:authenticate)
+ end
+
+ it "should return nil on email/password missmatch" do
+ User.authenticate(@attr[:email], "wrongpass").should be_nil
+ end
+
+ it "should return nil if email is not registered" do
+ User.authenticate("non_existent@email.com", @attr[:password]).should be_nil
+ end
+
+ it "should return the user on email/password match" do
+ User.authenticate(@attr[:email], @attr[:password]).should == @user
+ end
+
+ end
+
+ end
+
end
View
15 spec/spec_helper.rb
@@ -16,10 +16,19 @@
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
# Needed for Spork
- ActiveSupport::Dependencies.clear
+ ActiveSupport::Dependencies.clear
+
+ def test_sign_in(user)
+ User.authenticate(user.email, user.password)
+ session[:user_id] = user.id
+ end
+
end
end
+
Spork.each_run do
- load "#{Rails.root}/config/routes.rb"
- Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
+
+ load "#{Rails.root}/config/routes.rb"
+ Dir["#{Rails.root}/app/**/*.rb"].each { |f| load f }
+
end
View
5 spec/views/sessions/new.html.erb_spec.rb
@@ -1,5 +0,0 @@
-require 'spec_helper'
-
-describe "sessions/new.html.erb" do
- pending "add some examples to (or delete) #{__FILE__}"
-end
View
5 spec/views/users/new.html.erb_spec.rb
@@ -1,5 +0,0 @@
-require 'spec_helper'
-
-describe "users/new.html.erb" do
- pending "add some examples to (or delete) #{__FILE__}"
-end
Please sign in to comment.
Something went wrong with that request. Please try again.