Sha256: 1450fe48879c98d5bb6f9eecf6dd59b2191c49977885edc41628ff27108fa1bb

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'test_helper'

class ProtectedResourcesControllerTest < ActionController::TestCase
  def setup
    @user = users(:one)
    @token = KnockRails3::AuthToken.new(payload: { sub: @user.id }).token
  end

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

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

  test "responds with success with valid token in header" do
    authenticate
    get :index
    assert_response :success
  end

  test "responds with unauthorized with invalid token in header" do
    authenticate token: "invalid"
    get :index
    assert_response :unauthorized
  end

  test "responds with success with token in url" do
    get :index, params: {token: @token}
    assert_response :success
  end

  test "responds with unauthorized with invalid token in url" do
    get :index, params: {token: "invalid"}
    assert_response :unauthorized
  end

  test "has a current_user after authentication" do
    authenticate
    get :index
    assert_response :success
    assert @controller.current_user.id == @user.id
  end

  test "accepts any prefix in the authorization header" do
    @request.env['HTTP_AUTHORIZATION'] = "Other #{@token}"

    get :index

    assert_response :success
  end

  test "accepts authorization header without prefix" do
    @request.env['HTTP_AUTHORIZATION'] = "#{@token}"

    get :index

    assert_response :success
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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