2020-07-20 13:35:41 +00:00
|
|
|
# frozen_string_literal: false
|
|
|
|
|
2013-07-06 19:20:35 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
describe ArticlesController, type: :controller do
|
|
|
|
fixtures :articles, :impressions, :posts, :widgets
|
2013-07-06 19:20:35 +00:00
|
|
|
|
|
|
|
render_views
|
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'makes the impressionable_hash available' do
|
|
|
|
get :index
|
|
|
|
expect(response.body).to include('false')
|
2013-07-06 19:20:35 +00:00
|
|
|
end
|
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'logs an impression with a message' do
|
|
|
|
get 'index'
|
|
|
|
|
|
|
|
latest_impression = Article.first.impressions.last
|
|
|
|
|
|
|
|
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'
|
2013-07-06 19:20:35 +00:00
|
|
|
end
|
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'logs an impression without a message' do
|
|
|
|
get :show, params: { id: 1 }
|
|
|
|
|
|
|
|
latest_impression = Article.first.impressions.last
|
|
|
|
|
|
|
|
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'
|
2013-07-06 19:20:35 +00:00
|
|
|
end
|
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'logs the user_id if user is authenticated (@current_user before_action method)' do
|
2013-07-06 19:20:35 +00:00
|
|
|
session[:user_id] = 123
|
2020-07-20 13:35:41 +00:00
|
|
|
get :show, params: { id: 1 }
|
|
|
|
|
|
|
|
expect(Article.first.impressions.last.user_id).to eq 123
|
2013-07-06 19:20:35 +00:00
|
|
|
end
|
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'does not log the user_id if user is authenticated' do
|
|
|
|
get :show, params: { id: 1 }
|
|
|
|
|
|
|
|
expect(Article.first.impressions.last.user_id).to eq nil
|
2013-07-06 19:20:35 +00:00
|
|
|
end
|
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'logs the request_hash, ip_address, referrer and session_hash' do
|
|
|
|
get :show, params: { id: 1 }
|
|
|
|
|
|
|
|
impression = Impression.last
|
|
|
|
|
|
|
|
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
|
2013-07-06 19:20:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Capybara has change the way it works
|
|
|
|
# We need to pass :type options in order to make include helper methods
|
|
|
|
# see https://github.com/jnicklas/capybara#using-capybara-with-rspec
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'logs the referrer when you click a link', type: :feature do
|
|
|
|
default_url_options[:host] = "test.host"
|
|
|
|
|
2013-07-06 19:20:35 +00:00
|
|
|
visit article_url(Article.first)
|
2020-07-20 13:35:41 +00:00
|
|
|
click_link 'Same Page'
|
|
|
|
expect(Impression.last.referrer).to eq 'http://test.host/articles/1'
|
2013-07-06 19:20:35 +00:00
|
|
|
end
|
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'logs request with params (checked = true)' do
|
|
|
|
get :show, params: { id: 1, checked: true }
|
|
|
|
|
|
|
|
impression = Impression.last
|
|
|
|
|
|
|
|
expect(impression.params).to eq({ 'checked' => "true" })
|
|
|
|
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
|
2015-04-21 10:21:47 +00:00
|
|
|
end
|
2013-07-06 19:20:35 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
it 'logs request with params: {}' do
|
|
|
|
get 'index'
|
|
|
|
|
|
|
|
impression = Impression.last
|
|
|
|
|
|
|
|
expect(impression.params).to eq({})
|
|
|
|
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
|
2015-04-21 10:21:47 +00:00
|
|
|
end
|
2017-06-02 00:54:17 +00:00
|
|
|
|
2020-07-20 13:35:41 +00:00
|
|
|
describe 'when filtering params' do
|
2017-06-02 00:54:17 +00:00
|
|
|
before do
|
|
|
|
@_filtered_params = Rails.application.config.filter_parameters
|
|
|
|
Rails.application.config.filter_parameters = [:password]
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Rails.application.config.filter_parameters = @_filtered_params
|
|
|
|
end
|
2020-07-20 13:35:41 +00:00
|
|
|
|
|
|
|
it 'values should not be recorded' do
|
|
|
|
get 'index', params: { password: 'best-password-ever' }
|
|
|
|
expect(Impression.last.params).to eq('password' => '[FILTERED]')
|
|
|
|
end
|
2017-06-02 00:54:17 +00:00
|
|
|
end
|
2015-04-21 10:21:47 +00:00
|
|
|
end
|