1 # frozen_string_literal: true
 
   6 class TraceTest < ActiveSupport::TestCase
 
   8     public_trace_file = create(:trace)
 
   9     create(:trace, :deleted)
 
  10     check_query(Trace.visible, [public_trace_file])
 
  14     first_user = create(:user)
 
  15     second_user = create(:user)
 
  16     third_user = create(:user)
 
  17     fourth_user = create(:user)
 
  18     public_trace_file = create(:trace, :visibility => "public", :user => first_user)
 
  19     anon_trace_file = create(:trace, :visibility => "private", :user => second_user)
 
  20     identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => first_user)
 
  21     pending_trace_file = create(:trace, :visibility => "public", :user => second_user, :inserted => false)
 
  22     trackable_trace_file = create(:trace, :visibility => "trackable", :user => second_user)
 
  23     _other_trace_file = create(:trace, :visibility => "private", :user => third_user)
 
  25     check_query(Trace.visible_to(first_user), [
 
  26                   public_trace_file, identifiable_trace_file, pending_trace_file
 
  28     check_query(Trace.visible_to(second_user), [
 
  29                   public_trace_file, anon_trace_file, trackable_trace_file,
 
  30                   identifiable_trace_file, pending_trace_file
 
  32     check_query(Trace.visible_to(fourth_user), [
 
  33                   public_trace_file, identifiable_trace_file, pending_trace_file
 
  37   def test_visible_to_all
 
  38     public_trace_file = create(:trace, :visibility => "public")
 
  39     _private_trace_file = create(:trace, :visibility => "private")
 
  40     identifiable_trace_file = create(:trace, :visibility => "identifiable")
 
  41     _trackable_trace_file = create(:trace, :visibility => "trackable")
 
  42     deleted_trace_file = create(:trace, :deleted, :visibility => "public")
 
  43     pending_trace_file = create(:trace, :visibility => "public", :inserted => false)
 
  45     check_query(Trace.visible_to_all, [
 
  46                   public_trace_file, identifiable_trace_file,
 
  47                   deleted_trace_file, pending_trace_file
 
  52     london_trace_file = create(:trace) do |trace|
 
  53       create(:tracetag, :trace => trace, :tag => "London")
 
  55     birmingham_trace_file = create(:trace) do |trace|
 
  56       create(:tracetag, :trace => trace, :tag => "Birmingham")
 
  58     check_query(Trace.tagged("London"), [london_trace_file])
 
  59     check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
 
  60     check_query(Trace.tagged("Unknown"), [])
 
  65     trace_valid({ :user_id => nil }, :valid => false)
 
  66     trace_valid({ :name => "a" * 255 })
 
  67     trace_valid({ :name => "a" * 256 }, :valid => false)
 
  68     trace_valid({ :description => nil }, :valid => false)
 
  69     trace_valid({ :description => "a" * 255 })
 
  70     trace_valid({ :description => "a" * 256 }, :valid => false)
 
  71     trace_valid({ :visibility => "private" })
 
  72     trace_valid({ :visibility => "public" })
 
  73     trace_valid({ :visibility => "trackable" })
 
  74     trace_valid({ :visibility => "identifiable" })
 
  75     trace_valid({ :visibility => "foo" }, :valid => false)
 
  78   def test_tagstring_handles_space_separated_tags
 
  80     trace.tagstring = "foo bar baz"
 
  81     assert_predicate trace, :valid?
 
  82     assert_equal 3, trace.tags.length
 
  83     assert_equal "foo", trace.tags[0].tag
 
  84     assert_equal "bar", trace.tags[1].tag
 
  85     assert_equal "baz", trace.tags[2].tag
 
  86     assert_equal "foo, bar, baz", trace.tagstring
 
  89   def test_tagstring_handles_comma_separated_tags
 
  91     trace.tagstring = "foo, bar baz ,qux"
 
  92     assert_predicate trace, :valid?
 
  93     assert_equal 3, trace.tags.length
 
  94     assert_equal "foo", trace.tags[0].tag
 
  95     assert_equal "bar baz", trace.tags[1].tag
 
  96     assert_equal "qux", trace.tags[2].tag
 
  97     assert_equal "foo, bar baz, qux", trace.tagstring
 
 100   def test_tagstring_strips_whitespace
 
 101     trace = build(:trace)
 
 102     trace.tagstring = "   zero  ,  one , two  "
 
 103     assert_predicate trace, :valid?
 
 104     assert_equal 3, trace.tags.length
 
 105     assert_equal "zero", trace.tags[0].tag
 
 106     assert_equal "one", trace.tags[1].tag
 
 107     assert_equal "two", trace.tags[2].tag
 
 108     assert_equal "zero, one, two", trace.tagstring
 
 112     assert_predicate build(:trace, :visibility => "public"), :public?
 
 113     assert_not_predicate build(:trace, :visibility => "private"), :public?
 
 114     assert_not_predicate build(:trace, :visibility => "trackable"), :public?
 
 115     assert_predicate build(:trace, :visibility => "identifiable"), :public?
 
 116     assert_predicate build(:trace, :deleted, :visibility => "public"), :public?
 
 120     assert_not_predicate build(:trace, :visibility => "public"), :trackable?
 
 121     assert_not_predicate build(:trace, :visibility => "private"), :trackable?
 
 122     assert_predicate build(:trace, :visibility => "trackable"), :trackable?
 
 123     assert_predicate build(:trace, :visibility => "identifiable"), :trackable?
 
 124     assert_not_predicate build(:trace, :deleted, :visibility => "public"), :trackable?
 
 127   def test_identifiable?
 
 128     assert_not_predicate build(:trace, :visibility => "public"), :identifiable?
 
 129     assert_not_predicate build(:trace, :visibility => "private"), :identifiable?
 
 130     assert_not_predicate build(:trace, :visibility => "trackable"), :identifiable?
 
 131     assert_predicate build(:trace, :visibility => "identifiable"), :identifiable?
 
 132     assert_not_predicate build(:trace, :deleted, :visibility => "public"), :identifiable?
 
 136     # The ids refer to the .gpx fixtures in test/traces
 
 137     check_mime_type("a", "application/gpx+xml")
 
 138     check_mime_type("b", "application/gpx+xml")
 
 139     check_mime_type("c", "application/x-bzip2")
 
 140     check_mime_type("d", "application/gzip")
 
 141     check_mime_type("f", "application/zip")
 
 142     check_mime_type("g", "application/x-tar")
 
 143     check_mime_type("h", "application/x-tar+gzip")
 
 144     check_mime_type("i", "application/x-tar+x-bzip2")
 
 147   def test_extension_name
 
 148     # The ids refer to the .gpx fixtures in test/traces
 
 149     check_extension_name("a", ".gpx")
 
 150     check_extension_name("b", ".gpx")
 
 151     check_extension_name("c", ".gpx.bz2")
 
 152     check_extension_name("d", ".gpx.gz")
 
 153     check_extension_name("f", ".zip")
 
 154     check_extension_name("g", ".tar")
 
 155     check_extension_name("h", ".tar.gz")
 
 156     check_extension_name("i", ".tar.bz2")
 
 160     check_xml_file("a", "848caa72f2f456d1bd6a0fdf228aa1b9")
 
 161     check_xml_file("b", "db4cb5ed2d7d2b627b3b504296c4f701")
 
 162     check_xml_file("c", "848caa72f2f456d1bd6a0fdf228aa1b9")
 
 163     check_xml_file("d", "abd6675fdf3024a84fc0a1deac147c0d")
 
 164     check_xml_file("f", "a7c05d676c77dc14369c21be216a3713")
 
 165     check_xml_file("g", "a7c05d676c77dc14369c21be216a3713")
 
 166     check_xml_file("h", "a7c05d676c77dc14369c21be216a3713")
 
 167     check_xml_file("i", "a7c05d676c77dc14369c21be216a3713")
 
 170   def test_large_picture
 
 171     picture = Rails.root.join("test/gpx/fixtures/a.gif").read(:mode => "rb")
 
 172     trace = create(:trace, :fixture => "a")
 
 174     assert_equal picture, trace.large_picture
 
 177   def test_icon_picture
 
 178     picture = Rails.root.join("test/gpx/fixtures/a_icon.gif").read(:mode => "rb")
 
 179     trace = create(:trace, :fixture => "a")
 
 181     assert_equal picture, trace.icon_picture
 
 184   def test_import_removes_previous_tracepoints
 
 185     trace = create(:trace, :fixture => "a")
 
 186     # Tracepoints don't have a primary key, so we use a specific latitude to
 
 187     # check for successful deletion
 
 188     create(:tracepoint, :latitude => 54321, :trace => trace)
 
 189     assert_equal 1, Tracepoint.where(:latitude => 54321).count
 
 193     assert_equal 0, Tracepoint.where(:latitude => 54321).count
 
 196   def test_import_creates_tracepoints
 
 197     trace = create(:trace, :fixture => "a")
 
 198     assert_equal 0, Tracepoint.where(:trace => trace).count
 
 203     assert_equal 1, Tracepoint.where(:trace => trace).count
 
 205     # Check that the tile has been set prior to the bulk import
 
 206     # i.e. that the callbacks have been run correctly
 
 207     assert_equal 3221331576, Tracepoint.find_by(:trace => trace).tile
 
 210   def test_import_creates_icon
 
 211     trace = create(:trace, :inserted => false, :fixture => "a")
 
 213     assert_not_predicate trace.icon, :attached?
 
 217     assert_predicate trace.icon, :attached?
 
 220   def test_import_creates_large_picture
 
 221     trace = create(:trace, :inserted => false, :fixture => "a")
 
 223     assert_not_predicate trace.image, :attached?
 
 227     assert_predicate trace.image, :attached?
 
 230   def test_import_handles_bz2
 
 231     trace = create(:trace, :inserted => false, :fixture => "c")
 
 235     assert_equal 1, trace.size
 
 238   def test_import_handles_plain
 
 239     trace = create(:trace, :inserted => false, :fixture => "a")
 
 243     assert_equal 1, trace.size
 
 246   def test_import_handles_plain_with_bom
 
 247     trace = create(:trace, :inserted => false, :fixture => "b")
 
 251     assert_equal 1, trace.size
 
 254   def test_import_handles_gz
 
 255     trace = create(:trace, :inserted => false, :fixture => "d")
 
 259     assert_equal 1, trace.size
 
 262   def test_import_handles_zip
 
 263     trace = create(:trace, :inserted => false, :fixture => "f")
 
 267     assert_equal 2, trace.size
 
 270   def test_import_handles_tar
 
 271     trace = create(:trace, :inserted => false, :fixture => "g")
 
 275     assert_equal 2, trace.size
 
 278   def test_import_handles_tar_gz
 
 279     trace = create(:trace, :inserted => false, :fixture => "h")
 
 283     assert_equal 2, trace.size
 
 286   def test_import_handles_tar_bz2
 
 287     trace = create(:trace, :inserted => false, :fixture => "i")
 
 291     assert_equal 2, trace.size
 
 294   def test_import_enforces_limit
 
 295     trace = create(:trace, :inserted => false, :fixture => "f")
 
 297     with_settings(:max_trace_size => 1) do
 
 298       assert_raise GPX::FileTooBigError do
 
 303     assert_not trace.inserted
 
 308   def check_query(query, traces)
 
 309     traces = traces.map(&:id).sort
 
 310     assert_equal traces, query.order(:id).ids
 
 313   def check_mime_type(id, mime_type)
 
 314     assert_equal mime_type, create(:trace, :fixture => id).mime_type
 
 317   def check_extension_name(id, extension_name)
 
 318     assert_equal extension_name, create(:trace, :fixture => id).extension_name
 
 321   def check_xml_file(id, md5sum)
 
 322     assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
 
 325   def trace_valid(attrs, valid: true)
 
 326     entry = build(:trace, attrs)
 
 327     assert_equal valid, entry.valid?, "Expected #{attrs.inspect} to be #{valid}"
 
 331     io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest