Chapter 8 Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a stateless protocol?

A

A stateless protocol treats each request as an independent transaction that is unable to remember information from any previous requests. This is true of HTTP.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a session?

A

A semi-permanent connection between two computers (such as a client computer running a web browser and a server running Rails).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a cookie?

A

A small piece of text placed on a user’s browser, which allows sessions to be maintained after the browser has been closed (i.e. the application can quickly retrieve the logged in user ID based on the provided cookie)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can the elements of logging in and out correspond the REST actions?

A

A login form is handled by the ‘new’ action (GET), actually logging in sends a POST request to the ‘create’ action, and logging out is handled by sending a DELETE request to the ‘destroy’ action (assuming this is a temporary session w/o cookies)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In the following generate command for a Sessions controller, why do we create an action for ‘new’ and not ‘create’ or ‘delete’:

$ rails generate controller Sessions new

A

B/C only the ‘new’ action requires a view (we’re GETting a form). The other two do not render a view (at best they may flash a message). Since the generator makes views for all actions given as arguments, it is not necessary to generate the other two functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The login page creates a session. If 2 methods (new, create) correspond to the /login url, how can we route the two methods to different controller actions?

A

Use a different HTTP method for each:

get ‘login’ => ‘sessions#new’
post ‘login’ => ‘sessions#create’
delete ‘logout’ => ‘sessions#destroy’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the command to view all of the application’s routes?

A

$bundle exec rake routes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why does a generated controller not have access to Active Record’s functions?

A

B/C generated models inherit from Active Record, generated controllers do not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When creating a login page corresponding to our Sessions controller, why can’t we just pass the form_for function a variable.

A

B/C there is no corresponding model, so we cannot set an @sessions variable in the erb.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the following action:

def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
# Log the user in and redirect to the user’s show page.
else
# Create an error message.
render ‘new’
end
end

A

Set the user variable to a user corresponding to the values passed to the find_by method (from Active Record). The if statement makes sense b/c ‘user’ will be nil if no matching user is found, and remember that, along with false, nil is the only object in ruby that is equivalent to a boolean false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is flash persistence, and what can cause it?

A

It is when a flash message continues to appear after it should have disappeared. One possible cause is that re-rendering a template with ‘render’ doesn’t count as a request, which is what would cause the flash to vanish.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Explain the following test of a login page:

test "login with invalid information" do
    get login_path
    assert_template 'sessions/new'
    post login_path, session: { email: "", password: "" }
    assert_template 'sessions/new'
    assert_not flash.empty?
    get root_path
    assert flash.empty?
  end
A

Visit the login path.
Verify that the new sessions form renders properly.
Post to the sessions path with an invalid params hash.
Verify that the new sessions form gets re-rendered and that a flash message appears.
Visit another page (such as the Home page).
Verify that the flash message doesn’t appear on the new page.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the syntax to run a single test instead of a full battery?

A

$ bundle exec rake test TEST=test/fulltestpath…/testname.rb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the point of ‘flash.now’?

A

It disappears as soon as any additional request is received, avoiding flash persistence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

T/F: helpers are made available in both their corresponding view and controller.

A

F; the helper module is included automatically in the view but not in the controller. It can be added, however, with an ‘include’ statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference created by the cookie generated by the ‘session’ function vs. the ‘cookie’ function?

A

The cookie from ‘sessions’ expires when the browser is closed, the ‘cookies’ cookie persists.

17
Q

What is the point of the following code?

if @current_user.nil?
  @current_user = User.find_by(id: session[:user_id])
else
  @current_user
end
A

This prevents multiple database hits when retrieving the current user.

18
Q

Explain the following Ruby shorthand:

@foo ||= “bar”

A

Short for @foo = @foo || “bar”

Because of short circuit evaluation, the statement terminates if @foo is anything other than nil or false, leaving @foo as it is. Otherwise, it is assigned to “bar”.

19
Q

What is a fixture?

A

A way of organizing data to be loaded into the test database.

20
Q

Explain the following two test lines:

follow_redirect!

assert_select “a[href=?]”, login_path, count: 0

A

1) Visits the target page

2) Verifies that there are zero login path links on the page

21
Q

How do we delete something from a session?

A

session.delete(something)

22
Q

What kind of attack are persistent cookies vulnerable to, and what are the four methods and solutions of doing so?

A

Session hijacking, in which an attacker uses a stolen remember token to log in as a particular user. There are four main ways to steal cookies: (1) using a packet sniffer to detect cookies being passed over insecure networks (prevented with SSL) (2) compromising a database containing remember tokens (prevented by hashing the token) (3) using cross-site scripting (XSS), (prevented by Rails), and (4) gaining physical access to a machine with a logged-in user (somewhat mitigated by changing the token every time a user logs in or out and cryptographically sign sensitive information.

23
Q

How is a remember token used?

A

When the database is queried for a specific value, check that the token matches the associated hash digest for the value.

24
Q

What are the two pieces of information a cookie consists of?

A

A value and an optional ‘expires’ date.

25
Q

Explain the following code:

cookies.permanent.signed[:user_id] = user.id

A

Creates a cookie with the :user_id token and an expiration date 20 years from now (all from #permanent), and encrypts it as a signed cookie (#signed)

26
Q

Explain the following if statement:

if (user_id = session[:user_id])

A

user_id will evaluate to the :user_id of the session, so this if statement will be false only if :user_id does not exist (returns nil)