OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/modules/avatars/spec/helpers/avatar_helper_spec.rb

222 lines
7.4 KiB

require 'spec_helper'
describe AvatarHelper, type: :helper, with_settings: { protocol: 'http' } do
let(:user) { FactoryBot.build_stubbed(:user) }
let(:mail_digest) { Digest::MD5.hexdigest(user.mail) }
let(:avatar_stub) { FactoryBot.build_stubbed(:avatar_attachment) }
let(:enable_gravatars) { false }
let(:enable_local_avatars) { false }
let(:plugin_settings) do
{
'enable_gravatars' => enable_gravatars,
Boards module (#7008) * Hack spike to show D&D use case [ci skip] * Add ordered work packages * Save order on existing work packages * Boards WIP * CDK drag * Add dragula handler [ci skip] * Add filter to return all manual sorted work packages * Print icon on hover * Boards routing and list components * Better loading indicator on list with streaming result [ci skip] * Add new board and list buttons [ci skip] * Post new query [ci skip] * Added creation of new board lists with persisted queries [ci skip] * Render placeholder row in empty queries [ci skip] * Push boards on grid * Use base class in scope [ci skip] * Extend api for options * Hack spike to show D&D use case [ci skip] * Add ordered work packages * Save order on existing work packages * Boards WIP * CDK drag * Add dragula handler [ci skip] * Add filter to return all manual sorted work packages * Print icon on hover * Boards routing and list components * Better loading indicator on list with streaming result [ci skip] * Add new board and list buttons [ci skip] * Post new query [ci skip] * Added creation of new board lists with persisted queries [ci skip] * Render placeholder row in empty queries [ci skip] * Save queries in grids [ci skip] * Renaming queries [ci skip] * Add existing work packages to board [ci skip] * Introduce card view component for work packages * Extend grids to allow project scope for boards (#7025) Extends the grid backend to also be able to handle boards. In particular, it adds the ability of boards to be attached to projects and changes the page property of grids to a scope property that better describes that more than one board can belong to the same scope (e.g. /projects/:project_id/boards). For a fully featured board, though, widgets need to be able to store options, so that they can store queries. Those widgets might also need to have custom processing and validation. That part has not been implemented. * introduce project association for boards * have dedicated grid registration classes * update and create form for board grids * extract defaults into grid registration [ci skip] * Add drag and drop to card view [ci skip] * Add options to grid * Fix option migration name * Renaming boards [ci skip] * Frontend deletion of boards * Avoid map on NodeList which doesnt exist [ci skip] * Add inline create to boards [ci skip] * Smaller create button [ci skip] * Add navigation for boards * Make inner grid same height * Replace index page with table * Workaround for widget registration [ci skip] * Fixed height for cards and tables [ci skip] * Implement escape as cancel d&d action [ci skip] * Fix and extend grid specs for name and options * Extend board specs for required name * Fix migration for MySQL references https://stackoverflow.com/a/45825566/420614 * Make board list extend from widget Since we cannot configure widgets yet, it's not yet possible to use a board-list widget anywhere. * Fix specs * Fix escape listener removal [ci skip] * Fix renamed to_path in relation spec [ci skip] * Allow deletion of grids for boards * Avoid reloading resource multiple times with replays * Frontend synchronization on deletion [ci skip] * Delete through table * Use work packages board path * Use work packages board path * Fix augmented columns breaking re-rendering * Fix duplicated permission with forums * Strengthen tab switch in specs * Add hidden flag for project-context queries Allows the API to create a hidden query that will not be rendered to the user even if it is within a project context. * private queries * Add hidden flag for project-context queries Allows the API to create a hidden query that will not be rendered to the user even if it is within a project context. * Move boards below work packages * Add Board configuration modal * Fix reloading with onPush * Saving / Switching of display mode [ci skip] * Extract wp-query-selectable-title into common component * Fix renaming of board-list * Fix auto-hide notifications in boards * Add permissions to seeders * Reorder lists in board * Linting * Remove default gravatar from settings * Show assignees avatar in the card view of WPs * Fix specs * Add missing method * Fix timeline icon * Use URL as input to be able to show avatars for groups, too * Fix test * Add further specs * Use correct data attribute to avoid unnecessary data base calls * Add further specs * Deletion of board lists * Pass permission via gon to decide whether we can create boards * Fix rename spec * Cherry-pick of 7873d59 and 30abc7f
6 years ago
'enable_local_avatars' => enable_local_avatars
}
end
before do
allow(Setting)
.to receive(:plugin_openproject_avatars)
.and_return(plugin_settings)
allow(user).to receive(:local_avatar_attachment).and_return avatar_stub
end
def local_expected_user_avatar_tag(user)
tag_options = { 'data-user-avatar-src': local_expected_url(user),
'data-user-name': user.name,
'data-class-list': 'avatar' }
content_tag 'user-avatar', '', tag_options
end
def local_expected_url(user)
user_avatar_url(user)
end
def default_expected_user_avatar_tag(user)
tag_options = { 'data-use-fallback': "true",
Boards module (#7008) * Hack spike to show D&D use case [ci skip] * Add ordered work packages * Save order on existing work packages * Boards WIP * CDK drag * Add dragula handler [ci skip] * Add filter to return all manual sorted work packages * Print icon on hover * Boards routing and list components * Better loading indicator on list with streaming result [ci skip] * Add new board and list buttons [ci skip] * Post new query [ci skip] * Added creation of new board lists with persisted queries [ci skip] * Render placeholder row in empty queries [ci skip] * Push boards on grid * Use base class in scope [ci skip] * Extend api for options * Hack spike to show D&D use case [ci skip] * Add ordered work packages * Save order on existing work packages * Boards WIP * CDK drag * Add dragula handler [ci skip] * Add filter to return all manual sorted work packages * Print icon on hover * Boards routing and list components * Better loading indicator on list with streaming result [ci skip] * Add new board and list buttons [ci skip] * Post new query [ci skip] * Added creation of new board lists with persisted queries [ci skip] * Render placeholder row in empty queries [ci skip] * Save queries in grids [ci skip] * Renaming queries [ci skip] * Add existing work packages to board [ci skip] * Introduce card view component for work packages * Extend grids to allow project scope for boards (#7025) Extends the grid backend to also be able to handle boards. In particular, it adds the ability of boards to be attached to projects and changes the page property of grids to a scope property that better describes that more than one board can belong to the same scope (e.g. /projects/:project_id/boards). For a fully featured board, though, widgets need to be able to store options, so that they can store queries. Those widgets might also need to have custom processing and validation. That part has not been implemented. * introduce project association for boards * have dedicated grid registration classes * update and create form for board grids * extract defaults into grid registration [ci skip] * Add drag and drop to card view [ci skip] * Add options to grid * Fix option migration name * Renaming boards [ci skip] * Frontend deletion of boards * Avoid map on NodeList which doesnt exist [ci skip] * Add inline create to boards [ci skip] * Smaller create button [ci skip] * Add navigation for boards * Make inner grid same height * Replace index page with table * Workaround for widget registration [ci skip] * Fixed height for cards and tables [ci skip] * Implement escape as cancel d&d action [ci skip] * Fix and extend grid specs for name and options * Extend board specs for required name * Fix migration for MySQL references https://stackoverflow.com/a/45825566/420614 * Make board list extend from widget Since we cannot configure widgets yet, it's not yet possible to use a board-list widget anywhere. * Fix specs * Fix escape listener removal [ci skip] * Fix renamed to_path in relation spec [ci skip] * Allow deletion of grids for boards * Avoid reloading resource multiple times with replays * Frontend synchronization on deletion [ci skip] * Delete through table * Use work packages board path * Use work packages board path * Fix augmented columns breaking re-rendering * Fix duplicated permission with forums * Strengthen tab switch in specs * Add hidden flag for project-context queries Allows the API to create a hidden query that will not be rendered to the user even if it is within a project context. * private queries * Add hidden flag for project-context queries Allows the API to create a hidden query that will not be rendered to the user even if it is within a project context. * Move boards below work packages * Add Board configuration modal * Fix reloading with onPush * Saving / Switching of display mode [ci skip] * Extract wp-query-selectable-title into common component * Fix renaming of board-list * Fix auto-hide notifications in boards * Add permissions to seeders * Reorder lists in board * Linting * Remove default gravatar from settings * Show assignees avatar in the card view of WPs * Fix specs * Add missing method * Fix timeline icon * Use URL as input to be able to show avatars for groups, too * Fix test * Add further specs * Use correct data attribute to avoid unnecessary data base calls * Add further specs * Deletion of board lists * Pass permission via gon to decide whether we can create boards * Fix rename spec * Cherry-pick of 7873d59 and 30abc7f
6 years ago
'data-user-name': user.name,
'data-class-list': 'avatar avatar-default' }
content_tag 'user-avatar', '', tag_options
end
def gravatar_expected_user_avatar_tag(digest, options = {})
tag_options = { 'data-user-avatar-src': gravatar_expected_url(digest, options),
'data-user-name': user.name,
'data-class-list': 'avatar avatar--gravatar-image avatar--fallback' }
content_tag 'user-avatar', '', tag_options
end
def gravatar_expected_image_tag(digest, options = {})
tag_options = options.reverse_merge(title: user.name,
alt: 'Gravatar',
class: 'avatar avatar--gravatar-image avatar--fallback').delete_if { |key, value| value.nil? || key == :ssl }
image_tag gravatar_expected_url(digest, options), tag_options
end
def gravatar_expected_url(digest, options = {})
ssl = !!options[:ssl]
host =
if ssl
'https://secure.gravatar.com'
else
'http://gravatar.com'
end
"#{host}/avatar/#{digest}?default=404&secure=#{ssl}"
end
describe 'gravatar and local' do
context 'when enabled' do
let(:enable_gravatars) { true }
let(:enable_local_avatars) { true }
it "should return the image attached to the user" do
expect(helper.avatar(user)).to be_html_eql(local_expected_user_avatar_tag(user))
end
it "should return the gravatar image if no image uploaded for the user" do
allow(user).to receive(:local_avatar_attachment).and_return nil
expect(helper.avatar(user)).to be_html_eql(gravatar_expected_user_avatar_tag(mail_digest))
end
end
context 'when gravatar disabled' do
let(:enable_gravatars) { false }
let(:enable_local_avatars) { true }
it "should return blank if image attached to the user but gravatars disabled" do
expect(helper.avatar(user)).to be_html_eql(local_expected_user_avatar_tag(user))
end
end
context 'when all disabled' do
let(:enable_gravatars) { false }
let(:enable_local_avatars) { false }
it "should return blank" do
expect(helper.avatar(user)).to be_html_eql(default_expected_user_avatar_tag(user))
end
end
end
describe '#avatar_url' do
context 'when enabled' do
let(:enable_gravatars) { true }
let(:enable_local_avatars) { true }
it "should return the url to the image attached to the user" do
expect(helper.avatar_url(user)).to eq(local_expected_url(user))
end
it "should return the gravatar url if no image uploaded for the user" do
allow(user).to receive(:local_avatar_attachment).and_return nil
expect(helper.avatar_url(user)).to eq(gravatar_expected_url(mail_digest))
end
end
context 'when gravatar disabled' do
let(:enable_gravatars) { false }
let(:enable_local_avatars) { true }
it "should return the url if image attached to the user but gravatars disabled" do
expect(helper.avatar_url(user)).to eq(local_expected_url(user))
end
end
context 'when all disabled' do
let(:enable_gravatars) { false }
let(:enable_local_avatars) { false }
it "should return blank" do
expect(helper.avatar_url(user)).to eq ''
end
end
end
describe 'gravatar' do
context 'when enabled' do
let(:enable_gravatars) { true }
let(:enable_local_avatars) { false }
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(helper.default_gravatar_options[: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(helper.default_gravatar_options[:secure]).to be false
end
end
end
context 'with http', with_settings: { protocol: 'http' } do
it 'should return a gravatar image tag if a user is provided' do
expect(helper.avatar(user)).to be_html_eql(gravatar_expected_user_avatar_tag(mail_digest))
end
it 'should return a gravatar url if a user is provided' do
expect(helper.avatar_url(user)).to eq(gravatar_expected_url(mail_digest))
end
end
context 'with https', with_settings: { protocol: 'https' } do
it 'should return a gravatar image tag with ssl if the request was ssl required' do
expect(helper.avatar(user)).to be_html_eql(gravatar_expected_user_avatar_tag(mail_digest, ssl: true))
end
it 'should return a gravatar image tag with ssl if the request was ssl required' do
expect(helper.avatar_url(user)).to eq(gravatar_expected_url(mail_digest, ssl: true))
end
end
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
it 'should return an empty string if nil is provided' do
expect(helper.avatar(nil)).to eq('')
end
it 'should return an empty string if a parsable e-mail with default avatar is provided' do
mail = '<e-mail@mail.de>'
expect(helper.avatar(mail)).to eq('')
end
it 'should return a gravatar url 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_url(mail)).to eq(gravatar_expected_url(digest))
end
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
it 'should return an empty string if nil is provided' do
expect(helper.avatar_url(nil)).to eq('')
end
end
end
context 'when all disabled' do
let(:enable_gravatars) { false }
let(:enable_local_avatars) { false }
it 'should return an empty string if gravatar is disabled' do
expect(helper.avatar(user)).to be_html_eql(default_expected_user_avatar_tag(user))
end
it 'should return an empty string if gravatar is disabled' do
expect(helper.avatar_url(user)).to eq('')
end
end
end