Skip to content

Commit

Permalink
HW 6
Browse files Browse the repository at this point in the history
implementation Tests written
  • Loading branch information
niehusst authored Feb 26, 2020
2 parents 5fd8517 + 4cfadc1 commit 5a6796a
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 11 deletions.
2 changes: 1 addition & 1 deletion test/controllers/books_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class BooksControllerTest < ActionDispatch::IntegrationTest
setup do
@book = books(:one)
@book = books(:book1)
end

test "should get index" do
Expand Down
42 changes: 42 additions & 0 deletions test/controllers/devise_pages_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'test_helper'

class DevisePagesControllerTest < ActionDispatch::IntegrationTest
include Devise::Test::IntegrationHelpers
#< Capybara::Rails::TestCase
# include ActiveJob::TestHelper
def setup
@base_title = "BookRecommender"
end
Expand All @@ -16,4 +19,43 @@ def setup
assert_response :success
assert_select "title", "Sign up | #{@base_title}"
end

test "should let user create session" do
get '/users/sign_in'
sign_in users(:user1)
post user_session_url

follow_redirect!
assert_response :success, "Didn't redirect to login"
end

# test "shouldnt let user start session" do
## assert_raises(StandardError) do
# visit '/users/sign_in'
##bad_user = User.new({:email => "[email protected]", :password => "securepassword", :password_confirmation => "securepassword" })#.save(:validate => false)
## sign_in bad_user
##User.create!({:email => "[email protected]", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" })
# fill_in 'Email', with: '[email protected]'
# fill_in 'Password', with: 'securepassword'
#
# click_on 'Log in'
#
# assert_response :success, "Bad email accepted"
# assert_select 'title', /Log in/
# #flunk if commands succeed
##flunk "Not existing user recognized"
## end
#
## flunk "Bad password accepted"
## flunk "User was already logged in"
# end
#
# test "session end success" do
# flunk "Logged in user not logged out"
# end
#
# test "session end failure" do
# flunk "Not logged in user logged out"
# end

end
2 changes: 1 addition & 1 deletion test/fixtures/books.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
one:
book1:
id: 1
title: booko
author: jim
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
one:
user1:
id: 1
email: [email protected]
encrypted_password: "$2a$11$xuWIlN7/PUa5/aQK.cwe5ecbCfyo8mR6taYyZW5SaN1pYkf.cQK2y"
email: [email protected]
encrypted_password: <%= User.new.send(:password_digest, "securepassword") %>
reset_password_token:
reset_password_sent_at:
remember_created_at:
Expand Down
Binary file not shown.
201 changes: 201 additions & 0 deletions test/integration/devise_integration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
require 'test_helper'

class DeviseIntegrationTest< Capybara::Rails::TestCase

test "bad email start session" do
visit '/users/sign_in'

fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'securepassword'

click_on 'Log in'

assert_current_path '/users/sign_in'
assert page.has_content?('Log in')

end

test "bad password start session" do
visit '/users/sign_in'

fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'insecurepassword'

click_on 'Log in'

assert_current_path '/users/sign_in'
assert page.has_content?('Log in')
end

test "double login" do
visit '/users/sign_in'

fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'securepassword'

click_on 'Log in'

assert_selector 'h1', text:'Recommend me a book...'

#attempt to sign in again
visit '/users/sign_in'
assert_selector 'h1', text:'Recommend me a book...'
end

test "session end success" do
visit '/users/sign_in'

fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'securepassword'

click_on 'Log in'

assert_selector 'h1', text: 'Recommend me a book...'

click_on 'Logout'
assert_selector 'h1', text: 'Get some fresh Reads!'
end

test "create valid user" do
visit '/'

within('.centered') do
click_on 'Sign up'
end

fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'goodpassword'
fill_in 'Password confirmation', with: 'goodpassword'

click_button 'Sign up'

assert_selector 'h1', text: 'Recommend me a book...'

#assert user was put in db too
end

test "create invalid email user" do
visit '/'

within('.centered') do
click_on 'Sign up'
end

fill_in 'Email', with: 'goodemail.com'
fill_in 'Password', with: 'goodpassword'
fill_in 'Password confirmation', with: 'goodpassword'

click_button 'Sign up'

#assert did not go anywhere
assert_selector 'h2', text: 'Sign up'
end

test "create invalid password mismatch user" do
visit '/'

within('.centered') do
click_on 'Sign up'
end

fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'goodpassword'
fill_in 'Password confirmation', with: 'gooderpassword'

click_button 'Sign up'

assert page.has_content?("Password confirmation doesn't match Password")
end

test "create invalid password user" do
visit '/'

within('.centered') do
click_on 'Sign up'
end

fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'bad'
fill_in 'Password confirmation', with: 'bad'

click_button 'Sign up'

assert page.has_content?("Password is too short (minimum is 6 characters)")
end

test "create invalid empty user" do
visit '/'

within('.centered') do
click_on 'Sign up'
end

fill_in 'Email', with: ''
fill_in 'Password', with: ''
fill_in 'Password confirmation', with: ''

click_button 'Sign up'

assert page.has_content?("Email can't be blank")
assert page.has_content?("Password can't be blank")
end

test "create invalid identical user" do
visit '/'

within('.centered') do
click_on 'Sign up'
end

fill_in 'Email', with: '[email protected]'
fill_in 'Password', with: 'goodpassword'
fill_in 'Password confirmation', with: 'goodpassword'

click_button 'Sign up'

assert page.has_content?("Email has already been taken")
end

test "unlogged in navbar links" do
visit '/'

within('nav') do
click_on 'Sign up'
end
assert_selector 'h2', text: 'Sign up'

within('nav') do
click_on 'Login'
end
assert_selector 'h2', text: 'Log in'

within('nav') do
find('.navbar-brand').click
end
assert_selector 'h1', text: 'Get some fresh Reads!'
end

test "logged in navbar links" do
sign_in users(:user1)

visit '/'

click_on 'About'
assert_selector 'h1', text: 'About'

click_on 'Recommendations'
assert_selector 'h1', text: 'Recommend me a book...'

# click_on 'Profile'
# assert_selector 'h1', text: 'Reading List'

within('nav') do
find('.navbar-brand').click
end
assert_selector 'h1', text: 'Recommend me a book...'

click_on 'Logout'
assert_selector 'h1', text: 'Get some fresh Reads!'
end

end
14 changes: 11 additions & 3 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test "create valid user" do
skip "Valid credentials bad"
end

test "create invalid user" do
skip "Bad email was good"
flunk "Bad password was good"
flunk "Empty form was accepted"
flunk "Identical user emails created"
end

end
34 changes: 31 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/rails/capybara"

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all

# Add more helper methods to be used by all tests here...
include Devise::Test::IntegrationHelpers
include Warden::Test::Helpers

def log_in(user)
if integration_test?
#use warden
login_as(user, :scope => :user)
else
#use devise
sign_in(user)
end
end
end

require 'capybara/rails'
require 'capybara/minitest'
#
#class ActionDispatch::IntegrationTest
# # Make the Capybara DSL available in all integration tests
# include Capybara::DSL
# # Make `assert_*` methods behave like Minitest assertions
# include Capybara::Minitest::Assertions
#
# # Reset sessions and driver between tests
# teardown do
# Capybara.reset_sessions!
# Capybara.use_default_driver
# end
#end

0 comments on commit 5a6796a

Please sign in to comment.