aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2015-12-08 01:49:50 -0500
committerTeddy Wing2015-12-08 01:49:50 -0500
commit29aaafedef9f34c4ba62fb481fc5e6ba711c7eb7 (patch)
treea87a4268b16e0fe3e0d077c969d4ad8ce2bf0e52
parentd0e7441545f66aba4e0ba1af870978b675f5fa09 (diff)
parent968b1b131972738e82cd4a099bf3d75f3dc67969 (diff)
downloadRuby-Web-Sessions-Exercise-29aaafedef9f34c4ba62fb481fc5e6ba711c7eb7.tar.bz2
Merge branch 'cleaned-for-exercise'
-rw-r--r--Gemfile3
-rw-r--r--Gemfile.lock27
-rw-r--r--Makefile2
-rw-r--r--Rakefile9
-rw-r--r--app.rb29
-rw-r--r--test/integration/auth_test.rb43
-rw-r--r--test/integration_test_helper.rb18
-rw-r--r--views/index.erb15
-rw-r--r--views/layout.erb10
-rw-r--r--views/logged_in.erb1
10 files changed, 157 insertions, 0 deletions
diff --git a/Gemfile b/Gemfile
index c10abd7..bdb7087 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,3 +1,6 @@
source 'https://rubygems.org'
gem 'sinatra'
+gem 'rerun'
+
+gem 'capybara'
diff --git a/Gemfile.lock b/Gemfile.lock
index ad27301..ae43b5e 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -1,17 +1,44 @@
GEM
remote: https://rubygems.org/
specs:
+ capybara (2.5.0)
+ mime-types (>= 1.16)
+ nokogiri (>= 1.3.3)
+ rack (>= 1.0.0)
+ rack-test (>= 0.5.4)
+ xpath (~> 2.0)
+ ffi (1.9.10)
+ listen (3.0.5)
+ rb-fsevent (>= 0.9.3)
+ rb-inotify (>= 0.9)
+ mime-types (3.0)
+ mime-types-data (~> 3.2015)
+ mime-types-data (3.2015.1120)
+ mini_portile2 (2.0.0)
+ nokogiri (1.6.7)
+ mini_portile2 (~> 2.0.0.rc2)
rack (1.6.4)
rack-protection (1.5.3)
rack
+ rack-test (0.6.3)
+ rack (>= 1.0)
+ rb-fsevent (0.9.6)
+ rb-inotify (0.9.5)
+ ffi (>= 0.5.0)
+ rerun (0.11.0)
+ listen (~> 3.0)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
tilt (2.0.1)
+ xpath (2.0.0)
+ nokogiri (~> 1.3)
PLATFORMS
ruby
DEPENDENCIES
+ capybara
+ rerun
sinatra
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..25f0dcf
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,2 @@
+run:
+ bundle exec rerun 'ruby app.rb'
diff --git a/Rakefile b/Rakefile
new file mode 100644
index 0000000..4308fd1
--- /dev/null
+++ b/Rakefile
@@ -0,0 +1,9 @@
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+ t.libs << '.'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+end
+
+task :default => :test
diff --git a/app.rb b/app.rb
new file mode 100644
index 0000000..1f52920
--- /dev/null
+++ b/app.rb
@@ -0,0 +1,29 @@
+require 'sinatra'
+
+
+use Rack::Session::Cookie, :secret => "My session secret which shouldn't be committed to the repo in real life"
+
+get '/' do
+ # The root route should render the :index template when logged out and the
+ # :logged_in template when logged in.
+
+ erb :index
+end
+
+post '/' do
+ if params[:username] == 'hubertfarnsworth' &&
+ params[:password] == 'secret'
+ # The user has correctly authenticated. Persist their login status by
+ # setting a session key here.
+
+ redirect '/'
+ else
+ 'Unrecognized user'
+ end
+end
+
+get '/logout' do
+ # Unset the session here to log the user out.
+
+ redirect '/'
+end
diff --git a/test/integration/auth_test.rb b/test/integration/auth_test.rb
new file mode 100644
index 0000000..7fa2950
--- /dev/null
+++ b/test/integration/auth_test.rb
@@ -0,0 +1,43 @@
+require 'integration_test_helper'
+
+
+class TestAuth < CapybaraTestCase
+ def login(username, password)
+ visit '/'
+ fill_in 'username', :with => username
+ fill_in 'password', :with => password
+ click_button 'Login'
+ end
+
+ def login_with_correct_credentials
+ login('hubertfarnsworth', 'secret')
+ end
+
+
+ def test_index_has_login_form
+ visit '/'
+ assert_text 'Login'
+ assert has_selector?('//form')
+ end
+
+ def test_unrecognised_user_login
+ login('unregistered', 'password')
+
+ assert_text 'Unrecognized user'
+ end
+
+ def test_login
+ login_with_correct_credentials
+
+ assert_equal 200, page.status_code
+ assert_text "You're logged in!"
+ end
+
+ def test_logout
+ login_with_correct_credentials
+ visit '/logout'
+
+ assert_text 'Login'
+ assert_equal '/', current_path
+ end
+end
diff --git a/test/integration_test_helper.rb b/test/integration_test_helper.rb
new file mode 100644
index 0000000..609c08b
--- /dev/null
+++ b/test/integration_test_helper.rb
@@ -0,0 +1,18 @@
+ENV['RACK_ENV'] = 'test'
+
+require 'minitest/autorun'
+require 'capybara'
+
+require 'app'
+
+
+class CapybaraTestCase < Minitest::Unit::TestCase
+ include Capybara::DSL
+
+ def teardown
+ Capybara.reset_sessions!
+ Capybara.use_default_driver
+ end
+end
+
+Capybara.app = Sinatra::Application.new
diff --git a/views/index.erb b/views/index.erb
new file mode 100644
index 0000000..ff06792
--- /dev/null
+++ b/views/index.erb
@@ -0,0 +1,15 @@
+<h1>Login</h1>
+
+<form action="." method="post">
+ <div>
+ Username: <input type="text" name="username" />
+ </div>
+
+ <div>
+ Password: <input type="password" name="password" />
+ </div>
+
+ <div>
+ <input type="submit" value="Login" />
+ </div>
+</form>
diff --git a/views/layout.erb b/views/layout.erb
new file mode 100644
index 0000000..e4a6105
--- /dev/null
+++ b/views/layout.erb
@@ -0,0 +1,10 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <title>Ruby Session Exercise</title>
+</head>
+<body>
+ <%= yield %>
+</body>
+</html>
diff --git a/views/logged_in.erb b/views/logged_in.erb
new file mode 100644
index 0000000..df9b7f4
--- /dev/null
+++ b/views/logged_in.erb
@@ -0,0 +1 @@
+You're logged in!