Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dot in attachment name #131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ jobs:
run: bundle install

- name: Lint with Rubocop
run: bundle exec rubocop
run: bin/rubocop

- name: Run tests
run: bundle exec rspec
run: bin/rspec

- name: Build gem
run: bundle exec rake build
run: bin/rake build
2 changes: 1 addition & 1 deletion .github/workflows/release-gem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
touch $HOME/.gem/credentials
chmod 0600 $HOME/.gem/credentials
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
bundle exec rake build
bin/rake build
gem push pkg/*.gem
env:
GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'app/controllers/letter_opener_web/application_controller.rb'
- 'app/controllers/letter_opener_web/letters_controller.rb'
- 'app/controllers/letter_opener_web/application_controller.rb'
- 'app/models/letter_opener_web/letter.rb'
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased](https://github.com/fgrehm/letter_opener_web/compare/v2.0.0...master)

- Reliably strip Attachment links from the sidebar. [#132](https://github.com/fgrehm/letter_opener_web/pull/132)
- Allow dot (`.`) character in attachment file names. [#131](https://github.com/fgrehm/letter_opener_web/pull/131)
- Reliably strip Attachment links from the sidebar. [#132](https://github.com/fgrehm/letter_opener_web/pull/134)

## [v2.0.0](https://github.com/fgrehm/letter_opener_web/compare/v1.4.1...v2.0.0)

Expand Down
17 changes: 12 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ source 'http://rubygems.org'
# development dependencies will be added by default to the :development group.
gemspec

gem 'rails', '~> 6.1'
gem 'rspec-rails', '~> 5.0'
gem 'rubocop', '~> 1.22'
gem 'rubocop-rails', '~> 2.12'
gem 'rubocop-rspec', '~> 2.5'
group :development do
gem 'rails', '~> 6.1'
gem 'rspec-rails', '~> 5.0'
gem 'rubocop', '~> 1.22'
gem 'rubocop-rails', '~> 2.12'
gem 'rubocop-rspec', '~> 2.5'
end

group :development, :test do
gem 'pry-byebug'
gem 'pry-rails'
end
2 changes: 1 addition & 1 deletion app/controllers/letter_opener_web/letters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def show
end

def attachment
filename = "#{params[:file]}.#{params[:format]}"
filename = params[:file]
file = @letter.attachments[filename]

return render plain: 'Attachment not found!', status: 404 unless file.present?
Expand Down
27 changes: 27 additions & 0 deletions bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

bundle_binstub = File.expand_path('bundle', __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300).include?('This file was generated by Bundler')
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('rake', 'rake')
27 changes: 27 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

bundle_binstub = File.expand_path('bundle', __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300).include?('This file was generated by Bundler')
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('rspec-core', 'rspec')
27 changes: 27 additions & 0 deletions bin/rubocop
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

#
# This file was generated by Bundler.
#
# The application 'rubocop' is installed as part of a gem, and
# this file is here to facilitate running it.
#

ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

bundle_binstub = File.expand_path('bundle', __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 300).include?('This file was generated by Bundler')
load(bundle_binstub)
else
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
end
end

require 'rubygems'
require 'bundler/setup'

load Gem.bin_path('rubocop', 'rubocop')
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
post 'clear' => 'letters#clear', as: :clear_letters
get ':id(/:style)' => 'letters#show', as: :letter
post ':id/delete' => 'letters#destroy', as: :delete_letter
get ':id/attachments/:file' => 'letters#attachment'
get ':id/attachments/:file' => 'letters#attachment', constraints: { file: %r{[^/]+} }
end
34 changes: 26 additions & 8 deletions spec/controllers/letter_opener_web/letters_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,35 @@
allow(letter).to receive(:valid?).and_return(true)
end

it 'sends the file as an inline attachment' do
allow(controller).to receive(:send_file) { controller.head :ok }
get :attachment, params: { id: id, file: file_name.gsub(/\.\w+/, ''), format: File.extname(file_name)[1..] }
context 'when the file exists' do
before do
# Stub a 200 response b/c `send_file` will 204 when the file doesn't actually exist
allow(controller).to receive(:send_file) { controller.head :ok }
end

expect(response.status).to eq(200)
expect(controller).to have_received(:send_file)
.with(attachment_path, filename: file_name, disposition: 'inline')
it 'sends the file as an inline attachment' do
get :attachment, params: { id: id, file: file_name }

expect(response.status).to eq(200)
expect(controller).to have_received(:send_file)
.with(attachment_path, filename: file_name, disposition: 'inline')
end

context 'when file name includes a dot' do
let(:file_name) { 'image.dot.jpg' }

it 'sends the file as an inline attachment' do
expect(controller).to receive(:send_file).with(attachment_path, filename: file_name,
disposition: 'inline')
get :attachment, params: { id: id, file: file_name }
expect(response.status).to eq(200)
end
end
end

it "throws a 404 if attachment file can't be found" do
get :attachment, params: { id: id, file: 'unknown', format: 'woot' }
it "returns a 404 if attachment file can't be found" do
get :attachment, params: { id: id, file: 'unknown.woot' }

expect(response.status).to eq(404)
end
end
Expand Down
Loading