Add support for parent-scoped with_settings data

pull/4351/head
Oliver Günther 9 years ago
parent b2490bf958
commit e9cd24f2bf
  1. 14
      app/helpers/avatar_helper.rb
  2. 7
      spec/controllers/news_controller_spec.rb
  3. 17
      spec/controllers/users_controller_spec.rb
  4. 139
      spec/helpers/avatar_helper_spec.rb
  5. 73
      spec/helpers/pagination_helper_spec.rb
  6. 7
      spec/lib/api/v3/users/user_representer_spec.rb
  7. 9
      spec/models/news_spec.rb
  8. 5
      spec/models/repository/subversion_spec.rb
  9. 10
      spec/models/timeline_spec.rb
  10. 25
      spec/support/shared/with_settings.rb

@ -32,12 +32,16 @@ require 'gravatar_image_tag'
module AvatarHelper
include GravatarImageTag
GravatarImageTag.configure do |c|
c.include_size_attributes = false
c.secure = Setting.protocol == 'https'
c.default_image = Setting.gravatar_default.blank? ? nil : Setting.gravatar_default
def self.configure!
GravatarImageTag.configure do |c|
c.include_size_attributes = false
c.secure = Setting.protocol == 'https'
c.default_image = Setting.gravatar_default.presence
end
end
configure!
Setting.register_callback(:protocol) do |values|
GravatarImageTag.configure do |c|
c.secure = values[:value] == 'https'
@ -46,7 +50,7 @@ module AvatarHelper
Setting.register_callback(:gravatar_default) do |values|
GravatarImageTag.configure do |c|
c.default_image = values[:value].blank? ? nil : values[:value]
c.default_image = values[:value].presence
end
end

@ -103,10 +103,11 @@ describe NewsController, type: :controller do
end
describe '#create' do
it 'persists a news item and delivers email notifications' do
become_member_with_permissions(project, user)
context 'with news_added notifications',
with_settings: { notified_events: %w(news_added) } do
it 'persists a news item and delivers email notifications' do
become_member_with_permissions(project, user)
with_settings notified_events: ['news_added'] do
post :create, project_id: project.id, news: { title: 'NewsControllerTest',
description: 'This is the description',
summary: '' }

@ -254,7 +254,11 @@ describe UsersController, type: :controller do
end
end
describe '#change_status' do
describe '#change_status',
with_settings: {
available_languages: %i(en de),
bcc_recipients: 1
} do
describe 'WHEN activating a registered user' do
let!(:registered_user) do
FactoryGirl.create(:user, status: User::STATUSES[:registered],
@ -262,13 +266,10 @@ describe UsersController, type: :controller do
end
before do
with_settings(available_languages: [:en, :de],
bcc_recipients: '1') do
as_logged_in_user admin do
post :change_status, id: registered_user.id,
user: { status: User::STATUSES[:active] },
activate: '1'
end
as_logged_in_user admin do
post :change_status, id: registered_user.id,
user: { status: User::STATUSES[:active] },
activate: '1'
end
end

@ -50,121 +50,104 @@ describe AvatarHelper, type: :helper do
"#{host}/avatar/#{digest}?secure=#{ssl}"
end
describe 'ssl dependent on protocol settings' do
it "should be set to secure if protocol is 'https'" do
with_settings protocol: 'https' do
expect(described_class.secure?).to be true
end
end
it "should be set to unsecure if protocol is 'http'" do
with_settings protocol: 'http' do
expect(described_class.secure?).to be false
end
end
before do
AvatarHelper.configure!
end
describe 'default avatar dependent on settings' do
it 'should be set to value of setting' do
with_settings gravatar_default: 'Wavatars' do
expect(described_class.default).to eq 'Wavatars'
describe 'avatar' do
context 'when enabled', with_settings: { gravatar_enabled?: true } do
describe 'ssl dependent on protocol settings' do
context 'with https protocol', with_settings: { protocol: 'https' } do
it "should be set to secure if protocol is 'https'" do
expect(described_class.secure?).to be true
end
end
context 'with http protocol', with_settings: { protocol: 'http' } do
it "should be set to unsecure if protocol is 'http'" do
expect(described_class.secure?).to be false
end
end
end
end
it "should be set to unsecure if protocol is 'http'" do
with_settings gravatar_default: '' do
expect(described_class.default).to be_nil
describe 'default avatar dependent on settings' do
context 'with wavatars', with_settings: { gravatar_default: 'Wavatars' } do
it 'should be set to value of setting' do
expect(described_class.default).to eq 'Wavatars'
end
end
context 'when empty' do
it "should be set to unsecure if protocol is 'http'" do
expect(described_class.default).to be_nil
end
end
end
end
end
describe '#avatar' do
it 'should return a gravatar image tag if a user is provided' do
digest = Digest::MD5.hexdigest(user.mail)
context 'with http', with_settings: { protocol: 'http' } do
it 'should return a gravatar image tag if a user is provided' do
digest = Digest::MD5.hexdigest(user.mail)
expect(helper.avatar(user)).to be_html_eql(expected_image_tag(digest))
end
with_settings gravatar_enabled: '1', protocol: 'http' do
expect(helper.avatar(user)).to be_html_eql(expected_image_tag(digest))
it 'should return a gravatar url if a user is provided' do
digest = Digest::MD5.hexdigest(user.mail)
expect(helper.avatar_url(user)).to eq(expected_url(digest))
end
end
end
it 'should return a gravatar image tag with ssl if the request was ssl required' do
digest = Digest::MD5.hexdigest(user.mail)
context 'with https', with_settings: { protocol: 'https' } do
it 'should return a gravatar image tag with ssl if the request was ssl required' do
digest = Digest::MD5.hexdigest(user.mail)
expect(helper.avatar(user)).to be_html_eql(expected_image_tag(digest, ssl: true))
end
with_settings gravatar_enabled: '1', protocol: 'https' do
expect(helper.avatar(user)).to be_html_eql(expected_image_tag(digest, ssl: true))
it 'should return a gravatar image tag with ssl if the request was ssl required' do
digest = Digest::MD5.hexdigest(user.mail)
expect(helper.avatar_url(user)).to eq(expected_url(digest, ssl: true))
end
end
end
it 'should return an empty string if a non parsable (e-mail) string is provided' do
with_settings gravatar_enabled: '1' do
it 'should return an empty string if a non parsable (e-mail) string is provided' do
expect(helper.avatar('just the name')).to eq('')
end
end
it 'should return an empty string if nil is provided' do
with_settings gravatar_enabled: '1' do
it 'should return an empty string if nil is provided' do
expect(helper.avatar(nil)).to eq('')
end
end
it 'should return an empty string if gravatar is disabled' do
with_settings gravatar_enabled: '0' do
expect(helper.avatar(user)).to eq('')
end
end
it 'should return a gravatar image tag if a parsable e-mail string is provided' do
with_settings gravatar_enabled: '1' do
it 'should return a gravatar image tag if a parsable e-mail string is provided' do
mail = '<e-mail@mail.de>'
digest = Digest::MD5.hexdigest('e-mail@mail.de')
expect(helper.avatar(mail)).to be_html_eql(expected_image_tag(digest, title: nil))
end
end
end
describe '#avatar_url' do
it 'should return a gravatar url if a user is provided' do
digest = Digest::MD5.hexdigest(user.mail)
with_settings gravatar_enabled: '1', protocol: 'http' do
expect(helper.avatar_url(user)).to eq(expected_url(digest))
end
end
it 'should return a gravatar image tag with ssl if the request was ssl required' do
digest = Digest::MD5.hexdigest(user.mail)
it 'should return a gravatar image tag if a parsable e-mail string is provided' do
mail = '<e-mail@mail.de>'
digest = Digest::MD5.hexdigest('e-mail@mail.de')
with_settings gravatar_enabled: '1', protocol: 'https' do
expect(helper.avatar_url(user)).to eq(expected_url(digest, ssl: true))
expect(helper.avatar_url(mail)).to eq(expected_url(digest))
end
end
it 'should return an empty string if a non parsable (e-mail) string is provided' do
with_settings gravatar_enabled: '1' do
it 'should return an empty string if a non parsable (e-mail) string is provided' do
expect(helper.avatar_url('just the name')).to eq('')
end
end
it 'should return an empty string if nil is provided' do
with_settings gravatar_enabled: '1' do
it 'should return an empty string if nil is provided' do
expect(helper.avatar_url(nil)).to eq('')
end
end
end
context 'when disabled', with_settings: { gravatar_enabled?: false } do
it 'should return an empty string if gravatar is disabled' do
with_settings gravatar_enabled: '0' do
expect(helper.avatar_url(user)).to eq('')
end
expect(helper.avatar(user)).to eq('')
end
it 'should return a gravatar image tag if a parsable e-mail string is provided' do
with_settings gravatar_enabled: '1' do
mail = '<e-mail@mail.de>'
digest = Digest::MD5.hexdigest('e-mail@mail.de')
expect(helper.avatar_url(mail)).to eq(expected_url(digest))
end
it 'should return an empty string if gravatar is disabled' do
expect(helper.avatar_url(user)).to eq('')
end
end
end

@ -170,10 +170,11 @@ describe PaginationHelper, type: :helper do
expect(page_param(page: page)).to eq(1)
end
it 'should calculate page from offset and limit if page is not provided' do
# need to change settings as only multiples of per_page
# are allowed for limit
with_settings per_page_options: '5,10,15' do
context 'with multiples per_page',
with_settings: { per_page_options: '5,10,15' } do
it 'should calculate page from offset and limit if page is not provided' do
# need to change settings as only multiples of per_page
# are allowed for limit
offset = 55
limit = 10
@ -189,8 +190,9 @@ describe PaginationHelper, type: :helper do
expect(page_param(offset: offset, limit: limit, page: page)).to eq(page)
end
it 'should not break if limit is bogus (also faulty settings)' do
with_settings per_page_options: '-1,2,3' do
context 'faulty settings',
with_settings: { per_page_options: '-1,2,3' } do
it 'should not break if limit is bogus (also faulty settings)' do
offset = 55
limit = 'lorem'
@ -203,74 +205,59 @@ describe PaginationHelper, type: :helper do
end
end
describe '#per_page_param' do
describe '#per_page_param',
with_settings: { per_page_options: '1,2,3' } do
it 'should return per_page if provided and one of the values stored in the settings' do
with_settings per_page_options: '1,2,3' do
per_page = 2
per_page = 2
expect(per_page_param(per_page: per_page)).to eq(per_page)
end
expect(per_page_param(per_page: per_page)).to eq(per_page)
end
it 'should return per_page if provided and store it in the session' do
with_settings per_page_options: '1,2,3' do
session[:per_page] = 3
per_page = 2
session[:per_page] = 3
per_page = 2
expect(per_page_param(per_page: per_page)).to eq(per_page)
expect(session[:per_page]).to eq(2)
end
expect(per_page_param(per_page: per_page)).to eq(per_page)
expect(session[:per_page]).to eq(2)
end
it 'should take the smallest value stored in the settings if provided per_page param is not one of the configured' do
with_settings per_page_options: '1,2,3' do
per_page = 4
per_page = 4
expect(per_page_param(per_page: per_page)).to eq(1)
end
expect(per_page_param(per_page: per_page)).to eq(1)
end
it 'prefers the value stored in the session if it is valid according to the settings' do
with_settings per_page_options: '1,2,3' do
session[:per_page] = 2
session[:per_page] = 2
expect(per_page_param(per_page: 3)).to eq(session[:per_page])
end
expect(per_page_param(per_page: 3)).to eq(session[:per_page])
end
it 'ignores the value stored in the session if it is not valid according to the settings' do
with_settings per_page_options: '1,2,3' do
session[:per_page] = 4
session[:per_page] = 4
expect(per_page_param(per_page: 3)).to eq(3)
end
expect(per_page_param(per_page: 3)).to eq(3)
end
it 'uses limit synonymously to per_page' do
with_settings per_page_options: '1,2,3' do
limit = 2
limit = 2
expect(per_page_param(limit: limit)).to eq(limit)
end
expect(per_page_param(limit: limit)).to eq(limit)
end
it 'prefers per_page over limit' do
with_settings per_page_options: '1,2,3' do
limit = 2
per_page = 3
limit = 2
per_page = 3
expect(per_page_param(limit: limit, per_page: per_page)).to eq(per_page)
end
expect(per_page_param(limit: limit, per_page: per_page)).to eq(per_page)
end
it 'stores the value in the session' do
with_settings per_page_options: '1,2,3' do
limit = 2
limit = 2
per_page_param(limit: limit)
per_page_param(limit: limit)
expect(session[:per_page]).to eq(limit)
end
expect(session[:per_page]).to eq(limit)
end
end
end

@ -153,13 +153,6 @@ describe ::API::V3::Users::UserRepresenter do
expect(parse_json(subject, 'avatar')).to be_blank
end
it 'should be https if setting set to https' do
# have to actually set the setting for the lib to pick up the change
with_settings protocol: 'https' do
expect(parse_json(subject, 'avatar')).to start_with('https://secure.gravatar.com/avatar')
end
end
end
end
end

@ -93,17 +93,16 @@ describe News, type: :model do
end
end
describe '#save' do
describe '#save',
with_settings: { notified_events: %w(news_added) } do
it 'sends email notifications when created' do
user = FactoryGirl.create(:user)
become_member_with_permissions(project, user)
# reload
project.members(true)
with_settings notified_events: ['news_added'] do
FactoryGirl.create(:news, project: project)
expect(ActionMailer::Base.deliveries.size).to eq(1)
end
FactoryGirl.create(:news, project: project)
expect(ActionMailer::Base.deliveries.size).to eq(1)
end
end

@ -262,8 +262,9 @@ describe Repository::Subversion, type: :model do
expect(c.format_identifier).to eq(c.revision)
end
it 'should log encoding ignore setting' do
with_settings commit_logs_encoding: 'windows-1252' do
context 'with windows-1252 encoding',
with_settings: { commit_logs_encoding: %w(windows-1252) } do
it 'should log encoding ignore setting' do
s1 = "\xC2\x80"
s2 = "\xc3\x82\xc2\x80"
if s1.respond_to?(:force_encoding)

@ -36,13 +36,11 @@ describe Timeline, type: :model do
ba = FactoryGirl.create(:user, firstname: 'b', lastname: 'a')
t = Timeline.new
with_settings user_format: :firstname_lastname do
expect(t.available_responsibles).to eq([ab, ba])
end
Setting.user_format = :firstname_lastname
expect(t.available_responsibles).to eq([ab, ba])
with_settings user_format: :lastname_firstname do
expect(t.available_responsibles).to eq([ba, ab])
end
Setting.user_format = :lastname_firstname
expect(t.available_responsibles).to eq([ba, ab])
end
end
end

@ -27,13 +27,28 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
def aggregate_mocked_settings(example, settings)
# We have to manually check parent groups for with_settings:,
# since they are being ignored otherwise
example.example_group.parents.each do |parent|
if parent.respond_to?(:metadata) && parent.metadata[:with_settings]
settings.reverse_merge!(parent.metadata[:with_settings])
end
end
settings
end
RSpec.configure do |config|
config.before(:each) do |example|
if example.metadata[:with_settings]
example.metadata[:with_settings].each do |k,v|
allow(Setting)
.to receive(k)
.and_return(v)
settings = example.metadata[:with_settings]
if settings.present?
settings = aggregate_mocked_settings(example, settings)
settings.each do |k,v|
allow(Setting).to receive(k).and_return(v)
end
end
end

Loading…
Cancel
Save