lundi 30 mars 2020

How can these scenarios related to Octokit be checked in Rspec

I am trying to cover these exceptions raised by Octokit when something undesired happens. These are some of the scenarios that needs to be covered in my spec, however I am not getting any relevant tests to refer for this: Octokit::AbuseDetected

Octokit::UnprocessableEntity

Octokit::TooManyRequests

Also I am finding no related resources for the same. This is the file that has these.

# frozen_string_literal: true

require 'asset_ingester/helpers/logging'
require 'asset_ingester/helpers/poller'
require 'octokit'
require 'colorize'

module AssetIngester
  module SCM
    class GithubScm
      include AssetIngester::Helpers::Logging

      API_ENDPOINT = 'https://github.cerner.com/api/v3/'
      RESULTS_PER_PAGE = 100
      DEFAULT_WAIT = 60
      MAX_RETRY_ATTEMPTS = 5

      def initialize
        @client = Octokit::Client.new(access_token: ENV['GIT_ACCESS_TOKEN'], api_endpoint: API_ENDPOINT)
      end

      def search_code(search_terms)
        search_results = []

        search_terms.each do |search_term|
          retry_on_abuse  do
            response = @client.search_code(search_term, per_page: RESULTS_PER_PAGE)
            logger.info "Found '#{search_term}' code search results: #{response[:total_count]}"
            search_results.concat(response[:items])
            last_response = @client.last_response

            retry_on_abuse  do
              while last_response.rels[:next]
                last_response = last_response.rels[:next].get(query: {per_page: RESULTS_PER_PAGE})
                search_results.concat(last_response.data[:items])
              end
            end
          end
        end

        search_results
      end

      def all_organizations
        logger.info 'Searching all organizations...'
        search_results = []

        retry_on_abuse do
          response = @client.all_organizations
          search_results.concat(response)
          last_response = @client.last_response

          retry_on_abuse  do
            while last_response.rels[:next]
              last_response = last_response.rels[:next].get(query: {})
              search_results.concat(last_response.data)
            end
          end
        end

        search_results
      end

      def search_repos(search_term)
        search_results = []

        retry_on_abuse do
          response = @client.search_repositories(search_term, per_page: RESULTS_PER_PAGE)
          logger.info "Found '#{search_term}' repository search results: #{response[:total_count]}"
          search_results.concat(response[:items])
          last_response = @client.last_response

          retry_on_abuse  do
            while last_response.rels[:next]
              last_response = last_response.rels[:next].get(query: {per_page: RESULTS_PER_PAGE})
              search_results.concat(last_response.data[:items])
            end
          end
        end

        search_results
      end

      private

      def retry_on_abuse
        retry_template = 'Retrying after %d seconds. Response status: %s'
        Helpers::Poller.poll(DEFAULT_WAIT, MAX_RETRY_ATTEMPTS) do
          yield
        rescue Octokit::AbuseDetected => e
          logger.warn "Abuse detected. #{format(retry_template, DEFAULT_WAIT, e.response_headers[:status])}"
          :re_poll
        rescue Octokit::UnprocessableEntity => e
          logger.warn "Insufficient permissions. Response status: #{e.response_headers[:status]}"
        rescue Octokit::TooManyRequests => e
          logger.warn "API Request Limit Reached. #{format(retry_template, DEFAULT_WAIT, e.response_headers[:status])}"
          :re_poll
        end
      end
    end
  end
end

The method retry_on_abuse handles these scenarios. How do I write specs for these?

Aucun commentaire:

Enregistrer un commentaire