#-- # gravaty # Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Marco Bresciani # # This file is part of gravaty. # # gravaty is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the # Free Software Foundation, either version 3 of the License, or (at your # option) any later version. # # gravaty is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License # for more details. # # You should have received a copy of the GNU General Public License # along with gravaty. If not, see . #++ require_relative '../test_helper' describe Gravaty::Gravaty do describe 'when created with a valid email address ' do subject {Gravaty::Gravaty.new(Gravaty::TEST_MY_ADDRESS, Gravaty::parser)} Gravaty::AVATAR_FORMATS.each do |avatar| describe "when asked for '#{avatar}' image" do it "shall provide an URI with specified '#{avatar}' image" do subject.avatar(type: avatar).must_include(".#{avatar}") end end end describe 'when asked for unknown image' do it 'must raise an ArgumentError' do assert_raises ArgumentError do subject.avatar type: 'unknown' end end end Gravaty::ALLOWED_SIZES.each do |size| describe "when asked for a size of '#{size}' pixels" do it "shall provide an URI with specified '#{size}' image" do subject.avatar(pixelsize: size) .must_match(/#{Gravaty::TEST_SIZE_REGEXP}$/) end end end [-1, 0, Gravaty::ALLOWED_SIZES.max + 1].each do |size| describe "when asked for an invalid size of '#{size}' pixels" do it 'must raise an argument error' do assert_raises ArgumentError do subject.avatar(pixelsize: size) end end end end Gravaty::RATING_OPTIONS.each do |rating| describe "when asked for a specific image rating '#{rating}'" do it "shall provide an URI with specified '#{rating}'" do subject.avatar(rating: rating) .must_include("rating=#{rating}") end end end describe 'when asked for unknown rating' do it 'must raise an ArgumentError' do assert_raises ArgumentError do subject.avatar rating: 'unknown' end end end describe 'when asked for forcing default image' do it 'shall provide a forced URI' do subject.avatar(force: true).must_include Gravaty::TEST_FORCED end end Gravaty::DEFAULT_OPTIONS.each do |options| describe "when asked for a specific image options '#{options}'" do it "shall provide an URI with option '#{options}'" do subject.avatar(default: options).must_include("d=#{options}") end end end describe 'when provided with a own default image' do it 'shall provide a HTTP(S) URI' do ['', 's'].each do |type| an_uri = "http#{type}://www.example.com/example.jpg" subject.avatar(default: an_uri).must_include CGI::escape(an_uri) end end it 'must raise an ArgumentError when URI is not HTTP(S)' do assert_raises ArgumentError do subject.avatar default: 'ftp://www.example.com/example.jpg' end end it 'must raise an ArgumentError when image format is not supported' do assert_raises ArgumentError do subject.avatar default: 'http://www.example.com/example.svg' end end it 'must raise an ArgumentError when URI contains a query string' do assert_raises ArgumentError do subject.avatar default: 'http://www.example.com?image=example.jpg' end end end end end