Test download of pdf with rspec and pdfkit - ruby-on-rails

I am developing a rails 3.2 application with which users can download pdfs. I enjoy test driven development a lot using rspec and shoulda matchers, but I'm at a loss with this one.
I have the following code inside my controller:
def show_as_pdf
#client = Client.find(params[:client_id])
#invoice = #client.invoices.find(params[:id])
PDFKit.configure do |config|
config.default_options = {
:footer_font_size => "6",
:encoding => "UTF-8",
:margin_top=>"1in",
:margin_right=>"1in",
:margin_bottom=>"1in",
:margin_left=>"1in"
}
end
pdf = PDFKit.new(render_to_string "invoices/pdf", layout: false)
invoice_stylesheet_path = File.expand_path(File.dirname(__FILE__) + "/../assets/stylesheets/pdfs/invoices.css.scss")
bootstrap_path = File.expand_path(File.dirname(__FILE__) + "../../../vendor/assets/stylesheets/bootstrap.min.css")
pdf.stylesheets << invoice_stylesheet_path
pdf.stylesheets << bootstrap_path
send_data pdf.to_pdf, filename: "#{#invoice.created_at.strftime("%Y-%m-%d")}_#{#client.name.gsub(" ", "_")}_#{#client.company.gsub(" ", "_")}_#{#invoice.number.gsub(" ", "_")}", type: "application/pdf"
return true
end
This is fairly simple code, all it does is configure my PDFKit and download the generated pdf. Now I want to test the whole thing, including:
Assignment of instance variables (easy, of course, and that works)
The sending of data, i.e. the rendering of the pdf => And this is where I'm stuck
I have tried the following:
controller.should_receive(:send_data)
but that gives me
Failure/Error: controller.should_receive(:send_data)
(#<InvoicesController:0x007fd96fa3e580>).send_data(any args)
expected: 1 time
received: 0 times
Does anyone know of a way to test that the pdf is actually downloaded/sent? Also, what more things do you see that should be tested for good test coverage? E.g., testing for the data type, i.e. application/pdf, would be nice.
Thanks!

Not sure why you're getting that failure but you could instead test the response headers:
response_headers["Content-Type"].should == "application/pdf"
response_headers["Content-Disposition"].should == "attachment; filename=\"<invoice_name>.pdf\""
You asked for advice regarding better test coverage. I thought I'd recommend this: https://www.destroyallsoftware.com/screencasts. These screencasts have had a huge impact on my understanding of test-driven development -- highly recommended!

I recommend using the pdf-inspector gem for writing specs for PDF related Rails actions.
Here's an exemplary spec (which assumes the Rails #report action writes data about a Ticket model in the generated PDF):
describe 'GET /report.pdf' do
it 'returns downloadable PDF with the ticket' do
ticket = FactoryGirl.create :ticket
get report_path, format: :pdf
expect(response).to be_successful
analysis = PDF::Inspector::Text.analyze response.body
expect(analysis.strings).to include ticket.state
expect(analysis.strings).to include ticket.title
end
end

Related

Browsermob Proxy + Selenium + Ruby setup giving 550 response

So I am looking at trying to integrate Browsermob into a Ruby project so i can edit http responses.
I have been following the setup with Selenium instructions from the Github and another article I found about performance testing - which I also want to do.
For this, I have gone with the code from the performance testing article but the results I am getting are the same.
The code is
require 'selenium-webdriver'
require 'browsermob/proxy'
require 'rspec-expectations'
include RSpec::Matchers
require 'json'
def configure_proxy
proxy_binary = BrowserMob::Proxy::Server.new('my/path/to/browsermob-proxy-2.1.4/bin/browsermob-proxy')
proxy_binary.start
proxy_binary.create_proxy
end
def browser_profile
browser_profile = Selenium::WebDriver::Firefox::Profile.new
browser_profile.proxy = #proxy.selenium_proxy
browser_profile
end
def setup
#proxy = configure_proxy
#driver = Selenium::WebDriver.for :firefox, profile: browser_profile
end
def teardown
#driver.quit
#proxy.close
end
def capture_traffic
#proxy.new_har
yield
#proxy.har
end
def run
setup
har = capture_traffic { yield }
#har_file = "./selenium_#{Time.now.strftime("%m%d%y_%H%M%S")}.har"
har.save_to #har_file
teardown
end
run do
#driver.get 'http://the-internet.herokuapp.com/dynamic_loading/2'
#driver.find_element(css: '#start button').click
Selenium::WebDriver::Wait.new(timeout: 8).until do
#driver.find_element(css: '#finish')
end
end
performance_results = JSON.parse `yslow --info basic --format json #{#har_file}`
performance_grade = performance_results["o"]
performance_grade.should be > 90
Now, the problem I get is that as soon as I try to run this code (isolated away from my project or even within it) I get:
(.rvm/gems/ruby-2.5.3/gems/rest-client-2.0.2/lib/restclient/abstract_response.rb:220:in 'rescue in exception_with_response': HTTP status code 550 (RestClient::RequestFailed)
Does anyone know why I would be getting this? I understand that a 550 is an action not taken code but I'm confused as to why I would be getting this?
Any help would be VERY much appreciated!

AXLSX: Parsing xlsx file for rspec tests

Any idea how to write view specs (presence of headers, rows, etc.) for a generated xlsx file from the xlsx gem? Not sure if I'm doing it correctly in the first place but here's what I have so far
RSpec.describe "spreadsheet.xlsx.axlsx", :type => :view do
...
it "should have header Books" do
assign(:spreadsheet, spreadsheet)
render
# rendered.rows[0].cells.map(&:value).should include "Books"
end
end
In pry, rendered is in a utf-8 encoded string I'm not sure how to parse for headers, etc.
=> "PK\u0003\u0004\u0014\u0000\u0000\u0000\b\u0000\u0000\u0000!\xECc8k\xD4\
Is there a way I can just test the generated xlsx file like I would an html view?
Something like...
it "has header Books" do
assign(:worksheet, worksheet)
render
expect(rendered).to have_xpath("(//table)[1]/thead/tr/td", :text => "Books")
end
Thanks in advance!
It appears rendered is the raw response so you can use something like the axlsx_rails request specs:
File.open('/tmp/xlsx_temp.xlsx', 'w') {|f| f.write(rendered) }
wb = nil
expect{ wb = Roo::Excelx.new('/tmp/xlsx_temp.xlsx') }.to_not raise_error
wb.cell(2,1).should == 'Some value'
This uses the roo gem to parse the file since Axlsx does not read xlsx.
See:
https://github.com/straydogstudio/axlsx_rails/blob/master/spec/axlsx_request_spec.rb#L19-L22

Rspec: add some header requests inside routing specs

I'm working on a Rails application having a REST API in JSON format and versioned (according to this excellent Ryan's cast: http://railscasts.com/episodes/350-rest-api-versioning).
For instance, there is a spec/requests spec:
require 'spec_helper'
describe "My Friends" do
describe "GET /my/friends.json" do
it "should get my_friends_path" do
get v1_my_friends_path, {}, {'HTTP_ACCEPT' => 'application/vnd.myapp+json; level=1'}
response.status.should be(401)
end
end
end
And it works well. But (keeping this example) how can we write the routing spec? For instance this spec isn't correct:
require 'spec_helper'
describe "friends routing" do
it "routes to #index" do
get("/my/friends.json", nil, {'HTTP_ACCEPT' => 'application/vnd.myapp+json; level=1'}).
should route_to({ action: "index",
controller: "api/v1/private/my/friends",
format: "json" })
end
end
I tried different ways (such as request.headers['Accept'] and #request.headers['Accept'], where request is undefined and #request is nil); I really don't see how to do.
I'm on Ruby 1.9.3, Rails 3.2.6 and rspec-rails 2.11.0. Thanks.
By combining the ideas from Cristophe's and Piotr's answers, I came up with a solution that worked for me. I'm using rspec and rails 3.0.
it 'should route like i want it to' do
Rack::MockRequest::DEFAULT_ENV["HTTP_ACCEPT"] = "*/*"
{get: "/foo/bar"}.
should route_to(
controller: 'foo',
action: 'bar',
)
Rack::MockRequest::DEFAULT_ENV.delete "HTTP_ACCEPT"
end
Currently you can't send addititional Headers in Routing specs, this is due to line 608 in actionpack-3.2.5/lib/action_dispatch/routing/route_set.rb where it says:
env = Rack::MockRequest.env_for(path, {:method => method})
path is your requested path "/my/friends.json" and method is :get
The resulting env contains something like the following:
{
"rack.version"=>[1, 1],
"rack.input"=>#<StringIO:0xb908f5c>,
"rack.errors"=>#<StringIO:0xb908fac>,
"rack.multithread"=>true,
"rack.multiprocess"=>true,
"rack.run_once"=>false,
"REQUEST_METHOD"=>"GET",
"SERVER_NAME"=>"your-url.com", # if path was http://your-url.com/
"SERVER_PORT"=>"80",
"QUERY_STRING"=>"",
"PATH_INFO"=>"/",
"rack.url_scheme"=>"http",
"HTTPS"=>"off",
"SCRIPT_NAME"=>"",
"CONTENT_LENGTH"=>"0"
}
If you are able to mock Rack::MockRequest::env_for it should be possible to inject other headers than the ones generated by env_for (see Hash above).
Other than that you are currently using the route_to matcher wrong, you should call it on a Hash where you specify the method and the path like this:
{ get: '/' }.should route_to(controller: 'main', action: 'index')
Let us know if you were able to Mock out that env_for and let it return your headers, would be nice to know.
Regards
Christoph
before do
ActionDispatch::TestRequest::DEFAULT_ENV["action_dispatch.request.accepts"] = "application/vnd.application-v1+json"
end
after do
ActionDispatch::TestRequest::DEFAULT_ENV.delete("action_dispatch.request.accepts")
end
You can using rspec's and_wrap_original to mock the Rack::MockRequest.env_for method:
expect(Rack::MockRequest).to receive(:env_for).and_wrap_original do |original_method, *args, &block|
original_method.call(*args, &block).tap { |hash| hash['HTTP_ACCEPT'] = 'application/vnd.myapp+json; level=1' }
end
For Rails 3 and 4 I had done the following in an RSpec around hook:
around do |example|
Rack::MockRequest::DEFAULT_ENV['HTTP_ACCEPT'] = 'application/vnd.vendor+json; version=1'
example.run
Rack::MockRequest::DEFAULT_ENV.delete 'HTTP_ACCEPT'
end
Since Rack >= 2.0.3 (used by Rails 5) the Rack::MockRequest::DEFAULT_ENV hash is frozen.
You can redefine the constant and use Kernel.silence_warnings to silence the Ruby warnings:
around do |example|
silence_warnings do
Rack::MockRequest::DEFAULT_ENV = Rack::MockRequest::DEFAULT_ENV.dup
end
Rack::MockRequest::DEFAULT_ENV['HTTP_ACCEPT'] = 'application/vnd.vendor+json; version=1'
example.run
Rack::MockRequest::DEFAULT_ENV.delete 'HTTP_ACCEPT'
end
It's a bit of hack but it works like a charm.

How do I test a file upload in rails for integration testing?

I do some integration testing like this :
def user.excel_import
fixture_excel = fixture_file_upload('goodsins.xls', 'text/xls')
post excel_import_goods_ins_goods_ins_path, :dump=> {:excel_file=>fixture_excel}, :html => { :multipart => "true" }
assert_response :redirect
assert_redirected_to goods_ins_path
end
But when I run the testing it is said that : goodsins.xls file does not exist.
FYI : I put the file in the folder that named fixtures.
Any idea? Thx u very much
The notes here: http://apidock.com/rails/ActionController/TestProcess/fixture_file_upload indicate that you need to include a slash before the path or file name.
try fixture_file_upload('/goodsins.xls', 'text/xls') and see if that helps.
fixture_file_upload Source:
# File actionpack/lib/action_controller/test_process.rb, line 523
def fixture_file_upload(path, mime_type = nil, binary = false)
if ActionController::TestCase.respond_to?(:fixture_path)
fixture_path = ActionController::TestCase.send(:fixture_path)
end
ActionController::TestUploadedFile.new("#{fixture_path}#{path}",
mime_type, binary)
end
Update from Question Owner:
Solution:
add include ActionDispatch::TestProcess to test_helper.rb

Setting page/respone.body in Capybara used to work in Webrat

I am migrating to Capybara.
One of the problems I have is migrating the pdf step.
This step sets page.body to a parsed pdf.
That way I can use the default cucumber steps.
When 'I follow the PDF link "$label"' do |label|
click_link(label)
page.body = PDF::Inspector::Text.analyze(page.body).strings.join(" ")
end
Ex.
When I follow the PDF link "Catalogue"
Then I should see "Cheap products"
The error I get is this one:
undefined method `body=' for #<Capybara::`enter code here`Document> (NoMethodError)
On top, make sure you set :js => true like this:
scenario 'do something', :js => true do
str = PDF::Inspector::Text.analyze(page.body).strings.join(" ") # or whatever string you want
# then just use javascript to edit or add the body
page.evaluate_script("document.write(#{str});")
end
Now this is dependent on the driver, but it's one solution...
There is no setter for body defined in the source in capybara, so you cannot set it externally like that. Try this (untested):
page.instance_variable_set(:#body, PDF::Inspector::Text.analyze(page.body).strings.join(" "))
This worked for me:
Then /^I should be served the document as a PDF$/ do
page.response_headers['Content-Type'].should == "application/pdf"
pdf = PDF::Inspector::Text.analyze(page.source).strings.join(" ")
page.driver.response.instance_variable_set('#body', pdf)
end
Then /^I should see the document details$/ do
page.should have_content("#{#document.customer.name}")
page.should have_content("#{#document.resources.first.resource.name}")
page.should have_content("Document opened at #{#document.created_at.strftime("%e-%b-%4Y %r")}")
end
Note that I'm serving my PDF inline
pdf = DocumentPdf.new(#document)
send_data pdf.render, :filename => "document_#{#document.created_at.strftime("%Y-%m-%d")}",
:type => "application/pdf",
:disposition => "inline"

Resources