<commit_before>require 'spec_helper'
require 'json'
require 'base64'

describe Diplomat::Kv do

  let(:faraday) { fake }

  context "keys" do
    let(:key) { "key" }
    let(:key_url) { "/v1/kv/#{key}" }
    let(:key_params) { "toast" }
    let(:modify_index) { 99 }
    let(:valid_acl_token) { "<KEY>" }

    describe "#get" do
      context "ACLs NOT enabled" do
        it "GET" do
          json = JSON.generate([{
            "Key"   => key,
            "Value" => Base64.encode64(key_params),
            "Flags" => 0
          }])
          faraday.stub(:get).and_return(OpenStruct.new({ body: json }))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.get("key")).to eq("toast")
        end
      end
      context "ACLs enabled, without valid_acl_token" do
        it "GET with ACLs enabled, no valid_acl_token" do
          json = JSON.generate([{
            "Key"   => key,
            "Value" => Base64.encode64("Faraday::ResourceNotFound: the server responded with status 404"),
            "Flags" => 0
          }])
          faraday.stub(:get).and_return(OpenStruct.new({ body: json }))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.get("key")).to eq("Faraday::ResourceNotFound: the server responded with status 404")
        end
      end
      context "ACLs enabled, with valid_acl_token" do
        it "GET with ACLs enabled, valid_acl_token" do
          json = JSON.generate([{
            "Key"   => key,
            "Value" => Base64.encode64(key_params),
            "Flags" => 0
          }])
          faraday.stub(:get).and_return(OpenStruct.new({ body: json }))
          Diplomat.configuration.acl_token = valid_acl_token
          kv = Diplomat::Kv.new(faraday)
          expect(kv.get("key")).to eq("toast")
        end
      end
    end

    describe "#put" do
      context "ACLs NOT enabled" do
        it "PUT" do
          faraday.stub(:put).and_return(OpenStruct.new({ body: "true"}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params)).to eq(true)
          expect(kv.value).to eq(key_params)
        end
        it "PUT with CAS param" do
          options = {:cas => modify_index}
          faraday.stub(:put).and_return(OpenStruct.new({ body: "true"}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params, options)).to eq(true)
          expect(kv.value).to eq(key_params)
        end
      end
      context "ACLs enabled, without valid_acl_token" do
        it "PUT with ACLs enabled, no valid_acl_token" do
          faraday.stub(:put).and_return(OpenStruct.new({ body: "false" }))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params)).to eq(false)
        end
        it "PUT with CAS param, without valid_acl_token" do
          options = {:cas => modify_index}
          faraday.stub(:put).and_return(OpenStruct.new({ body: "false"}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params, options)).to eq(false)
        end
      end
      context "ACLs enabled, with valid_acl_token" do
        it "PUT with ACLs enabled, valid_acl_token" do
          faraday.stub(:put).and_return(OpenStruct.new({ body: "true"}))
          Diplomat.configuration.acl_token = valid_acl_token
          kv = Diplomat::Kv.new(faraday)

          expect(kv.put(key, key_params)).to eq(true)
          expect(kv.value).to eq(key_params)
        end
        it "PUT with CAS param" do
          options = {:cas => modify_index}
          faraday.stub(:put).and_return(OpenStruct.new({ body: "true"}))
          Diplomat.configuration.acl_token = valid_acl_token
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params, options)).to eq(true)
          expect(kv.put).to eq(key_params)
        end
      end
    end

    describe "#delete" do
      context "ACLs NOT enabled" do
        it "DELETE" do
          faraday.stub(:delete).and_return(OpenStruct.new({ status: 200}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.delete(key).status).to eq 200 
        end
      end
      context "ACLs enabled, without valid_acl_token" do
        it "DELETE" do
          faraday.stub(:delete).and_return(OpenStruct.new({ status: 403}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.delete(key).status).to eq 403 
        end
      end
      context "ACLs enabled, with valid_acl_token" do
        it "DELETE" do
          faraday.stub(:delete).and_return(OpenStruct.new({ status: 200}))
          Diplomat.configuration.acl_token = valid_acl_token
          kv = Diplomat::Kv.new(faraday)
          expect(kv.delete(key).status).to eq 200
        end
      end
    end
<commit_msg>Fix typo.<commit_after>require 'spec_helper'
require 'json'
require 'base64'

describe Diplomat::Kv do

  let(:faraday) { fake }

  context "keys" do
    let(:key) { "key" }
    let(:key_url) { "/v1/kv/#{key}" }
    let(:key_params) { "toast" }
    let(:modify_index) { 99 }
    let(:valid_acl_token) { "f45cbd0b-5022-47ab-8640-4eaa7c1f40f1" }

    describe "#get" do
      context "ACLs NOT enabled" do
        it "GET" do
          json = JSON.generate([{
            "Key"   => key,
            "Value" => Base64.encode64(key_params),
            "Flags" => 0
          }])
          faraday.stub(:get).and_return(OpenStruct.new({ body: json }))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.get("key")).to eq("toast")
        end
      end
      context "ACLs enabled, without valid_acl_token" do
        it "GET with ACLs enabled, no valid_acl_token" do
          json = JSON.generate([{
            "Key"   => key,
            "Value" => Base64.encode64("Faraday::ResourceNotFound: the server responded with status 404"),
            "Flags" => 0
          }])
          faraday.stub(:get).and_return(OpenStruct.new({ body: json }))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.get("key")).to eq("Faraday::ResourceNotFound: the server responded with status 404")
        end
      end
      context "ACLs enabled, with valid_acl_token" do
        it "GET with ACLs enabled, valid_acl_token" do
          json = JSON.generate([{
            "Key"   => key,
            "Value" => Base64.encode64(key_params),
            "Flags" => 0
          }])
          faraday.stub(:get).and_return(OpenStruct.new({ body: json }))
          Diplomat.configuration.acl_token = valid_acl_token
          kv = Diplomat::Kv.new(faraday)
          expect(kv.get("key")).to eq("toast")
        end
      end
    end

    describe "#put" do
      context "ACLs NOT enabled" do
        it "PUT" do
          faraday.stub(:put).and_return(OpenStruct.new({ body: "true"}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params)).to eq(true)
          expect(kv.value).to eq(key_params)
        end
        it "PUT with CAS param" do
          options = {:cas => modify_index}
          faraday.stub(:put).and_return(OpenStruct.new({ body: "true"}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params, options)).to eq(true)
          expect(kv.value).to eq(key_params)
        end
      end
      context "ACLs enabled, without valid_acl_token" do
        it "PUT with ACLs enabled, no valid_acl_token" do
          faraday.stub(:put).and_return(OpenStruct.new({ body: "false" }))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params)).to eq(false)
        end
        it "PUT with CAS param, without valid_acl_token" do
          options = {:cas => modify_index}
          faraday.stub(:put).and_return(OpenStruct.new({ body: "false"}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params, options)).to eq(false)
        end
      end
      context "ACLs enabled, with valid_acl_token" do
        it "PUT with ACLs enabled, valid_acl_token" do
          faraday.stub(:put).and_return(OpenStruct.new({ body: "true"}))
          Diplomat.configuration.acl_token = valid_acl_token
          kv = Diplomat::Kv.new(faraday)

          expect(kv.put(key, key_params)).to eq(true)
          expect(kv.value).to eq(key_params)
        end
        it "PUT with CAS param" do
          options = {:cas => modify_index}
          faraday.stub(:put).and_return(OpenStruct.new({ body: "true"}))
          Diplomat.configuration.acl_token = valid_acl_token
          kv = Diplomat::Kv.new(faraday)
          expect(kv.put(key, key_params, options)).to eq(true)
          expect(kv.value).to eq(key_params)
        end
      end
    end

    describe "#delete" do
      context "ACLs NOT enabled" do
        it "DELETE" do
          faraday.stub(:delete).and_return(OpenStruct.new({ status: 200}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.delete(key).status).to eq 200 
        end
      end
      context "ACLs enabled, without valid_acl_token" do
        it "DELETE" do
          faraday.stub(:delete).and_return(OpenStruct.new({ status: 403}))
          kv = Diplomat::Kv.new(faraday)
          expect(kv.delete(key).status).to eq 403 
        end
      end
      context "ACLs enabled, with valid_acl_token" do
        it "DELETE" do
          faraday.stub(:delete).and_return(OpenStruct.new({ status: 200}))
          Diplomat.configuration.acl_token = valid_acl_token
          kv = Diplomat::Kv.new(faraday)
          expect(kv.delete(key).status).to eq 200
        end
      end
    end

{'fraction_non_alphanumeric': 0.130130448615972, 'fraction_numerical': 0.01134796903171068, 'mean_word_length': 2.1697478991596637, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 28, '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': '1626234', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3019, 'n_words': 661}