]> git.openstreetmap.org Git - rails.git/blob - test/jobs/trace_importer_job_test.rb
Merge remote-tracking branch 'upstream/pull/5419'
[rails.git] / test / jobs / trace_importer_job_test.rb
1 require "test_helper"
2 require "minitest/mock"
3
4 class TraceImporterJobTest < ActiveJob::TestCase
5   def test_success_notification
6     # Check that the user gets a success notification when the trace has valid points
7     trace = create(:trace)
8
9     gpx = Minitest::Mock.new
10     def gpx.actual_points
11       5
12     end
13
14     trace.stub(:import, gpx) do
15       TraceImporterJob.perform_now(trace)
16     end
17
18     email = ActionMailer::Base.deliveries.last
19     assert_equal trace.user.email, email.to[0]
20     assert_match(/success/, email.subject)
21
22     ActionMailer::Base.deliveries.clear
23   end
24
25   def test_failure_notification
26     # Check that the user gets a failure notification when the trace has no valid points
27     trace = create(:trace)
28
29     gpx = Minitest::Mock.new
30     def gpx.actual_points
31       0
32     end
33
34     trace.stub(:import, gpx) do
35       TraceImporterJob.perform_now(trace)
36     end
37
38     email = ActionMailer::Base.deliveries.last
39     assert_equal trace.user.email, email.to[0]
40     assert_match(/failure/, email.subject)
41
42     ActionMailer::Base.deliveries.clear
43   end
44
45   def test_error_notification
46     # Check that the user gets a failure notification when something goes badly wrong
47     trace = create(:trace)
48     trace.stub(:import, -> { raise }) do
49       TraceImporterJob.perform_now(trace)
50     end
51
52     email = ActionMailer::Base.deliveries.last
53     assert_equal trace.user.email, email.to[0]
54     assert_match(/failure/, email.subject)
55     assert_no_match(/Start tag expected/, email.text_part.body.to_s, "should not include parser error")
56     assert_match(%r{jobs/trace_importer_job\.rb}, email.text_part.body.to_s, "should include stack backtrace")
57
58     ActionMailer::Base.deliveries.clear
59   end
60
61   def test_parse_error_notification
62     trace = create(:trace, :inserted => false, :fixture => "jpg")
63     Rails.logger.silence do
64       TraceImporterJob.perform_now(trace)
65     end
66
67     email = ActionMailer::Base.deliveries.last
68     assert_equal trace.user.email, email.to[0]
69     assert_match(/failure/, email.subject)
70     assert_match(/Start tag expected/, email.text_part.body.to_s, "should include parser error")
71     assert_no_match(%r{jobs/trace_importer_job\.rb}, email.text_part.body.to_s, "should not include stack backtrace")
72
73     ActionMailer::Base.deliveries.clear
74   end
75
76   def test_gz_parse_error_notification
77     trace = create(:trace, :inserted => false, :fixture => "jpg.gz")
78     Rails.logger.silence do
79       TraceImporterJob.perform_now(trace)
80     end
81
82     email = ActionMailer::Base.deliveries.last
83     assert_equal trace.user.email, email.to[0]
84     assert_match(/failure/, email.subject)
85     assert_match(/Start tag expected/, email.text_part.body.to_s, "should include parser error")
86     assert_no_match(%r{jobs/trace_importer_job\.rb}, email.text_part.body.to_s, "should not include stack backtrace")
87
88     ActionMailer::Base.deliveries.clear
89   end
90 end