diff --git a/.gitignore b/.gitignore index 8b7a449..8a557d3 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ *.komodoproject .DS_Store +.project diff --git a/Gemfile b/Gemfile index f56b307..2d1b02d 100644 --- a/Gemfile +++ b/Gemfile @@ -42,8 +42,13 @@ group :test, :development do gem 'guard' gem 'guard-spork' + gem 'rb-readline' + gem 'guard-rspec' gem 'rspec-rails' + + gem 'capybara' gem 'growl', :require => false + gem 'ruby_gntp' end diff --git a/Gemfile.lock b/Gemfile.lock index f01e681..dd0081e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -33,6 +33,15 @@ GEM arel (2.2.1) bcrypt-ruby (3.0.1) builder (3.0.0) + capybara (1.1.2) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + selenium-webdriver (~> 2.0) + xpath (~> 0.1.4) + childprocess (0.2.4) + ffi (~> 1.0.6) coffee-rails (3.1.1) coffee-script (>= 2.2.0) railties (~> 3.1.0) @@ -75,6 +84,7 @@ GEM treetop (~> 1.4.8) mime-types (1.17.2) multi_json (1.0.4) + nokogiri (1.5.0) orm_adapter (0.0.5) polyglot (0.3.3) rack (1.3.5) @@ -102,6 +112,7 @@ GEM rdoc (~> 3.4) thor (~> 0.14.6) rake (0.9.2.2) + rb-readline (0.4.2) rdoc (3.12) json (~> 1.4) rspec (2.7.0) @@ -117,12 +128,19 @@ GEM activesupport (~> 3.0) railties (~> 3.0) rspec (~> 2.7.0) + ruby_gntp (0.3.4) + rubyzip (0.9.5) sass (3.1.12) sass-rails (3.1.5) actionpack (~> 3.1.0) railties (~> 3.1.0) sass (~> 3.1.10) tilt (~> 1.3.2) + selenium-webdriver (2.15.0) + childprocess (>= 0.2.1) + ffi (~> 1.0.9) + multi_json (~> 1.0.4) + rubyzip spork (0.9.0.rc9) sprockets (2.0.3) hike (~> 1.2) @@ -142,11 +160,14 @@ GEM multi_json (>= 1.0.2) warden (1.1.0) rack (>= 1.0) + xpath (0.1.4) + nokogiri (~> 1.3) PLATFORMS ruby DEPENDENCIES + capybara coffee-rails (~> 3.1.1) devise factory_girl_rails @@ -156,7 +177,9 @@ DEPENDENCIES guard-spork jquery-rails rails (= 3.1.3) + rb-readline rspec-rails + ruby_gntp sass-rails (~> 3.1.5) spork (>= 0.9.0.rc8) sqlite3 diff --git a/app/views/application/_header.html.erb b/app/views/application/_header.html.erb index 53a603b..5db3e59 100644 --- a/app/views/application/_header.html.erb +++ b/app/views/application/_header.html.erb @@ -18,7 +18,7 @@ <%= form_tag user_session_path do %> <%= text_field_tag "user[email]", '', class: "input-small", placeholder: "Email" %> <%= password_field_tag "user[password]", '', class: "input-small", placeholder: "Password" %> - <%= button_tag "Sign in", class: "btn" %> + <%= button_tag "Sign in", class: "btn", id: "header_sign_in" %> <% end %> <% end %> diff --git a/spec/factories/users.rb b/spec/factories/users.rb index d2dd4de..c6817f5 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -2,5 +2,7 @@ FactoryGirl.define do factory :user do + sequence(:email) { |n| "test#{n}@example.com" } + password "secret" end end diff --git a/spec/integration/sign_in_spec.rb b/spec/integration/sign_in_spec.rb new file mode 100644 index 0000000..b7bd6d9 --- /dev/null +++ b/spec/integration/sign_in_spec.rb @@ -0,0 +1,78 @@ +require 'spec_helper' + +describe 'home page' do + it 'logs in with invalid credentials' do + visit '/' + + user = create(:user) + + fill_in('user[email]', :with => user.email) + fill_in('user[password]', :with => 'this_is_not_a_valid_password') + click_button('header_sign_in') + + current_path.should == '/users/sign_in' + end + + it 'logs in and out using valid credentials' do + visit '/' + + user = create(:user) + + fill_in('user[email]', :with => user.email) + fill_in('user[password]', :with => user.password) + click_button('header_sign_in') + + current_path.should == '/' + page.should have_content(user.email) + + click_link('Sign out') + + current_path.should == '/' + page.should have_no_content(user.email) + end + + it 'logs in with invalid credentails and then logs into /users/sign_in with invalid credentials' do + visit '/' + + user = create(:user) + + fill_in('user[email]', :with => user.email) + fill_in('user[password]', :with => 'this_is_not_a_valid_password') + click_button('header_sign_in') + + current_path.should == '/users/sign_in' + + within("form[@id='user_new']") do + fill_in('user[email]', :with => 'testing') + fill_in('user[password]', :with => 'testing') + click_button('Sign in') + end + + current_path.should == '/users/sign_in' + end + + it 'logs in with invalid credentails and then logs into /users/sign_in with valid credentials' do + visit '/' + + user = create(:user) + + fill_in('user[email]', :with => user.email) + fill_in('user[password]', :with => 'this_is_not_a_valid_password') + click_button('header_sign_in') + + current_path.should == '/users/sign_in' + + within("form[@id='user_new']") do + fill_in('user[email]', :with => user.email) + fill_in('user[password]', :with => user.password) + click_button('Sign in') + end + + current_path.should == '/' + + click_link('Sign out') + + current_path.should == '/' + page.should have_no_content(user.email) + end +end \ No newline at end of file diff --git a/spec/integration/signup_spec.rb b/spec/integration/signup_spec.rb new file mode 100644 index 0000000..ed54f11 --- /dev/null +++ b/spec/integration/signup_spec.rb @@ -0,0 +1,63 @@ +require 'spec_helper' + +# simple sign up macro for a user +def sign_up(user) + within "#user_new" do + fill_in "user_email", with: user.email + fill_in "user_password", with: user.password + fill_in "user_password_confirmation", with: user.password_confirmation + + click_button 'Sign up' + end +end + +describe "the signup process", type: :request do + + context "with valid parameters" do + before :each do + @user = FactoryGirl.build(:user) + end + + it "signs me up" do + visit new_user_registration_path + + @user.password_confirmation = @user.password + sign_up(@user) + + page.should have_content(@user.email) + current_path.should eq(root_path) + end + end + + context "with invalid parameters" do + before do + visit new_user_registration_path + end + + it "should require an email" do + user = build(:user, email: nil) + sign_up user + page.should have_content("Email can't be blank") + end + + it "should require a password" do + user = build(:user, password: nil) + sign_up user + page.should have_content("Password can't be blank") + end + + it "should confirm the password" do + user = build(:user) + sign_up user + page.should have_content("Password doesn't match confirmation") + end + + it "should not allow a user to sign up more than once" do + first = create(:user) + user = build(:user, email: first.email) + sign_up user + page.should have_content("Email has already been taken") + end + end + +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 44032b4..9b3fcbc 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,5 +1,20 @@ require 'spec_helper' describe User do - pending "add some examples to (or delete) #{__FILE__}" + + it "should require an email address" do + user = build(:user, email: nil) + user.should_not be_valid + end + + it "should only allow a valid email" do + user = build(:user, email: "bad/email") + user.should_not be_valid + end + + it "should require a password" do + user = build(:user, password: nil) + user.should_not be_valid + end + end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d0885bc..b0c9b38 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,9 @@ require 'rspec/rails' require 'rspec/autorun' + # Add this to load Capybara integration: + require 'capybara/rspec' + # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} @@ -34,5 +37,8 @@ # Require Factory Girls config.include FactoryGirl::Syntax::Methods + + # include devise test helpers + config.include Devise::TestHelpers, :type => :controller end -end \ No newline at end of file +end