]> git.openstreetmap.org Git - rails.git/blob - test/jobs/trace_importer_job_test.rb
Merge pull request #5932 from tomhughes/frozen-strings
[rails.git] / test / jobs / trace_importer_job_test.rb
1 # frozen_string_literal: true
2
3 require "test_helper"
4 require "minitest/mock"
5
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
9     trace = create(:trace)
10
11     gpx = Minitest::Mock.new
12     def gpx.actual_points
13       5
14     end
15
16     trace.stub(:import, gpx) do
17       TraceImporterJob.perform_now(trace)
18     end
19
20     email = ActionMailer::Base.deliveries.last
21     assert_equal trace.user.email, email.to[0]
22     assert_match(/success/, email.subject)
23
24     ActionMailer::Base.deliveries.clear
25   end
26
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)
30
31     gpx = Minitest::Mock.new
32     def gpx.actual_points
33       0
34     end
35
36     trace.stub(:import, gpx) do
37       TraceImporterJob.perform_now(trace)
38     end
39
40     email = ActionMailer::Base.deliveries.last
41     assert_equal trace.user.email, email.to[0]
42     assert_match(/failure/, email.subject)
43
44     ActionMailer::Base.deliveries.clear
45   end
46
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)
52     end
53
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")
59
60     ActionMailer::Base.deliveries.clear
61   end
62
63   def test_parse_error_notification
64     trace = create(:trace, :inserted => false, :fixture => "jpg")
65     Rails.logger.silence do
66       TraceImporterJob.perform_now(trace)
67     end
68
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")
74
75     ActionMailer::Base.deliveries.clear
76   end
77
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)
82     end
83
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")
89
90     ActionMailer::Base.deliveries.clear
91   end
92 end