Authentication MiniTest

I am new to MiniTest. Most tests are easy to understand, as this is Ruby code, as well as Rspec-style readability. However, I have authentication problems. As with any application, most controllers are hidden behind some kind of authentication, the most common of which is authenticate_userto ensure that the user is logged on.

How can I check user session→? I use authentication from scratch, not developing.

I have this as a link: https://github.com/chriskottom/minitest_cookbook_source/blob/master/minishop/test/support/session_helpers.rb

But not quite sure how to implement it.

Let me use this as an example controller:

class ProductsController < ApplicationController
  before_action :authenticate_user

  def index
    @products = Product.all
  end

  def show
   @product = Product.find(params[:id])
  end

end

How will my test look in these basic cases?

test "it should GET products index" do
  # insert code to check authenticate_user
  get :index
  assert_response :success
end

test "it should GET products show" do
  # insert code to check authenticate_user
  get :show
  assert_response :success
end

#refactor so logged in only has to be defined once across controllers.
+4
2

? , :

get(:show, {'id' => "12"}, {'user_id' => 5})

http://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers

, - , .

+2

, -, .

:

require 'test_helper'

class ProtectedControllerTest < ActionController::TestCase
  include Devise::TestHelpers

  test "authenticated user should get index" do
    sign_in users(:foo)
    get :index
    assert_response :success
  end

  test "not authenticated user should get redirect" do
    get :index
    assert_response :redirect
  end

end

:
: Rails 3 4 ( RSpec)

+2

Source: https://habr.com/ru/post/1623848/


All Articles