Sha256: 01749ce6ff482d16094173d9d0533e1f8e3461b1092339cbd25a462146b3f472

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'test_helper'

class AdminProtectedControllerTest < ActionController::TestCase
  def valid_auth
    @admin = admins(:one)
    @token = KnockRails3::AuthToken.new(payload: { sub: @admin.id }).token
    @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
  end

  def invalid_token_auth
    @token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
    @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
  end

  def invalid_entity_auth
    @token = KnockRails3::AuthToken.new(payload: { sub: 0 }).token
    @request.env['HTTP_AUTHORIZATION'] = "Bearer #{@token}"
  end

  test "responds with unauthorized" do
    get :index
    assert_response :unauthorized
  end

  test "responds with unauthorized to invalid token" do
    invalid_token_auth
    get :index
    assert_response :unauthorized
  end

  test "responds with unauthorized to invalid entity" do
    invalid_entity_auth
    get :index
    assert_response :unauthorized
  end

  test "responds with success if authenticated" do
    valid_auth
    get :index
    assert_response :success
  end

  test "has a current_admin after authentication" do
    valid_auth
    get :index
    assert_response :success
    assert @controller.current_admin.id == @admin.id
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
KnockRails3-2.1.1 test/dummy/test/controllers/admin_protected_controller_test.rb