Skip to content

Commit

Permalink
Version 4.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bodrovis committed Dec 23, 2019
1 parent fcdad73 commit 67e0efa
Show file tree
Hide file tree
Showing 22 changed files with 177 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ test/dummy/tmp/
test/dummy/.sass-cache
Gemfile.lock
.idea/
*.gem
*.gem
coverage/
56 changes: 56 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require:
- rubocop-performance

AllCops:
TargetRubyVersion: 2.3

Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: no_space

Metrics/BlockLength:
Max: 36
Exclude:
- spec/**/*.rb

Metrics/BlockNesting:
Max: 2

Layout/LineLength:
AllowURI: true
Enabled: false

Metrics/MethodLength:
CountComments: false
Max: 10

Metrics/ModuleLength:
Max: 100

Metrics/ParameterLists:
Max: 5
CountKeywordArgs: true

Style/CollectionMethods:
Enabled: true
PreferredMethods:
collect: 'map'
collect!: 'map!'
inject: 'reduce'
find: 'detect'
find_all: 'select'
delete: 'gsub'

Style/Documentation:
Enabled: false

Layout/DotPosition:
EnforcedStyle: trailing

Layout/AccessModifierIndentation:
Enabled: false

Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: 'no_comma'

Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: 'no_comma'
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ language: ruby
sudo: false
cache: bundler
rvm:
- 2.5.5
- 2.6.3
- 2.5.7
- 2.6.5

before_install:
- gem update --system
- gem install bundler
- gem update bundler

env:
- 'TEST_RAILS_VERSION="~> 5.0.7"'
- 'TEST_RAILS_VERSION="~> 5.1.6"'
- 'TEST_RAILS_VERSION="~> 5.2.3"'
- 'TEST_RAILS_VERSION="~> 6.0.0"'
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 4.0.1 (23-Dec-19)

* Updated dependencies, tested against more recent Rubies and Rails
* Updated Gemfile for Bundler 2
* Added Rubocop and SimpleCov

## 4.0.0 (20-Aug-19)

Updated:
Expand Down
8 changes: 5 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
source "https://rubygems.org"
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

group :test do
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
end
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
end
6 changes: 2 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

begin
require 'bundler/setup'
rescue LoadError
Expand All @@ -14,9 +16,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end




Bundler::GemHelper.install_tasks

require 'rake/testtask'
Expand All @@ -28,5 +27,4 @@ Rake::TestTask.new(:test) do |t|
t.verbose = false
end


task default: :test
17 changes: 12 additions & 5 deletions angular_rails_csrf.gemspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
$:.push File.expand_path("../lib", __FILE__)
# frozen_string_literal: true

$LOAD_PATH.push File.expand_path('lib', __dir__)

# Maintain your gem's version:
require "angular_rails_csrf/version"
require 'angular_rails_csrf/version'

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "angular_rails_csrf"
s.name = 'angular_rails_csrf'
s.version = AngularRailsCsrf::VERSION
s.authors = ['James Sanders', 'Ilya Bodrov']
s.email = ['[email protected]', '[email protected]']
Expand All @@ -19,13 +21,18 @@ Gem::Specification.new do |s|

s.required_ruby_version = '>= 2.3.0'

s.add_development_dependency 'rake', '~> 12.0'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'test-unit', '~> 3.2'
if ENV['TEST_RAILS_VERSION'].nil?
s.add_development_dependency 'rails', '6.0.0'
s.add_development_dependency 'rails', '6.0.2.1'
else
s.add_development_dependency 'rails', ENV['TEST_RAILS_VERSION'].to_s
end

s.add_runtime_dependency 'railties', '>= 3', '< 7'

s.add_development_dependency 'codecov', '~> 0.1'
s.add_development_dependency 'rubocop', '~> 0.60'
s.add_development_dependency 'rubocop-performance', '~> 1.5'
s.add_development_dependency 'simplecov', '~> 0.16'
end
2 changes: 2 additions & 0 deletions lib/angular_rails_csrf.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

require 'angular_rails_csrf/railtie'
16 changes: 9 additions & 7 deletions lib/angular_rails_csrf/concern.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module AngularRailsCsrf
module Concern
extend ActiveSupport::Concern
Expand All @@ -7,12 +9,12 @@ module Concern
end

def set_xsrf_token_cookie
if protect_against_forgery? && !respond_to?(:__exclude_xsrf_token_cookie?)
config = Rails.application.config
domain = config.respond_to?(:angular_rails_csrf_domain) ? config.angular_rails_csrf_domain : nil
cookie_name = config.respond_to?(:angular_rails_csrf_cookie_name) ? config.angular_rails_csrf_cookie_name : 'XSRF-TOKEN'
cookies[cookie_name] = { value: form_authenticity_token, domain: domain }
end
return unless protect_against_forgery? && !respond_to?(:__exclude_xsrf_token_cookie?)

config = Rails.application.config
domain = config.respond_to?(:angular_rails_csrf_domain) ? config.angular_rails_csrf_domain : nil
cookie_name = config.respond_to?(:angular_rails_csrf_cookie_name) ? config.angular_rails_csrf_cookie_name : 'XSRF-TOKEN'
cookies[cookie_name] = {value: form_authenticity_token, domain: domain}
end

def verified_request?
Expand All @@ -25,7 +27,7 @@ def verified_request?

module ClassMethods
def exclude_xsrf_token_cookie
self.class_eval do
class_eval do
def __exclude_xsrf_token_cookie?
true
end
Expand Down
2 changes: 2 additions & 0 deletions lib/angular_rails_csrf/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'angular_rails_csrf/concern'

module AngularRailsCsrf
Expand Down
4 changes: 3 additions & 1 deletion lib/angular_rails_csrf/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module AngularRailsCsrf
VERSION = '4.0.0'.freeze
VERSION = '4.0.1'
end
6 changes: 4 additions & 2 deletions test/angular_rails_csrf_exception_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'test_helper'

class AngularRailsCsrfExceptionTest < ActionController::TestCase
Expand All @@ -8,9 +10,9 @@ class AngularRailsCsrfExceptionTest < ActionController::TestCase
@correct_token = @controller.send(:form_authenticity_token)
end

test "a get does not set the XSRF-TOKEN cookie" do
test 'a get does not set the XSRF-TOKEN cookie' do
get :index
assert_not_equal @correct_token, cookies['XSRF-TOKEN']
assert_response :success
end
end
end
32 changes: 20 additions & 12 deletions test/angular_rails_csrf_test.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
# frozen_string_literal: true

require 'test_helper'

class AngularRailsCsrfTest < ActionController::TestCase
tests ApplicationController

test "a get sets the XSRF-TOKEN cookie but does not require the X-XSRF-TOKEN header" do
test 'a get sets the XSRF-TOKEN cookie but does not require the X-XSRF-TOKEN header' do
get :index
assert_valid_cookie
assert_response :success
end

test "a post raises an error without the X-XSRF-TOKEN header set" do
test 'a post raises an error without the X-XSRF-TOKEN header set' do
assert_raises ActionController::InvalidAuthenticityToken do
post :create
end
end

test "a post raises an error with the X-XSRF-TOKEN header set to the wrong value" do
set_header_to 'garbage'
test 'a post raises an error with the X-XSRF-TOKEN header set to the wrong value' do
header_to 'garbage'
assert_raises ActionController::InvalidAuthenticityToken do
post :create
end
end

test "a post is accepted if X-XSRF-TOKEN is set properly" do
set_header_to @controller.send(:form_authenticity_token)
test 'a post is accepted if X-XSRF-TOKEN is set properly' do
header_to @controller.send(:form_authenticity_token)
post :create
assert_valid_cookie
assert_response :success
end

test "the domain is used if present" do
test 'the domain is used if present' do
config = Rails.application.config
def config.angular_rails_csrf_domain; :all; end
def config.angular_rails_csrf_domain
:all
end

get :index
assert @response.headers['Set-Cookie'].include?('.test.host')
assert_valid_cookie
assert_response :success
end

test "a custom name is used if present" do
test 'a custom name is used if present' do
use_custom_cookie_name do
get :index
assert @response.headers['Set-Cookie'].include?('CUSTOM-COOKIE-NAME')
Expand All @@ -52,7 +56,7 @@ def config.angular_rails_csrf_domain; :all; end

# Helpers

def set_header_to(value)
def header_to(value)
@request.headers['X-XSRF-TOKEN'] = value
end

Expand All @@ -66,9 +70,13 @@ def assert_valid_cookie(name = 'XSRF-TOKEN')

def use_custom_cookie_name
config = Rails.application.config
def config.angular_rails_csrf_cookie_name; 'CUSTOM-COOKIE-NAME'; end
def config.angular_rails_csrf_cookie_name
'CUSTOM-COOKIE-NAME'
end
yield
ensure
config.instance_eval('undef :angular_rails_csrf_cookie_name')
eval <<-RUBY, binding, __FILE__, __LINE__ + 1
config.instance_eval('undef :angular_rails_csrf_cookie_name')
RUBY
end
end
4 changes: 4 additions & 0 deletions test/dummy/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//= link_tree ../images
//= link_tree ../fonts
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
11 changes: 9 additions & 2 deletions test/dummy/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

def index; head :ok; end
def create; head :ok; end
def index
head :ok
end

def create
head :ok
end
end
8 changes: 6 additions & 2 deletions test/dummy/app/controllers/exclusions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

class ExclusionsController < ApplicationController
exclude_xsrf_token_cookie

def index; head :ok; end
end
def index
head :ok
end
end
4 changes: 3 additions & 1 deletion test/dummy/config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
9 changes: 5 additions & 4 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require File.expand_path('../boot', __FILE__)
# frozen_string_literal: true

require "action_controller/railtie"
require File.expand_path('boot', __dir__)

require 'action_controller/railtie'

Bundler.require(:default, Rails.env)
require "angular_rails_csrf"
require 'angular_rails_csrf'

module Dummy
class Application < Rails::Application
Expand All @@ -12,4 +14,3 @@ class Application < Rails::Application
config.active_support.test_order = :random
end
end

4 changes: 3 additions & 1 deletion test/dummy/config/boot.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__)

require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
Loading

0 comments on commit 67e0efa

Please sign in to comment.