Fix CI builds (#286)

* update test app gems and fix more deprecation errors
* replace minitest with rspec for consistency. cleanup Rakefile
This commit is contained in:
Matt 2020-07-20 09:35:41 -04:00 committed by GitHub
parent a8fefb0b84
commit ef7bec89dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
50 changed files with 1623 additions and 682 deletions

4
.gitignore vendored
View File

@ -9,5 +9,7 @@
/test_app/vendor /test_app/vendor
Gemfile.lock Gemfile.lock
gemfiles/*.lock gemfiles/*.lock
.DS_Store
*.swo *.swo
*.swp .vscode/

24
.rubocop.yml Normal file
View File

@ -0,0 +1,24 @@
AllCops:
Exclude:
- 'tests/test_app/db/**/*.rb'
inherit_from: .rubocop_todo.yml
require:
- rubocop-rspec
- rubocop-rails
Layout/SpaceAroundMethodCallOperator:
Enabled: true
Lint/RaiseException:
Enabled: True
Lint/StructNewOverride:
Enabled: True
Style/ExponentialNotation:
Enabled: True
Style/HashEachMethods:
Enabled: True
Style/HashTransformKeys:
Enabled: True
Style/HashTransformValues:
Enabled: True

748
.rubocop_todo.yml Normal file
View File

@ -0,0 +1,748 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2020-04-25 16:13:40 -0400 using RuboCop version 0.82.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.
# Offense count: 7
# Cop supports --auto-correct.
# Configuration parameters: TreatCommentsAsGroupSeparators, Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Bundler/OrderedGems:
Exclude:
- 'tests/test_app/Gemfile'
# Offense count: 1
# Configuration parameters: Include.
# Include: **/*.gemspec
Gemspec/RubyVersionGlobalsUsage:
Exclude:
- 'impressionist.gemspec'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: outdent, indent
Layout/AccessModifierIndentation:
Exclude:
- 'lib/impressionist/engine.rb'
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: with_first_argument, with_fixed_indentation
Layout/ArgumentAlignment:
Exclude:
- 'lib/impressionist/is_impressionable.rb'
- 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb'
# Offense count: 1455
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: with_first_element, with_fixed_indentation
Layout/ArrayAlignment:
Exclude:
- 'app/models/impressionist/bots.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyleAlignWith.
# SupportedStylesAlignWith: either, start_of_block, start_of_line
Layout/BlockAlignment:
Exclude:
- 'lib/impressionist/engine.rb'
# Offense count: 3
# Cop supports --auto-correct.
Layout/ClosingParenthesisIndentation:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'lib/impressionist/controllers/mongoid/impressionist_controller.rb'
# Offense count: 3
# Cop supports --auto-correct.
Layout/CommentIndentation:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'tests/test_app/config/initializers/impression.rb'
# Offense count: 7
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: leading, trailing
Layout/DotPosition:
Exclude:
- 'lib/impressionist/counter_cache.rb'
- 'lib/impressionist/models/active_record/impressionist/impressionable.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
- 'lib/impressionist/setup_association.rb'
- 'lib/impressionist/update_counters.rb'
# Offense count: 1
# Cop supports --auto-correct.
Layout/EmptyLineAfterGuardClause:
Exclude:
- 'app/models/impressionist/bots.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowAdjacentOneLineDefs, NumberOfEmptyLines.
Layout/EmptyLineBetweenDefs:
Exclude:
- 'lib/impressionist/counter_cache.rb'
# Offense count: 8
# Cop supports --auto-correct.
Layout/EmptyLines:
Exclude:
- 'Rakefile'
- 'app/controllers/impressionist_controller.rb'
- 'lib/impressionist/counter_cache.rb'
- 'lib/impressionist/engine.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
- 'tests/test_app/Gemfile'
- 'tests/test_app/lib/tasks/cucumber.rake'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: around, only_before
Layout/EmptyLinesAroundAccessModifier:
Exclude:
- 'lib/impressionist/setup_association.rb'
# Offense count: 7
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines, beginning_only, ending_only
Layout/EmptyLinesAroundClassBody:
Exclude:
- 'lib/generators/impressionist_generator.rb'
- 'lib/impressionist/engine.rb'
- 'lib/impressionist/models/active_record/impression.rb'
- 'lib/impressionist/models/mongoid/impression.rb'
- 'lib/impressionist/rails_toggle.rb'
- 'lib/impressionist/update_counters.rb'
- 'tests/test_app/app/controllers/dummy_controller.rb'
# Offense count: 7
# Cop supports --auto-correct.
Layout/EmptyLinesAroundMethodBody:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'lib/impressionist/counter_cache.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
- 'tests/test_app/app/controllers/posts_controller.rb'
- 'upgrade_migrations/version_0_3_0.rb'
# Offense count: 19
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: empty_lines, empty_lines_except_namespace, empty_lines_special, no_empty_lines
Layout/EmptyLinesAroundModuleBody:
Exclude:
- 'app/models/impressionist/bots.rb'
- 'app/models/impressionist/impressionable.rb'
- 'lib/impressionist/counter_cache.rb'
- 'lib/impressionist/is_impressionable.rb'
- 'lib/impressionist/models/active_record/impressionist/impressionable.rb'
- 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
- 'lib/impressionist/rails_toggle.rb'
- 'lib/impressionist/update_counters.rb'
# Offense count: 7
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, AllowBeforeTrailingComments, ForceEqualSignAlignment.
Layout/ExtraSpacing:
Exclude:
- 'lib/impressionist/counter_cache.rb'
- 'tests/test_app/config.ru'
- 'tests/test_app/config/environments/pg_test.rb'
- 'tests/test_app/config/environments/test.rb'
- 'tests/test_app/script/rails'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: consistent, consistent_relative_to_receiver, special_for_inner_method_call, special_for_inner_method_call_in_parentheses
Layout/FirstArgumentIndentation:
Exclude:
- 'lib/impressionist/controllers/mongoid/impressionist_controller.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowMultipleStyles, EnforcedHashRocketStyle, EnforcedColonStyle, EnforcedLastArgumentHashStyle.
# SupportedHashRocketStyles: key, separator, table
# SupportedColonStyles: key, separator, table
# SupportedLastArgumentHashStyles: always_inspect, always_ignore, ignore_implicit, ignore_explicit
Layout/HashAlignment:
Exclude:
- 'lib/impressionist/controllers/mongoid/impressionist_controller.rb'
# Offense count: 18
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: normal, indented_internal_methods
Layout/IndentationConsistency:
Exclude:
- 'lib/impressionist/counter_cache.rb'
- 'lib/impressionist/engine.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
- 'lib/impressionist/rails_toggle.rb'
- 'lib/impressionist/setup_association.rb'
# Offense count: 8
# Cop supports --auto-correct.
# Configuration parameters: IndentationWidth, EnforcedStyle.
# SupportedStyles: spaces, tabs
Layout/IndentationStyle:
Exclude:
- 'lib/impressionist/controllers/mongoid/impressionist_controller.rb'
# Offense count: 25
# Cop supports --auto-correct.
# Configuration parameters: Width, IgnoredPatterns.
Layout/IndentationWidth:
Exclude:
- 'app/models/impressionist/impressionable.rb'
- 'lib/impressionist/controllers/mongoid/impressionist_controller.rb'
- 'lib/impressionist/counter_cache.rb'
- 'lib/impressionist/engine.rb'
- 'lib/impressionist/models/active_record/impressionist/impressionable.rb'
- 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
- 'lib/impressionist/rails_toggle.rb'
- 'lib/impressionist/setup_association.rb'
- 'tests/test_app/lib/tasks/cucumber.rake'
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: AllowDoxygenCommentStyle, AllowGemfileRubyComment.
Layout/LeadingCommentSpace:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'lib/impressionist/bots.rb'
- 'tests/test_app/config/initializers/impression.rb'
# Offense count: 6
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, IndentationWidth.
# SupportedStyles: aligned, indented, indented_relative_to_receiver
Layout/MultilineMethodCallIndentation:
Exclude:
- 'lib/impressionist/counter_cache.rb'
- 'lib/impressionist/models/active_record/impressionist/impressionable.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
- 'lib/impressionist/setup_association.rb'
- 'lib/impressionist/update_counters.rb'
# Offense count: 22
# Cop supports --auto-correct.
Layout/SpaceAfterComma:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'app/models/impressionist/bots.rb'
- 'lib/impressionist/bots.rb'
- 'tests/test_app/app/controllers/articles_controller.rb'
- 'upgrade_migrations/version_0_3_0.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyleInsidePipes.
# SupportedStylesInsidePipes: space, no_space
Layout/SpaceAroundBlockParameters:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 13
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: space, no_space
Layout/SpaceAroundEqualsInParameterDefault:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'app/models/impressionist/impressionable.rb'
- 'lib/impressionist/controllers/mongoid/impressionist_controller.rb'
- 'lib/impressionist/counter_cache.rb'
- 'lib/impressionist/is_impressionable.rb'
- 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
# Offense count: 8
# Cop supports --auto-correct.
# Configuration parameters: AllowForAlignment, EnforcedStyleForExponentOperator.
# SupportedStylesForExponentOperator: space, no_space
Layout/SpaceAroundOperators:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'lib/impressionist/controllers/mongoid/impressionist_controller.rb'
- 'lib/impressionist/counter_cache.rb'
- 'lib/impressionist/models/mongo_mapper/impression.rb'
- 'tests/test_app/app/controllers/widgets_controller.rb'
- 'tests/test_app/config/environments/pg_test.rb'
- 'tests/test_app/config/environments/test.rb'
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceBeforeBlockBraces:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces, SpaceBeforeBlockParameters.
# SupportedStyles: space, no_space
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideBlockBraces:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'tests/test_app/lib/tasks/cucumber.rake'
# Offense count: 10
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space, compact
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideHashLiteralBraces:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'lib/impressionist/update_counters.rb'
- 'tests/test_app/lib/tasks/cucumber.rake'
# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: final_newline, final_blank_line
Layout/TrailingEmptyLines:
Exclude:
- 'Gemfile'
- 'lib/impressionist/setup_association.rb'
- 'tests/test_app/config/initializers/impression.rb'
- 'upgrade_migrations/version_1_5_2.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AllowInHeredoc.
Layout/TrailingWhitespace:
Exclude:
- 'lib/impressionist/setup_association.rb'
# Offense count: 1
# Cop supports --auto-correct.
Lint/DeprecatedClassMethods:
Exclude:
- 'tests/test_app/config/boot.rb'
# Offense count: 1
Lint/DuplicateMethods:
Exclude:
- 'lib/impressionist/setup_association.rb'
# Offense count: 1
# Cop supports --auto-correct.
Lint/RedundantStringCoercion:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 1
# Cop supports --auto-correct.
Lint/ScriptPermission:
Exclude:
- 'tests/test_app/Rakefile'
# Offense count: 2
# Cop supports --auto-correct.
Lint/SendWithMixinArgument:
Exclude:
- 'lib/impressionist/models/active_record/impressionist/impressionable.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: IgnoreEmptyBlocks, AllowUnusedKeywordArguments.
Lint/UnusedBlockArgument:
Exclude:
- 'lib/impressionist/engine.rb'
# Offense count: 3
# Configuration parameters: IgnoredMethods.
Metrics/AbcSize:
Max: 23
# Offense count: 10
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 336
# Offense count: 3
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 15
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 1462
# Offense count: 2
# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros.
# NamePrefix: is_, has_, have_
# ForbiddenPrefixes: is_, has_, have_
# AllowedMethods: is_a?
# MethodDefinitionMacros: define_method, define_singleton_method
Naming/PredicateName:
Exclude:
- 'spec/**/*'
- 'lib/impressionist/is_impressionable.rb'
- 'lib/impressionist/models/mongo_mapper/impressionist/impressionable.rb'
# Offense count: 6
# Configuration parameters: Prefixes.
# Prefixes: when, with, without
RSpec/ContextWording:
Exclude:
- 'tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb'
- 'tests/test_app/spec/controllers/widgets_controller_spec.rb'
# Offense count: 31
# Configuration parameters: Max.
RSpec/ExampleLength:
Exclude:
- 'spec/setup_association_spec.rb'
- 'tests/test_app/spec/controllers/articles_controller_spec.rb'
- 'tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb'
- 'tests/test_app/spec/controllers/posts_controller_spec.rb'
- 'tests/test_app/spec/controllers/widgets_controller_spec.rb'
- 'tests/test_app/spec/rails_generators/rails_generators_spec.rb'
# Offense count: 5
# Configuration parameters: CustomTransform, IgnoreMethods.
RSpec/FilePath:
Exclude:
- 'spec/models/bots_spec.rb'
- 'spec/rails_toggle_spec.rb'
- 'spec/setup_association_spec.rb'
- 'tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb'
- 'tests/test_app/spec/models/bots_spec.rb'
# Offense count: 61
# Configuration parameters: AssignmentOnly.
RSpec/InstanceVariable:
Exclude:
- 'tests/test_app/spec/controllers/articles_controller_spec.rb'
- 'tests/test_app/spec/controllers/impressionist_uniqueness_spec.rb'
- 'tests/test_app/spec/controllers/widgets_controller_spec.rb'
- 'tests/test_app/spec/models/model_spec.rb'
# Offense count: 5
# Configuration parameters: .
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
EnforcedStyle: receive
# Offense count: 28
# Configuration parameters: AggregateFailuresByDefault.
RSpec/MultipleExpectations:
Max: 8
# Offense count: 4
RSpec/NestedGroups:
Max: 4
# Offense count: 2
RSpec/RepeatedExample:
Exclude:
- 'tests/test_app/spec/controllers/widgets_controller_spec.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, Include.
# SupportedStyles: action, filter
# Include: app/controllers/**/*.rb
Rails/ActionFilter:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 6
# Cop supports --auto-correct.
Rails/ApplicationRecord:
Exclude:
- 'lib/impressionist/models/active_record/impression.rb'
- 'tests/test_app/app/models/article.rb'
- 'tests/test_app/app/models/dummy.rb'
- 'tests/test_app/app/models/post.rb'
- 'tests/test_app/app/models/profile.rb'
- 'tests/test_app/app/models/widget.rb'
# Offense count: 2
# Configuration parameters: EnforcedStyle.
# SupportedStyles: slashes, arguments
Rails/FilePath:
Exclude:
- 'tests/test_app/lib/tasks/cucumber.rake'
- 'tests/test_app/spec/rails_generators/rails_generators_spec.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: NotNilAndNotEmpty, NotBlank, UnlessBlank.
Rails/Present:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 2
# Configuration parameters: Include.
# Include: **/Rakefile, **/*.rake
Rails/RakeEnvironment:
Exclude:
- 'lib/capistrano/tasks/**/*.rake'
- 'Rakefile'
- 'tests/test_app/lib/tasks/cucumber.rake'
# Offense count: 1
# Configuration parameters: Blacklist, Whitelist.
# Blacklist: decrement!, decrement_counter, increment!, increment_counter, toggle!, touch, update_all, update_attribute, update_column, update_columns, update_counters
Rails/SkipsModelValidations:
Exclude:
- 'lib/impressionist/update_counters.rb'
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: strict, flexible
Rails/TimeZone:
Exclude:
- 'app/models/impressionist/impressionable.rb'
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/BlockComments:
Exclude:
- 'spec/spec_helper.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, IgnoredMethods, AllowBracesOnProceduralOneLiners, BracesRequiredMethods.
# SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces
# ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object
# FunctionalMethods: let, let!, subject, watch
# IgnoredMethods: lambda, proc, it
Style/BlockDelimiters:
Exclude:
- 'lib/impressionist/counter_cache.rb'
# Offense count: 1
Style/ClassVars:
Exclude:
- 'lib/impressionist.rb'
# Offense count: 1
Style/CommentedKeyword:
Exclude:
- 'app/models/impressionist/impressionable.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SingleLineConditionsOnly, IncludeTernaryExpressions.
# SupportedStyles: assign_to_condition, assign_inside_condition
Style/ConditionalAssignment:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 36
Style/Documentation:
Enabled: false
# Offense count: 1
# Cop supports --auto-correct.
Style/EachWithObject:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 8
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: compact, expanded
Style/EmptyMethod:
Exclude:
- 'tests/test_app/app/controllers/dummy_controller.rb'
- 'tests/test_app/app/controllers/posts_controller.rb'
- 'tests/test_app/app/controllers/profiles_controller.rb'
- 'tests/test_app/app/controllers/widgets_controller.rb'
# Offense count: 7
# Cop supports --auto-correct.
Style/ExpandPathArguments:
Exclude:
- 'lib/generators/impressionist_generator.rb'
- 'tests/test_app/Rakefile'
- 'tests/test_app/config/application.rb'
- 'tests/test_app/config/boot.rb'
- 'tests/test_app/config/environment.rb'
- 'tests/test_app/script/rails'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: format, sprintf, percent
Style/FormatString:
Exclude:
- 'lib/generators/active_record/impressionist_generator.rb'
# Offense count: 1
# Configuration parameters: EnforcedStyle.
# SupportedStyles: annotated, template, unannotated
Style/FormatStringToken:
Exclude:
- 'lib/generators/active_record/impressionist_generator.rb'
# Offense count: 76
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: always, always_true, never
Style/FrozenStringLiteralComment:
Enabled: false
# Offense count: 6
# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'tests/test_app/app/controllers/articles_controller.rb'
- 'tests/test_app/app/controllers/posts_controller.rb'
- 'tests/test_app/app/controllers/profiles_controller.rb'
# Offense count: 134
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
Style/HashSyntax:
Enabled: false
# Offense count: 4
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'app/models/impressionist/impressionable.rb'
- 'impressionist.gemspec'
# Offense count: 1
Style/MultilineTernaryOperator:
Exclude:
- 'lib/impressionist/models/mongoid/impressionist/impressionable.rb'
# Offense count: 5
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: literals, strict
Style/MutableConstant:
Exclude:
- 'app/models/impressionist/bots.rb'
- 'app/models/impressionist/impressionable.rb'
- 'lib/impressionist/bots.rb'
- 'lib/impressionist/version.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/Not:
Exclude:
- 'lib/impressionist/rails_toggle.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: Strict.
Style/NumericLiterals:
MinDigits: 6
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: PreferredDelimiters.
Style/PercentLiteralDelimiters:
Exclude:
- 'tests/test_app/config/application.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/RedundantBegin:
Exclude:
- 'lib/impressionist/counter_cache.rb'
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: AllowMultipleReturnValues.
Style/RedundantReturn:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 4
# Cop supports --auto-correct.
Style/RedundantSelf:
Exclude:
- 'app/controllers/impressionist_controller.rb'
- 'lib/impressionist/counter_cache.rb'
# Offense count: 2
# Cop supports --auto-correct.
Style/RescueModifier:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 1
# Cop supports --auto-correct.
Style/StderrPuts:
Exclude:
- 'tests/test_app/lib/tasks/cucumber.rake'
# Offense count: 1647
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Enabled: false
# Offense count: 43
# Cop supports --auto-correct.
# Configuration parameters: MinSize.
# SupportedStyles: percent, brackets
Style/SymbolArray:
EnforcedStyle: brackets
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: IgnoredMethods.
# IgnoredMethods: respond_to, define_method
Style/SymbolProc:
Exclude:
- 'app/controllers/impressionist_controller.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, AllowSafeAssignment.
# SupportedStyles: require_parentheses, require_no_parentheses, require_parentheses_when_complex
Style/TernaryParentheses:
Exclude:
- 'lib/impressionist/controllers/mongoid/impressionist_controller.rb'
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, MinSize, WordRegex.
# SupportedStyles: percent, brackets
Style/WordArray:
Exclude:
- 'app/models/impressionist/bots.rb'
- 'lib/impressionist/bots.rb'
# Offense count: 433
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Layout/LineLength:
Max: 197

View File

@ -1,29 +1,29 @@
language: ruby language: ruby
before_install: env:
- gem install bundler - RUBYOPT='-W:no-deprecated -W:no-experimental'
before_script: before_script:
- bundle
- cd tests/test_app - cd tests/test_app
- bundle --gemfile=./Gemfile
- bundle exec rails g impressionist -f - bundle exec rails g impressionist -f
- bundle exec rake db:create db:migrate RAILS_ENV=test - bundle exec rake db:create db:migrate RAILS_ENV=test
- cd .. - cd ../..
script: script:
- bundle exec rake - rubocop
- bundle exec rspec
- cd tests/test_app
- export BUNDLE_GEMFILE=./Gemfile
- bundle
- bundle exec rspec
rvm: rvm:
- 1.9.3 - 2.7.0
- 2.0.0
- 2.1.0
- 2.2.0
- 2.3.0
- jruby-head - jruby-head
- rbx - rbx-4
- ruby-head - ruby-head
gemfile:
- gemfiles/rails32.gemfile
- gemfiles/rails40.gemfile
matrix: matrix:
allow_failures: allow_failures:
- rvm: jruby-head - rvm: jruby-head
- rvm: rbx - rvm: rbx-4
- rvm: ruby-head - rvm: ruby-head
fast_finish: true fast_finish: true
notifications: notifications:

11
Gemfile
View File

@ -9,16 +9,11 @@ platforms :jruby do
gem 'jruby-openssl' gem 'jruby-openssl'
end end
platforms :ruby, :mswin, :mingw do
gem 'sqlite3'
end
group :test do group :test do
gem 'capybara', '>= 2.0.3'
gem 'minitest' gem 'minitest'
gem 'minitest-rails' gem 'rspec', "~> 3.0"
gem 'rails', '>= 3.2.15' gem 'rubocop-rails', require: false
gem 'rspec-rails' gem 'rubocop-rspec', require: false
gem 'simplecov' gem 'simplecov'
gem 'systemu' gem 'systemu'
end end

View File

@ -1,18 +1,15 @@
require 'bundler/setup' require 'bundler/setup'
require 'rspec'
require 'rspec/core/rake_task' require 'rspec/core/rake_task'
require 'rake/testtask' require 'rake/testtask'
RSpec::Core::RakeTask.new(:spec)
task default: :spec
Bundler::GemHelper.install_tasks Bundler::GemHelper.install_tasks
# Impressionist will use MiniTest instead of RSpec
RSpec::Core::RakeTask.new do |task|
task.rspec_opts = "-I ./tests/test_app/spec"
task.pattern = "./tests/test_app/spec/**/*_spec.rb"
end
task :test_app => :spec
task :default => [:test, :test_app]
namespace :impressionist do namespace :impressionist do
require File.dirname(__FILE__) + "/lib/impressionist/bots" require File.dirname(__FILE__) + "/lib/impressionist/bots"
@ -20,15 +17,4 @@ namespace :impressionist do
task :bots do task :bots do
p Impressionist::Bots.consume p Impressionist::Bots.consume
end end
end
# setup :test task to minitest
# Rake libs default is lib
# libs << path to load test_helper, etc..
Rake::TestTask.new do |t|
t.libs << "tests/spec"
t.pattern = "tests/spec/**/*_spec.rb"
t.test_files = FileList["tests/spec/**/*_spec.rb"]
t.verbose = true
end end

View File

@ -1,5 +1,6 @@
require 'digest/sha2' require 'digest/sha2'
module ImpressionistController module ImpressionistController
module ClassMethods module ClassMethods
def impressionist(opts={}) def impressionist(opts={})
@ -51,7 +52,14 @@ module ImpressionistController
# creates a statment hash that contains default values for creating an impression via an AR relation. # creates a statment hash that contains default values for creating an impression via an AR relation.
def associative_create_statement(query_params={}) def associative_create_statement(query_params={})
# support older versions of rails:
# see https://github.com/rails/rails/pull/34039
if Rails::VERSION::MAJOR < 6
filter = ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters)
else
filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters) filter = ActiveSupport::ParameterFilter.new(Rails.application.config.filter_parameters)
end
query_params.reverse_merge!( query_params.reverse_merge!(
:controller_name => controller_name, :controller_name => controller_name,
:action_name => action_name, :action_name => action_name,
@ -133,14 +141,25 @@ module ImpressionistController
end end
def session_hash def session_hash
# # careful: request.session_options[:id] encoding in rspec test was ASCII-8BIT # # careful: request.session_options[:id] encoding in rspec test was ASCII-8BIT
# # that broke the database query for uniqueness. not sure if this is a testing only issue. # # that broke the database query for uniqueness. not sure if this is a testing only issue.
# str = request.session_options[:id] # str = request.session_options[:id]
# logger.debug "Encoding: #{str.encoding.inspect}" # logger.debug "Encoding: #{str.encoding.inspect}"
# # request.session_options[:id].encode("ISO-8859-1") # # request.session_options[:id].encode("ISO-8859-1")
if Rails::VERSION::MAJOR >= 4
session["init"] = true
id = session.id.to_s
else
id = request.session_options[:id] id = request.session_options[:id]
# rack 2.0.8 releases new version of session id, id.to_s will raise error! end
unless id.is_a? String
id = id.cookie_value if Rack::Session::SessionId.const_defined?(:ID_VERSION) && Rack::Session::SessionId::ID_VERSION == 2 id = id.cookie_value if Rack::Session::SessionId.const_defined?(:ID_VERSION) && Rack::Session::SessionId::ID_VERSION == 2
end
# id = cookies.session.id
# rack 2.0.8 releases new version of session id, id.to_s will raise error!
id id
end end

View File

@ -1,31 +0,0 @@
source 'https://rubygems.org'
gem 'rake', '>= 0.9', '< 11.0'
gem 'rdoc', '>= 2.4.2'
platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
gem 'jdbc-sqlite3'
gem 'jruby-openssl'
end
platforms :ruby, :mswin, :mingw do
gem 'sqlite3'
end
group :test do
gem 'public_suffix', '< 1.5.0'
gem 'capybara', '>= 2.0.3'
gem 'minitest'
gem 'minitest-rails'
gem 'rails', '~> 3.2.15'
gem 'rspec-rails', '~> 2.14.0'
gem 'simplecov'
gem 'systemu'
gem 'friendly_id', '~> 4.0.9'
# test/unit has been removed by default in Ruby 2.2.x+
gem 'test-unit'
end
gemspec :path => '../'

View File

@ -1,31 +0,0 @@
source 'https://rubygems.org'
gem 'rake', '>= 0.9', '< 11.0'
gem 'rdoc', '>= 2.4.2'
platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
gem 'jdbc-sqlite3'
gem 'jruby-openssl'
end
platforms :ruby, :mswin, :mingw do
gem 'sqlite3'
end
group :test do
gem 'public_suffix', '< 1.5.0'
gem 'capybara', '>= 2.0.3'
gem 'minitest'
gem 'minitest-rails'
gem 'rails', '~> 4.0.1'
gem 'rspec-rails', '~> 2.14.0'
gem 'simplecov'
gem 'systemu'
gem 'friendly_id', '~> 5.1.0'
# test/unit has been removed by default in Ruby 2.2.x+
gem 'test-unit'
end
gemspec :path => '../'

View File

@ -1,5 +1,6 @@
# -*- encoding: utf-8 -*- # frozen_string_literal: true
$:.push File.expand_path('../lib', __FILE__)
$LOAD_PATH.push File.expand_path('lib', __dir__)
require 'impressionist/version' require 'impressionist/version'
Gem::Specification.new do |s| Gem::Specification.new do |s|
@ -16,8 +17,12 @@ Gem::Specification.new do |s|
s.files = `git ls-files`.split("\n") s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- tests/**/*`.split("\n") s.test_files = `git ls-files -- tests/**/*`.split("\n")
s.require_path = 'lib' s.require_path = 'lib'
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6') if s.respond_to? :required_rubygems_version= if s.respond_to? :required_rubygems_version=
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
end
s.add_dependency 'nokogiri', RUBY_VERSION < '2.1.0' ? '~> 1.6.0' : '~> 1' s.add_dependency 'nokogiri', RUBY_VERSION < '2.1.0' ? '~> 1.6.0' : '~> 1'
s.add_development_dependency 'bundler', '~> 1.0' s.add_dependency 'rails', '>= 3.2.15'
s.add_development_dependency 'bundler', '~> 2.0'
s.add_development_dependency 'sqlite3', '~> 1.4'
end end

View File

@ -15,7 +15,7 @@ module ActiveRecord
end end
def create_migration_file def create_migration_file
migration_template 'create_impressions_table.rb', 'db/migrate/create_impressions_table.rb' migration_template 'create_impressions_table.rb.erb', 'db/migrate/create_impressions_table.rb'
end end
end end
end end

View File

@ -5,7 +5,7 @@ module Impressionist
source_root File.expand_path('../templates', __FILE__) source_root File.expand_path('../templates', __FILE__)
def copy_config_file def copy_config_file
template 'impression.rb', 'config/initializers/impression.rb' template 'impression.rb.erb', 'config/initializers/impression.rb'
end end
end end

25
spec/models/bots_spec.rb Normal file
View File

@ -0,0 +1,25 @@
require 'spec_helper'
require './app/models/impressionist/bots'
describe Impressionist::Bots do
describe "bot detection" do
it "matches wild card" do
expect(described_class).to be_bot("google.com bot")
end
it "matches a bot list" do
expect(described_class).to be_bot("A-Online Search")
end
it "skips blank user agents" do
expect(described_class).not_to be_bot
expect(described_class).not_to be_bot("")
expect(described_class).not_to be_bot(nil)
end
it "skips safe matches" do
expect(described_class).not_to be_bot('127.0.0.1')
end
end
end

11
spec/rails_helper.rb Normal file
View File

@ -0,0 +1,11 @@
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require 'rails/all'
require 'rspec/rails'
require File.expand_path('./test_app/config/environment', __dir__)
RSpec.configure do |config|
end

31
spec/rails_toggle_spec.rb Normal file
View File

@ -0,0 +1,31 @@
# frozen_string_literal: true
require 'spec_helper'
require 'impressionist/rails_toggle'
describe Impressionist::RailsToggle do
let(:toggle) { described_class.new }
context 'when using rails < 4' do
it 'will be included' do
stub_const('::Rails::VERSION::MAJOR', 3)
expect(toggle).to be_should_include
end
it 'will not be included when strong parameters is defined' do
stub_const('::Rails::VERSION::MAJOR', 3)
stub_const('StrongParameters', Module.new)
expect(toggle).not_to be_should_include
end
end
context 'when using rails >= 4' do
it 'will not be included' do
stub_const('::Rails::VERSION::MAJOR', 4)
expect(toggle).not_to be_should_include
end
end
end

View File

@ -0,0 +1,48 @@
# frozen_string_literal: false
require 'spec_helper'
require 'impressionist/setup_association'
require 'impressionist/rails_toggle'
describe Impressionist::SetupAssociation do
let(:mock) { double }
let(:setup_association) { described_class.new(mock) }
it 'will include when togglable' do
expect(mock).to receive(:attr_accessible).with(any_args).and_return(true)
expect(setup_association).to receive(:toggle).and_return(true)
expect(setup_association).to be_include_attr_acc
end
it 'will not include if it is not togglable' do
expect(setup_association).to receive(:toggle).and_return(false)
expect(setup_association).not_to be_include_attr_acc
end
context 'when using rails >= 5' do
it 'sets belongs_to' do
stub_const('::Rails::VERSION::MAJOR', 5)
expect(mock).to receive(:belongs_to).twice.with(
:impressionable, { polymorphic: true, optional: true }
).and_return(true)
expect(setup_association.define_belongs_to).to be_truthy
expect(setup_association.set).to be_falsy
end
end
context 'when using rails < 5' do
it 'sets belongs_to' do
stub_const('::Rails::VERSION::MAJOR', 4)
expect(mock).to receive(:belongs_to).twice.with(
:impressionable, { polymorphic: true }
).and_return(true)
expect(setup_association.define_belongs_to).to be_truthy
expect(setup_association.set).to be_falsy
end
end
end

100
spec/spec_helper.rb Normal file
View File

@ -0,0 +1,100 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = "doc"
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end

View File

@ -1,7 +0,0 @@
$:.unshift(File.dirname __FILE__)
require 'rails/all'
require 'rails/test_help'
require "minitest/autorun"
require "minitest/pride"
require 'minitest/rails'

View File

@ -1,38 +0,0 @@
# Responsability
# Test whether rails version > 4
# includes attr_accessible if < 4
require 'minitest_helper'
require 'impressionist/rails_toggle'
module Impressionist
describe RailsToggle do
before {
@toggle = RailsToggle.new
}
describe "Rails 4" do
# see your_minitest_path/lib/minitest/mock.rb
it "must not include attr_accessible" do
@toggle.stub :supported_by_rails?, false do
refute @toggle.should_include?
end
end
end
describe "Strong Parameters" do
# see your_minitest_path/lib/minitest/mock.rb
it "must not include attr_accessible" do
@toggle.stub :supported_by_rails?, true do
@toggle.stub :using_strong_parameters?, true do
refute @toggle.should_include?
end
end
end
end
end
end

View File

@ -1,56 +0,0 @@
require 'minitest_helper'
require 'impressionist/setup_association'
module Impressionist
describe SetupAssociation do
let(:mock) { Minitest::Mock.new }
let(:set_up) { SetupAssociation.new(mock) }
before do
# expects attr_accessible to return true
# and pass 12 arguments
mock.
expect(:attr_accessible, true) do |args|
args.size == 12
end
end
describe "attr_accessible" do
it "includes" do
set_up.stub :toggle, true do
set_up.include_attr_acc?.must_equal true
mock.verify
end
end
end
describe "belongs_to" do
it "active_record" do
mock.expect(:belongs_to, true, [Symbol, Hash])
set_up.define_belongs_to.must_equal true
end
end
describe "#set" do
it "sets an association" do
def set_up.include_attr_acc?; true; end
set_up.stub(:define_belongs_to, true) {
set_up.set.must_equal true
}
end
end
# returns done if thruthy
end
end

View File

@ -1 +1,2 @@
--color --color
--require spec_helper

View File

@ -1,7 +1,7 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'rails', '~> 3.2' gem 'rails', '~> 6'
gem 'rake', '< 11.0'
gem 'public_suffix', '< 1.5.0' gem 'public_suffix', '< 1.5.0'
gem 'impressionist', :path => '../../' gem 'impressionist', :path => '../../'
@ -12,11 +12,12 @@ platforms :jruby do
gem 'jruby-openssl' gem 'jruby-openssl'
end end
gem 'bigdecimal', '1.4.2'
platforms :ruby, :mswin, :mingw do platforms :ruby, :mswin, :mingw do
gem 'pg' gem 'pg'
gem 'sqlite3'
gem 'mysql2' gem 'mysql2'
gem 'mongoid', '~> 3.1' gem 'mongoid', '~> 7.0'
gem 'bson_ext' gem 'bson_ext'
end end
@ -25,8 +26,8 @@ gem 'json'
# Gems used only for assets and not required # Gems used only for assets and not required
# in production environments by default. # in production environments by default.
group :assets do group :assets do
gem 'sass-rails', '~> 3.2' gem 'sass-rails', '~> 5.0'
gem 'coffee-rails', '~> 3.2' gem 'coffee-rails', '~> 5.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes # See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer' # gem 'therubyracer'
@ -34,9 +35,11 @@ group :assets do
gem 'uglifier', '>= 1.0.3' gem 'uglifier', '>= 1.0.3'
end end
group :development, :test do group :development, :test do
gem 'autotest-notification' gem 'autotest-notification'
gem 'rspec-rails', '~> 2.14.0' gem 'sqlite3', '~> 1.4'
gem 'rspec-rails', '~> 4.0'
gem 'spork' gem 'spork'
# test/unit has been removed by default in Ruby 2.2.x+ # test/unit has been removed by default in Ruby 2.2.x+
@ -47,7 +50,8 @@ group :test do
gem 'capybara' gem 'capybara'
gem 'simplecov' gem 'simplecov'
gem 'systemu' gem 'systemu'
gem 'pry'
end end
gem 'jquery-rails' gem 'jquery-rails'
gem 'friendly_id', '~> 4.0.9' gem 'friendly_id', '~> 5'

View File

@ -0,0 +1 @@
{}

View File

@ -1,5 +1,5 @@
# This controller imports the impressionist module to make the modules methods available for testing # This controller imports the impressionist module to make the modules methods available for testing
class DummyController < ActionController::Base class DummyController < ApplicationController
impressionist impressionist

View File

@ -22,12 +22,10 @@ TestApp::Application.configure do
# Only use best-standards-support built into browsers # Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for Active Record models config.eager_load = false
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works # Raise exception on mass assignment protection for Active Record models
# with SQLite, MySQL, and PostgreSQL) # config.active_record.mass_assignment_sanitizer = :strict
config.active_record.auto_explain_threshold_in_seconds = 0.5
# Do not compress assets # Do not compress assets
config.assets.compress = false config.assets.compress = false

View File

@ -29,8 +29,12 @@ TestApp::Application.configure do
# ActionMailer::Base.deliveries array. # ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test config.action_mailer.delivery_method = :test
config.eager_load = false
# config.active_record.sqlite3.represent_boolean_as_integer = true
# Raise exception on mass assignment protection for Active Record models # Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict # config.active_record.mass_assignment_sanitizer = :strict
# Print deprecation notices to the stderr # Print deprecation notices to the stderr
config.active_support.deprecation = :stderr config.active_support.deprecation = :stderr

View File

@ -4,4 +4,4 @@
# If you change this key, all old signed cookies will become invalid! # If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random, # Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks. # no regular words or you'll be exposed to dictionary attacks.
TestApp::Application.config.secret_token = '4a6fd2eb397985331d209be32073259ed7c25aef4fafcabb00e483ee548e592322277eb15459bdb257b65f31146eda92684b3e7a98ea1b2dfad9b0d08ab62e10' TestApp::Application.config.secret_key_base = '4a6fd2eb397985331d209be32073259ed7c25aef4fafcabb00e483ee548e592322277eb15459bdb257b65f31146eda92684b3e7a98ea1b2dfad9b0d08ab62e10'

View File

@ -1,4 +1,4 @@
class CreateArticles < ActiveRecord::Migration class CreateArticles < ActiveRecord::Migration[4.2]
def self.up def self.up
create_table :articles do |t| create_table :articles do |t|
t.string :name t.string :name

View File

@ -1,4 +1,4 @@
class CreatePosts < ActiveRecord::Migration class CreatePosts < ActiveRecord::Migration[4.2]
def self.up def self.up
create_table :posts do |t| create_table :posts do |t|
t.string :name t.string :name

View File

@ -1,4 +1,4 @@
class CreateWidgets < ActiveRecord::Migration class CreateWidgets < ActiveRecord::Migration[4.2]
def self.up def self.up
create_table :widgets do |t| create_table :widgets do |t|
t.string :name t.string :name

View File

@ -1,4 +1,4 @@
class CreateImpressionsTable < ActiveRecord::Migration class CreateImpressionsTable < ActiveRecord::Migration[4.2]
def self.up def self.up
create_table :impressions, :force => true do |t| create_table :impressions, :force => true do |t|
t.string :impressionable_type t.string :impressionable_type

View File

@ -1,4 +1,4 @@
class CreateProfiles < ActiveRecord::Migration class CreateProfiles < ActiveRecord::Migration[4.2]
def change def change
create_table :profiles do |t| create_table :profiles do |t|
t.string :username t.string :username

View File

@ -1,4 +1,4 @@
class CreateFriendlyIdSlugs < ActiveRecord::Migration class CreateFriendlyIdSlugs < ActiveRecord::Migration[4.2]
def self.up def self.up
create_table :friendly_id_slugs do |t| create_table :friendly_id_slugs do |t|

View File

@ -1,36 +1,34 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead # This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to # of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition. # incrementally modify your database, and then regenerate this schema definition.
# #
# Note that this schema.rb definition is the authoritative source for your # This file is the source Rails uses to define your schema when running `rails
# database schema. If you need to create the application database on another # db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# system, you should be using db:schema:load, not running all the migrations # be faster and is potentially less error prone than running all of your
# from scratch. The latter is a flawed and unsustainable approach (the more migrations # migrations from scratch. Old migrations may fail to apply correctly if those
# you'll amass, the slower it'll run and the greater likelihood for issues). # migrations use external dependencies or application code.
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(:version => 20150207140310) do ActiveRecord::Schema.define(version: 2015_02_07_140310) do
create_table "articles", :force => true do |t| create_table "articles", force: :cascade do |t|
t.string "name" t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "friendly_id_slugs", :force => true do |t|
t.string "slug", :null => false
t.integer "sluggable_id", :null => false
t.string "sluggable_type", :limit => 40
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at"
end end
add_index "friendly_id_slugs", ["slug", "sluggable_type"], :name => "index_friendly_id_slugs_on_slug_and_sluggable_type", :unique => true create_table "friendly_id_slugs", force: :cascade do |t|
add_index "friendly_id_slugs", ["sluggable_id"], :name => "index_friendly_id_slugs_on_sluggable_id" t.string "slug", null: false
add_index "friendly_id_slugs", ["sluggable_type"], :name => "index_friendly_id_slugs_on_sluggable_type" t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 40
t.datetime "created_at"
t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type", unique: true
t.index ["sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_id"
t.index ["sluggable_type"], name: "index_friendly_id_slugs_on_sluggable_type"
end
create_table "impressions", :force => true do |t| create_table "impressions", force: :cascade do |t|
t.string "impressionable_type" t.string "impressionable_type"
t.integer "impressionable_id" t.integer "impressionable_id"
t.integer "user_id" t.integer "user_id"
@ -43,38 +41,37 @@ ActiveRecord::Schema.define(:version => 20150207140310) do
t.text "message" t.text "message"
t.text "params" t.text "params"
t.text "referrer" t.text "referrer"
t.datetime "created_at", :null => false t.datetime "created_at"
t.datetime "updated_at", :null => false t.datetime "updated_at"
t.index ["controller_name", "action_name", "ip_address"], name: "controlleraction_ip_index"
t.index ["controller_name", "action_name", "request_hash"], name: "controlleraction_request_index"
t.index ["controller_name", "action_name", "session_hash"], name: "controlleraction_session_index"
t.index ["impressionable_type", "impressionable_id", "ip_address"], name: "poly_ip_index"
t.index ["impressionable_type", "impressionable_id", "params"], name: "poly_params_request_index"
t.index ["impressionable_type", "impressionable_id", "request_hash"], name: "poly_request_index"
t.index ["impressionable_type", "impressionable_id", "session_hash"], name: "poly_session_index"
t.index ["impressionable_type", "message", "impressionable_id"], name: "impressionable_type_message_index"
t.index ["user_id"], name: "index_impressions_on_user_id"
end end
add_index "impressions", ["controller_name", "action_name", "ip_address"], :name => "controlleraction_ip_index" create_table "posts", force: :cascade do |t|
add_index "impressions", ["controller_name", "action_name", "request_hash"], :name => "controlleraction_request_index"
add_index "impressions", ["controller_name", "action_name", "session_hash"], :name => "controlleraction_session_index"
add_index "impressions", ["impressionable_type", "impressionable_id", "ip_address"], :name => "poly_ip_index"
add_index "impressions", ["impressionable_type", "impressionable_id", "params"], :name => "poly_params_request_index"
add_index "impressions", ["impressionable_type", "impressionable_id", "request_hash"], :name => "poly_request_index"
add_index "impressions", ["impressionable_type", "impressionable_id", "session_hash"], :name => "poly_session_index"
add_index "impressions", ["impressionable_type", "message", "impressionable_id"], :name => "impressionable_type_message_index"
add_index "impressions", ["user_id"], :name => "index_impressions_on_user_id"
create_table "posts", :force => true do |t|
t.string "name" t.string "name"
t.datetime "created_at", :null => false t.datetime "created_at"
t.datetime "updated_at", :null => false t.datetime "updated_at"
end end
create_table "profiles", :force => true do |t| create_table "profiles", force: :cascade do |t|
t.string "username" t.string "username"
t.string "slug" t.string "slug"
t.datetime "created_at", :null => false t.datetime "created_at"
t.datetime "updated_at", :null => false t.datetime "updated_at"
end end
create_table "widgets", :force => true do |t| create_table "widgets", force: :cascade do |t|
t.string "name" t.string "name"
t.integer "impressions_count", :default => 0 t.integer "impressions_count", default: 0
t.datetime "created_at", :null => false t.datetime "created_at"
t.datetime "updated_at", :null => false t.datetime "updated_at"
end end
end end

View File

@ -1,90 +1,113 @@
# frozen_string_literal: false
require 'spec_helper' require 'spec_helper'
describe ArticlesController do describe ArticlesController, type: :controller do
fixtures :articles,:impressions,:posts,:widgets fixtures :articles, :impressions, :posts, :widgets
render_views render_views
it "should make the impressionable_hash available" do it 'makes the impressionable_hash available' do
get "index" get :index
response.body.include?("false").should eq true expect(response.body).to include('false')
end end
it "should log an impression with a message" do it 'logs an impression with a message' do
get "index" get 'index'
Impression.all.size.should eq 12
Article.first.impressions.last.message.should eq "this is a test article impression" latest_impression = Article.first.impressions.last
Article.first.impressions.last.controller_name.should eq "articles"
Article.first.impressions.last.action_name.should eq "index" expect(Impression.all.size).to eq 12
expect(latest_impression.message).to eq 'this is a test article impression'
expect(latest_impression.controller_name).to eq 'articles'
expect(latest_impression.action_name).to eq 'index'
end end
it "should log an impression without a message" do it 'logs an impression without a message' do
get "show", :id=> 1 get :show, params: { id: 1 }
Impression.all.size.should eq 12
Article.first.impressions.last.message.should eq nil latest_impression = Article.first.impressions.last
Article.first.impressions.last.controller_name.should eq "articles"
Article.first.impressions.last.action_name.should eq "show" expect(Impression.all.size).to eq 12
expect(latest_impression.message).to eq nil
expect(latest_impression.controller_name).to eq 'articles'
expect(latest_impression.action_name).to eq 'show'
end end
it "should log the user_id if user is authenticated (@current_user before_action method)" do it 'logs the user_id if user is authenticated (@current_user before_action method)' do
session[:user_id] = 123 session[:user_id] = 123
get "show", :id=> 1 get :show, params: { id: 1 }
Article.first.impressions.last.user_id.should eq 123
expect(Article.first.impressions.last.user_id).to eq 123
end end
it "should not log the user_id if user is authenticated" do it 'does not log the user_id if user is authenticated' do
get "show", :id=> 1 get :show, params: { id: 1 }
Article.first.impressions.last.user_id.should eq nil
expect(Article.first.impressions.last.user_id).to eq nil
end end
it "should log the request_hash, ip_address, referrer and session_hash" do it 'logs the request_hash, ip_address, referrer and session_hash' do
get "show", :id=> 1 get :show, params: { id: 1 }
Impression.last.request_hash.size.should eq 64
Impression.last.ip_address.should eq "0.0.0.0" impression = Impression.last
Impression.last.session_hash.size.should eq 32
Impression.last.referrer.should eq nil expect(impression.request_hash.size).to eq 64
expect(impression.ip_address).to eq '0.0.0.0'
expect(impression.session_hash.size).to eq 32
expect(impression.referrer).to eq nil
end end
# Capybara has change the way it works # Capybara has change the way it works
# We need to pass :type options in order to make include helper methods # We need to pass :type options in order to make include helper methods
# see https://github.com/jnicklas/capybara#using-capybara-with-rspec # see https://github.com/jnicklas/capybara#using-capybara-with-rspec
it "should log the referrer when you click a link", :type => :feature do it 'logs the referrer when you click a link', type: :feature do
default_url_options[:host] = "test.host"
visit article_url(Article.first) visit article_url(Article.first)
click_link "Same Page" click_link 'Same Page'
Impression.last.referrer.should eq "http://test.host/articles/1" expect(Impression.last.referrer).to eq 'http://test.host/articles/1'
end end
it "should log request with params (checked = true)" do it 'logs request with params (checked = true)' do
get "show", id: 1, checked: true get :show, params: { id: 1, checked: true }
Impression.last.params.should eq({"checked"=>true})
Impression.last.request_hash.size.should eq 64 impression = Impression.last
Impression.last.ip_address.should eq "0.0.0.0"
Impression.last.session_hash.size.should eq 32 expect(impression.params).to eq({ 'checked' => "true" })
Impression.last.referrer.should eq nil expect(impression.request_hash.size).to eq 64
expect(impression.ip_address).to eq '0.0.0.0'
expect(impression.session_hash.size).to eq 32
expect(impression.referrer).to eq nil
end end
it "should log request with params {}" do it 'logs request with params: {}' do
get "index" get 'index'
Impression.last.params.should eq({})
Impression.last.request_hash.size.should eq 64 impression = Impression.last
Impression.last.ip_address.should eq "0.0.0.0"
Impression.last.session_hash.size.should eq 32 expect(impression.params).to eq({})
Impression.last.referrer.should eq nil expect(impression.request_hash.size).to eq 64
expect(impression.ip_address).to eq '0.0.0.0'
expect(impression.session_hash.size).to eq 32
expect(impression.referrer).to eq nil
end end
describe "when filtering params" do describe 'when filtering params' do
before do before do
@_filtered_params = Rails.application.config.filter_parameters @_filtered_params = Rails.application.config.filter_parameters
Rails.application.config.filter_parameters = [:password] Rails.application.config.filter_parameters = [:password]
end end
it "values should not be recorded" do
get "index", password: "best-password-ever"
Impression.last.params.should eq("password" => "[FILTERED]")
end
after do after do
Rails.application.config.filter_parameters = @_filtered_params Rails.application.config.filter_parameters = @_filtered_params
end end
it 'values should not be recorded' do
get 'index', params: { password: 'best-password-ever' }
expect(Impression.last.params).to eq('password' => '[FILTERED]')
end
end end
end end

View File

@ -1,11 +1,13 @@
# frozen_string_literal: false
require 'spec_helper' require 'spec_helper'
describe DummyController do describe DummyController, type: :controller do
fixtures :impressions fixtures :impressions
render_views render_views
it "should log impression at the per action level on non-restful controller" do it 'logs impression at the per action level on non-restful controller' do
get "index" get 'index'
Impression.all.size.should eq 12 expect(Impression.all.size).to eq 12
end end
end end

View File

@ -2,158 +2,192 @@ require 'spec_helper'
# we use the posts controller as it uses the impressionsist module. any such controller would do. # we use the posts controller as it uses the impressionsist module. any such controller would do.
describe DummyController do describe DummyController do
before do before do
@impression_count = Impression.all.size @impression_count = Impression.all.size
end end
describe "impressionist filter uniqueness" do describe "impressionist filter uniqueness" do
it "ignore uniqueness if not requested" do
it "should ignore uniqueness if not requested" do
controller.impressionist_subapp_filter controller.impressionist_subapp_filter
controller.impressionist_subapp_filter controller.impressionist_subapp_filter
Impression.should have(@impression_count + 2).records
expect(Impression.count).to eq(@impression_count + 2)
end end
it "should recognize unique session" do it "recognize unique session" do
controller.stub(:session_hash).and_return(request.session_options[:id]) allow(controller).to receive(:session_hash).and_return(request.session_options[:id])
controller.impressionist_subapp_filter(unique: [:session_hash]) controller.impressionist_subapp_filter(unique: [:session_hash])
controller.impressionist_subapp_filter(unique: [:session_hash]) controller.impressionist_subapp_filter(unique: [:session_hash])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to eq(@impression_count + 1)
end end
it "should recognize unique ip" do it "recognize unique ip" do
controller.request.stub(:remote_ip).and_return("1.2.3.4") allow(controller).to receive(:remote_ip).and_return("1.2.3.4")
controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address])
controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique request" do it "recognize unique request" do
controller.impressionist_subapp_filter(unique: [:request_hash]) controller.impressionist_subapp_filter(unique: [:request_hash])
controller.impressionist_subapp_filter(unique: [:request_hash]) controller.impressionist_subapp_filter(unique: [:request_hash])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique action" do it "recognize unique action" do
controller.stub(:action_name).and_return("test_action") allow(controller).to receive(:action_name).and_return("test_action")
controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name])
controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique controller" do it "recognize unique controller" do
controller.stub(:controller_name).and_return("post") allow(controller).to receive(:controller_name).and_return("post")
controller.impressionist_subapp_filter(unique: [:controller_name]) controller.impressionist_subapp_filter(unique: [:controller_name])
controller.impressionist_subapp_filter(unique: [:controller_name]) controller.impressionist_subapp_filter(unique: [:controller_name])
Impression.should have(@impression_count + 1).records expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique user" do it "recognize unique user" do
controller.stub(:user_id).and_return(42) allow(controller).to receive(:user_id).and_return(42)
controller.impressionist_subapp_filter(unique: [:user_id]) controller.impressionist_subapp_filter(unique: [:user_id])
controller.impressionist_subapp_filter(unique: [:user_id]) controller.impressionist_subapp_filter(unique: [:user_id])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique referer" do it "recognize unique referer" do
controller.request.stub(:referer).and_return("http://foo/bar") allow(controller.request).to receive(:referer).and_return("http://foo/bar")
controller.impressionist_subapp_filter(unique: [:referrer]) controller.impressionist_subapp_filter(unique: [:referrer])
controller.impressionist_subapp_filter(unique: [:referrer]) controller.impressionist_subapp_filter(unique: [:referrer])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique id" do it "recognize unique id" do
controller.stub(:params).and_return({:id => "666"}) # for correct impressionable id in filter allow(controller).to receive(:params).and_return({ :id => "666" }) # for correct impressionable id in filter
controller.impressionist_subapp_filter(unique: [:impressionable_id]) controller.impressionist_subapp_filter(unique: [:impressionable_id])
controller.impressionist_subapp_filter(unique: [:impressionable_id]) controller.impressionist_subapp_filter(unique: [:impressionable_id])
Impression.should have(@impression_count + 1).records expect(Impression.count).to equal(@impression_count + 1)
end end
# extra redundant test for important controller and action combination. # extra redundant test for important controller and action combination.
it "should recognize different controller and action" do it "recognize different controller and action" do
controller.stub(:controller_name).and_return("post") allow(controller).to receive(:controller_name).and_return("post")
controller.stub(:action_name).and_return("test_action") allow(controller).to receive(:action_name).and_return("test_action")
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
Impression.should have(@impression_count + 1).records
controller.stub(:action_name).and_return("another_action") expect(Impression.count).to equal(@impression_count + 1)
allow(controller).to receive(:action_name).and_return("another_action")
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
Impression.should have(@impression_count + 2).records
controller.stub(:controller_name).and_return("article") expect(Impression.count).to equal(@impression_count + 2)
allow(controller).to receive(:controller_name).and_return("article")
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
controller.impressionist_subapp_filter(unique: [:controller_name, :action_name]) controller.impressionist_subapp_filter(unique: [:controller_name, :action_name])
Impression.should have(@impression_count + 3).records
expect(Impression.count).to equal(@impression_count + 3)
end end
it "should recognize different action" do it "recognize different action" do
controller.stub(:action_name).and_return("test_action") allow(controller).to receive(:action_name).and_return("test_action")
controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name])
controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name])
Impression.should have(@impression_count + 1).records expect(Impression.count).to equal(@impression_count + 1)
controller.stub(:action_name).and_return("another_action") allow(controller).to receive(:action_name).and_return("another_action")
controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name])
controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name])
Impression.should have(@impression_count + 2).records expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize different controller" do it "recognize different controller" do
controller.stub(:controller_name).and_return("post") allow(controller).to receive(:controller_name).and_return("post")
controller.impressionist_subapp_filter(unique: [:controller_name]) controller.impressionist_subapp_filter(unique: [:controller_name])
controller.impressionist_subapp_filter(unique: [:controller_name]) controller.impressionist_subapp_filter(unique: [:controller_name])
Impression.should have(@impression_count + 1).records expect(Impression.count).to equal(@impression_count + 1)
controller.stub(:controller_name).and_return("article") allow(controller).to receive(:controller_name).and_return("article")
controller.impressionist_subapp_filter(unique: [:controller_name]) controller.impressionist_subapp_filter(unique: [:controller_name])
controller.impressionist_subapp_filter(unique: [:controller_name]) controller.impressionist_subapp_filter(unique: [:controller_name])
Impression.should have(@impression_count + 2).records expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize different session" do it "recognize different session" do
controller.stub(:session_hash).and_return("foo") allow(controller).to receive(:session_hash).and_return("foo")
controller.impressionist_subapp_filter(unique: [:session_hash]) controller.impressionist_subapp_filter(unique: [:session_hash])
controller.impressionist_subapp_filter(unique: [:session_hash]) controller.impressionist_subapp_filter(unique: [:session_hash])
Impression.should have(@impression_count + 1).records expect(Impression.count).to equal(@impression_count + 1)
controller.stub(:session_hash).and_return("bar") allow(controller).to receive(:session_hash).and_return("bar")
controller.impressionist_subapp_filter(unique: [:session_hash]) controller.impressionist_subapp_filter(unique: [:session_hash])
controller.impressionist_subapp_filter(unique: [:session_hash]) controller.impressionist_subapp_filter(unique: [:session_hash])
Impression.should have(@impression_count + 2).records expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize different ip" do it "recognize different ip" do
controller.request.stub(:remote_ip).and_return("1.2.3.4") allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4")
controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address])
controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address])
Impression.should have(@impression_count + 1).records
controller.request.stub(:remote_ip).and_return("5.6.7.8") expect(Impression.count).to equal(@impression_count + 1)
allow(controller.request).to receive(:remote_ip).and_return("5.6.7.8")
controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address])
controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address])
Impression.should have(@impression_count + 2).records
expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize different referer" do it "recognize different referer" do
controller.request.stub(:referer).and_return("http://foo/bar") allow(controller.request).to receive(:referer).and_return("http://foo/bar")
controller.impressionist_subapp_filter(unique: [:referrer]) controller.impressionist_subapp_filter(unique: [:referrer])
controller.impressionist_subapp_filter(unique: [:referrer]) controller.impressionist_subapp_filter(unique: [:referrer])
Impression.should have(@impression_count + 1).records
controller.request.stub(:referer).and_return("http://bar/fo") expect(Impression.count).to equal(@impression_count + 1)
allow(controller.request).to receive(:referer).and_return("http://bar/fo")
controller.impressionist_subapp_filter(unique: [:referrer]) controller.impressionist_subapp_filter(unique: [:referrer])
controller.impressionist_subapp_filter(unique: [:referrer]) controller.impressionist_subapp_filter(unique: [:referrer])
Impression.should have(@impression_count + 2).records expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize different id" do it "recognize different id" do
controller.stub(:params).and_return({:id => "666"}) # for correct impressionable id in filter allow(controller).to receive(:params).and_return({ :id => "666" }) # for correct impressionable id in filter
controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id]) controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id])
controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id]) controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id])
controller.stub(:params).and_return({:id => "42"}) # for correct impressionable id in filter
allow(controller).to receive(:params).and_return({ :id => "42" }) # for correct impressionable id in filter
controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id]) controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id])
controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id]) controller.impressionist_subapp_filter(unique: [:impressionable_type, :impressionable_id])
Impression.should have(@impression_count + 2).records
expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize combined uniqueness" do it "recognize combined uniqueness" do
controller.stub(:action_name).and_return("test_action") allow(controller).to receive(:action_name).and_return("test_action")
controller.impressionist_subapp_filter(unique: [:ip_address, :request_hash, :action_name]) controller.impressionist_subapp_filter(unique: [:ip_address, :request_hash, :action_name])
controller.impressionist_subapp_filter(unique: [:request_hash, :ip_address, :action_name]) controller.impressionist_subapp_filter(unique: [:request_hash, :ip_address, :action_name])
controller.impressionist_subapp_filter(unique: [:request_hash, :action_name]) controller.impressionist_subapp_filter(unique: [:request_hash, :action_name])
@ -162,110 +196,142 @@ describe DummyController do
controller.impressionist_subapp_filter(unique: [:action_name]) controller.impressionist_subapp_filter(unique: [:action_name])
controller.impressionist_subapp_filter(unique: [:ip_address]) controller.impressionist_subapp_filter(unique: [:ip_address])
controller.impressionist_subapp_filter(unique: [:request_hash]) controller.impressionist_subapp_filter(unique: [:request_hash])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize combined non-uniqueness" do it "recognize combined non-uniqueness" do
controller.stub(:action_name).and_return(nil) allow(controller).to receive(:action_name).and_return(nil)
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
controller.stub(:action_name).and_return("test_action")
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
controller.stub(:action_name).and_return("another_action")
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
Impression.should have(@impression_count + 3).records
end
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
allow(controller).to receive(:action_name).and_return("test_action")
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
allow(controller).to receive(:action_name).and_return("another_action")
controller.impressionist_subapp_filter(unique: [:ip_address, :action_name])
expect(Impression.count).to equal(@impression_count + 3)
end
end end
describe "impressionist method uniqueness for impressionables" do describe "impressionist method uniqueness for impressionables" do
# in this test we reuse the post model. might break if model changes. # in this test we reuse the post model. might break if model changes.
it "should ignore uniqueness if not requested" do it "ignore uniqueness if not requested" do
impressionable = Post.create impressionable = Post.create
controller.impressionist impressionable controller.impressionist impressionable
controller.impressionist impressionable controller.impressionist impressionable
Impression.should have(@impression_count + 2).records expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize unique session" do it "recognize unique session" do
controller.stub(:session_hash).and_return(request.session_options[:id]) allow(controller).to receive(:session_hash).and_return(request.session_options[:id])
impressionable = Post.create impressionable = Post.create
controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash])
controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique ip" do it "recognize unique ip" do
controller.request.stub(:remote_ip).and_return("1.2.3.4") allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4")
impressionable = Post.create impressionable = Post.create
controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address])
controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique request" do it "recognize unique request" do
impressionable = Post.create impressionable = Post.create
controller.impressionist(impressionable, nil, :unique => [:request_hash]) controller.impressionist(impressionable, nil, :unique => [:request_hash])
controller.impressionist(impressionable, nil, :unique => [:request_hash]) controller.impressionist(impressionable, nil, :unique => [:request_hash])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique user" do it "recognize unique user" do
controller.stub(:user_id).and_return(666) allow(controller).to receive(:user_id).and_return(666)
impressionable = Post.create impressionable = Post.create
controller.impressionist(impressionable, nil, :unique => [:user_id]) controller.impressionist(impressionable, nil, :unique => [:user_id])
controller.impressionist(impressionable, nil, :unique => [:user_id]) controller.impressionist(impressionable, nil, :unique => [:user_id])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize unique referer" do it "recognize unique referer" do
controller.request.stub(:referer).and_return("http://foo/bar") allow(controller.request).to receive(:referer).and_return("http://foo/bar")
impressionable = Post.create impressionable = Post.create
controller.impressionist(impressionable, nil, :unique => [:referrer]) controller.impressionist(impressionable, nil, :unique => [:referrer])
controller.impressionist(impressionable, nil, :unique => [:referrer]) controller.impressionist(impressionable, nil, :unique => [:referrer])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize different session" do it "recognize different session" do
impressionable = Post.create impressionable = Post.create
controller.stub(:session_hash).and_return("foo")
allow(controller).to receive(:session_hash).and_return("foo")
controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash])
controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash])
Impression.should have(@impression_count + 1).records
controller.stub(:session_hash).and_return("bar") expect(Impression.count).to equal(@impression_count + 1)
allow(controller).to receive(:session_hash).and_return("bar")
controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash])
controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash])
Impression.should have(@impression_count + 2).records
expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize different ip" do it "recognize different ip" do
controller.request.stub(:remote_ip).and_return("1.2.3.4") allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4")
impressionable = Post.create impressionable = Post.create
controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address])
controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address])
Impression.should have(@impression_count + 1).records
controller.request.stub(:remote_ip).and_return("5.6.7.8") expect(Impression.count).to equal(@impression_count + 1)
allow(controller.request).to receive(:remote_ip).and_return("5.6.7.8")
controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address])
controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address])
Impression.should have(@impression_count + 2).records
expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize different user" do it "recognize different user" do
impressionable = Post.create impressionable = Post.create
controller.stub(:user_id).and_return(666)
allow(controller).to receive(:user_id).and_return(666)
controller.impressionist(impressionable, nil, :unique => [:user_id]) controller.impressionist(impressionable, nil, :unique => [:user_id])
controller.impressionist(impressionable, nil, :unique => [:user_id]) controller.impressionist(impressionable, nil, :unique => [:user_id])
Impression.should have(@impression_count + 1).records
controller.stub(:user_id).and_return(42) expect(Impression.count).to equal(@impression_count + 1)
allow(controller).to receive(:user_id).and_return(42)
controller.impressionist(impressionable, nil, :unique => [:user_id]) controller.impressionist(impressionable, nil, :unique => [:user_id])
controller.impressionist(impressionable, nil, :unique => [:user_id]) controller.impressionist(impressionable, nil, :unique => [:user_id])
Impression.should have(@impression_count + 2).records
expect(Impression.count).to equal(@impression_count + 2)
end end
it "should recognize combined uniqueness" do it "recognize combined uniqueness" do
impressionable = Post.create impressionable = Post.create
controller.stub(:session_hash).and_return("foo") allow(controller).to receive(:session_hash).and_return("foo")
controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash, :session_hash])
controller.impressionist(impressionable, nil, :unique => [:request_hash, :ip_address, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:request_hash, :ip_address, :session_hash])
controller.impressionist(impressionable, nil, :unique => [:request_hash, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:request_hash, :session_hash])
@ -274,64 +340,62 @@ describe DummyController do
controller.impressionist(impressionable, nil, :unique => [:session_hash]) controller.impressionist(impressionable, nil, :unique => [:session_hash])
controller.impressionist(impressionable, nil, :unique => [:ip_address]) controller.impressionist(impressionable, nil, :unique => [:ip_address])
controller.impressionist(impressionable, nil, :unique => [:request_hash]) controller.impressionist(impressionable, nil, :unique => [:request_hash])
Impression.should have(@impression_count + 1).records
expect(Impression.count).to equal(@impression_count + 1)
end end
it "should recognize combined non-uniqueness" do it "recognize combined non-uniqueness" do
impressionable = Post.create impressionable = Post.create
controller.stub(:session_hash).and_return(nil) allow(controller).to receive(:session_hash).and_return(nil)
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
controller.stub(:session_hash).and_return("foo") allow(controller).to receive(:session_hash).and_return("foo")
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
controller.stub(:session_hash).and_return("bar") allow(controller).to receive(:session_hash).and_return("bar")
controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:ip_address, :session_hash])
Impression.should have(@impression_count + 3).records expect(Impression.count).to equal(@impression_count + 3)
end end
end end
describe "impressionist filter and method uniqueness" do describe "impressionist filter and method uniqueness" do
it "recognize uniqueness" do
it "should recognize uniqueness" do
impressionable = Post.create impressionable = Post.create
controller.stub(:controller_name).and_return("posts") # for correct impressionable type in filter allow(controller).to receive(:controller_name).and_return("posts") # for correct impressionable type in filter
controller.stub(:params).and_return({:id => impressionable.id.to_s}) # for correct impressionable id in filter allow(controller).to receive(:params).and_return({ :id => impressionable.id.to_s }) # for correct impressionable id in filter
controller.stub(:session_hash).and_return("foo") allow(controller).to receive(:session_hash).and_return("foo")
controller.request.stub(:remote_ip).and_return("1.2.3.4") allow(controller.request).to receive(:remote_ip).and_return("1.2.3.4")
# order of the following methods is important for the test! # order of the following methods is important for the test!
controller.impressionist_subapp_filter(unique: [:ip_address, :request_hash, :session_hash]) controller.impressionist_subapp_filter(unique: [:ip_address, :request_hash, :session_hash])
controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash, :session_hash]) controller.impressionist(impressionable, nil, :unique => [:ip_address, :request_hash, :session_hash])
Impression.should have(@impression_count + 1).records expect(Impression.count).to equal(@impression_count + 1)
end end
end end
describe 'impressionist with friendly id' do describe 'impressionist with friendly id' do
it 'should unique' do it 'unique' do
impressionable = Profile.create({username: 'test_profile', slug: 'test_profile'}) impressionable = Profile.create({ username: 'test_profile', slug: 'test_profile' })
controller.stub(:controller_name).and_return('profile') allow(controller).to receive(:controller_name).and_return('profile')
controller.stub(:action_name).and_return('show') allow(controller).to receive(:action_name).and_return('show')
controller.stub(:params).and_return({id: impressionable.slug}) allow(controller).to receive(:params).and_return({ id: impressionable.slug })
controller.request.stub(:remote_ip).and_return('1.2.3.4') allow(controller.request).to receive(:remote_ip).and_return('1.2.3.4')
controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id]) controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id])
controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id]) controller.impressionist(impressionable, nil, :unique => [:impressionable_type, :impressionable_id])
Impression.should have(@impression_count + 1).records expect(Impression.count).to equal(@impression_count + 1)
end end
end end
shared_examples_for 'an impressionable action' do shared_examples_for 'an impressionable action' do
it 'should record an impression' do it 'record an impression' do
controller.impressionist_subapp_filter(condition) controller.impressionist_subapp_filter(condition)
Impression.should have(@impression_count + 1).records expect(Impression.count).to equal(@impression_count + 1)
end end
end end
shared_examples_for 'an unimpressionable action' do shared_examples_for 'an unimpressionable action' do
it 'should record an impression' do it 'record an impression' do
controller.impressionist_subapp_filter(condition) controller.impressionist_subapp_filter(condition)
Impression.should have(@impression_count).records expect(Impression.count).to equal(@impression_count)
end end
end end
@ -339,27 +403,29 @@ describe DummyController do
describe ":if condition" do describe ":if condition" do
context "true condition" do context "true condition" do
before do before do
controller.stub(:true_condition).and_return(true) allow(controller).to receive(:true_condition).and_return(true)
end
it_behaves_like 'an impressionable action' do
let(:condition) {{ if: :true_condition }}
end end
it_behaves_like 'an impressionable action' do it_behaves_like 'an impressionable action' do
let(:condition) {{ if: lambda { true } }} let(:condition) { { if: :true_condition } }
end
it_behaves_like 'an impressionable action' do
let(:condition) { { if: -> { true } } }
end end
end end
context "false condition" do context "false condition" do
before do before do
controller.stub(:false_condition).and_return(false) allow(controller).to receive(:false_condition).and_return(false)
end
it_behaves_like 'an unimpressionable action' do
let(:condition) {{ if: :false_condition }}
end end
it_behaves_like 'an unimpressionable action' do it_behaves_like 'an unimpressionable action' do
let(:condition) {{ if: lambda { false } }} let(:condition) { { if: :false_condition } }
end
it_behaves_like 'an unimpressionable action' do
let(:condition) { { if: -> { false } } }
end end
end end
end end
@ -367,30 +433,31 @@ describe DummyController do
describe ":unless condition" do describe ":unless condition" do
context "true condition" do context "true condition" do
before do before do
controller.stub(:true_condition).and_return(true) allow(controller).to receive(:true_condition).and_return(true)
end
it_behaves_like 'an unimpressionable action' do
let(:condition) {{ unless: :true_condition }}
end end
it_behaves_like 'an unimpressionable action' do it_behaves_like 'an unimpressionable action' do
let(:condition) {{ unless: lambda { true } }} let(:condition) { { unless: :true_condition } }
end
it_behaves_like 'an unimpressionable action' do
let(:condition) { { unless: -> { true } } }
end end
end end
context "false condition" do context "false condition" do
before do before do
controller.stub(:false_condition).and_return(false) allow(controller).to receive(:false_condition).and_return(false)
end
it_behaves_like 'an impressionable action' do
let(:condition) {{ unless: :false_condition }}
end end
it_behaves_like 'an impressionable action' do it_behaves_like 'an impressionable action' do
let(:condition) {{ unless: lambda { false } }} let(:condition) { { unless: :false_condition } }
end
it_behaves_like 'an impressionable action' do
let(:condition) { { unless: -> { false } } }
end end
end end
end end
end end
end end

View File

@ -1,28 +1,36 @@
require 'spec_helper' require 'spec_helper'
describe PostsController do describe PostsController do
it "should log impression at the action level" do it "logs impression at the action level" do
get "show", :id=> 1 get :show, params: { id: 1 }
Impression.all.size.should eq 12
Impression.last.controller_name.should eq "posts" expect(Impression.all.size).to eq 12
Impression.last.action_name.should eq "show"
Impression.last.impressionable_type.should eq "Post" impression = Impression.last
Impression.last.impressionable_id.should eq 1
expect(impression.controller_name).to eq "posts"
expect(impression.action_name).to eq "show"
expect(impression.impressionable_type).to eq "Post"
expect(impression.impressionable_id).to eq 1
end end
it "should log the user_id if user is authenticated (current_user helper method)" do it "logs the user_id if user is authenticated (current_user helper method)" do
session[:user_id] = 123 session[:user_id] = 123
get "show", :id=> 1 get :show, params: { id: 1 }
Post.first.impressions.last.user_id.should eq 123 expect(Post.first.impressions.last.user_id).to eq 123
end end
it "should log impression at the action level with params" do it "logs impression at the action level with params" do
get "show", id: 1, checked: true get :show, params: { id: 1, checked: true }
Impression.all.size.should eq 12
Impression.last.params.should eq({"checked"=>true}) expect(Impression.all.size).to eq 12
Impression.last.controller_name.should eq "posts"
Impression.last.action_name.should eq "show" impression = Impression.last
Impression.last.impressionable_type.should eq "Post"
Impression.last.impressionable_id.should eq 1 expect(impression.params).to eq({ "checked" => "true" })
expect(impression.controller_name).to eq "posts"
expect(impression.action_name).to eq "show"
expect(impression.impressionable_type).to eq "Post"
expect(impression.impressionable_id).to eq 1
end end
end end

View File

@ -1,91 +1,103 @@
require 'spec_helper' require 'spec_helper'
describe WidgetsController do describe WidgetsController do
before do
before(:each) do
@widget = Widget.find(1) @widget = Widget.find(1)
Widget.stub(:find).and_return(@widget) allow(Widget).to receive(:find).and_return(@widget)
end end
it "should log impression at the per action level" do it "logs impression at the per action level" do
get "show", :id=> 1 get :show, params: { id: 1 }
Impression.all.size.should eq 12 expect(Impression.all.size).to eq 12
get "index"
Impression.all.size.should eq 13 get :index
get "new"
Impression.all.size.should eq 13 expect(Impression.all.size).to eq 13
get :new
expect(Impression.all.size).to eq 13
end end
it "should not log impression when user-agent is in wildcard list" do it "does not log impression when user-agent is in wildcard list" do
request.stub(:user_agent).and_return('somebot') request.env['HTTP_USER_AGENT'] = 'somebot'
get "show", :id=> 1
Impression.all.size.should eq 11 get :show, params: { id: 1 }
expect(Impression.all.size).to eq 11
end end
it "should not log impression when user-agent is in the bot list" do it "does not log impression when user-agent is in the bot list" do
request.stub(:user_agent).and_return('Acoon Robot v1.50.001') request.env['HTTP_USER_AGENT'] = 'Acoon Robot v1.50.001'
get "show", :id=> 1
Impression.all.size.should eq 11 get :show, params: { id: 1 }
expect(Impression.all.size).to eq 11
end end
context "impressionist unique options" do context "impressionist unique options" do
it "logs unique impressions at the per action level" do
it "should log unique impressions at the per action level" do get :show, params: { id: 1 }
get "show", :id=> 1 expect(Impression.all.size).to eq 12
Impression.all.size.should eq 12 get :show, params: { id: 2 }
get "show", :id=> 2 expect(Impression.all.size).to eq 13
Impression.all.size.should eq 13 get :show, params: { id: 2 }
get "show", :id => 2 expect(Impression.all.size).to eq 13
Impression.all.size.should eq 13 get :index
get "index" expect(Impression.all.size).to eq 14
Impression.all.size.should eq 14
end end
it "should log unique impressions only once per id" do it "logs unique impressions only once per id" do
get "show", :id=> 1 get :show, params: { id: 1 }
Impression.all.size.should eq 12 expect(Impression.all.size).to eq 12
get "show", :id=> 2 get :show, params: { id: 2 }
Impression.all.size.should eq 13 expect(Impression.all.size).to eq 13
get "show", :id => 2 get :show, params: { id: 2 }
Impression.all.size.should eq 13 expect(Impression.all.size).to eq 13
get "index" get :index
Impression.all.size.should eq 14 expect(Impression.all.size).to eq 14
end end
end end
context "Impresionist unique params options" do context "Impresionist unique params options" do
it "should log unique impressions at the per action and params level" do it "logs unique impressions at the per action and params level" do
get "show", :id => 1 get :show, params: { id: 1 }
Impression.all.size.should eq 12 expect(Impression.all.size).to eq 12
get "show", :id => 2, checked: true
Impression.all.size.should eq 13 get :show, params: { id: 2, checked: true }
get "show", :id => 2, checked: false expect(Impression.all.size).to eq 13
Impression.all.size.should eq 14
get "index" get :show, params: { id: 2, checked: false }
Impression.all.size.should eq 15 expect(Impression.all.size).to eq 14
get :index
expect(Impression.all.size).to eq 15
end end
it "should not log impression for same params and same id" do it "does not log impression for same params and same id" do
get "show", :id => 1 get :show, params: { id: 1 }
Impression.all.size.should eq 12 expect(Impression.all.size).to eq 12
get "show", :id => 1
Impression.all.size.should eq 12
get "show", :id => 1, checked: true
Impression.all.size.should eq 13
get "show", :id => 1, checked: false
Impression.all.size.should eq 14
get "show", :id => 1, checked: true
Impression.all.size.should eq 14
get "show", :id => 1, checked: false
Impression.all.size.should eq 14
get "show", :id => 1
Impression.all.size.should eq 14
get "show", :id => 2
Impression.all.size.should eq 15
end
end
get :show, params: { id: 1 }
expect(Impression.all.size).to eq 12
get :show, params: { id: 1, checked: true }
expect(Impression.all.size).to eq 13
get :show, params: { id: 1, checked: false }
expect(Impression.all.size).to eq 14
get :show, params: { id: 1, checked: true }
expect(Impression.all.size).to eq 14
get :show, params: { id: 1, checked: false }
expect(Impression.all.size).to eq 14
get :show, params: { id: 1 }
expect(Impression.all.size).to eq 14
get :show, params: { id: 2 }
expect(Impression.all.size).to eq 15
end
end
end end

View File

@ -3,18 +3,19 @@ require 'spec_helper'
describe Impressionist do describe Impressionist do
let(:imp) { RUBY_VERSION.match("1.8") ? "is_impressionable" : :is_impressionable } let(:imp) { RUBY_VERSION.match("1.8") ? "is_impressionable" : :is_impressionable }
it "should be extended from ActiveRecord::Base" do it "is extended from ActiveRecord::Base" do
expect(ActiveRecord::Base).to respond_to(imp) expect(ActiveRecord::Base).to respond_to(imp)
#ActiveRecord::Base.methods.include?(method).should be_true # ActiveRecord::Base.methods.include?(method).should be_truthy
end end
it "should include methods in ApplicationController" do it "includes methods in ApplicationController" do
method = RUBY_VERSION.match("1.8") ? "impressionist" : :impressionist method = RUBY_VERSION.match("1.8") ? "impressionist" : :impressionist
expect(ApplicationController).to respond_to(method) expect(ApplicationController).to respond_to(method)
end end
it "should include the before_action method in ApplicationController" do it "includes the before_action method in ApplicationController" do
filters = ApplicationController._process_action_callbacks.select { |c| c.kind == :before } filters = ApplicationController._process_action_callbacks.select { |c| c.kind == :before }
filters.collect{|filter|filter.filter}.include?(:impressionist_app_filter).should be_true
expect(filters.collect(&:filter)).to include(:impressionist_app_filter)
end end
end end

View File

@ -1,27 +1,26 @@
require 'spec_helper' require 'spec_helper'
describe Impressionist::Bots do describe Impressionist::Bots do
describe "self.bot?" do describe "self.bot?" do
it "is true if user_agent is matches wild card" do it "is true if user_agent is matches wild card" do
Impressionist::Bots.bot?("google.com bot").should be_true expect(described_class).to be_bot("google.com bot")
end end
it "is true if user_agent is on bot list" do it "is true if user_agent is on bot list" do
Impressionist::Bots.bot?("A-Online Search").should be_true expect(described_class).to be_bot("A-Online Search")
end end
it "is false if user_agent is blank" do it "is false if user_agent is blank" do
Impressionist::Bots.bot?("").should be_false expect(described_class).not_to be_bot("")
Impressionist::Bots.bot?(nil).should be_false expect(described_class).not_to be_bot(nil)
end end
it "is false if user_agent is safe" do it "is false if user_agent is safe" do
Impressionist::Bots.bot?('127.0.0.1').should be_false expect(described_class).not_to be_bot('127.0.0.1')
end end
it "is false if user_agent not given" do it "is false if user_agent not given" do
Impressionist::Bots.bot?.should be_false expect(described_class).not_to be_bot
end end
end end
end end

View File

@ -5,47 +5,45 @@ describe Impression do
let(:widget) { Widget.find(1) } let(:widget) { Widget.find(1) }
before(:each) do before do
Impression.destroy_all described_class.destroy_all
end end
describe "self#impressionist_counter_caching?" do describe "self#impressionist_counter_caching?" do
it "should know when counter caching is enabled" do it "knows when counter caching is enabled" do
Widget.should be_impressionist_counter_caching expect(Widget).to be_impressionist_counter_caching
end end
it "should know when counter caching is disabled" do it "knows when counter caching is disabled" do
Article.should_not be_impressionist_counter_caching expect(Article).not_to be_impressionist_counter_caching
end end
end end
describe "self#counter_caching?" do describe "self#counter_caching?" do
it "should know when counter caching is enabled" do it "knows when counter caching is enabled" do
ActiveSupport::Deprecation.should_receive(:warn) allow(ActiveSupport::Deprecation).to receive(:warn)
Widget.should be_counter_caching expect(Widget).to be_counter_caching
end end
it "should know when counter caching is disabled" do it "knows when counter caching is disabled" do
ActiveSupport::Deprecation.should_receive(:warn) allow(ActiveSupport::Deprecation).to receive(:warn)
Article.should_not be_counter_caching expect(Article).not_to be_counter_caching
end end
end end
describe "#update_impressionist_counter_cache" do describe "#update_impressionist_counter_cache" do
it "should update the counter cache column to reflect the correct number of impressions" do it "updates the counter cache column to reflect the correct number of impressions" do
expect { expect do
widget.impressions.create(:request_hash => 'abcd1234') widget.impressions.create(:request_hash => 'abcd1234')
widget.reload widget.reload
}.to change(widget, :impressions_count).from(0).to(1) end.to change(widget, :impressions_count).from(0).to(1)
end end
it "should not update the timestamp on the impressable" do it "does not update the timestamp on the impressable" do
expect { expect do
widget.impressions.create(:request_hash => 'abcd1234') widget.impressions.create(:request_hash => 'abcd1234')
widget.reload widget.reload
}.to_not change(widget, :updated_at) end.not_to change(widget, :updated_at)
end end
end end
end end

View File

@ -1,70 +1,70 @@
require 'spec_helper' require 'spec_helper'
describe Impression do describe Impression do
fixtures :articles,:impressions,:posts,:profiles fixtures :articles, :impressions, :posts, :profiles
before(:each) do before do
@article = Article.find(1) @article = Article.find(1)
end end
it "should save a blank impression for an Article that has 10 impressions" do it "saves a blank impression for an Article that has 10 impressions" do
@article.impressions.create @article.impressions.create
@article.impressions.size.should eq 12 expect(@article.impressions.size).to eq 12
end end
it "should save an impression with a message" do it "saves an impression with a message" do
@article.impressions.create(:message=>"test message") @article.impressions.create(:message => "test message")
@article.impressions.last.message.should eq "test message" expect(@article.impressions.last.message).to eq "test message"
end end
it "should return the impression count for the message specified" do it "returns the impression count for the message specified" do
@article.impressions.create(:message => "pageview") @article.impressions.create(:message => "pageview")
@article.impressions.create(:message => "pageview") @article.impressions.create(:message => "pageview")
@article.impressions.create(:message => "visit") @article.impressions.create(:message => "visit")
@article.impressionist_count(:message => "pageview", :filter => :all).should eq 2 expect(@article.impressionist_count(:message => "pageview", :filter => :all)).to eq 2
end end
it "should return the impression count for all with no date range specified" do it "returns the impression count for all with no date range specified" do
@article.impressionist_count(:filter=>:all).should eq 11 expect(@article.impressionist_count(:filter => :all)).to eq 11
end end
it "should return unique impression count with no date range specified" do it "returns unique impression count with no date range specified" do
@article.impressionist_count.should eq 9 expect(@article.impressionist_count).to eq 9
end end
it "should return impression count with only start date specified" do it "returns impression count with only start date specified" do
@article.impressionist_count(:start_date=>"2011-01-01",:filter=>:all).should eq 8 expect(@article.impressionist_count(:start_date => "2011-01-01", :filter => :all)).to eq 8
end end
it "should return impression count with whole date range specified" do it "returns impression count with whole date range specified" do
@article.impressionist_count(:start_date=>"2011-01-01",:end_date=>"2011-01-02",:filter=>:all).should eq 7 expect(@article.impressionist_count(:start_date => "2011-01-01", :end_date => "2011-01-02", :filter => :all)).to eq 7
end end
it "should return unique impression count with only start date specified" do it "returns unique impression count with only start date specified" do
@article.impressionist_count(:start_date=>"2011-01-01").should eq 7 expect(@article.impressionist_count(:start_date => "2011-01-01")).to eq 7
end end
it "should return unique impression count with date range specified" do it "returns unique impression count with date range specified" do
@article.impressionist_count(:start_date=>"2011-01-01",:end_date=>"2011-01-02").should eq 7 expect(@article.impressionist_count(:start_date => "2011-01-01", :end_date => "2011-01-02")).to eq 7
end end
it "should return unique impression count using ip address (which in turn eliminates duplicate request_hashes)" do it "returns unique impression count using ip address (which in turn eliminates duplicate request_hashes)" do
@article.impressionist_count(:filter=>:ip_address).should eq 8 expect(@article.impressionist_count(:filter => :ip_address)).to eq 8
end end
it "should return unique impression count using session_hash (which in turn eliminates duplicate request_hashes)" do it "returns unique impression count using session_hash (which in turn eliminates duplicate request_hashes)" do
@article.impressionist_count(:filter=>:session_hash).should eq 7 expect(@article.impressionist_count(:filter => :session_hash)).to eq 7
end end
# tests :dependent => :delete_all # tests :dependent => :delete_all
it "should delete impressions on deletion of impressionable" do it "deletes impressions on deletion of impressionable" do
#impressions_count = Impression.all.size # impressions_count = Impression.all.size
a = Article.create a = Article.create
i = a.impressions.create i = a.impressions.create
a.destroy a.destroy
a.destroyed?.should be_true
Impression.exists?(i.id).should be_false
end
expect(a).to be_destroyed
expect(described_class).not_to exist(i.id)
end
end end

View File

@ -1,23 +1,23 @@
require 'spec_helper' require 'spec_helper'
require 'systemu' require 'systemu'
# FIXME this test might break the others if run before them # FIXME: this test might break the others if run before them
# started fixing @nbit001 # started fixing @nbit001
describe Impressionist, :migration do describe Impressionist, :migration do
fixtures :articles,:impressions,:posts,:profiles fixtures :articles, :impressions, :posts, :profiles
it "should delete existing migration and generate the migration file" do it "deletes existing migration and generate the migration file" do
pending pending
migrations_dir = "#{Rails.root}/db/migrate" migrations_dir = "#{Rails.root}/db/migrate"
impressions_migration = Dir.entries(migrations_dir).grep(/impressions/)[0] impressions_migration = Dir.entries(migrations_dir).grep(/impressions/)[0]
File.delete("#{migrations_dir}/#{impressions_migration}") unless impressions_migration.blank? File.delete("#{migrations_dir}/#{impressions_migration}") if impressions_migration.present?
generator_output = systemu("rails g impressionist")[1] generator_output = systemu("rails g impressionist")[1]
migration_name = generator_output.split("migrate/")[1].strip migration_name = generator_output.split("migrate/")[1].strip
Dir.entries(migrations_dir).include?(migration_name).should be_true Dir.entries(migrations_dir).include?(migration_name).should be_truthy
end end
it "should run the migration created in the previous spec" do it "runs the migration created in the previous spec" do
pending pending
migrate_output = systemu("rake db:migrate RAILS_ENV=test") migrate_output = systemu("rake db:migrate RAILS_ENV=test")
migrate_output[1].include?("CreateImpressionsTable: migrated").should be_true migrate_output[1].include?("CreateImpressionsTable: migrated").should be_truthy
end end
end end

View File

@ -5,22 +5,18 @@ unless ENV['CI']
SimpleCov.start 'rails' SimpleCov.start 'rails'
end end
require File. require File.expand_path('../config/environment', __dir__)
expand_path("../../config/environment", __FILE__)
require 'rspec/rails' require 'rspec/rails'
require 'capybara/rails' require 'capybara/rails'
# Custom matchers and macros, etc... # Custom matchers and macros, etc...
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| Dir[Rails.root.join("spec/support/**/*.rb")].sort.each do |f|
require f require f
} end
RSpec.configure do |config| RSpec.configure do |config|
config.infer_spec_type_from_file_location!
# in order to pass tags(symbols) as true values
# you need to tell rspec to do so by
config.treat_symbols_as_metadata_keys_with_true_values = true
config.mock_with :rspec config.mock_with :rspec
@ -39,5 +35,4 @@ RSpec.configure do |config|
def logger def logger
Rails.logger Rails.logger
end end
end end

View File

@ -1,4 +1,4 @@
class CreateImpressionsTable < ActiveRecord::Migration class CreateImpressionsTable < ActiveRecord::Migration[4.2]
def self.up def self.up
add_column :impressions, :session_hash, :string add_column :impressions, :session_hash, :string
remove_index :impressions, :name => :poly_index remove_index :impressions, :name => :poly_index

View File

@ -1,4 +1,4 @@
class Version04UpdateImpressionsTable < ActiveRecord::Migration class Version04UpdateImpressionsTable < ActiveRecord::Migration[4.2]
def self.up def self.up
add_column :impressions, :referrer, :string add_column :impressions, :referrer, :string
end end

View File

@ -1,4 +1,4 @@
class Version152UpdateImpressionsTable < ActiveRecord::Migration class Version152UpdateImpressionsTable < ActiveRecord::Migration[4.2]
def self.up def self.up
add_column :impressions, :params, :text add_column :impressions, :params, :text