Commit ff19f8cf authored by Peter Hegman's avatar Peter Hegman Committed by Dmytro Zaporozhets (DZ)

Use `invite?` method to check for invite

Instead of checking for presence of `user`. This is a better check
because it is possible to have a member with an orphaned user.
parent 2ff0ba5f
...@@ -14,7 +14,7 @@ module Groups::GroupMembersHelper ...@@ -14,7 +14,7 @@ module Groups::GroupMembersHelper
end end
def linked_groups_data_json(group_links) def linked_groups_data_json(group_links)
GroupGroupLinkSerializer.new.represent(group_links).to_json GroupGroupLinkSerializer.new.represent(group_links, { current_user: current_user }).to_json
end end
def members_data_json(group, members) def members_data_json(group, members)
...@@ -47,10 +47,10 @@ module Groups::GroupMembersHelper ...@@ -47,10 +47,10 @@ module Groups::GroupMembersHelper
} }
}.merge(member_created_by_data(member.created_by)) }.merge(member_created_by_data(member.created_by))
if user.present? if member.invite?
data[:user] = member_user_data(user)
else
data[:invite] = member_invite_data(member) data[:invite] = member_invite_data(member)
elsif user.present?
data[:user] = member_user_data(user)
end end
data data
...@@ -77,6 +77,17 @@ module Groups::GroupMembersHelper ...@@ -77,6 +77,17 @@ module Groups::GroupMembersHelper
avatar_url: avatar_icon_for_user(user, AVATAR_SIZE), avatar_url: avatar_icon_for_user(user, AVATAR_SIZE),
blocked: user.blocked?, blocked: user.blocked?,
two_factor_enabled: user.two_factor_enabled? two_factor_enabled: user.two_factor_enabled?
}.merge(member_user_status_data(user.status))
end
def member_user_status_data(status)
return {} unless status.present?
{
status: {
emoji: status.emoji,
message_html: status.message_html
}
} }
end end
......
# frozen_string_literal: true # frozen_string_literal: true
class GroupGroupLinkEntity < Grape::Entity class GroupGroupLinkEntity < Grape::Entity
include RequestAwareEntity
expose :id expose :id
expose :created_at expose :created_at
expose :expires_at do |group_link| expose :expires_at do |group_link|
group_link.expires_at&.to_time group_link.expires_at&.to_time
end end
expose :can_update do |group_link|
can_manage?(group_link)
end
expose :can_remove do |group_link|
can_manage?(group_link)
end
expose :access_level do expose :access_level do
expose :human_access, as: :string_value expose :human_access, as: :string_value
expose :group_access, as: :integer_value expose :group_access, as: :integer_value
...@@ -23,4 +33,14 @@ class GroupGroupLinkEntity < Grape::Entity ...@@ -23,4 +33,14 @@ class GroupGroupLinkEntity < Grape::Entity
expose :shared_with_group, merge: true, using: GroupBasicEntity expose :shared_with_group, merge: true, using: GroupBasicEntity
end end
private
def current_user
options[:current_user]
end
def can_manage?(group_link)
can?(current_user, :admin_group_member, group_link.shared_group)
end
end end
{ {
"type": "object", "type": "object",
"required": ["id", "created_at", "expires_at", "access_level"], "required": ["id", "created_at", "expires_at", "can_update", "can_remove", "access_level"],
"properties": { "properties": {
"id": { "type": "integer" }, "id": { "type": "integer" },
"created_at": { "type": "date-time" }, "created_at": { "type": "date-time" },
"expires_at": { "type": ["date-time", "null"] }, "expires_at": { "type": ["date-time", "null"] },
"can_update": { "type": "boolean" },
"can_remove": { "type": "boolean" },
"access_level": { "access_level": {
"type": "object", "type": "object",
"required": ["integer_value", "string_value"], "required": ["integer_value", "string_value"],
......
...@@ -62,7 +62,18 @@ ...@@ -62,7 +62,18 @@
"avatar_url": { "type": ["string", "null"] }, "avatar_url": { "type": ["string", "null"] },
"web_url": { "type": "string" }, "web_url": { "type": "string" },
"blocked": { "type": "boolean" }, "blocked": { "type": "boolean" },
"two_factor_enabled": { "type": "boolean" } "two_factor_enabled": { "type": "boolean" },
"status": {
"type": "object",
"required": [
"emoji",
"message_html"
],
"properties": {
"emoji": { "type": "string" },
"message_html": { "type": "string" }
}
}
} }
}, },
"invite": { "invite": {
......
...@@ -5,6 +5,8 @@ require "spec_helper" ...@@ -5,6 +5,8 @@ require "spec_helper"
RSpec.describe Groups::GroupMembersHelper do RSpec.describe Groups::GroupMembersHelper do
include MembersPresentation include MembersPresentation
let_it_be(:current_user) { create(:user) }
describe '.group_member_select_options' do describe '.group_member_select_options' do
let(:group) { create(:group) } let(:group) { create(:group) }
...@@ -20,6 +22,10 @@ RSpec.describe Groups::GroupMembersHelper do ...@@ -20,6 +22,10 @@ RSpec.describe Groups::GroupMembersHelper do
describe '#linked_groups_data_json' do describe '#linked_groups_data_json' do
include_context 'group_group_link' include_context 'group_group_link'
before do
allow(helper).to receive(:current_user).and_return(current_user)
end
it 'matches json schema' do it 'matches json schema' do
json = helper.linked_groups_data_json(shared_group.shared_with_group_links) json = helper.linked_groups_data_json(shared_group.shared_with_group_links)
...@@ -28,7 +34,6 @@ RSpec.describe Groups::GroupMembersHelper do ...@@ -28,7 +34,6 @@ RSpec.describe Groups::GroupMembersHelper do
end end
describe '#members_data_json' do describe '#members_data_json' do
let(:current_user) { create(:user) }
let(:group) { create(:group) } let(:group) { create(:group) }
before do before do
...@@ -48,6 +53,14 @@ RSpec.describe Groups::GroupMembersHelper do ...@@ -48,6 +53,14 @@ RSpec.describe Groups::GroupMembersHelper do
let(:group_member) { create(:group_member, group: group, created_by: current_user) } let(:group_member) { create(:group_member, group: group, created_by: current_user) }
it_behaves_like 'group_members.json' it_behaves_like 'group_members.json'
context 'with user status set' do
let(:user) { create(:user) }
let!(:status) { create(:user_status, user: user) }
let(:group_member) { create(:group_member, group: group, user: user, created_by: current_user) }
it_behaves_like 'group_members.json'
end
end end
context 'for an invited group member' do context 'for an invited group member' do
......
...@@ -5,9 +5,27 @@ require 'spec_helper' ...@@ -5,9 +5,27 @@ require 'spec_helper'
RSpec.describe GroupGroupLinkEntity do RSpec.describe GroupGroupLinkEntity do
include_context 'group_group_link' include_context 'group_group_link'
subject(:json) { described_class.new(group_group_link).to_json } let_it_be(:current_user) { create(:user) }
let(:entity) { described_class.new(group_group_link) }
before do
allow(entity).to receive(:current_user).and_return(current_user)
end
it 'matches json schema' do it 'matches json schema' do
expect(json).to match_schema('entities/group_group_link') expect(entity.to_json).to match_schema('entities/group_group_link')
end
context 'a user with :admin_group_member permissions' do
before do
allow(entity).to receive(:can?).with(current_user, :admin_group_member, shared_group).and_return(true)
end
it 'sets `can_update` and `can_remove` to `true`' do
json = entity.as_json
expect(json[:can_update]).to be true
expect(json[:can_remove]).to be true
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