Skip to content

Commit

Permalink
Update specs and database_cleaner gem
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-hank committed Sep 21, 2021
1 parent 2c150ee commit 5656941
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
name: Database setup
command: |
cp config/database.yml.circle config/database.yml
bundle exec rails db:schema:load RAILS_ENV=test
bundle exec rails db:drop db:create db:migrate RAILS_ENV=test
- run: mkdir -p tmp/cache

# Precompile assets to speed up tests.
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ group :test do
gem 'factory_bot_rails'
gem 'parallel_tests'
gem 'rack-test', require: 'rack/test'
gem 'rspec-collection_matchers', '~> 1.1'
gem 'rails-controller-testing'
gem 'rspec-collection_matchers', '~> 1.1'
gem 'rspec_junit_formatter'
gem 'rspec-pride'
gem 'rspec-rails'
Expand Down
7 changes: 6 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ GEM
safe_yaml (~> 1.0.0)
crass (1.0.6)
curb (0.9.10)
database_cleaner (1.8.4)
database_cleaner (2.0.1)
database_cleaner-active_record (~> 2.0.0)
database_cleaner-active_record (2.0.1)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
date_validator (0.10.0)
activemodel (>= 3)
activesupport (>= 3)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/media_mentions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class MediaMentionsController < ApplicationController
def show
return resource_not_found("Can't fing research paper with id=#{params[:id]}") unless (@media_mention = MediaMention.find_by(id: params[:id]))
return resource_not_found("Can't find a research paper with id=#{params[:id]}") unless (@media_mention = MediaMention.find_by(id: params[:id]))

render :show
end
Expand Down
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@

# Store uploaded files on the local file system in a temporary directory.
config.active_storage.service = :test

config.maintain_test_schema = false
end
15 changes: 15 additions & 0 deletions spec/controllers/media_mentions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

describe MediaMentionsController do
context '#show' do
it 'finds the notice when it exists and renders successfully' do
media_mention = MediaMention.new(title: 'Go Lumen!', scale_of_mention: 'Small')
expect(MediaMention).to receive(:find_by).with(id: '1').and_return(media_mention)

get :show, params: { id: 1 }

expect(response).to be_successful
expect(assigns(:media_mention)).to eq media_mention
end
end
end
68 changes: 51 additions & 17 deletions spec/controllers/token_urls_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,6 @@
expect(TokenUrl.count).to eq orig_count
end

it 'creates a new token and strips out the email part between "+" and "@"' do
allow(controller).to receive(:verify_recaptcha).and_return(true)

notice = create(:dmca)

params = {
token_url: {
email: '[email protected]',
notice_id: notice.id
}
}

post :create, params: params

expect(TokenUrl.last.email).to eq '[email protected]'
end

it "won't call the validate method twice" do
allow(controller).to receive(:verify_recaptcha).and_return(true)
allow(controller).to receive(:validate).and_return(
Expand All @@ -121,6 +104,17 @@

post :create, params: params
end

it "won't let to request a new token from the same ip immediately" do
allow(controller).to receive(:verify_recaptcha).and_return(true)

orig_count = TokenUrl.count

create_token '[email protected]'
create_token '[email protected]'

expect(TokenUrl.count).to eq(orig_count + 1)
end
end

context '#generate_permanent' do
Expand Down Expand Up @@ -201,6 +195,30 @@
expect(TokenUrl.where(notice: notice, user: user, valid_forever: true).count).to eq(0)
expect_authorization_error
end

context '#clean_up_email_address' do
it 'downcases a new token email address' do
create_token '[email protected]'

expect(TokenUrl.last.email).to eq '[email protected]'
end

it 'removes everything between "+" and "@"' do
create_token '[email protected]'

expect(TokenUrl.last.email).to eq '[email protected]'
end

it 'removes periods if a new token email address is gmail' do
create_token '[email protected]'

expect(TokenUrl.last.email).to eq '[email protected]'

create_token '[email protected]', '2.2.2.2'

expect(TokenUrl.last.email).to eq '[email protected]'
end
end
end

private
Expand All @@ -209,4 +227,20 @@ def expect_authorization_error
expect(session['flash']['flashes']['alert']).to eq 'You are not authorized to access this page.'
expect(response.status).to eq 302
end

def create_token(email, ip = '1.1.1.1')
allow_any_instance_of(ActionDispatch::Request).to receive(:remote_ip) { ip }
allow(controller).to receive(:verify_recaptcha).and_return(true)

notice = create(:dmca)

params = {
token_url: {
email: email,
notice_id: notice.id
}
}

post :create, params: params
end
end
5 changes: 5 additions & 0 deletions spec/models/media_mention.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe MediaMention, type: :model do
it { is_expected.to validate_presence_of :scale_of_mention }
end
2 changes: 0 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
config.expect_with :rspec do |c|
c.syntax = :expect
Expand Down
6 changes: 4 additions & 2 deletions spec/support/database_cleaner.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
RSpec.configure do |config|
tables_to_skip_clean = %w[lumen_settings]

config.before(:suite) do
DatabaseCleaner.clean_with(:deletion)
DatabaseCleaner.clean_with(:deletion, except: tables_to_skip_clean)
end

config.before(:each) do
DatabaseCleaner.strategy = :transaction
end

config.before(:each, :js => true) do
DatabaseCleaner.strategy = :deletion
DatabaseCleaner.strategy = :deletion, { except: tables_to_skip_clean }
end

config.before(:each) do
Expand Down

0 comments on commit 5656941

Please sign in to comment.