A test_sign_in function to simulate user signin inside tests in Java

Creation Data Matrix ECC200 in Java A test_sign_in function to simulate user signin inside tests
Listing 919 A test_sign_in function to simulate user signin inside tests
DataMatrix Encoder In Java
Using Barcode generator for Java Control to generate, create Data Matrix ECC200 image in Java applications.
spec/spec_helperrb
Printing Barcode In Java
Using Barcode encoder for Java Control to generate, create barcode image in Java applications.
Rspecconfigure do |config| def test_sign_in(user) controllersign_in(user) end end
Barcode Reader In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
After running test_sign_in, the current_user will not be nil, so signed_in will be true
Data Matrix ECC200 Generation In Visual C#
Using Barcode maker for Visual Studio .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
10 If you are using Spork, this will be located inside the Sporkprefork block
Paint Data Matrix ECC200 In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
Signing Out
Data Matrix 2d Barcode Generation In VS .NET
Using Barcode generation for .NET framework Control to generate, create ECC200 image in VS .NET applications.
With this spec helper in hand, the test for signout is straightforward: sign in as a (factory) user and then hit the destroy action and verify that the user gets signed out (Listing 920)
Data Matrix 2d Barcode Printer In VB.NET
Using Barcode creation for Visual Studio .NET Control to generate, create Data Matrix image in .NET framework applications.
Listing 920 A test for destroying a session (user signout)
UPC-A Supplement 5 Creator In Java
Using Barcode encoder for Java Control to generate, create UPC-A Supplement 5 image in Java applications.
spec/controllers/sessions_controller_specrb
EAN-13 Supplement 5 Generator In Java
Using Barcode generation for Java Control to generate, create EAN / UCC - 13 image in Java applications.
describe SessionsController do describe "DELETE 'destroy'" do it "should sign a user out" do test_sign_in(Factory(:user)) delete :destroy controllershould_not be_signed_in responseshould redirect_to(root_path) end end end
Barcode Drawer In Java
Using Barcode generator for Java Control to generate, create barcode image in Java applications.
The only novel element here is the delete method, which issues an HTTP DELETE request (in analogy with the get and post methods seen in previous tests), as required by the REST conventions (Table 91) As with user signin, which relied on the sign_in function, user signout just defers the hard work to a sign_out function (Listing 921)
DataMatrix Generator In Java
Using Barcode generation for Java Control to generate, create ECC200 image in Java applications.
Listing 921 Destroying a session (user signout)
Code 128A Printer In Java
Using Barcode encoder for Java Control to generate, create Code-128 image in Java applications.
app/controllers/sessions_controllerrb
Drawing British Royal Mail 4-State Customer Code In Java
Using Barcode encoder for Java Control to generate, create RoyalMail4SCC image in Java applications.
class SessionsController < ApplicationController def destroy sign_out redirect_to root_path end end
USS Code 39 Generator In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create Code 39 Full ASCII image in ASP.NET applications.
As with the other authentication elements, we ll put sign_out in the Sessions helper module (Listing 922)
Generating GS1 - 13 In .NET
Using Barcode generator for ASP.NET Control to generate, create EAN13 image in ASP.NET applications.
9: Sign In, Sign Out
Universal Product Code Version A Printer In .NET
Using Barcode maker for VS .NET Control to generate, create UPC A image in Visual Studio .NET applications.
Listing 922 The sign_out method in the Sessions helper module
Bar Code Maker In .NET
Using Barcode generation for ASP.NET Control to generate, create barcode image in ASP.NET applications.
app/helpers/sessions_helperrb
Making Bar Code In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create bar code image in Visual Studio .NET applications.
module SessionsHelper def sign_in(user) cookiespermanentsigned[:remember_token] = [userid, usersalt] current_user = user end def sign_out cookiesdelete(:remember_token) current_user = nil end private end
Print Barcode In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.
As you can see, the sign_out method effectively undoes the sign_in method by deleting the remember token and by setting the current user to nil11
EAN 13 Reader In VS .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET applications.
942 Signin Upon Signup
In principle, we are now done with authentication, but as currently constructed there are no links to the signin or signout actions Moreover, newly registered users might be confused, as they are not signed in by default We ll fix the second problem first, starting with testing that a new user is automatically signed in (Listing 923)
Listing 923 Testing that newly signed-up users are also signed in
spec/controllers/users_controller_specrb
require 'spec_helper' describe UsersController do
11 You can learn about things like cookiesdelete by reading the cookies entry in the Rails API (Since Rails API links tend to go stale quickly, use your Google-fu to find a current version)
Signing Out
render_views describe "POST 'create'" do describe "success" do it "should sign the user in" do post :create, :user => @attr controllershould be_signed_in end end end end
With the sign_in method from Section 93, getting this test to pass by actually signing in the user is easy: just add sign_in @user right after saving the user to the database (Listing 924)
Listing 924 Signing in the user upon signup
app/controllers/users_controllerrb
class UsersController < ApplicationController def create @user = Usernew(params[:user]) if @usersave sign_in @user flash[:success] = "Welcome to the Sample App!" redirect_to @user else @title = "Sign up" render 'new' end end
9: Sign In, Sign Out
943 Changing the Layout Links
We come finally to a practical application of all our signin/out work: we ll change the layout links based on signin status In particular, as seen in the Figure 96 mockup, we ll arrange for the links change when users sign in or sign out, and we ll also add a profile link to the user show page for signed-in users We start with two integration tests: one to check that a "Sign in" link appears for non-signed-in users, and one to check that a "Sign out" link appears for signed-in users; both cases verify that the link goes to the proper URL We ll put these tests in the layout links test we created in Section 521; the result appears in Listing 925
Listing 925 Tests for the signin/signout links on the site layout
spec/requests/layout_links_specrb
describe "Layout links" do describe "when not signed in" do it "should have a signin link" do visit root_path responseshould have_selector("a", :href => signin_path, :content => "Sign in") end end describe "when signed in" do before(:each) do @user = Factory(:user) visit signin_path fill_in :email, :with => @useremail fill_in :password, :with => @userpassword click_button end it "should have a signout link" do visit root_path responseshould have_selector("a", :href => signout_path, :content => "Sign out") end it "should have a profile link" end end
Signing Out
Here the before(:each) block signs in by visiting the signin page and submitting a valid email/password pair12 We do this instead of using the test_sign_in function from Listing 919 because test_sign_in doesn t work inside integration tests for some reason (See Section 96 for an exercise to make an integration_sign_in function for use in integration tests) The application code uses an if-then branching structure inside of Embedded Ruby, using the signed_in method defined in Listing 918:
<% if signed_in %> <li><%= link_to "Sign out", signout_path, :method => :delete %></li> <% else %> <li><%= link_to "Sign in", signin_path %></li> <% end %>
Notice that the signout link passes a hash argument indicating that it should submit with an HTTP DELETE request13 With this snippet added, the full header partial appears as in Listing 926