Skip to content

Commit

Permalink
Fix Specs
Browse files Browse the repository at this point in the history
  • Loading branch information
x4d3 committed Nov 9, 2017
1 parent 6a09fe5 commit 455932a
Show file tree
Hide file tree
Showing 24 changed files with 84 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Jpi::V1::Admin::AppInstancesController < Jpi::V1::Admin::BaseResourceContr

# DELETE /mnoe/jpi/v1/app_instances/1
def destroy
app_instance = MnoEnterprise::AppInstance.find_one(params[:id])
app_instance = MnoEnterprise::AppInstance.find_one(params[:id], :owner)

if app_instance
MnoEnterprise::EventLogger.info('app_destroy', current_user.id, 'App destroyed', app_instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ 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 @@ -24,8 +24,8 @@ def index
# POST /mnoe/jpi/v1/organization/1/app_instances
def create
authorize! :manage_app_instances, orga_relation
input = { data: { attributes: { app_nid: params[:nid], owner_id: parent_organization_id, owner_type: 'Organization' } } }
app_instance = MnoEnterprise::AppInstance.provision!(input)
app_instance = MnoEnterprise::AppInstance.provision!(params[:nid], parent_organization_id, 'Organization' )
app_instance = app_instance.load_required(:owner)
MnoEnterprise::EventLogger.info('app_add', current_user.id, 'App added', app_instance)
head :created
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController
extend ActiveSupport::Concern

INCLUDED_DEPENDENCIES = %i(organizations orga_relations dashboards teams orga_relations.user orga_relations.organization)
INCLUDED_DEPENDENCIES = %i(organizations orga_relations orga_relations.organization dashboards teams orga_relations.user orga_relations.organization)

#==================================================================
# Included methods
Expand All @@ -28,7 +28,7 @@ def update
changed_attributes = current_user.changed_attributes
current_user.save!
current_user.refresh_user_cache
MnoEnterprise::EventLogger.info('user_update', current_user.id, 'User update', @user, changed_attributes)
MnoEnterprise::EventLogger.info('user_update', current_user.id, 'User update', current_user, changed_attributes)
@user = current_user.load_required(*INCLUDED_DEPENDENCIES)
render :show
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index
query = MnoEnterprise::Product.apply_query_params(params).includes(DEPENDENCIES).where(active: true)

# Ensure prices include organization-specific markups/discounts
if params[:organization_id] && parent_organization
if params[:organization_id] && orga_relation
query = query.with_params(_metadata: { organization_id: parent_organization_id })
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::SubscriptionEventsControll
#==================================================================
# GET /mnoe/jpi/v1/organizations/1/subscriptions/xyz/subscription_events
def index
authorize! :manage_app_instances, parent_organization
@subscription_events = fetch_subscription_events(parent_organization.id, params[:subscription_id])
authorize! :manage_app_instances, orga_relation
@subscription_events = fetch_subscription_events(parent_organization_id, params[:subscription_id])
end

# GET /mnoe/jpi/v1/organizations/1/subscriptions/xyz/subscription_events/id
def show
authorize! :manage_app_instances, parent_organization
@subscription_event = fetch_subscription_event(parent_organization.id, params[:subscription_id], params[:id])
authorize! :manage_app_instances, orga_relation
@subscription_event = fetch_subscription_event(parent_organization_id, params[:subscription_id], params[:id])
end

protected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::SubscriptionsController
#==================================================================
# GET /mnoe/jpi/v1/organizations/1/subscriptions
def index
authorize! :manage_app_instances, parent_organization
authorize! :manage_app_instances, orga_relation
@subscriptions = fetch_subscriptions(parent_organization_id)
end

# GET /mnoe/jpi/v1/organizations/1/subscriptions/id
def show
authorize! :manage_app_instances, parent_organization
authorize! :manage_app_instances, orga_relation
@subscription = fetch_subscription(parent_organization_id, params[:id])
end

# POST /mnoe/jpi/v1/organizations/1/subscriptions
def create
authorize! :manage_app_instances, parent_organization
authorize! :manage_app_instances, orga_relation

subscription = MnoEnterprise::Subscription.new(subscription_update_params)
subscription.relationships.organization = MnoEnterprise::Organization.new(id: parent_organization_id)
Expand All @@ -40,7 +40,7 @@ def create

# PUT /mnoe/jpi/v1/organizations/1/subscriptions/abc
def update
authorize! :manage_app_instances, parent_organization
authorize! :manage_app_instances, orga_relation

subscription = MnoEnterprise::Subscription.where(organization_id: parent_organization_id, id: params[:id]).first
return render_not_found('subscription') unless subscription
Expand All @@ -61,7 +61,7 @@ def update

# POST /mnoe/jpi/v1/organizations/1/subscriptions/abc/cancel
def cancel
authorize! :manage_app_instances, parent_organization
authorize! :manage_app_instances, orga_relation

subscription = MnoEnterprise::Subscription.where(organization_id: parent_organization_id, id: params[:id]).first
return render_not_found('subscription') unless subscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module MnoEnterprise::Concerns::Controllers::PagesController
# TODO: Access + existence checks could be added in the future. This is not
# mandatory as Mno Enterprise will do it anyway
def launch
app_instance = MnoEnterprise::AppInstance.where(uid: params[:id]).first
app_instance = MnoEnterprise::AppInstance.where(uid: params[:id]).includes(:owner).first
MnoEnterprise::EventLogger.info('app_launch', current_user.id, 'App launched', app_instance)
redirect_to MnoEnterprise.router.launch_url(params[:id], {wtk: MnoEnterprise.jwt(user_id: current_user.uid)}.reverse_merge(request.query_parameters))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def create

app_instances = []
params[:apps].each do |product_name|
app_instance = @organization.provision_app_instance!(product_name)
app_instance = MnoEnterprise::AppInstance.provision!(product_name, parent_organization_id, 'Organization' )
app_instance = app_instance.load_required(:owner)
app_instances << app_instance
MnoEnterprise::EventLogger.info('app_add', current_user.id, 'App added', app_instance)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module MnoEnterprise
before { request.env['HTTP_ACCEPT'] = 'application/json' }
before { stub_audit_events }

let(:user) { build(:user, :admin, :with_organizations) }
let(:user) { build(:user, :admin) }
let(:organization) { build(:organization) }
let!(:current_user_stub) { stub_user(user) }

before do
Expand All @@ -19,9 +20,9 @@ module MnoEnterprise

describe 'DELETE #destroy' do
# Stub AppInstance
let(:app_instance) { build(:app_instance) }
let(:app_instance) { build(:app_instance, owner: organization) }

before { stub_api_v2(:get, "/app_instances/#{app_instance.id}", app_instance) }
before { stub_api_v2(:get, "/app_instances/#{app_instance.id}", app_instance, [:owner]) }
let!(:stub) { stub_api_v2(:delete, "/app_instances/#{app_instance.id}/terminate") }

subject { delete :destroy, id: app_instance.id }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module MnoEnterprise
let!(:stub) { stub_api_v2(:post, '/app_instances/provision', app_instance) }
before do
sign_in user
stub_api_v2(:get, "/app_instances/#{app_instance.id}", app_instance, [:owner])
end

it do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ module MnoEnterprise
before { sign_in user }

# Stub organization
let(:orga_relation) { build(:orga_relation) }
let(:orga_relation) { build(:orga_relation, role: 'Admin') }
let!(:organization) { build(:organization) }
before do
stub_orga_relation(user, organization, orga_relation)
stub_orga_relation(user, organization, orga_relation, 'uid')
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 @@ -87,17 +87,25 @@ def hash_for(res)
end

# Stub user retrieval
let!(:user) { build(:user, :with_organizations, :kpi_enabled) }
let(:organization) { build(:organization) }
let!(:user) { build(:user, :kpi_enabled, organizations: [organization], orga_relations: [orga_relation]) }

let!(:orga_relation) { build(:orga_relation, organization: organization) }

let!(:current_user_stub) { stub_user(user) }

before do
stub_user(user, MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController::INCLUDED_DEPENDENCIES)
stub_orga_relation(user, organization, orga_relation)
stub_deletion_requests(user, [])
end

describe 'GET #show' do
subject { get :show }

before do
stub_user(user, MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController::INCLUDED_DEPENDENCIES)
end

describe 'guest' do
it 'is successful' do
subject
Expand Down Expand Up @@ -133,7 +141,7 @@ def hash_for(res)
updated_user.attributes = attrs
stub_api_v2(:patch, "/users/#{user.id}", updated_user)
# user reload
stub_user(updated_user)
stub_user(updated_user, MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController::INCLUDED_DEPENDENCIES)
}

subject { put :update, user: attrs }
Expand All @@ -153,24 +161,26 @@ def hash_for(res)
end

describe 'PUT #register_developer' do
before { stub_api_v2(:patch, "/users/#{user.id}/create_api_credentials", user) }
#user reload
before { stub_user(user) }
before { sign_in user }
subject { put :register_developer }

before do
sign_in user
stub_api_v2(:patch, "/users/#{user.id}/create_api_credentials", user)
#user reload
stub_user(user, MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController::INCLUDED_DEPENDENCIES)
subject
end
describe 'logged in' do
before { subject }
it { expect(response).to be_success }
end
end

describe 'PUT #update_password' do
subject { put :update_password, user: attrs }

let(:attrs) { { current_password: 'password', password: 'blablabla', password_confirmation: 'blablabla' } }
let!(:update_password_stub) { stub_api_v2(:patch, "/users/#{user.id}/update_password", user) }
#user reload
before { stub_user(user) }
subject { put :update_password, user: attrs }
before { stub_user(user, MnoEnterprise::Concerns::Controllers::Jpi::V1::CurrentUsersController::INCLUDED_DEPENDENCIES) }

it_behaves_like 'a user management action'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module MnoEnterprise
let!(:organization_stub) { stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(users orga_invites orga_relations credit_card invoices)) }
# Stub user and user call
let!(:current_user_stub) { stub_user(user) }

let(:role) { 'Admin' }
before { sign_in user }

# Advanced features - currently disabled
Expand Down Expand Up @@ -68,7 +68,7 @@ module MnoEnterprise

it_behaves_like 'jpi v1 protected action'

before { stub_orga_relation(user, organization, build(:orga_relation)) }
before { stub_orga_relation(user, organization, build(:orga_relation, role: role)) }

context 'success' do
before { subject }
Expand All @@ -80,21 +80,17 @@ module MnoEnterprise
end

context 'contains invoices' do
subject { get :show, id: organization.id }

let(:money) { Money.new(0, 'AUD') }
subject { get :show, id: organization.id }
let(:role) { 'Super Admin' }
let(:member_role) { role }
let(:member) { build(:user, id: user.id, email: user.email) }

let(:money) { Money.new(0, 'AUD') }
before do
allow_any_instance_of(MnoEnterprise::Organization).to receive(:current_billing).and_return(money)
allow_any_instance_of(MnoEnterprise::Organization).to receive(:current_credit).and_return(money)
organization.invoices << invoice
stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(users orga_invites orga_relations credit_card invoices))
end


it 'renders the list of invoices' do
subject
expect(JSON.parse(response.body)['invoices']).to eq(partial_hash_for_invoices(organization))
Expand All @@ -107,6 +103,7 @@ module MnoEnterprise
let(:orga_relation) { build(:orga_relation) }
subject { post :create, organization: params }
before do
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:post, '/organizations', organization)
stub_api_v2(:post, '/orga_relations', orga_relation)
stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(users orga_invites orga_relations))
Expand Down Expand Up @@ -309,7 +306,6 @@ module MnoEnterprise
stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(users orga_invites orga_relations credit_card invoices))
end
before do
stub_orga_relation(user, organization, orga_relation)
stub_api_v2(:get, "/orga_relations", [member_orga_relation], [], { filter: { organization_id: organization.id, user_id: member.id }, page: one_page })
stub_api_v2(:post, "/orga_relations/#{member_orga_relation.id}", orga_relation)
stub_api_v2(:patch, "/orga_relations/#{member_orga_relation.id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@ module MnoEnterprise
before { request.env['HTTP_ACCEPT'] = 'application/json' }

before(:all) do
Settings.merge!(dashboard: {marketplace: {local_products: true}})
Settings.merge!(dashboard: { marketplace: { local_products: true } })
Rails.application.reload_routes!
end

# Stub user and user call
let!(:user) { build(:user) }
let(:organization) { build(:organization) }
let!(:current_user_stub) { stub_user(user) }
before do
stub_orga_relation(user, organization, build(:orga_relation))
end

describe 'GET #index' do
subject { get :index, params }

let(:params) { {} }
let(:product) { build(:product) }
let(:organization) {
o = build(:organization, orga_relations: [])
o.orga_relations << build(:orga_relation, user_id: user.id, organization_id: o.id, role: 'Super Admin')
o
}

before { sign_in user }

Expand All @@ -40,7 +39,7 @@ module MnoEnterprise
before { stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(orga_relations users)) }
before do
stub_api_v2(:get, "/products", [product],
[:'values.field', :assets, :categories, :product_pricings, :product_contracts], { filter: { active: true }, _metadata: { organization_id: organization.id } })
[:'values.field', :assets, :categories, :product_pricings, :product_contracts], { filter: { active: true }, _metadata: { organization_id: organization.id } })
end

it_behaves_like 'jpi v1 protected action'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ module MnoEnterprise
let!(:current_user_stub) { stub_user(user) }

# Stub organization and association
let!(:organization) { build(:organization, orga_relations: []) }
let!(:orga_relation) { organization.orga_relations << build(:orga_relation, user_id: user.id, organization_id: organization.id, role: 'Super Admin') }
let!(:organization_stub) { stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(orga_relations users)) }
let!(:organization) { build(:organization) }
let!(:orga_relation) { build(:orga_relation, role: 'Super Admin') }
before do
stub_orga_relation(user, organization, orga_relation)
end

describe 'GET #index' do
let(:subscription) { build(:subscription) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ module MnoEnterprise
let!(:current_user_stub) { stub_user(user) }

# Stub organization and association
let!(:organization) { build(:organization, orga_relations: []) }
let!(:orga_relation) { organization.orga_relations << build(:orga_relation, user_id: user.id, organization_id: organization.id, role: 'Super Admin') }
let!(:organization_stub) { stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(orga_relations users)) }

let!(:organization) { build(:organization) }
let!(:orga_relation) { build(:orga_relation, role: 'Super Admin') }
before do
stub_orga_relation(user, organization, orga_relation)
end
# Stub license_assignments association
before { allow_any_instance_of(MnoEnterprise::Subscription).to receive(:license_assignments).and_return([]) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def hash_for_team(team)
subject { put :add_users, id: team.id, team: {users: [{id: user.id}]} }
before do
stub_user(user)
stub_orga_relation
stub_orga_relation(user, organization, build(:orga_relation))
stub_api_v2(:get, "/apps", [app])
stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(orga_relations))
stub_api_v2(:get, "/teams/#{team.id}", team, %i(organization))
Expand All @@ -71,8 +71,6 @@ def hash_for_team(team)
stub_audit_events
end



context 'success' do
before { subject }

Expand Down
Loading

0 comments on commit 455932a

Please sign in to comment.