Forum: RSpec spec for authenticated user

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2009-05-27 13:16
(Received via mailing list)
before_filter :requires_user,
               :except => :create

 def show
  #-------

    # the currently authenticated user
    @user = user

  end


How shall we check the currently authenticated user using rspec?
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2009-05-27 14:27
(Received via mailing list)
On Wed, May 27, 2009 at 7:08 AM, Diwakar, ANGLER - EIT
<diwakar@angleritech.com> wrote:
>
> How shall we check the currently authenticated user using rspec?

What's in requires_user?
76e01275c026bca5c46b2cb5e1ce174a?d=identicon&s=25 Mano ah (manoah)
on 2009-05-27 15:10
(Received via mailing list)
private; def requires_user
    if user.nil?

      if request.format.html?

        session[ :redirected_from ] ||= request.env[ "REQUEST_URI" ]

        redirect_to( sign_in_url )
      else
        render( :status => 403, :text => 'An authenticated user is
required.' )
      end
    end

  end
49de4cd2f26705785cbef2b15a9df7aa?d=identicon&s=25 Nick Hoffman (nickh)
on 2009-05-28 17:09
(Received via mailing list)
>>
>>     # the currently authenticated user
>>     @user = user
>>
>>   end
>>
>> How shall we check the currently authenticated user using rspec?
>
> What's in requires_user?

On Wed, May 27, 2009 at 8:40 AM, Diwakar, ANGLER - EIT
<diwakar@angleritech.com> wrote:
>      end
>    end
>
>  end

What's setting the "user" variable before #requires_user is called? Or
is "user" a method inside that controller?

Ignoring that question for a moment, all you need to do is write out
(on paper/in Vim/whatever) the behaviour of each scenario that can
occur while traversing through this controller. For example, one
[verbose] scenario is:

"When a user who's not logged-in and whose session isn't being
redirected requests HTML from FooBarsController#show , they should be
redirected to the sign-up page."

Once you have the behaviour of each scenario mapped out, write specs
for each scenario.

When that's all done, I recommend writing your specs before you write
code.
-Nick
This topic is locked and can not be replied to.