Skip to content

Commit

Permalink
Refactor extracting stub_orga_relation
Browse files Browse the repository at this point in the history
  • Loading branch information
x4d3 committed Nov 8, 2017
1 parent 168b88d commit 6a09fe5
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ def current_app
@app ||= MnoEnterprise::App.find_one(params[:id])
end

def orga_relation
@orga_relation ||= MnoEnterprise::OrgaRelation.where(organization_id: organization_id, user_id: current_user.id).first
end

def ensure_app_exists
render_not_found('App') unless current_app.present?
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def app_relation(org_id = nil)
rel
end

# Return the organization_id passed as query parameters if the current_user has access to it
# Return the organization_id passed as query parameters if the current_user has access to it
def app_instance_organization_id
return params[:organization_id] if current_user && params[:organization_id].presence && orga_relation
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::OrganizationsController
#==================================================================
# GET /mnoe/jpi/v1/organizations
def index
@organizations ||= current_user.organizations
@organizations ||= MnoEnterprise::Organization.where('users.id': current_user.id)
end

# GET /mnoe/jpi/v1/organizations/1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,39 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::TeamsController
def index
authorize! :read, orga_relation
@teams = MnoEnterprise::Team.includes(:organization, :app_instances, :users).where('organization.id': parent_organization_id)
load_parent_organization(parent_organization_id)
end

# GET /mnoe/jpi/v1/teams/:id
def show
authorize! :read, orga_relation
@team = MnoEnterprise::Team.find_one(params[:id], :organization, :app_instances, :users)
authorize! :read, current_user.orga_relation(@team.organization)
load_parent_organization(@team.organization.id)
end

# POST /mnoe/jpi/v1/organizations/:organization_id/teams
def create
authorize! :manage_teams, orga_relation
@team = MnoEnterprise::Team.create(create_params)
MnoEnterprise::EventLogger.info('team_add', current_user.id, 'Team created', @team) if @team
@team = MnoEnterprise::Team.create!(create_params)
MnoEnterprise::EventLogger.info('team_add', current_user.id, 'Team created', @team)
load_parent_organization(parent_organization_id)
render 'show'
end

# PUT /mnoe/jpi/v1/teams/:id
def update
@team = MnoEnterprise::Team.find_one!(params[:id], :organization)
authorize! :manage_teams, current_user.orga_relation(@team.organization)
organization = @team.organization
authorize! :manage_teams, current_user.orga_relation(organization)
@team.update_attributes!(update_params)
# # Update permissions
if params[:team] && params[:team][:app_instances]
list = params[:team][:app_instances].select { |e| e != {} }
MnoEnterprise::EventLogger.info('team_apps_update', current_user.id, 'Team apps updated', @team,
{apps: list.map{|l| l['name']}})
end
load_parent_organization(organization.id)
render 'show'
end

Expand All @@ -68,14 +74,19 @@ def destroy

private

def load_parent_organization(id)
@parent_organization = MnoEnterprise::Organization.find_one(id, :orga_relations)
end

# Update the members of a team
# Reduce duplication between add and remove
def update_members(action)
@team = MnoEnterprise::Team.find_one!(params[:id], :organization)
authorize! :manage_teams, current_user.orga_relation(@team.organization)

organization = @team.organization
authorize! :manage_teams, current_user.orga_relation(organization)
if params[:team] && params[:team][:users]
id_list = params[:team][:users].map { |h| h[:id].to_i }.compact
# TODO: use a Custom method to update user_ids
user_ids = case action
when :add_user
@team.user_ids | id_list
Expand All @@ -87,7 +98,7 @@ def update_members(action)
{action: action.to_s, user_ids: user_ids})
end
@team = @team.load_required(:organization, :users, :app_instances)
@parent_organization = MnoEnterprise::Organization.find_one(@team.organization.id, :orga_relations)
load_parent_organization(organization.id)
render 'show'
end

Expand Down
6 changes: 3 additions & 3 deletions api/lib/mno_enterprise/csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def self.process(file_path)

if event_type == :added
address = MnoEnterprise::Address.new(
city: row['city'],
city: row['city'],
country_code: row['country'],
street: [row['address1'], row['address2']].reject(&:blank?).join(' '),
state_code: row['state_province'],
Expand All @@ -72,10 +72,10 @@ def self.process(file_path)
user.phone = row['phone']

user.save
orga_relation = MnoEnterprise::OrgaRelation.where(user_id: user.id, organization_id: organization.id).first
orga_relation = MnoEnterprise::OrgaRelation.where('user.id': user.id, 'organization.id': organization.id).first
# Add User as Super Admin to Organization if he is not already in it
unless orga_relation
MnoEnterprise::OrgaRelation.create(user_id: user.id, organization_id: organization.id, role: 'Super Admin')
MnoEnterprise::OrgaRelation.create(user: user, organization: organization, role: 'Super Admin')
end
end
report
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,9 @@ module MnoEnterprise
stub_api_v2(:post, '/users', user2),
stub_api_v2(:patch, "/users/#{user1.id}", user1),

stub_api_v2(:get, '/orga_relations', [], [], { filter: { user_id: user1.id, organization_id: organization1.id }, page: { number: 1, size: 1 } }),
stub_api_v2(:get, '/orga_relations', [orga_relation1], [], { filter: { user_id: user2.id, organization_id: organization2.id }, page: { number: 1, size: 1 } }),

stub_api_v2(:post, '/orga_relations', orga_relation2)
stub_orga_relation(user1, organization1, nil),
stub_orga_relation(user2, organization2, orga_relation2),
stub_api_v2(:post, '/orga_relations', orga_relation1)
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module MnoEnterprise
let(:params) { {organization_id: organization.id, description: 'A Review', foo: 'bar', question_id: 'qid'} }

before do
stub_api_v2(:get, '/orga_relations', [orga_relation], [], {filter: {organization_id: organization.id, user_id: user.id}, page: {number: 1, size: 1}})
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:post, '/answers', answer1)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module MnoEnterprise
let(:params) { {organization_id: organization.id, description: 'A Review', foo: 'bar', feedback_id: 'fid'} }

before do
stub_api_v2(:get, '/orga_relations', [orga_relation], [], {filter: {organization_id: organization.id, user_id: user.id}, page: {number: 1, size: 1}})
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:post, '/comments', comment1)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module MnoEnterprise
let(:params) { {organization_id: organization.id, description: 'A Review', rating: 5} }
let(:feedback) { build(:feedback, id: feedback_id, comments: []) }
before do
stub_api_v2(:get, '/orga_relations', [orga_relation], [], {filter: {organization_id: organization.id, user_id: user.id}, page: {number: 1, size: 1}})
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:post, '/feedbacks', feedback)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module MnoEnterprise
let(:orga_relation) { build(:orga_relation) }
let!(:current_user_stub) { stub_user(user) }

before { stub_api_v2(:get, '/orga_relations', [orga_relation], [], { filter: { 'user.id': user.id, 'organization.id': organization.id }, page: { number: 1, size: 1 } }) }
before { stub_orga_relation(user, organization, orga_relation) }

describe 'GET #index' do
let(:app_instance) { build(:app_instance, status: 'running', under_free_trial: false) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module MnoEnterprise
let(:orga_relation) { build(:orga_relation) }
let!(:organization) { build(:organization) }
before do
stub_api_v2(:get, '/orga_relations', [orga_relation], [], { filter: { 'user.id': user.id, 'organization.uid': organization.uid }, page: { number: 1, size: 1 } })
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:get, '/organizations', [organization], [], { filter: { uid: organization.uid } })
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module MnoEnterprise
let(:params) { {organization_id: organization.id, description: 'A Review', foo: 'bar'} }
let(:question) { build(:question, id: question_id, answers: []) }
before do
stub_api_v2(:get, '/orga_relations', [orga_relation], [], {filter: {organization_id: organization.id, user_id: user.id}, page: {number: 1, size: 1}})
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:post, '/questions', question)
end
subject { post :create, id: app.id, app_question: params }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module MnoEnterprise
describe 'POST #create', focus: true do
let(:params) { {organization_id: organization.id, description: 'A Review', rating: 5} }
before do
stub_api_v2(:get, '/orga_relations', [orga_relation], [], {filter: {organization_id: organization.id, user_id: user.id}, page: {number: 1, size: 1}})
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:post, '/reviews', review)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module MnoEnterprise
let!(:current_user_stub) { stub_user(user) }
before { stub_api_v2(:get, "/organizations/#{organization.id}", organization) }
before do
stub_api_v2(:get, '/orga_relations', [orga_relation], [], { filter: { 'user.id': user.id, 'organization.id': organization.id }, page: { number: 1, size: 1 } })
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:get, '/audit_events', [audit_event], [], { filter: { organization_id: organization.id } })
sign_in user
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module MnoEnterprise
let!(:current_user_stub) { stub_user(user) }

before { sign_in user }
before { stub_api_v2(:get, '/orga_relations', [orga_relation], [], { filter: { 'user.id': user.id, 'organization.uid': organization.uid }, page: { number: 1, size: 1 } }) }
before { stub_orga_relation(user, organization, orga_relation, 'uid') }

describe 'GET index' do

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def hash_for_apps(apps)
before do
sign_in(user)
stub_api_v2(:get, "/organizations", [organization], [], { fields: { organizations: 'id' }, filter: { id: organization.id, 'users.id' => user.id }, page: one_page })
stub_api_v2(:get, '/orga_relations', [orga_relation], [], { filter: { 'user.id': user.id, 'organization.id': organization.id }, page: one_page })
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:get, '/apps', [app], DEPENDENCIES, { _metadata: { organization_id: organization.id }, filter: { active: true } })
stub_api_v2(:get, '/apps', [app], [],
{
Expand Down
Loading

0 comments on commit 6a09fe5

Please sign in to comment.