mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-11-03 20:48:07 +00:00 
			
		
		
		
	Chore: Added tests for subscriptions_controller (#483)
Added tests for subscriptions_controller ref: #37
This commit is contained in:
		@@ -1,16 +1,14 @@
 | 
			
		||||
class Api::BaseController < ApplicationController
 | 
			
		||||
  respond_to :json
 | 
			
		||||
  before_action :authenticate_user!
 | 
			
		||||
  unless Rails.env.development?
 | 
			
		||||
    rescue_from StandardError do |exception|
 | 
			
		||||
      Raven.capture_exception(exception)
 | 
			
		||||
      render json: { error: '500 error', message: exception.message }.to_json, status: 500
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  private
 | 
			
		||||
 | 
			
		||||
  def set_conversation
 | 
			
		||||
    @conversation ||= current_account.conversations.find_by(display_id: params[:conversation_id])
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def check_billing_enabled
 | 
			
		||||
    raise ActionController::RoutingError, 'Not Found' unless ENV['BILLING_ENABLED']
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
class Api::V1::SubscriptionsController < ApplicationController
 | 
			
		||||
class Api::V1::SubscriptionsController < Api::BaseController
 | 
			
		||||
  skip_before_action :check_subscription
 | 
			
		||||
 | 
			
		||||
  before_action :check_billing_enabled
 | 
			
		||||
 | 
			
		||||
  def index
 | 
			
		||||
    render json: current_account.subscription_data
 | 
			
		||||
  end
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@ class Api::V1::WebhooksController < ApplicationController
 | 
			
		||||
  skip_before_action :check_subscription
 | 
			
		||||
 | 
			
		||||
  before_action :login_from_basic_auth, only: [:chargebee]
 | 
			
		||||
  before_action :check_billing_enabled, only: [:chargebee]
 | 
			
		||||
  def chargebee
 | 
			
		||||
    chargebee_consumer.consume
 | 
			
		||||
    head :ok
 | 
			
		||||
 
 | 
			
		||||
@@ -67,7 +67,7 @@ class ApplicationController < ActionController::Base
 | 
			
		||||
  def check_subscription
 | 
			
		||||
    # This block is left over from the initial version of chatwoot
 | 
			
		||||
    # We might reuse this later in the hosted version of chatwoot.
 | 
			
		||||
    return unless ENV['BILLING_ENABLED']
 | 
			
		||||
    return if !ENV['BILLING_ENABLED'] || !current_user
 | 
			
		||||
 | 
			
		||||
    if current_subscription.trial? && current_subscription.expiry < Date.current
 | 
			
		||||
      render json: { error: 'Trial Expired' }, status: :trial_expired
 | 
			
		||||
 
 | 
			
		||||
@@ -94,7 +94,6 @@ Rails.application.routes.draw do
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      # this block is only required if subscription via chargebee is enabled
 | 
			
		||||
      if ENV['BILLING_ENABLED']
 | 
			
		||||
      resources :subscriptions, only: [:index] do
 | 
			
		||||
        collection do
 | 
			
		||||
          get :summary
 | 
			
		||||
@@ -108,7 +107,6 @@ Rails.application.routes.draw do
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  namespace :twitter do
 | 
			
		||||
    resource :authorization, only: [:create]
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										46
									
								
								spec/controllers/api/v1/subscriptions_controller_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								spec/controllers/api/v1/subscriptions_controller_spec.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
RSpec.describe 'Subscriptions API', type: :request do
 | 
			
		||||
  let(:account) { create(:account) }
 | 
			
		||||
 | 
			
		||||
  describe 'GET /api/v1/subscriptions' do
 | 
			
		||||
    context 'when it is an unauthenticated user' do
 | 
			
		||||
      it 'returns unauthorized' do
 | 
			
		||||
        ENV['BILLING_ENABLED'] = 'true'
 | 
			
		||||
 | 
			
		||||
        get '/api/v1/subscriptions'
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(:unauthorized)
 | 
			
		||||
 | 
			
		||||
        ENV['BILLING_ENABLED'] = nil
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when it is an authenticated user' do
 | 
			
		||||
      let(:agent) { create(:user, account: account, role: :agent) }
 | 
			
		||||
 | 
			
		||||
      it 'returns all subscriptions' do
 | 
			
		||||
        ENV['BILLING_ENABLED'] = 'true'
 | 
			
		||||
 | 
			
		||||
        get '/api/v1/subscriptions',
 | 
			
		||||
            headers: agent.create_new_auth_token,
 | 
			
		||||
            as: :json
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(:success)
 | 
			
		||||
        expect(JSON.parse(response.body)).to eq(account.subscription_data.as_json)
 | 
			
		||||
 | 
			
		||||
        ENV['BILLING_ENABLED'] = nil
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'throws 404 error if env variable is not set' do
 | 
			
		||||
        ENV['BILLING_ENABLED'] = nil
 | 
			
		||||
 | 
			
		||||
        get '/api/v1/subscriptions',
 | 
			
		||||
            headers: agent.create_new_auth_token,
 | 
			
		||||
            as: :json
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(:not_found)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user