import {expect, tap} from "tapbundle"; import {HaventecCloudPortal} from "../dist/index"; import * as request from "superagent"; import * as mockRequest from "superagent-mock"; var mockConfig = [ { pattern: 'http://mytest.com(.*)', fixtures: function (match, params, headers) { if (match[1] === '/authentication/login') { return ({body: {responseStatus: {status: "SUCCESS"}}}); } if (match[1] === '/authentication/logout') { return ({body: {responseStatus: {status: "SUCCESS"}}}); } if (match[1] === '/authentication/activate/user') { return ({body: {responseStatus: {status: "SUCCESS"}}}); } if (match[1] === 'NotUsed') { return ({body: {responseStatus: {status: "SUCCESS"}}}); } if (match[1] === 'NotUsed') { return ({body: {responseStatus: {status: "SUCCESS"}}}); } return 'foo'; }, get: function (match, data) { return data; }, post: function (match, data) { return data; } }, { pattern: 'http://myerrortest.com(.*)', fixtures: function (match, params, headers) { if (match[1] === '/authentication/login') { return ({body: {responseStatus: {status: "ERROR"}}}); } if (match[1] === '/authentication/logout') { return ({body: {responseStatus: {status: "ERROR"}}}); } if (match[1] === '/authentication/activate/user') { return ({body: {responseStatus: {status: "ERROR"}}}); } if (match[1] === 'NotUsed') { return ({body: {responseStatus: {status: "ERROR"}}}); } if (match[1] === 'NotUsed') { return ({body: {responseStatus: {status: "ERROR"}}}); } return 'foo'; }, get: function (match, data) { return data; }, post: function (match, data) { return data; } } ]; var logger = function(log) { // console.log('superagent call', log); }; tap.test("it should test HaventecCloudPortal sessionValid - returning false", async () => { let ha = new HaventecCloudPortal("http://mytest.com", true); let ret = await ha.sessionValid(); expect(ret).to.be.false; }); tap.test("it should test HaventecCloudPortal sessionValid - returning true", async () => { let ha = new HaventecCloudPortal("http://mytest.com", true); let as = ha.getAuthService(); as.setAccessToken({mytoken: 'token'}); let ret = await ha.sessionValid(); expect(ret).to.be.true; }); tap.test("it should test HaventecCloudPortal login", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ret = await ha.login("testuser", "1234"); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); let ret2 = await ha.login("testuser2", "1234"); expect(ret2['responseStatus']['status']).to.be.equal("SUCCESS"); let ret3 = await ha.login("testuser", "1234"); expect(ret3['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal logout", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ret = await ha.logout(); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal activate", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ret = await ha.activate("testuser", "1234", 'MyApplicationUUID', 'my_token'); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal forgotpin", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ret = await ha.forgotpin("testuser", "1234"); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal reprovision", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ret = await ha.reprovision("testuser", "1234", 'tesRequestId', 'TestToken'); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal login error", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://myerrortest.com", true); let did_error = false; try { let ret = await ha.login("testuser", "1234"); } catch ( error ) { did_error = true; } expect(did_error).to.be.true; superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal logout error", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://myerrortest.com", true); let did_error = false; try { let ret = await ha.logout(); } catch ( error ) { did_error = true; } expect(did_error).to.be.true; superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal activate error", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://myerrortest.com", true); let did_error = false; try { let ret = await ha.activate("testuser", "1234", 'MyApplicationUUID', 'my_token'); } catch ( error ) { did_error = true; } expect(did_error).to.be.true; superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal forgotpin error", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://myerrortest.com", true); let did_error = false; try { let ret = await ha.forgotpin("testuser", "1234"); } catch ( error ) { did_error = true; } expect(did_error).to.be.true; superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal reprovision() error", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://myerrortest.com", true); let did_error = false; try { let ret = await ha.reprovision("testuser", "1234", 'tesRequestId', 'TestToken'); } catch ( error ) { did_error = true; } expect(did_error).to.be.true; superagentMock.unset(); }); tap.start()