<|start_filename|>tests/cache.js<|end_filename|>
import chai from "chai";
import sinon from "sinon";
const expect = chai.expect;
const initializationParams = require("./data").initializationParams

import ImageKit from "../index";
import nock from "nock";
var imagekit = new ImageKit(initializationParams);

const dummyAPISuccessResponse = {
    dummyKey: "dummyValue"
};

const dummyAPIErrorResponse = {
    help: "help",
    message: "message"
}

describe("Cache purge API", function () {
    describe("Request body check", function () {
        it('Purge cache', function (done) {
            var url = "http://ik.imagekit.io/demo/default-image.jpg";

            const scope = nock('https://api.imagekit.io')
                .post("/v1/files/purge")
                .basicAuth({ user: initializationParams.privateKey, pass: '' })
                .reply(function (uri, requestBody) {
                    expect(requestBody).to.deep.equal({
                        url: url
                    });
                    done();
                    return [200];
                })

            imagekit.purgeCache(url);
        });

        it('Purge cache no url', function (done) {
            imagekit.purgeCache("", function (err, response) {
                expect(err).to.deep.equal({
                    help: "",
                    message: "Missing URL parameter for this request"
                });
                done();
            });
        });

        it('Purge cache', function (done) {
            var requestId = "sdfdsfksjfldsjfjsdf";

            const scope = nock('https://api.imagekit.io')
                .get(`/v1/files/purge/${requestId}`)
                .basicAuth({ user: initializationParams.privateKey, pass: '' })
                .reply(function (uri, requestBody) {
                    done();
                    return [200];
                })

            imagekit.getPurgeCacheStatus(requestId);
        });

        it('Purge cache missing requestId', function (done) {
            imagekit.getPurgeCacheStatus("", function (err, response) {
                expect(err).to.deep.equal({
                    help: "",
                    message: "Missing Request ID parameter for this request"
                });
                done();
            });
        });
    });

    describe("Success callbacks", function () {
        it('Purge cache', function (done) {
            var url = "http://ik.imagekit.io/demo/default-image.jpg";

            const scope = nock('https://api.imagekit.io')
                .post("/v1/files/purge")
                .basicAuth({ user: initializationParams.privateKey, pass: '' })
                .reply(200, dummyAPISuccessResponse)

            var callback = sinon.spy();
            imagekit.purgeCache(url, callback);

            setTimeout(function () {
                expect(callback.calledOnce).to.be.true;
                sinon.assert.calledWith(callback, null, dummyAPISuccessResponse);
                done();
            }, 50);
        });

        it('Purge cache', function (done) {
            var requestId = "sdfdsfksjfldsjfjsdf";

            const scope = nock('https://api.imagekit.io')
                .get(`/v1/files/purge/${requestId}`)
                .basicAuth({ user: initializationParams.privateKey, pass: '' })
                .reply(200, dummyAPISuccessResponse)

            var callback = sinon.spy();

            imagekit.getPurgeCacheStatus(requestId, callback);

            setTimeout(function () {
                expect(callback.calledOnce).to.be.true;
                sinon.assert.calledWith(callback, null, dummyAPISuccessResponse);
                done();
            }, 50);
        });
    });

    describe("Error callbacks", function () {
        it('Purge cache', function (done) {
            var url = "http://ik.imagekit.io/demo/default-image.jpg";

            const scope = nock('https://api.imagekit.io')
                .post("/v1/files/purge")
                .basicAuth({ user: initializationParams.privateKey, pass: '' })
                .reply(500, dummyAPIErrorResponse)

            var callback = sinon.spy();
            imagekit.purgeCache(url, callback);

            setTimeout(function () {
                expect(callback.calledOnce).to.be.true;
                sinon.assert.calledWith(callback, dummyAPIErrorResponse, null);
                done();
            }, 50);
        });

        it('Purge cache', function (done) {
            var requestId = "sdfdsfksjfldsjfjsdf";

            const scope = nock('https://api.imagekit.io')
                .get(`/v1/files/purge/${requestId}`)
                .basicAuth({ user: initializationParams.privateKey, pass: '' })
                .reply(500, dummyAPIErrorResponse)

            var callback = sinon.spy();

            imagekit.getPurgeCacheStatus(requestId, callback);

            setTimeout(function () {
                expect(callback.calledOnce).to.be.true;
                sinon.assert.calledWith(callback, dummyAPIErrorResponse, null);
                done();
            }, 50);
        });
    });
});



<|start_filename|>tests/helpers/spies.js<|end_filename|>
// packages
import sinon from 'sinon';
// internal modules
import pHashUtils from "../../utils/phash";

// spies
const pHashDistanceSpy = sinon.spy(pHashUtils, 'pHashDistance');

module.exports = {
  pHashDistanceSpy,
};


<|start_filename|>tests/helpers/errors.js<|end_filename|>
import errors from '../../libs/constants/errorMessages';
module.exports = { ...errors };


<|start_filename|>tests/initialization.js<|end_filename|>
import chai from "chai";
const expect = chai.expect;
const initializationParams = require("./data").initializationParams
import ImageKit from "../index";

describe("Initialization checks", function () {
    var imagekit = new ImageKit(initializationParams);

    it('should throw error', function () {
        try {
            new ImageKit({});
        } catch(err) {
            expect(err.message).to.be.equal('Missing publicKey during ImageKit initialization');
        }
    });

    it('should throw error', function () {
        try {
            new ImageKit({
                publicKey: "test_public_key"
            });
        } catch(err) {
            expect(err.message).to.be.equal('Missing privateKey during ImageKit initialization');
        }
    });

    it('should throw error', function () {
        try {
            new ImageKit({
                publicKey: "test_public_key",
                privateKey: "test_private_key"
            });
        } catch(err) {
            expect(err.message).to.be.equal('Missing urlEndpoint during ImageKit initialization');
        }
    });

    it('callback', function () {
        var imagekit = new ImageKit({
            urlEndpoint: "https://ik.imagekit.io/demo",
            publicKey: "test_public_key",
            privateKey: "test_private_key"
        });
        try {
            imagekit.getFileDetails("fileId","wrongCallback");
        } catch(err) {
            expect(err.message).to.be.equal("Callback must be a function.")
        }
    });

    it('should have options object', function () {
        expect(imagekit.options).to.be.an('object');
    });

    it('should have correctly initialized options object.', function () {
        expect(imagekit.options).to.have.property('publicKey').to.be.equal(initializationParams.publicKey);
        expect(imagekit.options).to.have.property('urlEndpoint').to.be.equal(initializationParams.urlEndpoint);
        expect(imagekit.options).to.have.property('authenticationEndpoint').to.be.equal(initializationParams.authenticationEndpoint);
    });

    it("should have callable functions 'url' and 'upload'", function () {
        expect(imagekit.url).to.exist.and.to.be.a('function');
        expect(imagekit.upload).to.exist.and.to.be.a('function');
    });
});