<commit_before>"use strict";

const _         = require('underscore');
const co        = require('co');
const should    = require('should');
const duniter     = require('../../index');
const bma       = require('duniter-bma').duniter.methods.bma;
const user      = require('./tools/user');
const constants = require('../../app/lib/constants');
const rp        = require('request-promise');
const httpTest  = require('./tools/http');
const commit    = require('./tools/commit');

require('duniter-bma').duniter.methods.noLimit(); // Disables the HTTP limiter

const expectAnswer   = httpTest.expectAnswer;

const MEMORY_MODE = true;
const commonConf = {
  ipv4: '127.0.0.1',
  currency: 'bb',
  httpLogs: true,
  forksize: 3,
  xpercent: 0.9,
  msValidity: 10000,
  sigQty: 1
};

const s1 = duniter(
  '/bb11',
  MEMORY_MODE,
  _.extend({
  port: '7799',
  pair: {
    pub: '<KEY>',
    sec: '<KEY>'
  }
}, commonConf));

const cat = user('cat', { pub: '<KEY>', sec: '<KEY>'}, { server: s1 });
const tac = user('tac', { pub: '2LvDg21dVXvetTD9GdkPLURavLYEqP3whauvPWX4c2qc', sec: '2HuRLWgKgED1bVio1tdpeXrf7zuUszv1yPHDsDj7kcMC4rVSN9RC58ogjtKNfTbH1eFz7rn38U1PywNs3m6Q7UxE'}, { server: s1 });
const toc = user('toc', { pub: '<KEY>', sec: '64EYRvdPpTfLGGmaX5nijLXRqWXaVz8r1Z1GtaahXwVSJGQRn7tqkxLb288zwSYzELMEG5ZhXSBYSxsTsz1m9y8F'}, { server: s1 });
const tic = user('tic', { pub: '<KEY>', sec: '<KEY>'}, { server: s1 });
const tic2 = user('tic', { pub: '<KEY>', sec: '48vHGE2xkhnC81ChSu7dHaNv8JqnYubyyHRbkmkeAPKNg8Tv2BE7kVi3voh2ZhfVpQhEJLzceufzqpJ2dqnyXNSp'}, { server: s1 });
const man1 = user('man1', { pub: '<KEY>', sec: '2h8UNKE4YRnjmTGQTrgf4DZp2h3F5LqjnecxP8AgU6aH1x4dvbNVirsNeBiSR2UQfExuLAbdXiyM465hb5qUxYC1'}, { server: s1 });
const man2 = user('man2', { pub: '<KEY>', sec: 'pJRwpaCWshKZNWsbDxAHFQbVjk6X8gz9eBy9jaLnVY9gUZRqotrZLZPZe68ag4vEX1Y8mX77NhPXV2hj9F1UkX3'}, { server: s1 });
const man3 = user('man3', { pub: '5bfpAfZJ4xYspUBYseASJrofhRm6e6JMombt43HBaRzW', sec: '2VFQtEcYZRwjoc8Lxwfzcejtw9VP8VAi47WjwDDjCJCXu7g1tXUAbVZN3QmvG6NJqaSuLCuYP7WDHWkFmTrUEMaE'}, { server: s1 });

describe("Identities collision", function() {

  before(function() {

    const commitS1 = commit(s1);

    return co(function *() {
      yield s1.initWithDAL().then(bma).then((bmapi) => bmapi.openConnections());
      require('duniter-prover').duniter.methods.hookServer(s1);
      yield cat.createIdentity();
      yield tac.createIdentity();
      yield toc.createIdentity();
      yield tic.createIdentity();
      yield toc.cert(cat);
      yield cat.cert(toc);
      yield cat.cert(tic);
      yield tic.cert(tac);
      yield tic.cert(cat);
      yield cat.join();
      yield toc.join();
      yield tic.join();
      yield tac.join();
      yield commitS1();
      yield commitS1();

      // We have the following WoT (diameter 3):

      /**
       *  toc <=> cat <=> tic -> tac
       */

      // cat is the sentry

      // Man1 is someone who just needs a commit to join
      yield man1.createIdentity();
      yield man1.join();
      yield tac.cert(man1);

      /**
       *  toc <=> cat -> tic -> tac -> man1
       */

      // Man2 is someone who has no certifications yet has sent a JOIN
      yield man2.createIdentity();
      yield man2.join();

      // Man3 is someone who has only published its identity
      yield man3.createIdentity();

      // tic RENEW, but not written
      yield tic.join();

      try {
        yield tic.createIdentity();
        throw 'Should have thrown an error for already used pubkey';
      } catch (e) {
        JSON.parse(e).message.should.equal('Pubkey already used in the blockchain');
      }
      try {
        yield tic2.createIdentity();
        throw 'Should have thrown an error for already used uid';
      } catch (e) {
        JSON.parse(e).message.should.equal('UID already used in the blockchain');
      }
    });
  });

  it('should have 4 members', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/members', { json: true }), function(res) {
      res.should.have.property('results').length(4);
      _.pluck(res.results, 'uid').sort().should.deepEqual(['cat', 'tac', 'tic', 'toc']);
    });
  });

  it('should have identity-of/cat', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/identity-of/cat', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('cat');
      res.should.have.property('sigDate').be.a.Number;
    });
  });

  it('should have identity-of/toc', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/identity-of/toc', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('toc');
      res.should.have.property('sigDate').be.a.Number;
    });
  });

  it('should have identity-of/tic', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/identity-of/tic', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('tic');
      res.should.have.property('sigDate').be.a.Number;
    });
  });

  it('should have identity-of/aaa', function() {
    return httpTest.expectError(404, "No member matching this pubkey or uid", rp('http://127.0.0.1:7799/wot/identity-of/aaa'));
  });

  it('should have certifiers-of/cat giving results', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/certifiers-of/cat', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('cat');
      res.should.have.property('isMember').equal(true);
      res.should.have.property('sigDate').be.a.Number;
      res.should.have.property('certifications').length(2);
      let certs = res.certifications;
      certs[1].should.have.property('pubkey').equal('DKpQPUL4ckzXYdnDRvCRKAm1gNvSdmAXnTrJZ7LvM5Qo');
      certs[1].should.have.property('uid').equal('toc');
      certs[1].should.have.property('isMember').equal(true);
      certs[1].should.have.property('wasMember').equal(true);
      certs[1].should.have.property('sigDate').be.a.Number;
      certs[1].should.have.property('cert_time').property('block').be.a.Number;
      certs[1].should.have.property('cert_time').property('medianTime').be.a.Number;
      certs[1].should.have.property('written').property('number').equal(0);
      certs[1].should.have.property('written').property('hash').not.equal('');
      certs[1].should.have.property('signature').not.equal('');
    });
  });

  it('should have certifiers-of/tic giving results', function() {
    return expectAnswer(rp('http://1172.16.58.3:7799/wot/certifiers-of/tic', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('tic');
      res.should.have.property('isMember').equal(true);
      res.should.have.property('sigDate').be.a.Number;
      res.should.have.property('certifications').length(1);
      let certs = res.certifications;
      certs[0].should.have.property('pubkey').equal('<KEY>');
      certs[0].should.have.property('uid').equal('cat');
      certs[0].should.have.property('isMember').equal(true);
      certs[0].should.have.property('wasMember').equal(true);
      certs[0].should.have.property('sigDate').be.a.Number;
      certs[0].should.have.property('cert_time').property('block').be.a.Number;
      certs[0].should.have.property('cert_time').property('medianTime').be.a.Number;
      certs[0].should.have.property('written').property('number').equal(0);
      certs[0].should.have.property('written').property('hash').not.equal('');
      certs[0].should.have.property('signature').not.equal('');
    });<commit_msg>[fix] Identity test failing<commit_after>"use strict";

const _         = require('underscore');
const co        = require('co');
const should    = require('should');
const duniter     = require('../../index');
const bma       = require('duniter-bma').duniter.methods.bma;
const user      = require('./tools/user');
const constants = require('../../app/lib/constants');
const rp        = require('request-promise');
const httpTest  = require('./tools/http');
const commit    = require('./tools/commit');

require('duniter-bma').duniter.methods.noLimit(); // Disables the HTTP limiter

const expectAnswer   = httpTest.expectAnswer;

const MEMORY_MODE = true;
const commonConf = {
  ipv4: '127.0.0.1',
  currency: 'bb',
  httpLogs: true,
  forksize: 3,
  xpercent: 0.9,
  msValidity: 10000,
  sigQty: 1
};

const s1 = duniter(
  '/bb11',
  MEMORY_MODE,
  _.extend({
  port: '7799',
  pair: {
    pub: '<KEY>',
    sec: '<KEY>'
  }
}, commonConf));

const cat = user('cat', { pub: 'HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd', sec: '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP'}, { server: s1 });
const tac = user('tac', { pub: '<KEY>', sec: '2HuRLWgKgED1bVio1tdpeXrf7zuUszv1yPHDsDj7kcMC4rVSN9RC58ogjtKNfTbH1eFz7rn38U1PywNs3m6Q7UxE'}, { server: s1 });
const toc = user('toc', { pub: '<KEY>', sec: '64EYRvdPpTfLGGmaX5nijLXRqWXaVz8r1Z1GtaahXwVSJGQRn7tqkxLb288zwSYzELMEG5ZhXSBYSxsTsz1m9y8F'}, { server: s1 });
const tic = user('tic', { pub: '<KEY>', sec: '468Q1XtTq7h84NorZdWBZFJrGkB18CbmbHr9tkp9snt5GiERP7ySs3wM8myLccbAAGejgMRC9rqnXuW3iAfZACm7'}, { server: s1 });
const tic2 = user('tic', { pub: '<KEY>', sec: '48vHGE2xkhnC81ChSu7dHaNv8JqnYubyyHRbkmkeAPKNg8Tv2BE7kVi3voh2ZhfVpQhEJLzceufzqpJ2dqnyXNSp'}, { server: s1 });
const man1 = user('man1', { pub: '<KEY>', sec: '2h8UNKE4YRnjmTGQTrgf4DZp2h3F5LqjnecxP8AgU6aH1x4dvbNVirsNeBiSR2UQfExuLAbdXiyM465hb5qUxYC1'}, { server: s1 });
const man2 = user('man2', { pub: '<KEY>', sec: 'pJRwpaCWshKZNWsbDxAHFQbVjk6X8gz9eBy9jaLnVY9gUZRqotrZLZPZe68ag4vEX1Y8mX77NhPXV2hj9F1UkX3'}, { server: s1 });
const man3 = user('man3', { pub: '<KEY>', sec: '2VFQtEcYZRwjoc8Lxwfzcejtw9VP8VAi47WjwDDjCJCXu7g1tXUAbVZN3QmvG6NJqaSuLCuYP7WDHWkFmTrUEMaE'}, { server: s1 });

describe("Identities collision", function() {

  before(function() {

    const commitS1 = commit(s1);

    return co(function *() {
      yield s1.initWithDAL().then(bma).then((bmapi) => bmapi.openConnections());
      require('duniter-prover').duniter.methods.hookServer(s1);
      yield cat.createIdentity();
      yield tac.createIdentity();
      yield toc.createIdentity();
      yield tic.createIdentity();
      yield toc.cert(cat);
      yield cat.cert(toc);
      yield cat.cert(tic);
      yield tic.cert(tac);
      yield tic.cert(cat);
      yield cat.join();
      yield toc.join();
      yield tic.join();
      yield tac.join();
      yield commitS1();
      yield commitS1();

      // We have the following WoT (diameter 3):

      /**
       *  toc <=> cat <=> tic -> tac
       */

      // cat is the sentry

      // Man1 is someone who just needs a commit to join
      yield man1.createIdentity();
      yield man1.join();
      yield tac.cert(man1);

      /**
       *  toc <=> cat -> tic -> tac -> man1
       */

      // Man2 is someone who has no certifications yet has sent a JOIN
      yield man2.createIdentity();
      yield man2.join();

      // Man3 is someone who has only published its identity
      yield man3.createIdentity();

      // tic RENEW, but not written
      yield tic.join();

      try {
        yield tic.createIdentity();
        throw 'Should have thrown an error for already used pubkey';
      } catch (e) {
        JSON.parse(e).message.should.equal('Pubkey already used in the blockchain');
      }
      try {
        yield tic2.createIdentity();
        throw 'Should have thrown an error for already used uid';
      } catch (e) {
        JSON.parse(e).message.should.equal('UID already used in the blockchain');
      }
    });
  });

  it('should have 4 members', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/members', { json: true }), function(res) {
      res.should.have.property('results').length(4);
      _.pluck(res.results, 'uid').sort().should.deepEqual(['cat', 'tac', 'tic', 'toc']);
    });
  });

  it('should have identity-of/cat', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/identity-of/cat', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('cat');
      res.should.have.property('sigDate').be.a.Number;
    });
  });

  it('should have identity-of/toc', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/identity-of/toc', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('toc');
      res.should.have.property('sigDate').be.a.Number;
    });
  });

  it('should have identity-of/tic', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/identity-of/tic', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('tic');
      res.should.have.property('sigDate').be.a.Number;
    });
  });

  it('should have identity-of/aaa', function() {
    return httpTest.expectError(404, "No member matching this pubkey or uid", rp('http://127.0.0.1:7799/wot/identity-of/aaa'));
  });

  it('should have certifiers-of/cat giving results', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/certifiers-of/cat', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('cat');
      res.should.have.property('isMember').equal(true);
      res.should.have.property('sigDate').be.a.Number;
      res.should.have.property('certifications').length(2);
      let certs = res.certifications;
      // Look for the index
      const index = certs[0].pubkey === '<KEY>' ? 0 : 1;
      certs[index].should.have.property('pubkey').equal('<KEY>');
      certs[index].should.have.property('uid').equal('toc');
      certs[index].should.have.property('isMember').equal(true);
      certs[index].should.have.property('wasMember').equal(true);
      certs[index].should.have.property('sigDate').be.a.Number;
      certs[index].should.have.property('cert_time').property('block').be.a.Number;
      certs[index].should.have.property('cert_time').property('medianTime').be.a.Number;
      certs[index].should.have.property('written').property('number').equal(0);
      certs[index].should.have.property('written').property('hash').not.equal('');
      certs[index].should.have.property('signature').not.equal('');
    });
  });

  it('should have certifiers-of/tic giving results', function() {
    return expectAnswer(rp('http://127.0.0.1:7799/wot/certifiers-of/tic', { json: true }), function(res) {
      res.should.have.property('pubkey').equal('<KEY>');
      res.should.have.property('uid').equal('tic');
      res.should.have.property('isMember').equal(true);
      res.should.have.property('sigDate').be.a.Number;
      res.should.have.property('certifications').length(1);
      let certs = res.certifications;
      certs[0].should.have.property('pubkey').equal('<KEY>');
      certs[0].should.have.property('uid').equal('cat');
      certs[0].should.have.property('isMember').equal(true);
      certs[0].should.have.property('wasMember').equal(true);
      certs[0].should.have.property('sigDate').be.a.Number;
      certs[0].should.have.property('cert_time').property('block').be.a.Number;
      certs[0].should.have.property('cert_time').property('medianTime').be.a.Number;
      certs[0].should.have.property('written').property('number').equal(0);
      certs[0].should.have.property('written').property('hash').not.equal('');
      certs[0].should.have.property('signature').not.equal('');
    });
{'fraction_non_alphanumeric': 0.18423383525243578, 'fraction_numerical': 0.033784638744780465, 'mean_word_length': 3.6382042253521125, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 16, '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': '5608903', 'n_tokens_mistral': 6045, 'n_tokens_neox': 5759, 'n_words': 859}