<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
var expect = require("chai").expect;
var crypto = require("crypto");

var getStorage = require("../loop/storage");
var conf = require("../loop").conf;
var hmac = require("../loop").hmac;

var uuid = "1234";
var user = "<EMAIL>";
var userMac = hmac(user, conf.get("userMacSecret"));
var callerId = '<EMAIL>';
var simplePushURL = "https://push.mozilla.com/test";
var fakeCallInfo = conf.get("fakeCallInfo");


describe("Storage", function() {
  function testStorage(name, createStorage) {
    var storage, 
        a_second = 1 / 3600,  // A second in hours.
        calls = [
        {
          callId:       crypto.randomBytes(16).toString("hex"),
          callerId:     callerId,
          userMac:      userMac,
          sessionId:    fakeCallInfo.session1,
          calleeToken:  fakeCallInfo.token1,
          timestamp:    0
        },
        {
          callId:       crypto.randomBytes(16).toString("hex"),
          callerId:     callerId,
          userMac:      userMac,
          sessionId:    fakeCallInfo.session2,
          calleeToken:  fakeCallInfo.token2,
          timestamp:    1
        },
        {
          callId:       crypto.randomBytes(16).toString("hex"),
          callerId:     callerId,
          userMac:      userMac,
          sessionId:    fakeCallInfo.session3,
          calleeToken:  fakeCallInfo.token2,
          timestamp:    2
        }
      ],
      call = calls[0];

    describe(name, function() {
      beforeEach(function() {
        storage = createStorage({
          tokenDuration: conf.get('tokBox').tokenDuration,
          hawkSessionDuration: conf.get('hawkSessionDuration')
        });
      });
  
      afterEach(function(done) {
        storage.drop(function(err) {
          // Remove the storage reference so tests blow up in an explicit way.
          storage = undefined;
          done(err);
        });
      });

      describe('#revokeURLToken', function() {
        it("should add a revoked url", function(done) {
          storage.revokeURLToken({uuid: uuid, expires: a_second},
            function(err) {
              if (err)  {
                throw err;
              }
              storage.isURLRevoked(uuid, function(err, value){
                expect(value).to.equal(true);
                done(err);
              });
            });
        });
      });

      describe('isURLRevoked', function() {
        it("should not return an expired token", function(done) {
          storage.revokeURLToken({uuid: uuid, expires: a_second / 100},
            function(err) {
              setTimeout(function() {
                storage.isURLRevoked(uuid, function(err, value) {
                  if (err) {
                    throw err;
                  }
                  expect(value).to.equal(false);
                  done();
                });
              }, 20);
            });
        });

        it("should not return a non existing token", function(done) {
          storage.isURLRevoked("does-not-exist", function(err, value) {
            expect(value).to.equal(false);
            done(err);
          });
        });
      });

      describe("#addUserSimplePushURL", function() {
        it("should be able to add a user simple push URL", function(done) {
          storage.addUserSimplePushURL(userMac, simplePushURL, function(err) {
            if (err) {
              throw err;
            }
            storage.getUserSimplePushURLs(userMac, function(err, urls) {
              expect(urls).to.have.length(1);
              expect(urls).to.eql([simplePushURL]);
              done(err);
            });
          });
        });

        it("should overwrite existing simple push URLs", function(done) {
          storage.addUserSimplePushURL(userMac, simplePushURL, function(err) {
            storage.addUserSimplePushURL(userMac, simplePushURL + '2',
              function(err) {
                storage.getUserSimplePushURLs(userMac, function(err, urls) {
                  expect(urls).to.have.length(1);
                  expect(urls).to.eql([simplePushURL + '2']);
                  done(err);
                });
              });
          });
        });
      });

      describe("#getUserSimplePushURLs", function() {
        it("should return an empty list if nothing had been registered",
          function(done) {
            storage.getUserSimplePushURLs("does-not-exist",
              function(err, urls) {
                if (err) {
                  throw err;
                }
                expect(urls).to.eql([]);
                done();
              });
          });
      });

      
      describe("#addUserCalls", function() {
        it("should be able to add one call to the store", function(done) {
          storage.addUserCall(userMac, call, function(err) {
            if (err) {
              throw err;
            }
            storage.getUserCalls(userMac, function(err, results) {
              if (err) {
                throw err;
              }
              expect(results).to.have.length(1);
              expect(results).to.eql([call]);
              done();
            });
          });
        });
      });

      describe("#getUserCalls", function() {
        it("should keep a list of the user calls", function(done) {
          storage.addUserCall(userMac, calls[0], function() {
            storage.addUserCall(userMac, calls[1], function() {
              storage.addUserCall(userMac, calls[2], function() {
                storage.getUserCalls(userMac, function(err, results) {
                  expect(results).to.have.length(3);
                  expect(results).to.eql(calls);
                  done(err);
                });
              });
            });
          });
        });
      });

      describe("#getCall", function() {
        it("should be able to list a call by its id", function(done) {
          storage.addUserCall(userMac, call, function(err) {
            if (err) {<commit_msg>Add a test.<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";
var expect = require("chai").expect;
var crypto = require("crypto");
var sinon = require("sinon");

var getStorage = require("../loop/storage");
var conf = require("../loop").conf;
var hmac = require("../loop").hmac;

var uuid = "1234";
var user = "<EMAIL>";
var userMac = hmac(user, conf.get("userMacSecret"));
var callerId = '<EMAIL>';
var simplePushURL = "https://push.mozilla.com/test";
var fakeCallInfo = conf.get("fakeCallInfo");


describe("Storage", function() {
  function testStorage(name, createStorage) {
    var storage, 
        a_second = 1 / 3600,  // A second in hours.
        calls = [
        {
          callId:       crypto.randomBytes(16).toString("hex"),
          callerId:     callerId,
          userMac:      userMac,
          sessionId:    fakeCallInfo.session1,
          calleeToken:  fakeCallInfo.token1,
          timestamp:    0
        },
        {
          callId:       crypto.randomBytes(16).toString("hex"),
          callerId:     callerId,
          userMac:      userMac,
          sessionId:    fakeCallInfo.session2,
          calleeToken:  fakeCallInfo.token2,
          timestamp:    1
        },
        {
          callId:       crypto.randomBytes(16).toString("hex"),
          callerId:     callerId,
          userMac:      userMac,
          sessionId:    fakeCallInfo.session3,
          calleeToken:  fakeCallInfo.token2,
          timestamp:    2
        }
      ],
      call = calls[0];

    describe(name, function() {
      beforeEach(function() {
        storage = createStorage({
          tokenDuration: conf.get('tokBox').tokenDuration,
          hawkSessionDuration: conf.get('hawkSessionDuration')
        });
      });
  
      afterEach(function(done) {
        storage.drop(function(err) {
          // Remove the storage reference so tests blow up in an explicit way.
          storage = undefined;
          done(err);
        });
      });

      describe('#revokeURLToken', function() {
        it("should add a revoked url", function(done) {
          storage.revokeURLToken({uuid: uuid, expires: a_second},
            function(err) {
              if (err)  {
                throw err;
              }
              storage.isURLRevoked(uuid, function(err, value){
                expect(value).to.equal(true);
                done(err);
              });
            });
        });
      });

      describe('isURLRevoked', function() {
        it("should not return an expired token", function(done) {
          storage.revokeURLToken({uuid: uuid, expires: a_second / 100},
            function(err) {
              setTimeout(function() {
                storage.isURLRevoked(uuid, function(err, value) {
                  if (err) {
                    throw err;
                  }
                  expect(value).to.equal(false);
                  done();
                });
              }, 20);
            });
        });

        it("should not return a non existing token", function(done) {
          storage.isURLRevoked("does-not-exist", function(err, value) {
            expect(value).to.equal(false);
            done(err);
          });
        });
      });

      describe("#addUserSimplePushURL", function() {
        it("should be able to add a user simple push URL", function(done) {
          storage.addUserSimplePushURL(userMac, simplePushURL, function(err) {
            if (err) {
              throw err;
            }
            storage.getUserSimplePushURLs(userMac, function(err, urls) {
              expect(urls).to.have.length(1);
              expect(urls).to.eql([simplePushURL]);
              done(err);
            });
          });
        });

        it("should overwrite existing simple push URLs", function(done) {
          storage.addUserSimplePushURL(userMac, simplePushURL, function(err) {
            storage.addUserSimplePushURL(userMac, simplePushURL + '2',
              function(err) {
                storage.getUserSimplePushURLs(userMac, function(err, urls) {
                  expect(urls).to.have.length(1);
                  expect(urls).to.eql([simplePushURL + '2']);
                  done(err);
                });
              });
          });
        });
      });

      describe("#getUserSimplePushURLs", function() {
        it("should return an empty list if nothing had been registered",
          function(done) {
            storage.getUserSimplePushURLs("does-not-exist",
              function(err, urls) {
                if (err) {
                  throw err;
                }
                expect(urls).to.eql([]);
                done();
              });
          });
      });

      
      describe("#addUserCalls", function() {
        it("should be able to add one call to the store", function(done) {
          storage.addUserCall(userMac, call, function(err) {
            if (err) {
              throw err;
            }
            storage.getUserCalls(userMac, function(err, results) {
              if (err) {
                throw err;
              }
              expect(results).to.have.length(1);
              expect(results).to.eql([call]);
              done();
            });
          });
        });
      });

      describe("#getUserCalls", function() {
        var sandbox;

        beforeEach(function() {
          sandbox = sinon.sandbox.create();
        });

        afterEach(function() {
          sandbox.restore();
        });

        it("should keep a list of the user calls", function(done) {
          storage.addUserCall(userMac, calls[0], function() {
            storage.addUserCall(userMac, calls[1], function() {
              storage.addUserCall(userMac, calls[2], function() {
                storage.getUserCalls(userMac, function(err, results) {
                  expect(results).to.have.length(3);
                  expect(results).to.eql(calls);
                  done(err);
                });
              });
            });
          });
        });

        it("should handle storage errors correctly.", function(done) {
          sandbox.stub(storage._client, "smembers",
            function(key, cb){
              cb("error");
            });

          storage.getUserCalls(userMac, function(err, results) {
            expect(err).to.eql("error");
            expect(typeof results).to.eql("undefined");
            done();
          });
        });
      });

      describe("#getCall", function() {
        it("should be able to list a call by its id", function(done) {
          storage.addUserCall(userMac, call, function(err) {
            if (err) {
{'fraction_non_alphanumeric': 0.12846077153155916, 'fraction_numerical': 0.006595597821918858, 'mean_word_length': 1.6043539045336528, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '3702122', 'n_tokens_mistral': 3585, 'n_tokens_neox': 3483, 'n_words': 738}