require 'spec_helper'
describe Auth0::Api::V2::ClientGrants do
  before :all do
    dummy_instance = DummyClass.new
    dummy_instance.extend(Auth0::Api::V2::ClientGrants)
    @instance = dummy_instance
  end

  context '.client_grants' do
    it { expect(@instance).to respond_to(:client_grants) }
    it { expect(@instance).to respond_to(:get_all_client_grants) }

    it 'is expected to get /api/v2/client-grants/' do
      expect(@instance).to receive(:get).with(
        '/api/v2/client-grants', {
        client_id: nil,
        allow_any_organization: nil,
        audience: nil,
        page: nil,
        per_page: nil
      })
      expect { @instance.client_grants }.not_to raise_error
    end

    it 'is expected to send get /api/v2/client-grants/ with client_id and audience' do
      audience = "https://samples.auth0.com/api/v2/"

      expect(@instance).to receive(:get).with(
        '/api/v2/client-grants', {
        client_id: '1',
        allow_any_organization: nil,
        audience: audience,
        page: nil,
        per_page: nil
      })
      expect { @instance.client_grants(client_id: '1', audience: audience ) }.not_to raise_error
    end

    it 'is expected to send get /api/v2/client-grants/ with pagination' do
      expect(@instance).to receive(:get).with(
        '/api/v2/client-grants', {
        client_id: nil,
        allow_any_organization: nil,
        audience: nil,
        page: 1,
        per_page: 2
      })
      expect { @instance.client_grants(page: 1, per_page: 2) }.not_to raise_error
    end

    it 'is expected to send get /api/v2/client-grants/ with allow_any_organization' do
      expect(@instance).to receive(:get).with(
        '/api/v2/client-grants', {
        client_id: nil,
        allow_any_organization: true,
        audience: nil,
        page: nil,
        per_page: nil
      })
      expect { @instance.client_grants(allow_any_organization: true) }.not_to raise_error
    end
  end

  context '.create_client_grant' do
    it { expect(@instance).to respond_to(:create_client_grant) }
    it 'is expected to send post to /api/v2/client-grants' do
      expect(@instance).to receive(:post).with('/api/v2/client-grants', { opt: 'test body' })
      expect { @instance.create_client_grant(opt: 'test body') }.not_to raise_error
    end
  end

  context '.delete_client_grant' do
    it { expect(@instance).to respond_to(:delete_client_grant) }
    it 'is expected to send delete to /api/v2/client-grants/1' do
      expect(@instance).to receive(:delete).with('/api/v2/client-grants/1')
      expect { @instance.delete_client_grant('1') }.not_to raise_error
    end
    it { expect { @instance.delete_client_grant('') }.to raise_error 'Must specify a client grant id' }
  end

  context '.patch_client_grant' do
    it { expect(@instance).to respond_to(:patch_client_grant) }
    it { expect(@instance).to respond_to(:update_client_grant) }
    it 'is expected to send patch to /api/v2/client-grants/1' do
      expect(@instance).to receive(:patch).with('/api/v2/client-grants/1', 'test body')
      expect { @instance.patch_client_grant('1', 'test body') }.not_to raise_error
    end
    it { expect { @instance.patch_client_grant('', nil) }.to raise_error 'Must specify a client grant id' }
    it { expect { @instance.patch_client_grant('some', nil) }.to raise_error 'Must specify a valid body' }
  end

  context '.get_client_grants_organizations' do
    it { expect(@instance).to respond_to(:get_client_grants_organizations) }
    it 'is expected to send get to /api/v2/client-grants/organizations' do
      expect(@instance).to receive(:get).with('/api/v2/client-grants/1/organizations', {
        per_page: nil,
        page: nil,
        from: nil,
        take: nil,
        include_totals: nil
      })
      expect { @instance.get_client_grants_organizations('1') }.not_to raise_error
    end
    it { expect { @instance.get_client_grants_organizations('') }.to raise_error 'Must specify a client grant id' }
  end
end
