<commit_before>require 'spec_helper'
require 'fernet'

describe Fernet do
  let(:token_data) do
    { :email => '<EMAIL>', :id => '123', :arbitrary => 'data' }
  end

  let(:secret)     { '<KEY> }
  let(:bad_secret) { '<KEY> }

  it 'can verify tokens it generates' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(secret, token) do |verifier|
      verifier.data['email'] == '<EMAIL>'
    end.should be_true
  end

  it 'fails with a bad secret' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(bad_secret, token) do |verifier|
      verifier.data['email'] == '<EMAIL>'
    end.should be_false
  end

  it 'fails with a bad custom verification' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(bad_secret, token) do |verifier|
      verifier.data['email'] == '<EMAIL>'
    end.should be_false
  end

  it 'fails if the token is too old' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(bad_secret, token) do |verifier|
      verifier.ttl = 0
    end.should be_false
  end

  it 'verifies without a custom verification' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(secret, token).should be_true
  end

  it 'can TTL enforcement' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(secret, token) do |verifier|
      def verifier.now
        Time.now + 99999999999
      end
      verifier.enforce_ttl = false
      true
    end.should be_true
  end

  it 'generates without custom data' do
    token = Fernet.generate(secret)

    Fernet.verify(secret, token).should be_true
  end

  it 'can encrypt the payload' do<commit_msg>Exercise global config in test<commit_after>require 'spec_helper'
require 'fernet'

describe Fernet do
  let(:token_data) do
    { :email => '<EMAIL>', :id => '123', :arbitrary => 'data' }
  end

  let(:secret)     { '<KEY> }
  let(:bad_secret) { '<KEY> }

  it 'can verify tokens it generates' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(secret, token) do |verifier|
      verifier.data['email'] == '<EMAIL>'
    end.should be_true
  end

  it 'fails with a bad secret' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(bad_secret, token) do |verifier|
      verifier.data['email'] == '<EMAIL>'
    end.should be_false
  end

  it 'fails with a bad custom verification' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(bad_secret, token) do |verifier|
      verifier.data['email'] == '<EMAIL>'
    end.should be_false
  end

  it 'fails if the token is too old' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(bad_secret, token) do |verifier|
      verifier.ttl = 0
    end.should be_false
  end

  it 'verifies without a custom verification' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(secret, token).should be_true
  end

  it 'can ignore TTL enforcement' do
    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(secret, token) do |verifier|
      def verifier.now
        Time.now + 99999999999
      end
      verifier.enforce_ttl = false
      true
    end.should be_true
  end

  it 'can ignore TTL enforcement via global config' do
    Fernet::Configuration.run do |config|
      config.enforce_ttl = false
    end

    token = Fernet.generate(secret) do |generator|
      generator.data = token_data
    end

    Fernet.verify(secret, token) do |verifier|
      def verifier.now
        Time.now + 99999999999
      end
      true
    end.should be_true
  end

  it 'generates without custom data' do
    token = Fernet.generate(secret)

    Fernet.verify(secret, token).should be_true
  end

  it 'can encrypt the payload' do
{'fraction_non_alphanumeric': 0.10030537937514682, 'fraction_numerical': 0.009631195677707306, 'mean_word_length': 2.836036036036036, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, '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': '6395277', 'n_tokens_mistral': 1427, 'n_tokens_neox': 1370, 'n_words': 395}