<commit_before>require 'spec_helper'

describe Travis::API::V3::Services::KeyPair::Find, set_app: true do
  let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first_or_create }
  let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
  let(:key_pair) { { 'id' => '<KEY> 'repository_id' => repo.id } }
  let(:auth_headers) { { 'HTTP_AUTHORIZATION' => "token #{token}" } }

  describe 'not authenticated' do
    before { get("/v3/repo/#{repo.id}/key_pair") }
    include_examples 'not authenticated'
  end

  context 'authenticated' do
    describe 'missing repo' do
      before { get('/v3/repo/999999999/key_pair', {}, auth_headers) }
      include_examples 'missing repo'
    end

    describe 'existing repo, no key pair' do
      before { get("/v3/repo/#{repo.id}/key_pair", {}, auth_headers) }

      example { expect(last_response.status).to eq 404 }
      example do
        expect(JSON.parse(last_response.body)).to eq(
          '@type' => 'error',
          'error_message' => 'key_pair not found (or insufficient access)',
          'error_type' => 'not_found',
          'resource_type' => 'key_pair'
        )
      end
    end

    describe 'existing repo, existing key pair' do
      before do
        repo.update_attribute(:settings, JSON.generate('ssh_key' => key_pair))
        get("/v3/repo/#{repo.id}/key_pair", {}, auth_headers)
      end

      example { expect(last_response.status).to eq 200 }
      example do
        expect(JSON.parse(last_response.body)).to eq(
          '@type' => 'key_pair',
          '@href' => "/v3/repo/#{repo.id}/key_pair",
          '@representation' => 'standard',
          'id' => 'abc123'
        )
      end
    end
  end
end<commit_msg>Fix find spec to match key pair model<commit_after>require 'spec_helper'
require 'openssl'

describe Travis::API::V3::Services::KeyPair::Find, set_app: true do
  let(:repo) { Travis::API::V3::Models::Repository.where(owner_name: 'svenfuchs', name: 'minimal').first_or_create }
  let(:token) { Travis::Api::App::AccessToken.create(user: repo.owner, app_id: 1) }
  let(:key) { OpenSSL::PKey::RSA.generate(4096) }
  let(:key_pair) { { description: 'foo key pair', value: key.to_pem, repository_id: repo.id } }
  let(:auth_headers) { { 'HTTP_AUTHORIZATION' => "token #{token}" } }

  describe 'not authenticated' do
    before { get("/v3/repo/#{repo.id}/key_pair") }
    include_examples 'not authenticated'
  end

  context 'authenticated' do
    describe 'missing repo' do
      before { get('/v3/repo/999999999/key_pair', {}, auth_headers) }
      include_examples 'missing repo'
    end

    describe 'existing repo, no key pair' do
      before { get("/v3/repo/#{repo.id}/key_pair", {}, auth_headers) }

      example { expect(last_response.status).to eq 404 }
      example do
        expect(JSON.parse(last_response.body)).to eq(
          '@type' => 'error',
          'error_message' => 'key_pair not found (or insufficient access)',
          'error_type' => 'not_found',
          'resource_type' => 'key_pair'
        )
      end
    end

    describe 'existing repo, existing key pair' do
      before do
        repo.update_attributes(settings: JSON.generate(ssh_key: key_pair))
        get("/v3/repo/#{repo.id}/key_pair", {}, auth_headers)
      end

      example { expect(last_response.status).to eq 200 }
      example do
        expect(JSON.parse(last_response.body)).to eq(
          '@type' => 'key_pair',
          '@href' => "/v3/repo/#{repo.id}/key_pair",
          '@representation' => 'standard',
          'description' => 'foo key pair',
          'fingerprint' => Travis::API::V3::Models::Fingerprint.calculate(key.to_pem)
        )
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.15675819781391628, 'fraction_numerical': 0.014396161023727006, 'mean_word_length': 3.043103448275862, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1995634', 'n_tokens_mistral': 1273, 'n_tokens_neox': 1205, 'n_words': 254}