<commit_before>#-------------------------------------------------------------------------------
# # Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#   http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
# ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
# PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
#
# See the Apache License, Version 2.0 for the specific language
# governing permissions and limitations under the License.
#-------------------------------------------------------------------------------

require_relative '../support/fake_data'

require 'spec_helper'

# Load constants used by the fake token endpoint.
include FakeData

describe ADAL::AuthenticationContext do
  let(:auth_ctx) { ADAL::AuthenticationContext.new(AUTHORITY, TENANT) }
  let(:client_cred) { ADAL::ClientCredential.new(CLIENT_ID, CLIENT_SECRET) }

  describe '#acquire_token_with_authorization_code' do
    it 'should return a SuccessResponse when successful' do
      token_response = auth_ctx.acquire_token_with_authorization_code(
        AUTH_CODE, REDIRECT_URI, client_cred, RESOURCE)
      expect(token_response).to be_a(ADAL::SuccessResponse)
    end

    it 'should return an ErrorResponse when unauthorized' do
      token_response = auth_ctx.acquire_token_with_authorization_code(
        AUTH_CODE, 'bad', client_cred, RESOURCE)
      expect(token_response).to be_a(ADAL::ErrorResponse)
    end

    it 'should fail if any of the required parameters are nil' do
      expect do
        auth_ctx.acquire_token_with_authorization_code(
          nil, REDIRECT_URI, client_cred, RESOURCE)
      end.to raise_error(ArgumentError)
    end
  end

  describe '#acquire_token_for_client' do
    it 'should return a SuccessResponse when successful' do
      response = auth_ctx.acquire_token_for_client(RESOURCE, client_cred)
      expect(response).to be_a(ADAL::SuccessResponse)
    end

    it 'should return an ErrorResponse when unauthorized' do
      token_response = auth_ctx.acquire_token_for_client(RESOURCE, 'bad')
      expect(token_response).to be_a(ADAL::ErrorResponse)
    end

    it 'should fail if any of the parameters are nil' do
      expect do
        auth_ctx.acquire_token_for_client(RESOURCE, nil)
      end.to raise_error(ArgumentError)
    end
  end

  describe '#acquire_token_with_refresh_token' do
    it 'should return a SuccessResponse when successful' do
      token_response = auth_ctx.acquire_token_with_refresh_token(
        REFRESH_TOKEN, client_cred, RESOURCE)
      expect(token_response).to be_a(ADAL::SuccessResponse)
    end

    it 'should return an ErrorResponse when unauthorized' do
      token_response = auth_ctx.acquire_token_with_refresh_token(
        REFRESH_TOKEN, 'bad', RESOURCE)
      expect(token_response).to be_a(ADAL::ErrorResponse)
    end

    it 'should return an ErrorResponse when the refresh token is invalid' do
      token_response = auth_ctx.acquire_token_with_refresh_token(
        'bad', client_cred, RESOURCE)
      expect(token_response).to be_a(ADAL::ErrorResponse)
    end

    it 'should fail if any of the required parameters are nil' do
      expect do
        auth_ctx.acquire_token_with_refresh_token(
          nil, client_cred, RESOURCE)
      end.to raise_error(ArgumentError)
    end
  end

  describe '#acquire_token_for_user' do
    context 'with valid parameters' do
      let(:user) { ADAL::UserAssertion.new(USER_ASSERTION) }
      subject { auth_ctx.acquire_token_for_user(RESOURCE, client_cred, user) }

      it { is_expected.to be_a(ADAL::SuccessResponse) }
    end

    context 'with invalid parameters' do
      it 'should fail' do
        expect { auth_ctx.acquire_token_for_user(nil, nil) }
          .to raise_error(ArgumentError)
      end
    end
  end
end
<commit_msg>Added some missing tests for ADAL::AuthenticationContext.<commit_after>#-------------------------------------------------------------------------------
# # Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#   http://www.apache.org/licenses/LICENSE-2.0
#
# THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
# ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A
# PARTICULAR PURPOSE, MERCHANTABILITY OR NON-INFRINGEMENT.
#
# See the Apache License, Version 2.0 for the specific language
# governing permissions and limitations under the License.
#-------------------------------------------------------------------------------

require_relative '../support/fake_data'

require 'spec_helper'

# Load constants used by the fake token endpoint.
include FakeData

describe ADAL::AuthenticationContext do
  let(:auth_ctx) { ADAL::AuthenticationContext.new(AUTHORITY, TENANT) }
  let(:client_cred) { ADAL::ClientCredential.new(CLIENT_ID, CLIENT_SECRET) }

  describe '#authorization_request_url' do
    let(:resource) { 'http://graph.windows.net' }
    let(:client_id) { 'some-client-id' }
    let(:redirect_uri) { 'http://contoso.com/login' }

    context 'with no extra params' do
      it 'should produce the correct request url' do
        authorization_url =
          auth_ctx.authorization_request_url(resource, client_id, redirect_uri)
        expect(authorization_url.to_s.strip)
          .to eq "https://#{AUTHORITY}/#{TENANT}/oauth2/authorize?client_id=" \
                 "#{client_id}&response_mode=form_post&redirect_uri=http%3A%2" \
                 'F%2Fcontoso.com%2Flogin&resource=http%3A%2F%2Fgraph.windows' \
                 '.net&response_type=code'
      end
    end

    context 'with extra params' do
      it 'should produce the correct request url' do
        params = { foo: :bar }
        authorization_url = auth_ctx.authorization_request_url(
          resource, client_id, redirect_uri, params)
        expect(authorization_url.to_s.strip)
          .to eq "https://#{AUTHORITY}/#{TENANT}/oauth2/authorize?client_id=" \
                 "#{client_id}&response_mode=form_post&redirect_uri=http%3A%2" \
                 'F%2Fcontoso.com%2Flogin&resource=http%3A%2F%2Fgraph.windows' \
                 '.net&response_type=code&foo=bar'
      end
    end
  end

  describe '#correlation_id=' do
    let(:correlation_id) { 'correlation_id_1' }
    let(:user) { ADAL::UserAssertion.new(USER_ASSERTION) }

    it 'should put the correlation id on all request headers' do
      auth_ctx.correlation_id = correlation_id
      stub_request(:post, %r{.*#{TENANT}\/oauth2\/token})
        .with(headers: { 'client-request-id' => correlation_id })
        .and_return(body: '{"access_token":"my access token"}')
      result = auth_ctx.acquire_token_for_user(RESOURCE, CLIENT_ID, user)
      expect(result.access_token).to eq('my access token')
    end
  end

  describe '#acquire_token_with_authorization_code' do
    it 'should return a SuccessResponse when successful' do
      token_response = auth_ctx.acquire_token_with_authorization_code(
        AUTH_CODE, REDIRECT_URI, client_cred, RESOURCE)
      expect(token_response).to be_a(ADAL::SuccessResponse)
    end

    it 'should return an ErrorResponse when unauthorized' do
      token_response = auth_ctx.acquire_token_with_authorization_code(
        AUTH_CODE, 'bad', client_cred, RESOURCE)
      expect(token_response).to be_a(ADAL::ErrorResponse)
    end

    it 'should fail if any of the required parameters are nil' do
      expect do
        auth_ctx.acquire_token_with_authorization_code(
          nil, REDIRECT_URI, client_cred, RESOURCE)
      end.to raise_error(ArgumentError)
    end
  end

  describe '#acquire_token_for_client' do
    it 'should return a SuccessResponse when successful' do
      response = auth_ctx.acquire_token_for_client(RESOURCE, client_cred)
      expect(response).to be_a(ADAL::SuccessResponse)
    end

    it 'should return an ErrorResponse when unauthorized' do
      token_response = auth_ctx.acquire_token_for_client(RESOURCE, 'bad')
      expect(token_response).to be_a(ADAL::ErrorResponse)
    end

    it 'should fail if any of the parameters are nil' do
      expect do
        auth_ctx.acquire_token_for_client(RESOURCE, nil)
      end.to raise_error(ArgumentError)
    end
  end

  describe '#acquire_token_with_refresh_token' do
    it 'should return a SuccessResponse when successful' do
      token_response = auth_ctx.acquire_token_with_refresh_token(
        REFRESH_TOKEN, client_cred, RESOURCE)
      expect(token_response).to be_a(ADAL::SuccessResponse)
    end

    it 'should return an ErrorResponse when unauthorized' do
      token_response = auth_ctx.acquire_token_with_refresh_token(
        REFRESH_TOKEN, 'bad', RESOURCE)
      expect(token_response).to be_a(ADAL::ErrorResponse)
    end

    it 'should return an ErrorResponse when the refresh token is invalid' do
      token_response = auth_ctx.acquire_token_with_refresh_token(
        'bad', client_cred, RESOURCE)
      expect(token_response).to be_a(ADAL::ErrorResponse)
    end

    it 'should fail if any of the required parameters are nil' do
      expect do
        auth_ctx.acquire_token_with_refresh_token(
          nil, client_cred, RESOURCE)
      end.to raise_error(ArgumentError)
    end
  end

  describe '#acquire_token_for_user' do
    context 'with valid parameters' do
      let(:user) { ADAL::UserAssertion.new(USER_ASSERTION) }
      subject { auth_ctx.acquire_token_for_user(RESOURCE, client_cred, user) }

      it { is_expected.to be_a(ADAL::SuccessResponse) }
    end

    context 'with invalid parameters' do
      it 'should fail' do
        expect { auth_ctx.acquire_token_for_user(nil, nil) }
          .to raise_error(ArgumentError)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11076228784459924, 'fraction_numerical': 0.002943196311193957, 'mean_word_length': 3.186447638603696, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1676674', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2977, 'n_words': 792}