From 2c3a8f9d42ce8e1d38ad3a4adbd639bc74ced303 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Fri, 25 Aug 2017 21:16:55 +0200 Subject: [PATCH] test: Add unit test for a scenario when the database is corrupted --- test/unit/local-data.spec.js | 37 +++++++++++++++++++ .../storage/verdaccio-corrupted.db.json | 1 + test/unit/partials/storage/verdaccio.db.json | 1 + 3 files changed, 39 insertions(+) create mode 100644 test/unit/local-data.spec.js create mode 100644 test/unit/partials/storage/verdaccio-corrupted.db.json create mode 100644 test/unit/partials/storage/verdaccio.db.json diff --git a/test/unit/local-data.spec.js b/test/unit/local-data.spec.js new file mode 100644 index 000000000..0627889f6 --- /dev/null +++ b/test/unit/local-data.spec.js @@ -0,0 +1,37 @@ +'use strict'; + +const assert = require('assert'); +const LocalData = require('../../src/lib/storage/local/local-data'); +const path = require('path'); +const _ = require('lodash'); + + +describe('Local Database', function() { + + + describe('reading database', () => { + + const buildCorruptedPath = () => path.join(__dirname, './partials/storage/verdaccio-corrupted.db.json'); + + it('should return empty database on read corrupted database', () => { + const config = new LocalData(buildCorruptedPath()); + assert(_.isEmpty(config.data.list)); + }); + + it('should return a database on read valid database', () => { + const config = new LocalData(path.join(__dirname, './partials/storage/verdaccio.db.json')); + assert(_.isEmpty(config.data.list) === false); + }); + + it('should fails on sync a corrupted database', () => { + const config = new LocalData(buildCorruptedPath()); + const error = config.sync(); + + assert(_.isError(error)); + assert(error.message.match(/locked/)); + }); + + }); + +}); + diff --git a/test/unit/partials/storage/verdaccio-corrupted.db.json b/test/unit/partials/storage/verdaccio-corrupted.db.json new file mode 100644 index 000000000..9950bb074 --- /dev/null +++ b/test/unit/partials/storage/verdaccio-corrupted.db.json @@ -0,0 +1 @@ +{"list"[],"secret":"14705eeaed167749990dafa07f908d2a9504bfdb0f6ca4102eed5acba0bc9076"} diff --git a/test/unit/partials/storage/verdaccio.db.json b/test/unit/partials/storage/verdaccio.db.json new file mode 100644 index 000000000..3759b48f0 --- /dev/null +++ b/test/unit/partials/storage/verdaccio.db.json @@ -0,0 +1 @@ +{"list":["@verdaccio/test"],"secret":"48cd053de97d4ef34aea4f1efb902334442bea1e735df5fdc9424c986a281b3d"}