From 2b32f184970411da3865ba6cb504d2d5401af8c9 Mon Sep 17 00:00:00 2001 From: Wes Delp Date: Wed, 8 Jun 2016 12:13:29 -0400 Subject: [PATCH] Finished Capybara101 --- Gemfile.lock | 14 +++++++++ app.rb | 9 ++++-- features/login.feature | 11 ++++++- features/logout.feature | 10 ++++++- features/step_definitions/login_steps.rb | 36 +++++++++++++++++++---- features/step_definitions/logout_steps.rb | 15 +++++++++- 6 files changed, 84 insertions(+), 11 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index da8b141..7bbfbc8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,10 +40,19 @@ GEM rack rack-test (0.6.2) rack (>= 1.0) + rspec (3.1.0) + rspec-core (~> 3.1.0) + rspec-expectations (~> 3.1.0) + rspec-mocks (~> 3.1.0) + rspec-core (3.1.7) + rspec-support (~> 3.1.0) rspec-expectations (3.1.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.1.0) + rspec-mocks (3.1.3) + rspec-support (~> 3.1.0) rspec-support (3.1.2) + sass (3.4.22) sinatra (1.4.5) rack (~> 1.4) rack-protection (~> 1.4) @@ -66,5 +75,10 @@ DEPENDENCIES cucumber-sinatra haml pry + rspec rspec-expectations + sass sinatra + +BUNDLED WITH + 1.12.5 diff --git a/app.rb b/app.rb index 670284d..7d3c4b7 100644 --- a/app.rb +++ b/app.rb @@ -31,7 +31,12 @@ def logged_in? end get "/dashboard" do - @username = session[:user] - haml :dashboard + if logged_in? + @username = session[:user] + haml :dashboard + else + @message = 'Access Denied: Please login to view page' + haml :index + end end end diff --git a/features/login.feature b/features/login.feature index 3fff7cf..e0d54b4 100644 --- a/features/login.feature +++ b/features/login.feature @@ -7,4 +7,13 @@ Feature: Login Page Given the login page When I log in with proper credentials Then I should see the secret page - + + Scenario: Un-Authenticated User + Given the login page + When I log in with improper credentials + Then I should see the error message + + Scenario: No Credentials + Given the login page + When I visit secret page with no credentials + Then I should see the login page with error diff --git a/features/logout.feature b/features/logout.feature index 251c445..1fef6ca 100644 --- a/features/logout.feature +++ b/features/logout.feature @@ -1 +1,9 @@ -#Add your codez here +Feature: Logout Page + In order to keep my page secret after using + As a user that is logged in + I want to log out from my private page + + Scenario: Authenticated User + Given the secret page + When I log out + Then I should see the login page with logout message diff --git a/features/step_definitions/login_steps.rb b/features/step_definitions/login_steps.rb index 61f803a..fb63f07 100644 --- a/features/step_definitions/login_steps.rb +++ b/features/step_definitions/login_steps.rb @@ -1,14 +1,38 @@ Given(/^the login page$/) do - #put your code here - pending + visit '/' end +UserPass = 'Wes' + When(/^I log in with proper credentials$/) do - #put your code here - pending + fill_in('username', with: UserPass) + fill_in('password', with: UserPass) + + click_button('Login') end Then(/^I should see the secret page$/) do - #put your code here - pending + expect(page).to have_content('This is the secret page.') + expect(page).to have_content("Your username is #{UserPass}.") +end + +WrongPass = 'Wess' + +When(/^I log in with improper credentials$/) do + fill_in('username', with: UserPass) + fill_in('password', with: WrongPass) + + click_button('Login') +end + +Then(/^I should see the error message$/) do + expect(page).to have_content('Your username & password did not match') +end + +When(/^I visit secret page with no credentials$/) do + visit '/dashboard' +end + +Then(/^I should see the login page with error$/) do + expect(page).to have_content('Access Denied: Please login to view page') end diff --git a/features/step_definitions/logout_steps.rb b/features/step_definitions/logout_steps.rb index 251c445..8bc0c15 100644 --- a/features/step_definitions/logout_steps.rb +++ b/features/step_definitions/logout_steps.rb @@ -1 +1,14 @@ -#Add your codez here +Given(/^the secret page$/) do + step 'the login page' + step 'I log in with proper credentials' + step 'I should see the secret page' +end + +When(/^I log out$/) do + click_link('Logout') +end + +Then(/^I should see the login page with logout message$/) do + expect(page).to have_content('This is login page for users.') + expect(page).to have_content('You have been logged out') +end