import {expect, tap} from "tapbundle"; import {HaventecCloudPortal} from "../dist/index"; import * as request from "superagent"; import * as mockRequest from "superagent-mock"; import {LocalData} from "@haventec/common-js"; var mockConfig = [ { pattern: 'http://mytest.com(.*)', fixtures: function (match, params, headers) { if (match[1] === '/admin/groups/TestApplicationUUID/internal?page=0&size=10') { return ({ body: {"responseStatus": {"status":"SUCCESS","message":"Loaded","groups":{"number":0,"size":10,"totalPages":1,"numberOfElements":1,"totalElements":1,"previousPage":false,"firstPage":true,"nextPage":false,"lastPage":true,"content":[{"groupname":"root","email":null,"tenancyUUID":"648bd4bf-d7b8-4c9f-9b51-433460dda0ae","lastLogin":1494309643,"dateCreated":1494309643,"provisionToken":"","locked":false,"preProvisioned":false}]}}} }); } if (match[1] === '/admin/groups/TestApplicationUUID/internal/testgroup') { return ({ body: {"responseStatus": {"status":"SUCCESS","message":"Loaded","groups":{"number":1,"size":1,"totalPages":1,"numberOfElements":1,"totalElements":1,"previousPage":false,"firstPage":true,"nextPage":false,"lastPage":true,"content":[{"groupname":"root","email":null,"tenancyUUID":"648bd4bf-d7b8-4c9f-9b51-433460dda0ae","lastLogin":1494309643,"dateCreated":1494309643,"provisionToken":"","locked":false,"preProvisioned":false}]}}} }); } if (match[1] === '/admin/groups/TestApplicationUUID/internal') { return ({ body: {"responseStatus": {"status":"SUCCESS","message":"Loaded","groups":{"number":1,"size":1,"totalPages":1,"numberOfElements":1,"totalElements":1,"previousPage":false,"firstPage":true,"nextPage":false,"lastPage":true,"content":[{"groupname":"root","email":null,"tenancyUUID":"648bd4bf-d7b8-4c9f-9b51-433460dda0ae","lastLogin":1494309643,"dateCreated":1494309643,"provisionToken":"","locked":false,"preProvisioned":false}]}}} }); } if (match[1] === '/admin/groups/TestApplicationUUID/role/testgroup') { return ({ body: {"responseStatus": {"status":"SUCCESS"}} }); } return 'foo'; }, get: function (match, data) { return data; }, delete: function (match, data) { return data; }, post: function (match, data) { return data; } }, { pattern: 'http://myerrortest.com(.*)', fixtures: function (match, params, headers) { 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 list groups", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ld : LocalData = new LocalData("testuser", null, null, null, null, "TestApplicationUUID", null, null, null, null, null); ha.getAuthService().getLocalDataService().setLocalData("testuser", ld); let ret = await ha.listGroups(0, 10); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal get group", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ld : LocalData = new LocalData("testuser", null, null, null, null, "TestApplicationUUID", null, null, null, null, null); ha.getAuthService().getLocalDataService().setLocalData("testuser", ld); let ret = await ha.getGroup('testgroup'); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal delete a group", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ld : LocalData = new LocalData("testuser", null, null, null, null, "TestApplicationUUID", null, null, null, null, null); ha.getAuthService().getLocalDataService().setLocalData("testuser", ld); let ret = await ha.deleteGroup('testgroup'); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal update a group", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ld : LocalData = new LocalData("testuser", null, null, null, null, "TestApplicationUUID", null, null, null, null, null); ha.getAuthService().getLocalDataService().setLocalData("testuser", ld); let ret = await ha.updateGroup('testgroup', 'test group'); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal getGroupRoles for a group", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ld : LocalData = new LocalData("testuser", null, null, null, null, "TestApplicationUUID", null, null, null, null, null); ha.getAuthService().getLocalDataService().setLocalData("testuser", ld); let ret = await ha.getGroupRoles('testgroup'); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.test("it should test HaventecCloudPortal updateGroupRoles for a group", async () => { var superagentMock = mockRequest(request, mockConfig, logger); let ha = new HaventecCloudPortal("http://mytest.com", true); let ld : LocalData = new LocalData("testuser", null, null, null, null, "TestApplicationUUID", null, null, null, null, null); let roleNames = ['AUDIT', 'SUPPORT']; ha.getAuthService().getLocalDataService().setLocalData("testuser", ld); let ret = await ha.updateGroupRoles('testgroup', roleNames); expect(ret['responseStatus']['status']).to.be.equal("SUCCESS"); superagentMock.unset(); }); tap.start()