|
| 1 | +/*global describe, it, before, after, beforeEach*/ |
| 2 | +import mockedFunctions from "../setupMocks.js"; |
| 3 | +import * as chai from 'chai'; |
| 4 | +import {countDownload, getCountDownloadSchema} from "../../../src/api/countDownload.js"; |
| 5 | +import {getSimpleGetReply, getSimpleGETRequest} from '../data/simple-request.js'; |
| 6 | +import {REGISTRY_PACKAGE_JSON, VALID_PACKAGE_JSON} from '../data/packagejson.js'; |
| 7 | +import Ajv from "ajv"; |
| 8 | +import db from "../../../src/db.js"; |
| 9 | + |
| 10 | +export const AJV = new Ajv(); |
| 11 | + |
| 12 | + |
| 13 | +let expect = chai.expect; |
| 14 | + |
| 15 | +describe('unit Tests for countDownload api', function () { |
| 16 | + let savedGetFromIndex, savedMathAdd; |
| 17 | + before(()=>{ |
| 18 | + savedGetFromIndex = db.getFromIndex; |
| 19 | + savedMathAdd = db.mathAdd; |
| 20 | + }); |
| 21 | + after(()=>{ |
| 22 | + db.getFromIndex = savedGetFromIndex; |
| 23 | + db.mathAdd = savedMathAdd; |
| 24 | + }); |
| 25 | + beforeEach(function () { |
| 26 | + db.getFromIndex = function (_tableName) { |
| 27 | + return { |
| 28 | + isSuccess: true, |
| 29 | + documents: [REGISTRY_PACKAGE_JSON]}; |
| 30 | + }; |
| 31 | + db.mathAdd = function (_tableName) { |
| 32 | + return { |
| 33 | + isSuccess: true |
| 34 | + }; |
| 35 | + }; |
| 36 | + }); |
| 37 | + function _getRequest(extensionName="ext.name", extensionVersion ="0.0.1") { |
| 38 | + let request = getSimpleGETRequest(); |
| 39 | + request.query.extensionName = extensionName; |
| 40 | + request.query.extensionVersion = extensionVersion; |
| 41 | + return request; |
| 42 | + } |
| 43 | + |
| 44 | + it('should countDownload throw if db get failed', async function () { |
| 45 | + db.getFromIndex = function (_tableName) { |
| 46 | + return {isSuccess: false}; |
| 47 | + }; |
| 48 | + let error; |
| 49 | + try{ |
| 50 | + await countDownload(_getRequest(), getSimpleGetReply()); |
| 51 | + } catch(e){ |
| 52 | + error = e; |
| 53 | + } |
| 54 | + expect(error.message).eql("Error getting extensionPKG details from db: ext.name"); |
| 55 | + }); |
| 56 | + it('should countDownload throw if no such extension', async function () { |
| 57 | + db.getFromIndex = function (_tableName) { |
| 58 | + return {isSuccess: true, documents:[]}; |
| 59 | + }; |
| 60 | + let error; |
| 61 | + try{ |
| 62 | + await countDownload(_getRequest(), getSimpleGetReply()); |
| 63 | + } catch(e){ |
| 64 | + error = e; |
| 65 | + } |
| 66 | + expect(error.message).eql("No such extension"); |
| 67 | + }); |
| 68 | + it('should countDownload throw if no such extension version', async function () { |
| 69 | + let error; |
| 70 | + try{ |
| 71 | + await countDownload(_getRequest("dd", "1.2.3"), getSimpleGetReply()); |
| 72 | + } catch(e){ |
| 73 | + error = e; |
| 74 | + } |
| 75 | + expect(error.message).eql("No such extension version"); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should countDownload increment download', async function () { |
| 79 | + let helloResponse = await countDownload(_getRequest(), getSimpleGetReply()); |
| 80 | + expect(helloResponse).eql({ |
| 81 | + "message": "Done" |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should countDownload failed is failed to increment in db', async function () { |
| 86 | + db.mathAdd = function (_tableName) {return {isSuccess: false};}; |
| 87 | + let error; |
| 88 | + try{ |
| 89 | + await countDownload(_getRequest(), getSimpleGetReply()); |
| 90 | + } catch(e){ |
| 91 | + error = e; |
| 92 | + } |
| 93 | + expect(error.message).eql("Could not increment download count."); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should validate schemas for sample request/responses', async function () { |
| 97 | + let request = _getRequest(); |
| 98 | + // request |
| 99 | + const requestValidator = AJV.compile(getCountDownloadSchema().schema.querystring); |
| 100 | + expect(requestValidator(request.query)).to.be.true; |
| 101 | + // remove a required field |
| 102 | + delete request.query.extensionName; |
| 103 | + expect(requestValidator(request.query)).to.be.false; |
| 104 | + // response |
| 105 | + const successResponseValidator = AJV.compile(getCountDownloadSchema().schema.response["200"]); |
| 106 | + let response = await countDownload(_getRequest(), getSimpleGetReply()); |
| 107 | + expect(successResponseValidator(response)).to.be.true; |
| 108 | + }); |
| 109 | +}); |
0 commit comments