1 # frozen_string_literal: true
4 require "minitest/mock"
6 class TraceImporterJobTest < ActiveJob::TestCase
7 def test_success_notification
8 # Check that the user gets a success notification when the trace has valid points
11 gpx = Minitest::Mock.new
16 trace.stub(:import, gpx) do
17 TraceImporterJob.perform_now(trace)
20 email = ActionMailer::Base.deliveries.last
21 assert_equal trace.user.email, email.to[0]
22 assert_match(/success/, email.subject)
24 ActionMailer::Base.deliveries.clear
27 def test_failure_notification
28 # Check that the user gets a failure notification when the trace has no valid points
29 trace = create(:trace)
31 gpx = Minitest::Mock.new
36 trace.stub(:import, gpx) do
37 TraceImporterJob.perform_now(trace)
40 email = ActionMailer::Base.deliveries.last
41 assert_equal trace.user.email, email.to[0]
42 assert_match(/failure/, email.subject)
44 ActionMailer::Base.deliveries.clear
47 def test_error_notification
48 # Check that the user gets a failure notification when something goes badly wrong
49 trace = create(:trace)
50 trace.stub(:import, -> { raise }) do
51 TraceImporterJob.perform_now(trace)
54 email = ActionMailer::Base.deliveries.last
55 assert_equal trace.user.email, email.to[0]
56 assert_match(/failure/, email.subject)
57 assert_no_match(/Start tag expected/, email.text_part.body.to_s, "should not include parser error")
58 assert_match(%r{jobs/trace_importer_job\.rb}, email.text_part.body.to_s, "should include stack backtrace")
60 ActionMailer::Base.deliveries.clear
63 def test_parse_error_notification
64 trace = create(:trace, :inserted => false, :fixture => "jpg")
65 Rails.logger.silence do
66 TraceImporterJob.perform_now(trace)
69 email = ActionMailer::Base.deliveries.last
70 assert_equal trace.user.email, email.to[0]
71 assert_match(/failure/, email.subject)
72 assert_match(/Start tag expected/, email.text_part.body.to_s, "should include parser error")
73 assert_no_match(%r{jobs/trace_importer_job\.rb}, email.text_part.body.to_s, "should not include stack backtrace")
75 ActionMailer::Base.deliveries.clear
78 def test_gz_parse_error_notification
79 trace = create(:trace, :inserted => false, :fixture => "jpg.gz")
80 Rails.logger.silence do
81 TraceImporterJob.perform_now(trace)
84 email = ActionMailer::Base.deliveries.last
85 assert_equal trace.user.email, email.to[0]
86 assert_match(/failure/, email.subject)
87 assert_match(/Start tag expected/, email.text_part.body.to_s, "should include parser error")
88 assert_no_match(%r{jobs/trace_importer_job\.rb}, email.text_part.body.to_s, "should not include stack backtrace")
90 ActionMailer::Base.deliveries.clear