<commit_before>require 'spec_helper_integration'

module Doorkeeper
  describe AccessToken do
    subject { FactoryGirl.build(:access_token) }

    it { should be_valid }

    it_behaves_like "an accessible token"
    it_behaves_like "a revocable token"
    it_behaves_like "an unique token" do
      let(:factory_name) { :access_token }
    end

    describe :refresh_token do
      it 'has empty refresh token if it was not required' do
        token = FactoryGirl.create :access_token
        expect(token.refresh_token).to be_nil
      end

      it 'generates a refresh token if it was requested' do
        token = FactoryGirl.create :access_token, :use_refresh_token => true
        expect(token.refresh_token).not_to be_nil
      end

      it "is not valid if token exists" do
        token1 = FactoryGirl.create :access_token, :use_refresh_token => true
        token2 = FactoryGirl.create :access_token, :use_refresh_token => true
        token2.send :write_attribute, :refresh_token, token1.refresh_token
        expect(token2).not_to be_valid
      end

      it 'expects database to raise an error if refresh tokens are the same' do
        token1 = FactoryGirl.create :access_token, :use_refresh_token => true
        token2 = FactoryGirl.create :access_token, :use_refresh_token => true
        expect {
          token2.write_attribute :refresh_token, token1.refresh_token
          token2.save(:validate => false)
        }.to raise_error
      end
    end

    describe "validations" do
      it "is valid without resource_owner_id" do
        # For client credentials flow
        subject.resource_owner_id = nil
        should be_valid
      end
    end

    describe "#same_credential?" do

      context "with default parameters" do

        let(:resource_owner_id) { 100 }
        let(:application)    { FactoryGirl.create :application }
        let(:default_attributes) do
          { :application => application, :resource_owner_id => resource_owner_id }
        end
        let(:access_token1) { FactoryGirl.create :access_token, default_attributes }
        
        context 'the second token has the same owner and same app' do
          let(:access_token2) { FactoryGirl.create :access_token, default_attributes }
          it 'success' do
            expect(access_token1.same_credential?(access_token2)).to be_true 
          end
        end

        context 'the second token has same owner and different app' do
          let(:other_application) { FactoryGirl.create :application }
          let(:access_token2) { FactoryGirl.create :access_token, :application => other_application, :resource_owner_id => resource_owner_id }
          
          it 'fail' do
            expect(access_token1.same_credential?(access_token2)).to be_false
          end
        end

        context 'the second token has different owner and different app' do

          let(:other_application) { FactoryGirl.create :application }
          let(:access_token2) { FactoryGirl.create :access_token, :application => other_application, :resource_owner_id => 42 }
          
          it 'fail' do
            expect(access_token1.same_credential?(access_token2)).to be_false
          end
        end

        context 'the second token has different owner and same app' do
          let(:other_application) { FactoryGirl.create :application }
          let(:access_token2) { FactoryGirl.create :access_token, :application => application, :resource_owner_id => 42 }
          
          it 'fail' do
            expect(access_token1.same_credential?(access_token2)).to be_false
          end
        end
      end
    end

    describe '.revoke_all_for' do
      let(:resource_owner) { double(:id => 100) }
      let(:application)    { FactoryGirl.create :application }
      let(:default_attributes) do
        { :application => application, :resource_owner_id => resource_owner.id }
      end

      it 'revokes all tokens for given application and resource owner' do
        FactoryGirl.create :access_token, default_attributes
        AccessToken.revoke_all_for application.id, resource_owner
        AccessToken.all.each do |token|
          expect(token).to be_revoked
        end
      end

      it 'matches application' do
        FactoryGirl.create :access_token, default_attributes.merge(:application => FactoryGirl.create(:application))
        AccessToken.revoke_all_for application.id, resource_owner
        expect(AccessToken.all).not_to be_empty
      end<commit_msg>Refactor and complete the #same_credential test (2)<commit_after>require 'spec_helper_integration'

module Doorkeeper
  describe AccessToken do
    subject { FactoryGirl.build(:access_token) }

    it { should be_valid }

    it_behaves_like "an accessible token"
    it_behaves_like "a revocable token"
    it_behaves_like "an unique token" do
      let(:factory_name) { :access_token }
    end

    describe :refresh_token do
      it 'has empty refresh token if it was not required' do
        token = FactoryGirl.create :access_token
        expect(token.refresh_token).to be_nil
      end

      it 'generates a refresh token if it was requested' do
        token = FactoryGirl.create :access_token, :use_refresh_token => true
        expect(token.refresh_token).not_to be_nil
      end

      it "is not valid if token exists" do
        token1 = FactoryGirl.create :access_token, :use_refresh_token => true
        token2 = FactoryGirl.create :access_token, :use_refresh_token => true
        token2.send :write_attribute, :refresh_token, token1.refresh_token
        expect(token2).not_to be_valid
      end

      it 'expects database to raise an error if refresh tokens are the same' do
        token1 = FactoryGirl.create :access_token, :use_refresh_token => true
        token2 = FactoryGirl.create :access_token, :use_refresh_token => true
        expect {
          token2.write_attribute :refresh_token, token1.refresh_token
          token2.save(:validate => false)
        }.to raise_error
      end
    end

    describe "validations" do
      it "is valid without resource_owner_id" do
        # For client credentials flow
        subject.resource_owner_id = nil
        should be_valid
      end
    end

    describe "#same_credential?" do

      context "with default parameters" do

        let(:resource_owner_id) { 100 }
        let(:application)    { FactoryGirl.create :application }
        let(:default_attributes) do
          { :application => application, :resource_owner_id => resource_owner_id }
        end
        let(:access_token1) { FactoryGirl.create :access_token, default_attributes }
        
        context 'the second token has the same owner and same app' do
          let(:access_token2) { FactoryGirl.create :access_token, default_attributes }
          it 'success' do
            expect(access_token1.same_credential?(access_token2)).to be_true 
          end
        end

        context 'the second token has same owner and different app' do
          let(:other_application) { FactoryGirl.create :application }
          let(:access_token2) { FactoryGirl.create :access_token, :application => other_application, :resource_owner_id => resource_owner_id }
          
          it 'fail' do
            expect(access_token1.same_credential?(access_token2)).to be_false
          end
        end

        context 'the second token has different owner and different app' do

          let(:other_application) { FactoryGirl.create :application }
          let(:access_token2) { FactoryGirl.create :access_token, :application => other_application, :resource_owner_id => 42 }
          
          it 'fail' do
            expect(access_token1.same_credential?(access_token2)).to be_false
          end
        end

        context 'the second token has different owner and same app' do
          let(:access_token2) { FactoryGirl.create :access_token, :application => application, :resource_owner_id => 42 }
          
          it 'fail' do
            expect(access_token1.same_credential?(access_token2)).to be_false
          end
        end
      end
    end

    describe '.revoke_all_for' do
      let(:resource_owner) { double(:id => 100) }
      let(:application)    { FactoryGirl.create :application }
      let(:default_attributes) do
        { :application => application, :resource_owner_id => resource_owner.id }
      end

      it 'revokes all tokens for given application and resource owner' do
        FactoryGirl.create :access_token, default_attributes
        AccessToken.revoke_all_for application.id, resource_owner
        AccessToken.all.each do |token|
          expect(token).to be_revoked
        end
      end

      it 'matches application' do
        FactoryGirl.create :access_token, default_attributes.merge(:application => FactoryGirl.create(:application))
        AccessToken.revoke_all_for application.id, resource_owner
        expect(AccessToken.all).not_to be_empty
      end
{'fraction_non_alphanumeric': 0.0743801652892562, 'fraction_numerical': 0.007482689300871119, 'mean_word_length': 2.695831613702022, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, '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': '3423283', 'n_tokens_mistral': 2493, 'n_tokens_neox': 2394, 'n_words': 664}