Sha256: 87e286b865a70213d9dbd8f55b3dee956d86a02881fad618d5d1e63a806264f7

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require 'spec_helper'

describe Keen::ScopedKey do
  let(:api_key) { "ab428324dbdbcfe744" }
  let(:bad_api_key) { "badbadbadbad" }
  let(:data) { {
   "filters" => [{
      "property_name" => "accountId",
      "operator" => "eq",
      "property_value" => "123456"
    }]
  }}
  let(:new_scoped_key) { Keen::ScopedKey.new(api_key, data) }

  describe "constructor" do
    it "should retain the api_key" do
      new_scoped_key.api_key.should == api_key
    end

    it "should retain the data" do
      new_scoped_key.data.should == data
    end
  end

  describe "encrypt! and decrypt!" do
    it "should encrypt and hex encode the data using the api key" do
      encrypted_str = new_scoped_key.encrypt!
      other_api_key = Keen::ScopedKey.decrypt!(api_key, encrypted_str)
      other_api_key.data.should == data
    end

    describe "when an IV is not provided" do
      it "should not produce the same encrypted key text" do
        new_scoped_key.encrypt!.should_not == (new_scoped_key.encrypt!)
      end
    end

    describe "when an IV is provided" do
      it "should produce the same encrypted key text for a " do
        iv = "\0" * 16
        new_scoped_key.encrypt!(iv).should == (new_scoped_key.encrypt!(iv))
      end

      it "should raise error when an invalid IV is supplied" do
        iv = "fakeiv"
        expect { new_scoped_key.encrypt!(iv) }.to raise_error(OpenSSL::Cipher::CipherError)
      end
    end

    it "should not decrypt the scoped key with a bad api key" do
      encrypted_str = new_scoped_key.encrypt!
      expect {
        other_api_key = Keen::ScopedKey.decrypt!(bad_api_key, encrypted_str)
      }.to raise_error(OpenSSL::Cipher::CipherError)
    end

    it "should properly encrypt with master keys that are 32 characters long" do
       master_key = "\0" * 32
       scoped_key = Keen::ScopedKey.new(master_key, data).encrypt!
       Keen::ScopedKey.decrypt!(master_key, scoped_key).data.should == data
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
keen-0.9.8 spec/keen/scoped_key_old_spec.rb