<commit_before>var mercurius = require('../index.js');
var request = require('supertest');
var assert = require('assert');
var nock = require('nock');
var crypto = require('crypto');
var urlBase64 = require('urlsafe-base64');
var testUtils = require('./testUtils.js');
var redis = require('../redis.js');
var chai = require('chai');

chai.should();

var userCurve = crypto.createECDH('prime256v1');

var userPublicKey = userCurve.generateKeys();
var userPrivateKey = userCurve.getPrivateKey();

describe('mercurius unregisterMachine', function() {
  var token;

  before(function() {
    return mercurius.ready
    .then(() => testUtils.register(mercurius.app, 'machine_1', 'https://android.googleapis.com/gcm/send/someSubscriptionID', ''))
    .then(gotToken => token = gotToken)
    .then(() => testUtils.register(mercurius.app, 'machine_2', 'https://localhost:50005', urlBase64.encode(userPublicKey), token, '/'));
  });

  it('created the machines properly', function() {
    redis.exists(token)
    .then(function(exists) {
      exists.should.equal(1);
      return redis.exists(token + ':clients');
    })
    .then(function(exists) {
      exists.should.equal(1);
      return redis.exists('machine_2:clients');
    })
    .then(function(exists) {
      exists.should.equal(1);
    });
  });

  it('replies with 404 when trying to unregister a non existing token', function(done) {
    request(mercurius.app)
    .post('/unregisterMachine')
    .send({
      token: 'nonexistingtoken',
      machineId: 'machine',
    })
    .expect(404, done);
  });

  it('replies with 404 when trying to unregister a non registered machine', function(done) {
    request(mercurius.app)
    .post('/unregisterMachine')
    .send({
      token: token,
      machineId: 'non-existing-machine',
    })
    .expect(404, done);
  });

  it('replies with 404 on `getPayload` if there\'s no payload available', function(done) {
    request(mercurius.app)
    .get('/getPayload/' + token)
    .send()
    .expect(404, done);
  });

  it('successfully unregisters a GCM endpoint', function(done) {
    nock('https://android.googleapis.com/')
    .post('/gcm/send')
    .reply(200);

    request(mercurius.app)
    .post('/unregisterMachine')
    .send({
      token: token,
      machineId: 'machine_1',
    })
    .expect(200, done);
  });

  it('replies with the payload encoded in JSON on `getPayload` if there\'s a payload available', function(done) {
    request(mercurius.app)
    .get('/getPayload/' + token)
    .send()
    .expect(function(res) {
      assert.equal(res.status, 200);
      var obj = JSON.parse(res.text);
      assert.equal(obj.title, 'unregister');
      assert.equal(obj.body, 'called from unregisterMachine');
    })<commit_msg>Test sending notification to a token after unregistering one of the machines assigned to it. Fixes #83<commit_after>var mercurius = require('../index.js');
var request = require('supertest');
var assert = require('assert');
var nock = require('nock');
var crypto = require('crypto');
var urlBase64 = require('urlsafe-base64');
var testUtils = require('./testUtils.js');
var redis = require('../redis.js');
var chai = require('chai');

chai.should();

var userCurve = crypto.createECDH('prime256v1');

var userPublicKey = userCurve.generateKeys();
var userPrivateKey = userCurve.getPrivateKey();

describe('mercurius unregisterMachine', function() {
  var token;

  before(function() {
    return mercurius.ready
    .then(() => testUtils.register(mercurius.app, 'machine_1', 'https://android.googleapis.com/gcm/send/someSubscriptionID', ''))
    .then(gotToken => token = gotToken)
    .then(() => testUtils.register(mercurius.app, 'machine_2', 'https://localhost:50005', urlBase64.encode(userPublicKey), token, '/'));
  });

  it('created the machines properly', function() {
    redis.exists(token)
    .then(function(exists) {
      exists.should.equal(1);
      return redis.exists(token + ':clients');
    })
    .then(function(exists) {
      exists.should.equal(1);
      return redis.exists('machine_2:clients');
    })
    .then(function(exists) {
      exists.should.equal(1);
    });
  });

  it('replies with 404 when trying to unregister a non existing token', function(done) {
    request(mercurius.app)
    .post('/unregisterMachine')
    .send({
      token: 'nonexistingtoken',
      machineId: 'machine',
    })
    .expect(404, done);
  });

  it('replies with 404 when trying to unregister a non registered machine', function(done) {
    request(mercurius.app)
    .post('/unregisterMachine')
    .send({
      token: token,
      machineId: 'non-existing-machine',
    })
    .expect(404, done);
  });

  it('replies with 404 on `getPayload` if there\'s no payload available', function(done) {
    request(mercurius.app)
    .get('/getPayload/' + token)
    .send()
    .expect(404, done);
  });

  it('successfully unregisters a GCM endpoint', function(done) {
    nock('https://android.googleapis.com/')
    .post('/gcm/send')
    .reply(200);

    request(mercurius.app)
    .post('/unregisterMachine')
    .send({
      token: token,
      machineId: 'machine_1',
    })
    .expect(200, done);
  });

  it('doesn\'t send notifications to unregistered machines but sends them to machines of the same token set of an unregistered machine', function(done) {
    var req1 = nock('https://android.googleapis.com/')
    .post('/gcm/send')
    .reply(200);

    var req2 = nock('https://localhost:50005')
    .post('/')
    .reply(201);

    request(mercurius.app)
    .post('/notify')
    .send({
      token: token,
      client: 'aClient',
    })
    .expect(200, function() {
      assert(!req1.isDone(), 'Notification isn\'t sent do an unregistered machine');
      assert(req2.isDone(), 'Notification is sent do a machine in the same token set of an unregistered machine');
      nock.cleanAll();
      done();
    });
  });

  it('replies with the payload encoded in JSON on `getPayload` if there\'s a payload available', function(done) {
    request(mercurius.app)
    .get('/getPayload/' + token)
    .send()
    .expect(function(res) {
      assert.equal(res.status, 200);
      var obj = JSON.parse(res.text);
      assert.equal(obj.title, 'unregister');
      assert.equal(obj.body, 'called from unregisterMachine');
    })
{'fraction_non_alphanumeric': 0.1636219221604448, 'fraction_numerical': 0.018745035742652898, 'mean_word_length': 3.440056417489422, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 8, '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': '336384', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1986, 'n_words': 448}