Created
October 25, 2022 19:57
-
-
Save jcreed/e3c50e8f8ad80343823837b72e18d4ae to your computer and use it in GitHub Desktop.
test/models/documents/downloadable_document_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "test_helper" | |
require "support/page_objects/webadmit_administrator" | |
describe Documents::DownloadableDocument do | |
let(:user_identity) { PageObjects::WebadmitAdministrator.new } | |
let(:downloader) { Documents::Downloader.name } | |
let(:downloadable_document) { Documents::DownloadableDocument.new(options) } | |
let(:filename) { "BroDawg.pdf" } | |
let(:md5_sum) { "f3e86ec5310eab180a8d884dd483f307" } | |
let(:options) do | |
{ | |
"applicant_id" => 1234, | |
"url" => "http://example.com", | |
"filename" => filename, | |
"document_type_name" => "ApplicationPdf", | |
"downloader_name" => downloader | |
} | |
end | |
before do | |
Thread.current[:current_user_identity] = user_identity | |
end | |
module DownloadableDocumentSharedAssertions | |
def self.included(klass) | |
klass.instance_eval do | |
specify { subject.id.must_equal md5_sum } | |
specify { subject.key.must_equal "webadmit:doc_callbacks:#{user_identity.id}:#{md5_sum}" } | |
specify { subject.applicant_id.must_equal 1234 } | |
specify { subject.respond_to?(:applicant_id).must_equal true } | |
specify { -> { subject.silly_non_method }.must_raise(NoMethodError) } | |
specify { subject.respond_to?(:silly_non_method).must_equal false } | |
specify { subject.filename.must_equal "BroDawg.pdf" } | |
describe "with no filename" do | |
let(:filename) { nil } | |
specify { subject.filename.must_equal "Document.pdf" } | |
end | |
end | |
end | |
end | |
describe "#initialize" do | |
it "requires a document_type_name" do | |
options.delete("document_type_name") | |
-> { Documents::DownloadableDocument.new(options) }.must_raise ArgumentError | |
end | |
it "requires a downloader_name" do | |
options.delete("downloader_name") | |
-> { Documents::DownloadableDocument.new(options) }.must_raise ArgumentError | |
end | |
describe "when downloader is blank" do | |
let(:downloader) { "" } | |
specify { -> { Documents::DownloadableDocument.new(options) }.must_raise ArgumentError } | |
end | |
describe "when downloader is not expected" do | |
let(:downloader) { "BadDownloaderName" } | |
specify { -> { Documents::DownloadableDocument.new(options) }.must_raise ArgumentError } | |
end | |
it "calculates a different id for different document types" do | |
downloadable_doc1 = Documents::DownloadableDocument.new(options.merge("document_type_name" => "Documents::Downloader")) | |
downloadable_doc2 = Documents::DownloadableDocument.new(options.merge("document_type_name" => "Documents::ApplicantPdfDownloader")) | |
downloadable_doc1.id.wont_equal downloadable_doc2.id | |
end | |
end | |
it "#create_request" do | |
downloader = mock | |
downloader.expects(:create_request) | |
Documents::Downloader.stubs(:new).returns(downloader) | |
downloadable_document.create_request | |
end | |
describe "#content" do | |
before do | |
downloadable_document.save | |
Documents::Downloader.any_instance.stubs(:call).returns("regular downloader") | |
Documents::ApplicantPdfDownloader.any_instance.stubs(:call).returns("applicant pdf downloader") | |
end | |
subject { downloadable_document } | |
describe "when downloader is a Documents::Downloader" do | |
specify { subject.content.must_equal "regular downloader" } | |
end | |
describe "when downloader is a Documents::ApplicantPdfDownloader" do | |
let(:downloader) { Documents::ApplicantPdfDownloader.name } | |
specify { subject.content.must_equal "applicant pdf downloader" } | |
end | |
describe "fails to get content" do | |
it 'retries 3 times' do | |
Documents::Downloader.any_instance.stubs(:call).raises(RuntimeError) | |
Airbrake.expects(:notify).once | |
-> { subject.content }.must_raise RuntimeError | |
subject.errors_count.must_equal 4 | |
end | |
end | |
end | |
describe "#save" do | |
before do | |
downloadable_document.save | |
end | |
subject { downloadable_document } | |
include DownloadableDocumentSharedAssertions | |
it "persists the data" do | |
data = RedisConnection.connection.get(subject.key) | |
data.wont_be_nil | |
JSON.parse(data).must_equal options | |
end | |
end | |
describe ".find" do | |
let(:found_downloadable_document) do | |
downloadable_document.save | |
Documents::DownloadableDocument.find(user_identity.id, downloadable_document.id) | |
end | |
subject { found_downloadable_document } | |
include DownloadableDocumentSharedAssertions | |
it "raises error if it can't find the document" do | |
-> do | |
Documents::DownloadableDocument.find(user_identity.id, "I don't exist") | |
end.must_raise(Documents::DownloadableDocument::DocumentNotFoundError) | |
end | |
it "raises error if the user_identity is different" do | |
-> do | |
Documents::DownloadableDocument.find(9876, downloadable_document) | |
end.must_raise(Documents::DownloadableDocument::DocumentNotFoundError) | |
end | |
end | |
describe "add_options" do | |
subject { downloadable_document.options } | |
it "merges options to existing options" do | |
downloadable_document.add_options(a:1, b:2) | |
subject.must_equal( | |
'applicant_id' => 1234, | |
'url' => "http://example.com", | |
'filename' => "BroDawg.pdf", | |
'document_type_name' => "ApplicationPdf", | |
'downloader_name' => "Documents::Downloader", | |
'a' => 1, | |
'b' => 2 | |
) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment