|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class AvatarsTest < ActionDispatch::IntegrationTest |
| 4 | + test "returns 404 when no user is found" do |
| 5 | + get avatar_user_path("user", size: 64) |
| 6 | + |
| 7 | + assert_response :not_found |
| 8 | + end |
| 9 | + |
| 10 | + test "redirects to default avatar when gravatar returns 404" do |
| 11 | + stub_request(:get, Addressable::Template.new("https://secure.gravatar.com/avatar/{hash}.png?d=404&r=PG&s={size}")) |
| 12 | + .to_return(status: 404) |
| 13 | + |
| 14 | + user = create(:user) |
| 15 | + get avatar_user_path(user.id, size: 64) |
| 16 | + |
| 17 | + assert_response :found |
| 18 | + assert_equal "http://localhost/images/avatar.svg", response.headers["Location"] |
| 19 | + end |
| 20 | + |
| 21 | + test "serves gravatar response on 200" do |
| 22 | + stub_request(:get, Addressable::Template.new("https://secure.gravatar.com/avatar/{hash}.png?d=404&r=PG&s=64")) |
| 23 | + .to_return(status: 200, body: "image", headers: { |
| 24 | + "Content-Type" => "image/jpeg", |
| 25 | + "Last-Modified" => "Wed, 21 Oct 2015 07:28:00 GMT", |
| 26 | + "Link" => "foo" |
| 27 | + }) |
| 28 | + |
| 29 | + user = create(:user) |
| 30 | + get avatar_user_path(user.id, size: 64) |
| 31 | + |
| 32 | + assert_response :success |
| 33 | + assert_equal "image/jpeg", response.headers["Content-Type"] |
| 34 | + assert_equal "image", response.body |
| 35 | + assert_nil response.headers["Link"] |
| 36 | + end |
| 37 | + |
| 38 | + test "serves default avatar with theme when user has no gravatar" do |
| 39 | + user = create(:user) |
| 40 | + get avatar_user_path(user.id, size: 64, theme: "dark") |
| 41 | + |
| 42 | + assert_response :found |
| 43 | + assert_equal "http://localhost/images/avatar_inverted.svg", response.headers["Location"] |
| 44 | + end |
| 45 | + |
| 46 | + test "falls back to default avatar when gravatar returns 500" do |
| 47 | + stub_request(:get, Addressable::Template.new("https://secure.gravatar.com/avatar/{hash}.png?d=404&r=PG&s=64")) |
| 48 | + .to_return(status: 500) |
| 49 | + |
| 50 | + user = create(:user) |
| 51 | + get avatar_user_path(user.id, size: 64) |
| 52 | + |
| 53 | + assert_response :found |
| 54 | + assert_equal "http://localhost/images/avatar.svg", response.headers["Location"] |
| 55 | + end |
| 56 | + |
| 57 | + test "returns 400 when size is invalid" do |
| 58 | + user = create(:user) |
| 59 | + get avatar_user_path(user.id, size: 0) |
| 60 | + |
| 61 | + assert_response :bad_request |
| 62 | + assert_equal "Invalid size", response.body |
| 63 | + |
| 64 | + get avatar_user_path(user.id, size: 2049) |
| 65 | + |
| 66 | + assert_response :bad_request |
| 67 | + assert_equal "Invalid size", response.body |
| 68 | + end |
| 69 | + |
| 70 | + test "returns 400 when theme is invalid" do |
| 71 | + user = create(:user) |
| 72 | + get avatar_user_path(user.id, size: 64, theme: "unknown") |
| 73 | + |
| 74 | + assert_response :bad_request |
| 75 | + assert_equal "Invalid theme", response.body |
| 76 | + end |
| 77 | +end |
0 commit comments