Skip to content

Commit

Permalink
Merge pull request #16032 from opf/sharing-tests
Browse files Browse the repository at this point in the history
Implement tests for sharing project queries
  • Loading branch information
klaustopher authored Jul 12, 2024
2 parents 2234efc + 3df9566 commit 616a6c6
Show file tree
Hide file tree
Showing 30 changed files with 1,344 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
icon: "share-android",
mobile_icon: "share-android",
label: t(:label_share),
data: { controller: "async-dialog" }
data: { controller: "async-dialog", test_selector: "toggle-share-dialog-button" }
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/components/shares/invite_user_form_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%=
component_wrapper do
component_wrapper(data_test_selector: 'invite-user-form') do
primer_form_with(
model: new_share,
url: url_for([entity, Member]),
Expand Down
2 changes: 1 addition & 1 deletion app/components/shares/share_dialog_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%=
render(Primer::Alpha::Dialog.new(title: t(:label_share_project_list), id: 'sharing-modal', data: { 'keep-open-on-submit': true }, size: :xlarge)) do |d|
render(Primer::Alpha::Dialog.new(title: strategy.title, id: 'sharing-modal', data: { 'keep-open-on-submit': true }, size: :xlarge, open: open)) do |d|
d.with_header(variant: :large, mb: 3)
d.with_body do
render(strategy.modal_body_component(errors))
Expand Down
5 changes: 3 additions & 2 deletions app/components/shares/share_dialog_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ class ShareDialogComponent < ApplicationComponent
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers

def initialize(strategy:, errors:)
def initialize(strategy:, errors:, open: false)
super

@strategy = strategy
@errors = errors
@open = open
end

private

attr_reader :strategy, :errors
attr_reader :strategy, :errors, :open
end
end
2 changes: 1 addition & 1 deletion app/components/shares/share_row_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
grid_layout(grid_css_classes, tag: :div, align_items: :center, classes: 'ellipsis') do |user_row_grid|
user_row_grid.with_area(:selection, tag: :div) do
if share_editable?
render(Primer::Alpha::CheckBox.new(name: "share_ids", value: share.id, label: principal.name,
render(Primer::Alpha::CheckBox.new(name: "share_ids", value: (share.id || VIRTUAL_SHARE_ID), label: principal.name,
visually_hide_label: true, scheme: :array,
data: {
'shares--bulk-selection-target': 'shareCheckbox',
Expand Down
2 changes: 2 additions & 0 deletions app/components/shares/share_row_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

module Shares
class ShareRowComponent < ApplicationComponent # rubocop:disable OpenProject/AddPreviewForViewComponent
VIRTUAL_SHARE_ID = "virtual-share-id"

include ApplicationHelper
include OpTurbo::Streamable
include OpPrimer::ComponentHelpers
Expand Down
8 changes: 7 additions & 1 deletion app/controllers/shares_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def create # rubocop:disable Metrics/AbcSize,Metrics/PerceivedComplexity

visible_shares_before_adding = sharing_strategy.shares.present?

find_or_create_users(send_notification: false) do |member_params|
find_or_create_users(send_notification: send_notification?) do |member_params|
user = User.find_by(id: member_params[:user_id])
if user.present? && user.locked?
@errors.add(:base, I18n.t("sharing.warning_locked_user", user: user.name))
Expand Down Expand Up @@ -279,6 +279,12 @@ def respond_with_bulk_removed_shares(selected_shares)
respond_with_turbo_streams
end

def send_notification?
return false if @entity.is_a?(WorkPackage) # For WorkPackages we have a custom notification

true
end

def load_entity # rubocop:disable Metrics/AbcSize
if params["work_package_id"]
@entity = WorkPackage.visible.find(params["work_package_id"])
Expand Down
4 changes: 4 additions & 0 deletions app/models/sharing_strategies/base_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def share_description(share)
raise NotImplementedError, "Override in a subclass and return a description for the shared user"
end

def title
raise NotImplementedError, "Override in a subclass and return a title for the sharing dialog"
end

def custom_body_components?
!additional_body_components.empty?
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/sharing_strategies/project_query_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def manage_shares_component(modal_content:, errors:)
end
end

def title
I18n.t(:label_share_project_list)
end

private

def virtual_owner_share
Expand Down
4 changes: 4 additions & 0 deletions app/models/sharing_strategies/work_package_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def modal_body_component(errors)
end
end

def title
I18n.t(:label_share_work_package)
end

private

def project_member?(share)
Expand Down
3 changes: 2 additions & 1 deletion app/services/shares/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def add_group_memberships(share)
end

def send_notification(share)
# TODO: We should select what sort of notification is sent out based on the shared entity
return unless share.entity.is_a?(WorkPackage)

OpenProject::Notifications.send(OpenProject::Events::WORK_PACKAGE_SHARED,
work_package_member: share,
send_notifications: true)
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,7 @@ en:
label_invite_user: "Invite user"
label_share: "Share"
label_share_project_list: "Share project list"
label_share_work_package: "Share work package"
label_show_hide: "Show/hide"
label_show_hide_n_items: "Show/hide %{count} items"
label_show_all_registered_users: "Show all registered users"
Expand Down
11 changes: 5 additions & 6 deletions lookbook/previews/shares/share_dialog_component_preview.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
module Shares
# @logical_path OpenProject/Shares
class ShareDialogComponentPreview < Lookbook::Preview
def project_query
user = FactoryBot.build_stubbed(:admin)
query = FactoryBot.build_stubbed(:project_query, user:)
strategy = SharingStrategies::ProjectQueryStrategy.new(query, user:)
strategy = SharingStrategies::ProjectQueryStrategy.new(query, user:, query_params: {})
errors = []
shares = []

render(Shares::ShareDialogComponent.new(strategy:, shares:, errors:))
render(Shares::ShareDialogComponent.new(strategy:, errors:, open: true))
end

def work_package
user = FactoryBot.build_stubbed(:admin)
work_package = FactoryBot.build_stubbed(:work_package)
strategy = SharingStrategies::WorkPackageStrategy.new(work_package, user:)
strategy = SharingStrategies::WorkPackageStrategy.new(work_package, user:, query_params: {})
errors = []
shares = []

render(Shares::ShareDialogComponent.new(strategy:, shares:, errors:))
render(Shares::ShareDialogComponent.new(strategy:, errors:, open: true))
end
end
end
Loading

0 comments on commit 616a6c6

Please sign in to comment.