<commit_before>require 'spec_helper'

describe RainForest::CloudFront do
  let(:credentials){ double("AWS Crendetials") }
  let(:client){ double("AWS Client") }

  let(:cloudfront_distribution){ 
    {
      etag: 'ETAG',
      distribution: {
        distribution_config: {
          comment: nil,
          logging: {
            bucket: nil,
            prefix: nil
          },
          origins: {
            items: [
              {origin_path: 'OLD_ORIGIN' }
            ]
          }
        }
      }
    }
  }

  before do
    expect(::Aws::CloudFront::Client).to receive(:new).with(access_key_id: ENV["RAIN_FOREST_AWS_AKID"], secret_access_key: ENV["RAIN_FOREST_AWS_SECRET"], region: 'cloudfront.amazonaws.com').and_return(client).at_least(:once)
  end

  describe "#update_origin" do
    it "works with default public-read permissions" do
      expected_config = { 
        comment: '',
        logging: {
          bucket: '',
          prefix: ''
        },
        origins: {
          items: [
            {origin_path: '/NEW_ORIGIN' }
          ]
        }
      }

      expect(client).to receive(:get_distribution).with(id: 'DISTRIBUTION_ID').and_return(cloudfront_distribution)
      expect(client).to receive(:update_distribution).with(id: 'DISTRIBUTION_ID', if_match: 'ETAG', distribution_config: expected_config)

      success, message = RainForest::CloudFront.update_origin('DISTRIBUTION_ID', "/NEW_ORIGIN")

      expect(message).to eq(nil)
      expect(success).to eq(true)
    end

=begin
    it "handles errors" do
      expect(client).to receive(:put_object).with(bucket: ENV["RAIN_FOREST_AWS_BUCKET"], key: "STORAGE-KEY", body: "Some test data...", acl: "public-read").and_raise(Exception.new("KABOOM!"))

      success, message = RainForest::S3.write("STORAGE-KEY", "Some test data...")

      expect(success).to eq(false)
      expect(message).to eq("KABOOM!")
    end
=end
  end
end<commit_msg>add_CloudFront - add error test<commit_after>require 'spec_helper'

describe RainForest::CloudFront do
  let(:credentials){ double("AWS Crendetials") }
  let(:client){ double("AWS Client") }

  let(:cloudfront_distribution){ 
    {
      etag: 'ETAG',
      distribution: {
        distribution_config: {
          comment: nil,
          logging: {
            bucket: nil,
            prefix: nil
          },
          origins: {
            items: [
              {origin_path: 'OLD_ORIGIN' }
            ]
          }
        }
      }
    }
  }

  before do
    expect(::Aws::CloudFront::Client).to receive(:new).with(access_key_id: ENV["RAIN_FOREST_AWS_AKID"], secret_access_key: ENV["RAIN_FOREST_AWS_SECRET"], region: 'cloudfront.amazonaws.com').and_return(client).at_least(:once)
  end

  describe "#update_origin" do
    it "works with default public-read permissions" do
      expected_config = { 
        comment: '',
        logging: {
          bucket: '',
          prefix: ''
        },
        origins: {
          items: [
            {origin_path: '/NEW_ORIGIN' }
          ]
        }
      }

      expect(client).to receive(:get_distribution).with(id: 'DISTRIBUTION_ID').and_return(cloudfront_distribution)
      expect(client).to receive(:update_distribution).with(id: 'DISTRIBUTION_ID', if_match: 'ETAG', distribution_config: expected_config)

      success, message = RainForest::CloudFront.update_origin('DISTRIBUTION_ID', "/NEW_ORIGIN")

      expect(message).to eq(nil)
      expect(success).to eq(true)
    end

    it "handles errors" do
      expect(client).to receive(:get_distribution).with(id: 'DISTRIBUTION_ID').and_return(cloudfront_distribution)
      expect(client).to receive(:update_distribution).and_raise(Exception.new("KABOOM!"))

      success, message = RainForest::CloudFront.update_origin('DISTRIBUTION_ID', "/NEW_ORIGIN")

      expect(message).to eq("KABOOM!")
      expect(success).to eq(false)
    end
  end
end
{'fraction_non_alphanumeric': 0.1331277306604986, 'fraction_numerical': 0.00025700334104343357, 'mean_word_length': 2.699619771863118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7133188', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1225, 'n_words': 202}