Skip to content

Commit

Permalink
Adapt AppInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
x4d3 committed May 24, 2017
1 parent 9d3bbad commit 6e422a4
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module MnoEnterprise::Concerns::Controllers::Jpi::V1::AppInstancesController
#==================================================================
# GET /mnoe/jpi/v1/organization/1/apps.json?tg=151452452345
def index
@app_instances = Mno::AppInstance.includes(:owner,:app).where(owner_id: parent_organization.id, status: Mno::AppInstance::ACTIVE_STATUSES).select do |i|
@app_instances = Mno::AppInstance.includes(:app, :owner).where(owner_id: parent_organization.id, status: Mno::AppInstance::ACTIVE_STATUSES).to_a.select do |i|
can?(:access,i)
end
end
Expand All @@ -34,12 +34,12 @@ def create

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

if app_instance
authorize! :manage_app_instances, app_instance.owner
MnoEnterprise::EventLogger.info('app_destroy', current_user.id, 'App destroyed', app_instance)
app_instance.destroy
if @app_instance
authorize! :manage_app_instances, @app_instance.owner
MnoEnterprise::EventLogger.info('app_destroy', current_user.id, 'App destroyed', @app_instance)
@app_instance = @app_instance.terminate.first
end

head :accepted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,84 @@ module MnoEnterprise
include MnoEnterprise::TestingSupport::JpiV1TestHelper
render_views
routes { MnoEnterprise::Engine.routes }
before { request.env["HTTP_ACCEPT"] = 'application/json' }
before { request.env['HTTP_ACCEPT'] = 'application/json' }

# Stub ability
let!(:ability) { stub_ability }
before { allow(ability).to receive(:can?).with(any_args).and_return(true) }

# Stub user and user call
let(:user) { build(:user) }
before { api_stub_for(get: "/users/#{user.id}", response: from_api(user)) }
# Stub organization + associations
let(:organization) { build(:organization) }
before { allow_any_instance_of(MnoEnterprise::User).to receive(:organizations).and_return([organization]) }
let!(:current_user_stub) { stub_api_v2(:get, "/users/#{user.id}", user, %i(deletion_requests organizations orga_relations dashboards)) }

# Stub organization and association
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 { stub_api_v2(:get, "/organizations/#{organization.id}", organization, %i(orga_relations users)) }

describe 'GET #index' do
let(:app_instance) { build(:app_instance, status: "running") }
let(:app_instance) { build(:app_instance, under_free_trial: false) }
let(:app_instances) { instance_double('Her::Model::Relation') }

before do
allow(organization).to receive(:app_instances).and_return(app_instances)
# Only active instances
allow(app_instances).to receive(:active).and_return(app_instances)
# Updated since last tick
allow(app_instances).to receive(:where).and_return([app_instance])
end
let(:app_instance) { build(:app_instance, status: 'running' , under_free_trial: false) }

before { stub_api_v2(:get, '/app_instances', [app_instance], [:app, :owner], {filter: {owner_id: organization.id, status: Mno::AppInstance::ACTIVE_STATUSES}}) }

before { sign_in user }
let(:timestamp) { nil }
subject { get :index, organization_id: organization.id, timestamp: timestamp }
subject { get :index, organization_id: organization.id }

it_behaves_like "jpi v1 protected action"
it_behaves_like 'jpi v1 protected action'

describe 'all' do
it { subject; expect(assigns(:app_instances)).to eq([app_instance]) }

it 'filter only active instances' do
expect(app_instances).to receive(:active)
it {
subject
end
end

context 'with timestamp' do
let(:timestamp) { Time.current.to_i }

it 'filter updates since last request' do
expect(app_instances).to receive(:where).with("updated_at.gt" => Time.at(timestamp))
subject
end
# TODO: Test that the rendered json is the expected one
# expect(assigns(:app_instances)).to eq([app_instance])
assert_requested(:get, api_v2_url('/app_instances', [:app, :owner], {filter: {owner_id: organization.id, status: Mno::AppInstance::ACTIVE_STATUSES}}))
}
end

context 'without access to the app_instance' do
before { allow(ability).to receive(:can?).with(any_args).and_return(false) }
before { subject }
it { expect(assigns(:app_instances)).to be_empty }
it {
subject
expect(assigns(:app_instances)).to be_empty
}
end
end

describe 'POST #create' do
let(:app) { build(:app, nid: 'my-app' ) }
let(:app_instance) { build(:app_instance, app: app, owner: organization ) }
before { stub_audit_events }
let(:app) { build(:app, nid: 'my-app') }
let(:app_instance) { build(:app_instance, app: app, owner: organization) }
subject { post :create, organization_id: organization.id, nid: 'my-app' }

before do
api_stub_for(post: "/organizations/#{organization.id}/app_instances", response: from_api(app_instance))
api_stub_for(get: "/organizations/#{organization.id}/app_instances", response: from_api([app_instance]))
stub_api_v2(:post, '/app_instances/provision', app_instance)
end
it_behaves_like "jpi v1 protected action"
it_behaves_like 'jpi v1 protected action'
it {
subject
}
end

describe 'DELETE #destroy' do
before { stub_audit_events }
let(:app_instance) { build(:app_instance) }
before { api_stub_for(get: "/app_instances/#{app_instance.id}", respond_with: app_instance)}
before { api_stub_for(delete: "/app_instances/#{app_instance.id}", response: ->{ app_instance.status = 'terminated'; from_api(app_instance) }) }
let(:terminated_app_instance) { build(:app_instance, id: app_instance.id, status: 'terminated') }
before { stub_api_v2(:get, "/app_instances/#{app_instance.id}", app_instance)}
before { stub_api_v2(:delete, "/app_instances/#{app_instance.id}/terminate", terminated_app_instance)}
before { sign_in user }
subject { delete :destroy, id: app_instance.id }

it_behaves_like "jpi v1 protected action"
it_behaves_like 'jpi v1 protected action'

it { subject; expect(app_instance.status).to eq('terminated')}
it {
subject
assert_requested_api_v2(:delete, "/app_instances/#{app_instance.id}/terminate")
expect(assigns(:app_instance).status).to eq('terminated')
}
end
end
end
2 changes: 1 addition & 1 deletion core/app/models/mno/app_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class AppInstance < BaseResource
property :created_at, type: :time
property :updated_at, type: :time
# PATCH <api_root>/app_instances/:id/terminate
custom_endpoint :terminate, on: :member, request_method: :patch
custom_endpoint :terminate, on: :member, request_method: :delete
custom_endpoint :provision, on: :collection, request_method: :post

#==============================================================
Expand Down
4 changes: 4 additions & 0 deletions core/app/models/mno/arrears_situation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Mno
class ArrearsSituation < BaseResource
end
end
4 changes: 4 additions & 0 deletions core/app/models/mno/identity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Mno
class Identity < BaseResource
end
end
15 changes: 15 additions & 0 deletions core/app/models/mno/invoice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Mno
class Invoice < BaseResource
# this invoice covers
def period_label
return '' unless self.started_at && self.ended_at
"#{self.started_at.strftime("%b %d,%Y")} to #{self.ended_at.strftime("%b %d,%Y")}"
end

# Return true if the invoice has been paid
# false otherwise
def paid?
!self.paid_at.blank?
end
end
end
6 changes: 6 additions & 0 deletions core/app/models/mno/tenant_invoice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Mno
class TenantInvoice < BaseResource
property :created_at, type: :time
property :updated_at, type: :time
end
end
31 changes: 15 additions & 16 deletions core/lib/mno_enterprise/testing_support/factories/app_instances.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@

FactoryGirl.define do
factory :mno_enterprise_app_instance, :class => 'AppInstance' do
factory :app_instance, class: MnoEnterprise::AppInstance do
sequence(:id)
sequence(:uid) { |n| "bla#{1}.mcube.co" }
name "SomeApp"
status "running"
created_at 3.days.ago
updated_at 1.hour.ago

factory :app_instance, class: Mno::AppInstance do
sequence(:id)
sequence(:uid) { |n| "bla#{1}.mcube.co" }
name 'SomeApp'
status 'running'
created_at 3.days.ago
updated_at 1.hour.ago
started_at 3.days.ago
stack "cube"
stack 'cube'
terminated_at nil
stopped_at nil
billing_type "hourly"
billing_type 'hourly'
autostop_at nil
autostop_interval nil
next_status nil
soa_enabled true

app { build(:app).attributes }
owner { build(:organization).attributes }

# Properly build the resource with Her
initialize_with { new(attributes).tap { |e| e.clear_attribute_changes! } }
oauth_keys_valid true
oauth_company 'oauth company'
app { build(:app) }
owner { build(:organization) }

end
end
end
4 changes: 1 addition & 3 deletions core/lib/mno_enterprise/testing_support/factories/apps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

FactoryGirl.define do
factory :app, class: Mno::App do
sequence(:id) { |n| n }
sequence(:id, &:to_s)
sequence(:name) { |n| "TestApp#{n}" }
nid { name.parameterize }
description "This is a description"
Expand Down Expand Up @@ -41,7 +41,5 @@
factory :cloud_app, traits: [:cloud]
factory :connector_app, traits: [:connector]

# Properly build the resource with Her
initialize_with { new(attributes) }
end
end
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
FactoryGirl.define do
factory :mno_enterprise_arrears_situation, :class => 'MnoEnterprise::ArrearsSituation' do

factory :arrears_situation, class: MnoEnterprise::ArrearsSituation do
factory :arrears_situation, class: Mno::ArrearsSituation do
sequence(:name) { |n| "Team#{n}" }
payment Money.new(5680,'AUD')
category 'payment_failed'
status 'pending'

# Properly build the resource with Her
initialize_with { new(attributes).tap { |e| e.clear_attribute_changes! } }
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryGirl.define do
factory :mno_enterprise_audit_event, :class => 'AuditEvent' do

factory :audit_event, class: MnoEnterprise::AuditEvent do
factory :audit_event, class: Mno::AuditEvent do
sequence(:key) { |n| "event-fab3#{n}" }
created_at 2.days.ago
updated_at 2.days.ago
Expand All @@ -12,8 +12,6 @@
organization { {name: 'Org'} }
user { {name: 'John', surname: 'Doe'} }

# Properly build the resource with Her
initialize_with { new(attributes).tap { |e| e.clear_attribute_changes! } }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

FactoryGirl.define do
factory :mno_enterprise_credit_card, :class => 'CreditCard' do


factory :credit_card, class: Mno::CreditCard do
sequence(:id, &:to_s)
organization_id 265
Expand All @@ -24,8 +22,6 @@
billing_country 'AU'
verification_value 'CVV'

# Properly build the resource with Her
initialize_with { new(attributes)}
end


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
# See http://stackoverflow.com/questions/10032760/how-to-define-an-array-hash-in-factory-girl
FactoryGirl.define do

factory :deletion_request, class: MnoEnterprise::DeletionRequest do
sequence(:id)
factory :deletion_request, class: Mno::DeletionRequest do
sequence(:id, &:to_s)
sequence(:token) { |n| "1dfg567fda44f87ds89F7DS8#{n}" }
status 'pending'
created_at Time.now
updated_at Time.now
# Properly build the resource with Her
initialize_with { new(attributes).tap { |e| e.clear_attribute_changes! } }
end

end
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
FactoryGirl.define do
factory :identity, class: MnoEnterprise::Identity do
factory :identity, class: Mno::Identity do
provider 'someprovider'
uid '123456'

# Properly build the resource with Her
initialize_with { new(attributes).tap { |e| e.clear_attribute_changes! } }
end
end
Loading

0 comments on commit 6e422a4

Please sign in to comment.