]> git.openstreetmap.org Git - rails.git/blob - test/helpers/user_helper_test.rb
Truncate descriptions horizontally in changeset lists
[rails.git] / test / helpers / user_helper_test.rb
1 require "test_helper"
2
3 class UserHelperTest < ActionView::TestCase
4   include ERB::Util
5
6   def test_user_image
7     user = create(:user)
8     gravatar_user = create(:user, :image_use_gravatar => true)
9
10     image = user_image(user)
11     assert_match %r{^<img class="user_image border border-secondary-subtle bg-body" .* src="/images/avatar.svg" />$}, image
12
13     image = user_image(user, :class => "foo")
14     assert_match %r{^<img class="foo" .* src="/images/avatar.svg" />$}, image
15     image = user_image(gravatar_user)
16     assert_match %r{^<img class="user_image border border-secondary-subtle bg-body" .* src="http://www.gravatar.com/avatar/.*" />$}, image
17
18     image = user_image(gravatar_user, :class => "foo")
19     assert_match %r{^<img class="foo" .* src="http://www.gravatar.com/avatar/.*" />$}, image
20   end
21
22   def test_user_thumbnail
23     user = create(:user)
24     gravatar_user = create(:user, :image_use_gravatar => true)
25
26     image = user_thumbnail(user)
27     assert_match %r{^<img class="user_thumbnail border border-secondary-subtle bg-body" .* src="/images/avatar.svg" />$}, image
28
29     image = user_thumbnail(user, :class => "foo")
30     assert_match %r{^<img class="foo" .* src="/images/avatar.svg" />$}, image
31
32     image = user_thumbnail(gravatar_user)
33     assert_match %r{^<img class="user_thumbnail border border-secondary-subtle bg-body" .* src="http://www.gravatar.com/avatar/.*" />$}, image
34
35     image = user_thumbnail(gravatar_user, :class => "foo")
36     assert_match %r{^<img class="foo" .* src="http://www.gravatar.com/avatar/.*" />$}, image
37   end
38
39   def test_user_thumbnail_tiny
40     user = create(:user)
41     gravatar_user = create(:user, :image_use_gravatar => true)
42
43     image = user_thumbnail_tiny(user)
44     assert_match %r{^<img class="user_thumbnail_tiny border border-secondary-subtle bg-body" .* src="/images/avatar.svg" />$}, image
45
46     image = user_thumbnail_tiny(user, :class => "foo")
47     assert_match %r{^<img class="foo" .* src="/images/avatar.svg" />$}, image
48
49     image = user_thumbnail_tiny(gravatar_user)
50     assert_match %r{^<img class="user_thumbnail_tiny border border-secondary-subtle bg-body" .* src="http://www.gravatar.com/avatar/.*" />$}, image
51
52     image = user_thumbnail_tiny(gravatar_user, :class => "foo")
53     assert_match %r{^<img class="foo" .* src="http://www.gravatar.com/avatar/.*" />$}, image
54   end
55
56   def test_user_image_url
57     user = create(:user)
58     user.avatar.attach(:io => File.open("test/gpx/fixtures/a.gif"), :filename => "a.gif")
59     gravatar_user = create(:user, :image_use_gravatar => true)
60
61     url = user_image_url(user)
62     assert_match %r{^http://test.host/rails/active_storage/representations/redirect/[^/]+/[^/]+/a.gif$}, url
63
64     url = user_image_url(gravatar_user)
65     assert_match %r{^http://www.gravatar.com/avatar/}, url
66   end
67
68   def test_user_image_sizes_default_image
69     user = create(:user)
70
71     image = user_image(user)
72     assert_match %r{^<img .* width="100" height="100" .* />$}, image
73
74     thumbnail = user_thumbnail(user)
75     assert_match %r{^<img .* width="50" height="50" .* />$}, thumbnail
76   end
77
78   def test_user_image_sizes_avatar
79     user = create(:user)
80     user.avatar.attach(:io => File.open("test/gpx/fixtures/a.gif"), :filename => "a.gif")
81
82     # first time access, no width or height is found
83     image = user_image(user)
84     assert_no_match %r{^<img .* width="100" height="100" .* />$}, image
85
86     thumbnail = user_thumbnail(user)
87     assert_no_match %r{^<img .* width="50" height="50" .* />$}, thumbnail
88
89     # Small hacks to simulate what happens when the images have been fetched at least once before
90     variant = user.avatar.variant(:resize_to_limit => [100, 100])
91     variant.processed.send(:record).image.blob.analyze
92     variant = user.avatar.variant(:resize_to_limit => [50, 50])
93     variant.processed.send(:record).image.blob.analyze
94
95     image = user_image(user)
96     assert_match %r{^<img .* width="100" height="100" .* />$}, image
97
98     thumbnail = user_thumbnail(user)
99     assert_match %r{^<img .* width="50" height="50" .* />$}, thumbnail
100   end
101
102   def test_user_image_sizes_gravatar
103     user = create(:user, :image_use_gravatar => true)
104
105     image = user_image(user)
106     assert_match %r{^<img .* width="100" height="100" .* />$}, image
107
108     thumbnail = user_thumbnail(user)
109     assert_match %r{^<img .* width="50" height="50" .* />$}, thumbnail
110   end
111
112   def test_openid_logo
113     logo = openid_logo
114     assert_match %r{^<img .* src="/images/openid_small.png" />$}, logo
115   end
116
117   def test_auth_button
118     button = auth_button("google", "google")
119     img_tag = "<img alt=\"Log in with a Google OpenID\" class=\"rounded-1\" src=\"/images/google.svg\" width=\"24\" height=\"24\" />"
120     assert_equal("<a class=\"auth_button p-2 d-block\" title=\"Log in with Google\" rel=\"nofollow\" data-method=\"post\" href=\"/auth/google\">#{img_tag}</a>", button)
121   end
122
123   private
124
125   def request
126     controller.request
127   end
128 end