From 82b084abc25656398d895e690f62ccde42f2257d Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Tue, 8 Dec 2015 01:35:26 -0500
Subject: Add Gemfile from branch 'solution'
---
Gemfile | 3 +++
Gemfile.lock | 27 +++++++++++++++++++++++++++
2 files changed, 30 insertions(+)
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
--
cgit v1.2.3
From 6f4e62ff0f3c21807afbc7e1c7a62f0dadadde00 Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Tue, 8 Dec 2015 01:36:10 -0500
Subject: Add Rakefile from branch 'solution'
---
Rakefile | 9 +++++++++
1 file changed, 9 insertions(+)
create mode 100644 Rakefile
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
--
cgit v1.2.3
From f6b6a0ddd8cb1bf45190a8f204b0903da8e9ba40 Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Tue, 8 Dec 2015 01:38:27 -0500
Subject: Add views/ from branch 'solution'
---
views/index.erb | 15 +++++++++++++++
views/layout.erb | 10 ++++++++++
views/logged_in.erb | 1 +
3 files changed, 26 insertions(+)
create mode 100644 views/index.erb
create mode 100644 views/layout.erb
create mode 100644 views/logged_in.erb
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 @@
+
Login
+
+
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 @@
+
+
+
+
+ Ruby Session Exercise
+
+
+ <%= yield %>
+
+
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!
--
cgit v1.2.3
From f3a35d4ff0606ea59aecd67c5daa01ccf51b01a8 Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Tue, 8 Dec 2015 01:38:50 -0500
Subject: Add test/ from branch 'solution'
---
test/integration/auth_test.rb | 43 +++++++++++++++++++++++++++++++++++++++++
test/integration_test_helper.rb | 18 +++++++++++++++++
2 files changed, 61 insertions(+)
create mode 100644 test/integration/auth_test.rb
create mode 100644 test/integration_test_helper.rb
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
--
cgit v1.2.3
From 8ef7b1279d566f4907be99dfc09c01556e13f770 Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Tue, 8 Dec 2015 01:43:39 -0500
Subject: Add app.rb from branch 'solution'
This file has been cleaned from its original implementation in the
'solution' branch to remove the solution to the exercise dealing with
the session.
Comments are added in spots where additional code must be filled in.
---
app.rb | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
create mode 100644 app.rb
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
--
cgit v1.2.3
From 968b1b131972738e82cd4a099bf3d75f3dc67969 Mon Sep 17 00:00:00 2001
From: Teddy Wing
Date: Tue, 8 Dec 2015 01:47:54 -0500
Subject: Add Makefile
Provide a task that runs the development server.
---
Makefile | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 Makefile
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'
--
cgit v1.2.3