/**
 * @license
 * Copyright 2014 Google 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @fileoverview Tests for the LIST_KEYS action.
 */

/** @suppress {extraProvide} */
goog.provide('e2e.ext.actions.ListKeysTest');

goog.require('e2e.async.Result');
goog.require('e2e.ext.actions.ListKeys');
goog.require('e2e.ext.testingstubs');
goog.require('e2e.openpgp.ContextImpl');
goog.require('goog.testing.AsyncTestCase');
goog.require('goog.testing.MockControl');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.asserts');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.mockmatchers');
goog.require('goog.testing.mockmatchers.ArgumentMatcher');
goog.setTestOnly();

var mockControl = null;
var stubs = new goog.testing.PropertyReplacer();
var testCase = goog.testing.AsyncTestCase.createAndInstall(document.title);

var USER_ID = 'test 4';

var PRIVATE_KEY_ASCII =
    '-----BEGIN PGP PRIVATE KEY BLOCK-----\n' +
    'Version: GnuPG v1.4.11 (GNU/Linux)\n' +
    '\n' +
    'lQIGBFHMug4BBACW9E+4EJXykFpkdHS1K7nkTFcvFHpnJsbHSB99Px6ib6jusVNJ\n' +
    'SjWhbWlcQXXpwDKRKKItRHE4v2zin89+hWPxQEU4l79S17i8xSXT8o02I4e7cPrj\n' +
    'j1JSyQk2YpIK5zNO7cU1IlVRHrTmvrp8ip9exF9D5UqQGxmXncjtJxsF8wARAQAB\n' +
    '/gkDAgxGSvTcN/9nYDs6DJVcH5zs/RiEw8xwMhVxHepb0D0jHDxWpPxHoT6enWSS\n' +
    'expqlvP6Oclgp0AgUBZNLr1G8i6cFTbH8VP1f+be3isyt/DzBYU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SdndCehuoA4g\n' +
    'QYJ0M9XzBtCaCJ7ti2azTNAYYtw0vWkvGfgzWxw6IbLttHRIWEdvBMul+u2NzPhy\n' +
    'x8MpulrIyAER0SgaE0oJlHm8LfjV/qJd4Gpb9NG9QmdFrpPrIvDFh/mJC6CyqdVU\n' +
    'ZfahmuzfFANMEZehsrFHZmpIAzfrv5BBppVV4/vVVuoR74ohcur36sqiSZPI4pkg\n' +
    'LE7BR0A4PGdSRroZZFB4djV+6dIM0LKwqb+d50UUsJy7JIyIFHZAR70tEIfyyF0I\n' +
    '7ZzlmO9ebwy/XiJnxYuVKh3M1q97b7lGlVGD4hvi37jv+YYqLe4Rd4T9Ho+qM33T\n' +
    'OfVHAfr6v5YhlnaMYfKC7407kWA9bRnItdjy/m5br05bncH7iJ8EGAECAAkFAlHM\n' +
    'ug4CGwwACgkQBv+crAktqAhENwQAkMY/nds36KgzwfMPpxtBaq8GbrUqY1r8lBl6\n' +
    'a/bi8qeOuEgQmIxM2OpVPtL04c1c1hLflPCi1SQUlCIh3DkEGQIcy0/wxUZdCvZK\n' +
    '0mF5nZSq6tez3CwqbeOA4nBOLwbxho50VqxBpR4qypYrB2ipykxlwiqudEe0sE2b\n' +
    '1KwNtVw=\n' +
    '=wHzz\n' +
    '-----END PGP PRIVATE KEY BLOCK-----';

var PUBLIC_KEY_ASCII =
    '-----BEGIN PGP PUBLIC KEY BLOCK-----\n' +
    'Version: GnuPG v1.4.11 (GNU/Linux)\n' +
    '\n' +
    'mI0EUcy6DgEEAJb0T7gQlfKQWmR0dLUrueRMVy8UemcmxsdIH30/HqJvqO6xU0lK\n' +
    'NaFtaVxBdenAMpEooi1EcTi/bOKfz36FY/FARTiXv1LXuLzFJdPyjTYjh7tw+uOP\n' +
    'UlLJCTZikgrnM07txTUiVVEetOa+unyKn17EX0PlSpAbGZedyO0nGwXzABEBAAG0\n' +
    'BnRlc3QgNIi4BBMBAgAiBQJRzLoOAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIX\n' +
    'gAAKCRAG/5ysCS2oCL2SA/9EV9j3T/TM3VRD0NvNySHodcxCP1BF0zm/M84I/WHQ\n' +
    'sGKmHStfCqqEGruB8E6NHQMJwNp1TzcswuxE0wiTJiXKe3w3+GZhPHdW5zcgiMKK\n' +
    'YLn80Tk6fUMx1zVZtXlSBYCN5Op/axjQRyb+fGnXOhmboqQodYaWS7qhJWQJilH6\n' +
    'iriNBFHMug4BBADDTMshHtyYhLmWC7793FlOFl5tkcEfdFKJRm30k/9yky4cuz//\n' +
    'Xe4uXM72SaTI1Dfi6UIz5ZuFTxw3bnAXav+SV4Q4dZo0hb4jU8YaQfDL4TsRp7uO\n' +
    '6iqxd8nlsh9JnBKE6Fk/CW5FoMZZ3/yEm3pq924Uv2AZlO6dafgXecyqNQARAQAB\n' +
    'iJ8EGAECAAkFAlHMug4CGwwACgkQBv+crAktqAhENwQAkMY/nds36KgzwfMPpxtB\n' +
    'aq8GbrUqY1r8lBl6a/bi8qeOuEgQmIxM2OpVPtL04c1c1hLflPCi1SQUlCIh3DkE\n' +
    'GQIcy0/wxUZdCvZK0mF5nZSq6tez3CwqbeOA4nBOLwbxho50VqxBpR4qypYrB2ip\n' +
    'ykxlwiqudEe0sE2b1KwNtVw=\n' +
    '=nHBL\n' +
    '-----END PGP PUBLIC KEY BLOCK-----';


function setUp() {
  window.localStorage.clear();
  mockControl = new goog.testing.MockControl();
  e2e.ext.testingstubs.initStubs(stubs);
}


function tearDown() {
  stubs.reset();
  mockControl.$tearDown();
}


function testExecuteEmpty() {
  var pgpContext = new e2e.openpgp.ContextImpl();
  pgpContext.setKeyRingPassphrase(''); // No passphrase.

  var errorCallback = mockControl.createFunctionMock('errorCallback');
  errorCallback(new goog.testing.mockmatchers.ArgumentMatcher(function(arg) {
    assertTrue(arg instanceof Error);
    return true;
  }));

  var callback = mockControl.createFunctionMock('callback');
  callback(goog.testing.mockmatchers.ignoreArgument);

  var action = new e2e.ext.actions.ListKeys();

  mockControl.$replayAll();
  action.execute(pgpContext, {
    content: 'private'
  }, null, callback, errorCallback);
  mockControl.$verifyAll();
}


function testExecutePublicKeys() {
  var pgpContext = new e2e.openpgp.ContextImpl();
  pgpContext.setKeyRingPassphrase(''); // No passphrase.

  var errorCallback = mockControl.createFunctionMock('errorCallback');
  var callback = mockControl.createFunctionMock('callback');
  callback(new goog.testing.mockmatchers.ArgumentMatcher(function(arg) {
    return USER_ID in arg;
  }));

  var action = new e2e.ext.actions.ListKeys();

  mockControl.$replayAll();
  testCase.waitForAsync('Waiting for keys to be populated.');
  pgpContext.importKey(goog.nullFunction, PUBLIC_KEY_ASCII).
      addCallback(function() {
        action.execute(pgpContext, {
          content: 'public'
        }, null, callback, errorCallback);
        mockControl.$verifyAll();
        testCase.continueTesting();
      });
}


function testExecutePrivateKeys() {
  var pgpContext = new e2e.openpgp.ContextImpl();
  pgpContext.setKeyRingPassphrase(''); // No passphrase.

  var errorCallback = mockControl.createFunctionMock('errorCallback');
  var callback = mockControl.createFunctionMock('callback');
  callback(new goog.testing.mockmatchers.ArgumentMatcher(function(arg) {
    return USER_ID in arg;
  }));

  var action = new e2e.ext.actions.ListKeys();

  mockControl.$replayAll();
  testCase.waitForAsync('Waiting for keys to be populated.');
  pgpContext.importKey(function(uid) {
    return e2e.async.Result.toResult('test');
  }, PRIVATE_KEY_ASCII).addCallback(function() {
    action.execute(pgpContext, {
      content: 'private'
    }, null, callback, errorCallback);
    mockControl.$verifyAll();
    testCase.continueTesting();
  });
}
