<commit_before>require 'spec_helper'

describe Dnsimple::Client, ".webhooks" do

  subject { described_class.new(base_url: "https://api.dnsimple.test", access_token: "a1b2c3").webhooks }

  describe "#webhooks" do
    let(:account_id) { 1010 }

    before do
      stub_request(:get, %r[/v2/#{account_id}/webhooks])
          .to_return(read_http_fixture("listWebhooks/success.http"))
    end

    it "builds the correct request" do
      subject.webhooks(account_id)

      expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/webhooks")
          .with(headers: { 'Accept' => 'application/json' })
    end

    it "supports extra request options" do
      subject.webhooks(account_id, query: { foo: "bar" })

      expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/webhooks?foo=bar")
    end

    it "returns the webhooks" do
      response = subject.webhooks(account_id)

      expect(response).to be_a(Dnsimple::CollectionResponse)
      expect(response.data).to be_a(Array)
      expect(response.data.size).to eq(2)

      response.data.each do |result|
        expect(result).to be_a(Dnsimple::Struct::Webhook)
        expect(result.id).to be_a(Fixnum)
      end
    end
  end

  describe "#create_webhook" do
    let(:account_id) { 1010 }

    before do
      stub_request(:post, %r[/v2/#{account_id}/webhooks$])
          .to_return(read_http_fixture("createWebhook/created.http"))
    end

    let(:attributes) { {url: "https://webhook.test"} }

    it "builds the correct request" do
      subject.create_webhook(account_id, attributes)

      expect(WebMock).to have_requested(:post, "https://api.dnsimple.test/v2/#{account_id}/webhooks")
          .with(body: attributes)
          .with(headers: { 'Accept' => 'application/json' })
    end

    it "returns the webhook" do
      response = subject.create_webhook(account_id, attributes)
      expect(response).to be_a(Dnsimple::Response)

      result = response.data
      expect(result).to be_a(Dnsimple::Struct::Webhook)
      expect(result.id).to be_a(Fixnum)
    end
  end

  describe "#webhook" do
    let(:account_id) { 1010 }

    before do
      stub_request(:get, %r[/v2/#{account_id}/webhooks/.+$])
          .to_return(read_http_fixture("getWebhook/success.http"))
    end

    it "builds the correct request" do
      subject.webhook(account_id, webhook = 1)

      expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/webhooks/#{webhook}")
          .with(headers: { 'Accept' => 'application/json' })
    end

    it "returns the webhook" do
      response = subject.webhook(account_id, 0)
      expect(response).to be_a(Dnsimple::Response)

      result = response.data
      expect(result).to be_a(Dnsimple::Struct::Webhook)
      expect(result.id).to  eq(1)
      expect(result.url).to eq("https://webhook.test")
    end

    context "when the webhook does not exist" do
      it "raises NotFoundError" do
        stub_request(:get, %r[/v2])
            .to_return(read_http_fixture("notfound-webhook.http"))

        expect {
          subject.webhook(account_id, 0)
        }.to raise_error(Dnsimple::NotFoundError)<commit_msg>Increase code clarity for Webhooks specs<commit_after>require 'spec_helper'

describe Dnsimple::Client, ".webhooks" do

  subject { described_class.new(base_url: "https://api.dnsimple.test", access_token: "a1b2c3").webhooks }

  describe "#webhooks" do
    let(:account_id) { 1010 }

    before do
      stub_request(:get, %r[/v2/#{account_id}/webhooks])
          .to_return(read_http_fixture("listWebhooks/success.http"))
    end

    it "builds the correct request" do
      subject.webhooks(account_id)

      expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/webhooks")
          .with(headers: { 'Accept' => 'application/json' })
    end

    it "supports extra request options" do
      subject.webhooks(account_id, query: { foo: "bar" })

      expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/webhooks?foo=bar")
    end

    it "returns the webhooks" do
      response = subject.webhooks(account_id)

      expect(response).to be_a(Dnsimple::CollectionResponse)
      expect(response.data).to be_a(Array)
      expect(response.data.size).to eq(2)

      response.data.each do |result|
        expect(result).to be_a(Dnsimple::Struct::Webhook)
        expect(result.id).to be_a(Fixnum)
      end
    end
  end

  describe "#create_webhook" do
    let(:account_id) { 1010 }

    before do
      stub_request(:post, %r[/v2/#{account_id}/webhooks$])
          .to_return(read_http_fixture("createWebhook/created.http"))
    end

    let(:attributes) { {url: "https://webhook.test"} }

    it "builds the correct request" do
      subject.create_webhook(account_id, attributes)

      expect(WebMock).to have_requested(:post, "https://api.dnsimple.test/v2/#{account_id}/webhooks")
          .with(body: attributes)
          .with(headers: { 'Accept' => 'application/json' })
    end

    it "returns the webhook" do
      response = subject.create_webhook(account_id, attributes)
      expect(response).to be_a(Dnsimple::Response)

      result = response.data
      expect(result).to be_a(Dnsimple::Struct::Webhook)
      expect(result.id).to be_a(Fixnum)
    end
  end

  describe "#webhook" do
    let(:account_id) { 1010 }

    before do
      stub_request(:get, %r[/v2/#{account_id}/webhooks/.+$])
          .to_return(read_http_fixture("getWebhook/success.http"))
    end

    it "builds the correct request" do
      subject.webhook(account_id, webhook_id = 1)

      expect(WebMock).to have_requested(:get, "https://api.dnsimple.test/v2/#{account_id}/webhooks/#{webhook_id}")
          .with(headers: { 'Accept' => 'application/json' })
    end

    it "returns the webhook" do
      webhook_id = 0
      response   = subject.webhook(account_id, webhook_id)
      expect(response).to be_a(Dnsimple::Response)

      result = response.data
      expect(result).to be_a(Dnsimple::Struct::Webhook)
      expect(result.id).to  eq(1)
      expect(result.url).to eq("https://webhook.test")
    end

    context "when the webhook does not exist" do
      it "raises NotFoundError" do
        webhook_id = 0
        stub_request(:get, %r[/v2])
            .to_return(read_http_fixture("notfound-webhook.http"))

        expect {
          subject.webhook(account_id, webhook_id)
        }.to raise_error(Dnsimple::NotFoundError)
{'fraction_non_alphanumeric': 0.1380052251421546, 'fraction_numerical': 0.008606116489933917, 'mean_word_length': 3.15581098339719, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 14, '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': '5985441', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2190, 'n_words': 384}