简体   繁体   中英

How to use cucumber to test authentication in a sinatra application?

I am doing a BDD/TDD approach to writing my sinatra application. I would like to add authentication. I currently have a feature file that looks like this:

Scenario: Unauthenticated redirects to login page
  Given I am not logged in
  When I go to the homepage
  Then I should be redirected to the login page

My steps look like this:

Given /^I am not logged in$/ do
  # not sure how to ensure this
end

When /^I go to the homepage$/ do
  visit '/'
end

Then /^I should be redirected to the login page$/ do
  current_path.should == '/auth/login'
end

I am already setting up my app in support/env.rb :

require 'capybara/cucumber'
Capybara.app = MySintraApp

And my app looks like this:

class MySinatraApp < Sinatra::Base
  get '/' do
    redirect '/auth/login' #todo: unless logged_in?
    haml :index
  end

  get '/auth/login' do
    haml :login
  end
end

How do I implement the step to ensure "not logged in"? How would I start to implement the login functionality in a way that would be in a BDD/TDD style?

One way I've checked a user isn't logged in is to check whether the page displays a "log in" link/button (and associated functions). You can do the converse to see if a user is logged in. For example, whether the user's name is displayed in the banner portion of the page.

It seems the best way to me, because that's how a user would ascertain it, and it's all about their behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM