Commit c09a798c authored by Robert Speicher's avatar Robert Speicher

Merge branch '225598-refactor-invites-view-code' into 'master'

Refactor invites view code

Closes #225598

See merge request gitlab-org/gitlab!37208
parents c8b4fdd8 f493c477
...@@ -4,6 +4,7 @@ class InvitesController < ApplicationController ...@@ -4,6 +4,7 @@ class InvitesController < ApplicationController
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
before_action :member before_action :member
before_action :invite_details
skip_before_action :authenticate_user!, only: :decline skip_before_action :authenticate_user!, only: :decline
helper_method :member?, :current_user_matches_invite? helper_method :member?, :current_user_matches_invite?
...@@ -16,9 +17,8 @@ class InvitesController < ApplicationController ...@@ -16,9 +17,8 @@ class InvitesController < ApplicationController
def accept def accept
if member.accept_invite!(current_user) if member.accept_invite!(current_user)
label, path = source_info(member.source) redirect_to invite_details[:path], notice: _("You have been granted %{member_human_access} access to %{title} %{name}.") %
{ member_human_access: member.human_access, title: invite_details[:title], name: invite_details[:name] }
redirect_to path, notice: _("You have been granted %{member_human_access} access to %{label}.") % { member_human_access: member.human_access, label: label }
else else
redirect_back_or_default(options: { alert: _("The invitation could not be accepted.") }) redirect_back_or_default(options: { alert: _("The invitation could not be accepted.") })
end end
...@@ -26,8 +26,6 @@ class InvitesController < ApplicationController ...@@ -26,8 +26,6 @@ class InvitesController < ApplicationController
def decline def decline
if member.decline_invite! if member.decline_invite!
label, _ = source_info(member.source)
path = path =
if current_user if current_user
dashboard_projects_path dashboard_projects_path
...@@ -35,7 +33,8 @@ class InvitesController < ApplicationController ...@@ -35,7 +33,8 @@ class InvitesController < ApplicationController
new_user_session_path new_user_session_path
end end
redirect_to path, notice: _("You have declined the invitation to join %{label}.") % { label: label } redirect_to path, notice: _("You have declined the invitation to join %{title} %{name}.") %
{ title: invite_details[:title], name: invite_details[:name] }
else else
redirect_back_or_default(options: { alert: _("The invitation could not be declined.") }) redirect_back_or_default(options: { alert: _("The invitation could not be declined.") })
end end
...@@ -79,21 +78,22 @@ class InvitesController < ApplicationController ...@@ -79,21 +78,22 @@ class InvitesController < ApplicationController
redirect_to new_user_session_path, notice: notice redirect_to new_user_session_path, notice: notice
end end
def source_info(source) def invite_details
case source @invite_details ||= case @member.source
when Project when Project
project = member.source {
label = "project #{project.full_name}" name: @member.source.full_name,
path = project_path(project) url: project_url(@member.source),
title: _("project"),
path: project_path(@member.source)
}
when Group when Group
group = member.source {
label = "group #{group.name}" name: @member.source.name,
path = group_path(group) url: group_url(@member.source),
else title: _("group"),
label = "who knows what" path: group_path(@member.source)
path = dashboard_projects_path }
end end
[label, path]
end end
end end
...@@ -2,28 +2,19 @@ ...@@ -2,28 +2,19 @@
%h3.page-title= _("Invitation") %h3.page-title= _("Invitation")
%p %p
You have been invited = _("You have been invited")
- if inviter = @member.created_by - inviter = @member.created_by
by - if inviter
= _("by")
= link_to inviter.name, user_url(inviter) = link_to inviter.name, user_url(inviter)
to join = _("to join %{source_name}") % { source_name: @invite_details[:title] }
- case @member.source
- when Project
- project = @member.source
project
%strong %strong
= link_to project.full_name, project_url(project) = link_to @invite_details[:name], @invite_details[:url]
- when Group = _("as %{role}.") % { role: @member.human_access }
- group = @member.source
group
%strong
= link_to group.name, group_url(group)
as #{@member.human_access}.
- if member? - if member?
%p %p
- member_source = @member.source.is_a?(Group) ? _("group") : _("project") = _("However, you are already a member of this %{member_source}. Sign in using a different account to accept the invitation.") % { member_source: @invite_details[:title] }
= _("However, you are already a member of this %{member_source}. Sign in using a different account to accept the invitation.") % { member_source: member_source }
- if !current_user_matches_invite? - if !current_user_matches_invite?
%p %p
...@@ -32,7 +23,7 @@ ...@@ -32,7 +23,7 @@
- link_to_current_user = link_to(current_user.to_reference, user_url(current_user)) - link_to_current_user = link_to(current_user.to_reference, user_url(current_user))
= _("Note that this invitation was sent to %{mail_to_invite_email}, but you are signed in as %{link_to_current_user} with email %{mail_to_current_user}.").html_safe % { mail_to_invite_email: mail_to_invite_email, mail_to_current_user: mail_to_current_user, link_to_current_user: link_to_current_user } = _("Note that this invitation was sent to %{mail_to_invite_email}, but you are signed in as %{link_to_current_user} with email %{mail_to_current_user}.").html_safe % { mail_to_invite_email: mail_to_invite_email, mail_to_current_user: mail_to_current_user, link_to_current_user: link_to_current_user }
- unless member? - if !member?
.actions .actions
= link_to _("Accept invitation"), accept_invite_url(@token), method: :post, class: "btn btn-success" = link_to _("Accept invitation"), accept_invite_url(@token), method: :post, class: "btn btn-success"
= link_to _("Decline"), decline_invite_url(@token), method: :post, class: "btn btn-danger gl-ml-3" = link_to _("Decline"), decline_invite_url(@token), method: :post, class: "btn btn-danger gl-ml-3"
...@@ -27477,13 +27477,16 @@ msgstr "" ...@@ -27477,13 +27477,16 @@ msgstr ""
msgid "You have been granted %{access_level} access to the %{source_name} %{source_type}." msgid "You have been granted %{access_level} access to the %{source_name} %{source_type}."
msgstr "" msgstr ""
msgid "You have been granted %{member_human_access} access to %{label}." msgid "You have been granted %{member_human_access} access to %{title} %{name}."
msgstr ""
msgid "You have been invited"
msgstr "" msgstr ""
msgid "You have been unsubscribed from this thread." msgid "You have been unsubscribed from this thread."
msgstr "" msgstr ""
msgid "You have declined the invitation to join %{label}." msgid "You have declined the invitation to join %{title} %{name}."
msgstr "" msgstr ""
msgid "You have imported from this project %{numberOfPreviousImports} times before. Each new import will create duplicate issues." msgid "You have imported from this project %{numberOfPreviousImports} times before. Each new import will create duplicate issues."
...@@ -27953,6 +27956,9 @@ msgstr "" ...@@ -27953,6 +27956,9 @@ msgstr ""
msgid "archived:" msgid "archived:"
msgstr "" msgstr ""
msgid "as %{role}."
msgstr ""
msgid "assign yourself" msgid "assign yourself"
msgstr "" msgstr ""
...@@ -29232,6 +29238,9 @@ msgstr "" ...@@ -29232,6 +29238,9 @@ msgstr ""
msgid "to help your contributors communicate effectively!" msgid "to help your contributors communicate effectively!"
msgstr "" msgstr ""
msgid "to join %{source_name}"
msgstr ""
msgid "to list" msgid "to list"
msgstr "" msgstr ""
......
...@@ -34,25 +34,4 @@ RSpec.describe InvitesController do ...@@ -34,25 +34,4 @@ RSpec.describe InvitesController do
expect(flash[:notice]).to be_nil expect(flash[:notice]).to be_nil
end end
end end
describe 'POST #accept' do
it 'accepts user' do
expect do
post :accept, params: { id: token }
end.to change { project_members.include?(user) }.from(false).to(true)
expect(response).to have_gitlab_http_status(:found)
expect(flash[:notice]).to include 'You have been granted'
end
end
describe 'GET #decline' do
it 'declines user' do
get :decline, params: { id: token }
expect { member.reload }.to raise_error ActiveRecord::RecordNotFound
expect(response).to have_gitlab_http_status(:found)
expect(flash[:notice]).to include 'You have declined the invitation to join'
end
end
end end
...@@ -63,6 +63,8 @@ RSpec.describe 'Invites', :aggregate_failures do ...@@ -63,6 +63,8 @@ RSpec.describe 'Invites', :aggregate_failures do
it 'shows message user already a member' do it 'shows message user already a member' do
visit invite_path(group_invite.raw_invite_token) visit invite_path(group_invite.raw_invite_token)
expect(page).to have_link(owner.name, href: user_url(owner))
expect(page).to have_content('However, you are already a member of this group.') expect(page).to have_content('However, you are already a member of this group.')
end end
end end
...@@ -197,8 +199,10 @@ RSpec.describe 'Invites', :aggregate_failures do ...@@ -197,8 +199,10 @@ RSpec.describe 'Invites', :aggregate_failures do
it 'declines application and redirects to dashboard' do it 'declines application and redirects to dashboard' do
page.click_link 'Decline' page.click_link 'Decline'
expect(current_path).to eq(dashboard_projects_path) expect(current_path).to eq(dashboard_projects_path)
expect(page).to have_content('You have declined the invitation to join group Owned.') expect(page).to have_content('You have declined the invitation to join group Owned.')
expect { group_invite.reload }.to raise_error ActiveRecord::RecordNotFound
end end
end end
...@@ -209,7 +213,9 @@ RSpec.describe 'Invites', :aggregate_failures do ...@@ -209,7 +213,9 @@ RSpec.describe 'Invites', :aggregate_failures do
it 'declines application and redirects to sign in page' do it 'declines application and redirects to sign in page' do
expect(current_path).to eq(new_user_session_path) expect(current_path).to eq(new_user_session_path)
expect(page).to have_content('You have declined the invitation to join group Owned.') expect(page).to have_content('You have declined the invitation to join group Owned.')
expect { group_invite.reload }.to raise_error ActiveRecord::RecordNotFound
end end
end end
end end
...@@ -223,9 +229,13 @@ RSpec.describe 'Invites', :aggregate_failures do ...@@ -223,9 +229,13 @@ RSpec.describe 'Invites', :aggregate_failures do
end end
it 'grants access and redirects to group page' do it 'grants access and redirects to group page' do
expect(group.users.include?(user)).to be false
page.click_link 'Accept invitation' page.click_link 'Accept invitation'
expect(current_path).to eq(group_path(group)) expect(current_path).to eq(group_path(group))
expect(page).to have_content('You have been granted Owner access to group Owned.') expect(page).to have_content('You have been granted Owner access to group Owned.')
expect(group.users.include?(user)).to be true
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment