Add some tests for the user helper
authorTom Hughes <tom@compton.nu>
Thu, 6 Jul 2017 18:11:21 +0000 (19:11 +0100)
committerTom Hughes <tom@compton.nu>
Thu, 6 Jul 2017 18:11:21 +0000 (19:11 +0100)
app/helpers/user_helper.rb
test/helpers/user_helper_test.rb [new file with mode: 0644]

index 6043edf..27d6faa 100644 (file)
@@ -57,6 +57,8 @@ module UserHelper
     )
   end
 
+  private
+
   # Gravatar support
 
   # See http://en.gravatar.com/site/implement/images/ for details.
diff --git a/test/helpers/user_helper_test.rb b/test/helpers/user_helper_test.rb
new file mode 100644 (file)
index 0000000..ea34640
--- /dev/null
@@ -0,0 +1,86 @@
+require "test_helper"
+
+class UserHelperTest < ActionView::TestCase
+  include ERB::Util
+
+  def test_user_image
+    user = create(:user)
+    gravatar_user = create(:user, :image_use_gravatar => true)
+
+    image = user_image(user)
+    assert_match %r{^<img class="user_image" .* src="/assets/users/images/large-.*" />$}, image
+
+    image = user_image(user, :class => "foo")
+    assert_match %r{^<img class="foo" .* src="/assets/users/images/large-.*" />$}, image
+
+    image = user_image(gravatar_user)
+    assert_match %r{^<img class="user_image" .* src="http://www.gravatar.com/avatar/.*" />$}, image
+
+    image = user_image(gravatar_user, :class => "foo")
+    assert_match %r{^<img class="foo" .* src="http://www.gravatar.com/avatar/.*" />$}, image
+  end
+
+  def test_user_thumbnail
+    user = create(:user)
+    gravatar_user = create(:user, :image_use_gravatar => true)
+
+    image = user_thumbnail(user)
+    assert_match %r{^<img class="user_thumbnail" .* src="/assets/users/images/small-.*" />$}, image
+
+    image = user_thumbnail(user, :class => "foo")
+    assert_match %r{^<img class="foo" .* src="/assets/users/images/small-.*" />$}, image
+
+    image = user_thumbnail(gravatar_user)
+    assert_match %r{^<img class="user_thumbnail" .* src="http://www.gravatar.com/avatar/.*" />$}, image
+
+    image = user_thumbnail(gravatar_user, :class => "foo")
+    assert_match %r{^<img class="foo" .* src="http://www.gravatar.com/avatar/.*" />$}, image
+  end
+
+  def test_user_thumbnail_tiny
+    user = create(:user)
+    gravatar_user = create(:user, :image_use_gravatar => true)
+
+    image = user_thumbnail_tiny(user)
+    assert_match %r{^<img class="user_thumbnail_tiny" .* src="/assets/users/images/small-.*" />$}, image
+
+    image = user_thumbnail_tiny(user, :class => "foo")
+    assert_match %r{^<img class="foo" .* src="/assets/users/images/small-.*" />$}, image
+
+    image = user_thumbnail_tiny(gravatar_user)
+    assert_match %r{^<img class="user_thumbnail_tiny" .* src="http://www.gravatar.com/avatar/.*" />$}, image
+
+    image = user_thumbnail_tiny(gravatar_user, :class => "foo")
+    assert_match %r{^<img class="foo" .* src="http://www.gravatar.com/avatar/.*" />$}, image
+  end
+
+  def test_user_image_url
+    user = create(:user)
+    gravatar_user = create(:user, :image_use_gravatar => true)
+
+    url = user_image_url(user)
+    assert_match %r{^/users/images/original.png$}, url
+
+    url = user_image_url(gravatar_user)
+    assert_match %r{^http://www.gravatar.com/avatar/}, url
+  end
+
+  def test_openid_logo
+    logo = openid_logo
+    assert_match %r{^<img .* class="openid_logo" src="/images/openid_small.png" />$}, logo
+  end
+
+  def test_auth_button
+    button = auth_button("google", "google")
+    assert_equal button, "<a class=\"auth_button\" title=\"Login with Google\" href=\"/auth/google\"><img alt=\"Login with a Google OpenID\" src=\"/images/google.png\" /></a>"
+
+    button = auth_button("yahoo", "openid", :openid_url => "yahoo.com")
+    assert_equal button, "<a class=\"auth_button\" title=\"Login with Yahoo\" href=\"/auth/openid?openid_url=yahoo\.com\"><img alt=\"Login with a Yahoo OpenID\" src=\"/images/yahoo.png\" /></a>"
+  end
+
+  private
+
+  def request
+    controller.request
+  end
+end