const fs = require('fs');

const expect = require('expect');

const {findPrivateKey} = require('../lib/private-key');

describe('private-key', function () {
  let privateKey;
  let keyfilePath;

  beforeEach(function () {
    privateKey = 'I AM PRIVET KEY!?!!~1!';
    keyfilePath = '/some/path';
    expect.spyOn(fs, 'readFileSync')
      .andReturn(privateKey);
  });

  afterEach(function () {
    expect.restoreSpies();
  });

  describe('findPrivateKey()', function () {
    describe('when a filepath is provided', function () {
      it('should read the file at given filepath', function () {
        findPrivateKey(keyfilePath);
        expect(fs.readFileSync)
          .toHaveBeenCalledWith(keyfilePath);
      });

      it('should return the key', function () {
        expect(findPrivateKey(keyfilePath))
          .toEqual(privateKey);
      });
    });

    describe('when a PRIVATE_KEY env var is provided', function () {
      beforeEach(function () {
        process.env.PRIVATE_KEY = privateKey;
      });

      afterEach(function () {
        delete process.env.PRIVATE_KEY;
      });

      it('should return the key', function () {
        expect(findPrivateKey())
          .toEqual(privateKey);
      });
    });

    describe('when a PRIVATE_KEY_PATH env var is provided', function () {
      beforeEach(function () {
        process.env.PRIVATE_KEY_PATH = keyfilePath;
      });

      afterEach(function () {
        delete process.env.PRIVATE_KEY_PATH;
      });

      it('should read the file at given filepath', function () {
        findPrivateKey();
        expect(fs.readFileSync)
          .toHaveBeenCalledWith(keyfilePath);
      });

      it('should return the key', function () {
        expect(findPrivateKey())
          .toEqual(privateKey);
      });
    });

    describe('when no private key is provided', function () {
      beforeEach(function () {
        expect.spyOn(fs, 'readdirSync')
          .andReturn([
            'foo.txt',
            'foo.pem'
          ]);
      });

      it('should look for one in the current directory', function () {
        findPrivateKey();
        expect(fs.readdirSync)
          .toHaveBeenCalledWith(process.cwd());
      });

      describe('and a key file is present', function () {
        it('should load the key file', function () {
          findPrivateKey();
          expect(fs.readFileSync)
            .toHaveBeenCalledWith('foo.pem');
        });
      });

      describe('and a key file is not present', function () {
        beforeEach(function () {
          fs.readdirSync.restore();
        });

        it('should throw an error', function () {
          expect(findPrivateKey)
            .toThrow(Error, /missing private key for GitHub App/i);
        });
      });
    });
  });
});

@@ -1223,32 +1223,411 @@
    %7D);%0A    %7D);%0A%0A
+    describe('when a PRIVATE_KEY has line breaks', function () %7B%0A      beforeEach(function () %7B%0A        process.env.PRIVATE_KEY = 'line 1%5C%5Cnline 2';%0A      %7D);%0A%0A      afterEach(function () %7B%0A        delete process.env.PRIVATE_KEY;%0A      %7D);%0A%0A      it('should return the key', function () %7B%0A        expect(findPrivateKey())%0A          .toEqual('line 1%5Cnline 2');%0A      %7D);%0A    %7D);%0A%0A
     describe('wh

allow to set PRIVATE_KEY with "\\n" characters