Commit 6c0c1d9b authored by Peter Hegman's avatar Peter Hegman Committed by Vitali Tatarintev

Setup helper methods to pass data to Vue app

Setup helper methods to pass data to project members Vue app.
Also refactor the group_link_entity
parent 86395d32
......@@ -14,7 +14,7 @@ module Groups::GroupMembersHelper
end
def group_group_links_data_json(group_links)
GroupGroupLinkSerializer.new.represent(group_links, { current_user: current_user }).to_json
GroupLink::GroupGroupLinkSerializer.new.represent(group_links, { current_user: current_user }).to_json
end
def members_data_json(group, members)
......
......@@ -26,4 +26,30 @@ module Projects::ProjectMembersHelper
project.group.has_owner?(current_user)
end
def project_group_links_data_json(group_links)
GroupLink::ProjectGroupLinkSerializer.new.represent(group_links, { current_user: current_user }).to_json
end
def project_members_data_json(project, members)
MemberSerializer.new.represent(members, { current_user: current_user, group: project.group }).to_json
end
def project_members_list_data_attributes(project, members)
{
members: project_members_data_json(project, members),
member_path: project_project_member_path(project, ':id'),
source_id: project.id,
can_manage_members: can_manage_project_members?(project)
}
end
def project_group_links_list_data_attributes(project, group_links)
{
members: project_group_links_data_json(group_links),
member_path: project_group_link_path(project, ':id'),
source_id: project.id,
can_manage_members: can_manage_project_members?(project)
}
end
end
# frozen_string_literal: true
class GroupGroupLinkEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :created_at
expose :expires_at do |group_link|
group_link.expires_at&.to_time
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 :human_access, as: :string_value
expose :group_access, as: :integer_value
end
expose :valid_roles do |group_link|
group_link.class.access_options
end
expose :shared_with_group do
expose :avatar_url do |group_link|
group_link.shared_with_group.avatar_url(only_path: false)
end
expose :web_url do |group_link|
group_link.shared_with_group.web_url
end
expose :shared_with_group, merge: true, using: GroupBasicEntity
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
# frozen_string_literal: true
class GroupGroupLinkSerializer < BaseSerializer
entity GroupGroupLinkEntity
end
# frozen_string_literal: true
module GroupLink
class GroupGroupLinkEntity < GroupLink::GroupLinkEntity
include RequestAwareEntity
expose :can_update do |group_link|
can_manage?(group_link)
end
expose :can_remove do |group_link|
can_manage?(group_link)
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
# frozen_string_literal: true
module GroupLink
class GroupGroupLinkSerializer < BaseSerializer
entity GroupLink::GroupGroupLinkEntity
end
end
# frozen_string_literal: true
module GroupLink
class GroupLinkEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :created_at
expose :expires_at do |group_link|
group_link.expires_at&.to_time
end
expose :access_level do
expose :human_access, as: :string_value
expose :group_access, as: :integer_value
end
expose :valid_roles do |group_link|
group_link.class.access_options
end
expose :shared_with_group do
expose :avatar_url do |group_link|
group_link.shared_with_group.avatar_url(only_path: false, size: Member::AVATAR_SIZE)
end
expose :web_url do |group_link|
group_link.shared_with_group.web_url
end
expose :shared_with_group, merge: true, using: GroupBasicEntity
end
end
end
# frozen_string_literal: true
module GroupLink
class ProjectGroupLinkEntity < GroupLink::GroupLinkEntity
include RequestAwareEntity
include Projects::ProjectMembersHelper
expose :can_update do |group_link|
can_manage_project_members?(group_link.project)
end
expose :can_remove do |group_link|
can_manage_project_members?(group_link.project)
end
private
def current_user
options[:current_user]
end
end
end
# frozen_string_literal: true
module GroupLink
class ProjectGroupLinkSerializer < BaseSerializer
entity GroupLink::ProjectGroupLinkEntity
end
end
......@@ -132,7 +132,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end
end
resources :project_members, except: [:show, :new, :edit], constraints: { id: %r{[a-zA-Z./0-9_\-#%+]+} }, concerns: :access_requestable do
resources :project_members, except: [:show, :new, :edit], constraints: { id: %r{[a-zA-Z./0-9_\-#%+:]+} }, concerns: :access_requestable do
collection do
delete :leave
......@@ -219,7 +219,7 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
resources :starrers, only: [:index]
resources :forks, only: [:index, :new, :create]
resources :group_links, only: [:create, :update, :destroy], constraints: { id: /\d+/ }
resources :group_links, only: [:create, :update, :destroy], constraints: { id: /\d+|:id/ }
resource :import, only: [:new, :create, :show]
resource :avatar, only: [:show, :destroy]
......
{
"type": "object",
"allOf": [
{ "$ref": "group_link.json" },
{
"required": [
"can_update",
"can_remove"
],
"properties": {
"can_update": { "type": "boolean" },
"can_remove": { "type": "boolean" }
}
}
]
}
{
"type": "array",
"items": {
"$ref": "entities/group_group_link.json"
"$ref": "group_group_link.json"
}
}
......@@ -4,8 +4,6 @@
"id",
"created_at",
"expires_at",
"can_update",
"can_remove",
"access_level",
"valid_roles"
],
......@@ -13,15 +11,14 @@
"id": { "type": "integer" },
"created_at": { "type": "date-time" },
"expires_at": { "type": ["date-time", "null"] },
"can_update": { "type": "boolean" },
"can_remove": { "type": "boolean" },
"access_level": {
"type": "object",
"required": ["integer_value", "string_value"],
"properties": {
"integer_value": { "type": "integer" },
"string_value": { "type": "string" }
}
},
"additionalProperties": false
},
"valid_roles": { "type": "object" },
"shared_with_group": {
......@@ -34,7 +31,8 @@
"full_path": { "type": "string" },
"avatar_url": { "type": ["string", "null"] },
"web_url": { "type": "string" }
}
},
"additionalProperties": false
}
}
}
{
"type": "object",
"allOf": [
{ "$ref": "group_link.json" },
{
"required": [
"can_update",
"can_remove"
],
"properties": {
"can_update": { "type": "boolean" },
"can_remove": { "type": "boolean" }
}
}
]
}
{
"type": "array",
"items": {
"$ref": "project_group_link.json"
}
}
......@@ -29,7 +29,7 @@ RSpec.describe Groups::GroupMembersHelper do
it 'matches json schema' do
json = helper.group_group_links_data_json(shared_group.shared_with_group_links)
expect(json).to match_schema('group_group_links')
expect(json).to match_schema('group_link/group_group_links')
end
end
......
......@@ -3,6 +3,8 @@
require 'spec_helper'
RSpec.describe Projects::ProjectMembersHelper do
include MembersPresentation
let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project) }
......@@ -142,4 +144,58 @@ RSpec.describe Projects::ProjectMembersHelper do
it { is_expected.to be(false) }
end
end
describe 'project members' do
let_it_be(:project_members) { create_list(:project_member, 1, project: project) }
describe '#project_members_data_json' do
it 'matches json schema' do
expect(helper.project_members_data_json(project, present_members(project_members))).to match_schema('members')
end
end
describe '#project_members_list_data_attributes' do
let(:allow_admin_project) { true }
before do
allow(helper).to receive(:project_project_member_path).with(project, ':id').and_return('/foo-bar/-/project_members/:id')
end
it 'returns expected hash' do
expect(helper.project_members_list_data_attributes(project, present_members(project_members))).to include({
members: helper.project_members_data_json(project, present_members(project_members)),
member_path: '/foo-bar/-/project_members/:id',
source_id: project.id,
can_manage_members: true
})
end
end
end
describe 'project group links' do
let_it_be(:project_group_links) { create_list(:project_group_link, 1, project: project) }
let(:allow_admin_project) { true }
describe '#project_group_links_data_json' do
it 'matches json schema' do
expect(helper.project_group_links_data_json(project_group_links)).to match_schema('group_link/project_group_links')
end
end
describe '#project_group_links_list_data_attributes' do
before do
allow(helper).to receive(:project_group_link_path).with(project, ':id').and_return('/foo-bar/-/group_links/:id')
allow(helper).to receive(:can?).with(current_user, :admin_project_member, project).and_return(true)
end
it 'returns expected hash' do
expect(helper.project_group_links_list_data_attributes(project, project_group_links)).to include({
members: helper.project_group_links_data_json(project_group_links),
member_path: '/foo-bar/-/group_links/:id',
source_id: project.id,
can_manage_members: true
})
end
end
end
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe GroupGroupLinkEntity do
RSpec.describe GroupLink::GroupGroupLinkEntity do
include_context 'group_group_link'
let_it_be(:current_user) { create(:user) }
......@@ -13,15 +13,15 @@ RSpec.describe GroupGroupLinkEntity do
end
it 'matches json schema' do
expect(entity.to_json).to match_schema('entities/group_group_link')
expect(entity.to_json).to match_schema('group_link/group_group_link')
end
context 'a user with :admin_group_member permissions' do
context 'when current user has `: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
it 'exposes `can_update` and `can_remove` as `true`' do
json = entity.as_json
expect(json[:can_update]).to be true
......
......@@ -2,12 +2,12 @@
require 'spec_helper'
RSpec.describe GroupGroupLinkSerializer do
RSpec.describe GroupLink::GroupGroupLinkSerializer do
include_context 'group_group_link'
subject(:json) { described_class.new.represent(shared_group.shared_with_group_links).to_json }
it 'matches json schema' do
expect(json).to match_schema('group_group_links')
expect(json).to match_schema('group_link/group_group_links')
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GroupLink::GroupLinkEntity do
include_context 'group_group_link'
let(:entity) { described_class.new(group_group_link) }
let(:entity_hash) { entity.as_json }
it 'matches json schema' do
expect(entity.to_json).to match_schema('group_link/group_link')
end
it 'correctly exposes `valid_roles`' do
expect(entity_hash[:valid_roles]).to include(Gitlab::Access.options_with_owner)
end
it 'correctly exposes `shared_with_group.avatar_url`' do
avatar_url = 'https://gitlab.com/uploads/-/system/group/avatar/24/foobar.png?width=40'
allow(shared_with_group).to receive(:avatar_url).with(only_path: false, size: Member::AVATAR_SIZE).and_return(avatar_url)
expect(entity_hash[:shared_with_group][:avatar_url]).to match(avatar_url)
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GroupLink::ProjectGroupLinkEntity do
let_it_be(:current_user) { create(:user) }
let_it_be(:project_group_link) { create(:project_group_link) }
let(:entity) { described_class.new(project_group_link) }
before do
allow(entity).to receive(:current_user).and_return(current_user)
end
it 'matches json schema' do
expect(entity.to_json).to match_schema('group_link/project_group_link')
end
context 'when current user has `admin_project_member` permissions' do
before do
allow(entity).to receive(:can?).with(current_user, :admin_project_member, project_group_link.project).and_return(true)
end
it 'exposes `can_update` and `can_remove` as `true`' do
json = entity.as_json
expect(json[:can_update]).to be true
expect(json[:can_remove]).to be true
end
end
end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe GroupLink::ProjectGroupLinkSerializer do
let_it_be(:project_group_links) { create_list(:project_group_link, 1) }
subject(:json) { described_class.new.represent(project_group_links).to_json }
it 'matches json schema' do
expect(json).to match_schema('group_link/project_group_links')
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