feat: Add APIs for limit check in accounts (#7242)

This commit is contained in:
Shivam Mishra
2023-06-16 08:41:40 +05:30
committed by GitHub
parent 0d465362ac
commit e8a27bea4b
9 changed files with 223 additions and 5 deletions

View File

@@ -110,4 +110,99 @@ RSpec.describe 'Enterprise Billing APIs', type: :request do
end
end
end
describe 'GET /enterprise/api/v1/accounts/{account.id}/limits' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/enterprise/api/v1/accounts/#{account.id}/limits", as: :json
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
context 'when it is an agent' do
it 'returns unauthorized' do
get "/enterprise/api/v1/accounts/#{account.id}/limits",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an admin' do
before do
create(:conversation, account: account)
create(:channel_api, account: account)
InstallationConfig.where(name: 'DEPLOYMENT_ENV').first_or_create(value: 'cloud')
InstallationConfig.where(name: 'CHATWOOT_CLOUD_PLANS').first_or_create(value: [{ 'name': 'Hacker' }])
end
it 'returns the limits if the plan is default' do
account.update!(custom_attributes: { plan_name: 'Hacker' })
get "/enterprise/api/v1/accounts/#{account.id}/limits",
headers: admin.create_new_auth_token,
as: :json
expected_response = {
'id' => account.id,
'limits' => {
'conversation' => {
'allowed' => 500,
'consumed' => 1
},
'non_web_inboxes' => {
'allowed' => 0,
'consumed' => 1
}
}
}
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)).to eq(expected_response)
end
it 'returns nil if the plan is not default' do
account.update!(custom_attributes: { plan_name: 'Startups' })
get "/enterprise/api/v1/accounts/#{account.id}/limits",
headers: admin.create_new_auth_token,
as: :json
expected_response = {
'id' => account.id,
'limits' => {
'conversation' => {},
'non_web_inboxes' => {}
}
}
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)).to eq(expected_response)
end
it 'returns limits if a plan is not configured' do
get "/enterprise/api/v1/accounts/#{account.id}/limits",
headers: admin.create_new_auth_token,
as: :json
expected_response = {
'id' => account.id,
'limits' => {
'conversation' => {
'allowed' => 500,
'consumed' => 1
},
'non_web_inboxes' => {
'allowed' => 0,
'consumed' => 1
}
}
}
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)).to eq(expected_response)
end
end
end
end
end

View File

@@ -16,6 +16,8 @@ describe Enterprise::Billing::HandleStripeEventService do
'id' => 'test', 'product' => 'plan_id', 'name' => 'plan_name'
})
allow(subscription).to receive(:[]).with('quantity').and_return('10')
allow(subscription).to receive(:[]).with('status').and_return('active')
allow(subscription).to receive(:[]).with('current_period_end').and_return(1_686_567_520)
allow(subscription).to receive(:customer).and_return('cus_123')
create(:installation_config, {
name: 'CHATWOOT_CLOUD_PLANS',
@@ -44,7 +46,9 @@ describe Enterprise::Billing::HandleStripeEventService do
'stripe_price_id' => 'test',
'stripe_product_id' => 'plan_id',
'plan_name' => 'Hacker',
'subscribed_quantity' => '10'
'subscribed_quantity' => '10',
'subscription_ends_on' => '2023-06-12T10:58:40.000Z',
'subscription_status' => 'active'
})
end
@@ -57,7 +61,9 @@ describe Enterprise::Billing::HandleStripeEventService do
'stripe_price_id' => 'test',
'stripe_product_id' => 'plan_id',
'plan_name' => 'Hacker',
'subscribed_quantity' => '10'
'subscribed_quantity' => '10',
'subscription_ends_on' => '2023-06-12T10:58:40.000Z',
'subscription_status' => 'active'
})
expect(account).not_to be_feature_enabled('channel_email')
expect(account).not_to be_feature_enabled('help_center')
@@ -94,7 +100,9 @@ describe Enterprise::Billing::HandleStripeEventService do
'stripe_price_id' => 'test',
'stripe_product_id' => 'plan_id_2',
'plan_name' => 'Startups',
'subscribed_quantity' => '10'
'subscribed_quantity' => '10',
'subscription_ends_on' => '2023-06-12T10:58:40.000Z',
'subscription_status' => 'active'
})
expect(account).to be_feature_enabled('channel_email')
expect(account).to be_feature_enabled('help_center')