Commit cbdd3de7 authored by Varunkumar Manohar's avatar Varunkumar Manohar
Browse files

Fix unit tests

parent b556784d
Pipeline #33514 passed with stages
in 8 minutes and 55 seconds
......@@ -24,7 +24,6 @@ import { DatasetDAO } from '../../../src/services/dataset';
import { SubProjectDAO, SubprojectGroups, SubProjectModel } from '../../../src/services/subproject';
import { SubProjectHandler } from '../../../src/services/subproject/handler';
import { SubProjectOP } from '../../../src/services/subproject/optype';
import { SubProjectParser } from '../../../src/services/subproject/parser';
import { TenantDAO, TenantModel } from '../../../src/services/tenant';
import { Response } from '../../../src/shared';
import { Tx } from '../utils';
......@@ -217,15 +216,21 @@ export class TestSubProjectSVC {
});
Tx.testExp(async (done: any) => {
// this.sandbox.stub(Datastore.prototype, 'createQuery').returns(this.query);
this.journal.runQuery.resolves([[]] as never);
this.sandbox.stub(SubProjectDAO, 'constructServiceGroupACLs').resolves({
"admins": ["admin@xyz.com"],
"viewers": ["viewer@xyz.com"]
})
await SubProjectDAO.list(this.journal, 'tnx');
done();
});
Tx.testExp(async (done: any) => {
const entityID = []; entityID[this.journal.KEY] = { name: 'name' };
// this.sandbox.stub(Datastore.prototype, 'createQuery').returns(this.query);
this.sandbox.stub(SubProjectDAO, 'constructServiceGroupACLs').resolves({
"admins": ["admin@xyz.com"],
"viewers": ["viewer@xyz.com"]
})
this.journal.runQuery.resolves([[entityID]] as never);
await SubProjectDAO.list(this.journal, 'tnx');
done();
......@@ -233,7 +238,10 @@ export class TestSubProjectSVC {
Tx.testExp(async (done: any) => {
const entityID = []; entityID[this.journal.KEY] = { name: 'name' };
// this.sandbox.stub(Datastore.prototype, 'createQuery').returns(this.query);
this.sandbox.stub(SubProjectDAO, 'constructServiceGroupACLs').resolves({
"admins": ["admin@xyz.com"],
"viewers": ["viewer@xyz.com"]
})
this.journal.runQuery.resolves([[{ name: 'name', tenant: 'tenant' }]] as never);
await SubProjectDAO.list(this.journal, 'tnx');
done();
......@@ -298,9 +306,9 @@ export class TestSubProjectSVC {
randomBucketName() { return ''; }
};
this.sandbox.stub(StorageFactory, 'build').returns(storage);
this.sandbox.stub(SubprojectGroups, 'adminGroup').returns('admingroup');
this.sandbox.stub(SubprojectGroups, 'editorGroup').returns('editorgroup');
this.sandbox.stub(SubprojectGroups, 'viewerGroup').returns('viewergroup');
this.sandbox.stub(SubprojectGroups, 'serviceAdminGroup').returns('admingroup');
this.sandbox.stub(SubprojectGroups, 'serviceEditorGroup').returns('editorgroup');
this.sandbox.stub(SubprojectGroups, 'serviceViewerGroup').returns('viewergroup');
this.sandbox.stub(AuthGroups, 'clearGroup').resolves();
await SubProjectHandler.handler(expReq, expRes, SubProjectOP.Delete);
......
......@@ -16,18 +16,17 @@
import { Datastore } from '@google-cloud/datastore';
import { Request as expRequest, Response as expResponse } from 'express';
import { TenantDAO, TenantGroups, TenantModel } from '../../../src/services/tenant';
import { TenantHandler } from '../../../src/services/tenant/handler';
import sinon from 'sinon';
import { Auth, AuthGroups } from '../../../src/auth';
import { google, JournalFactoryServiceClient } from '../../../src/cloud';
import { Config } from '../../../src/cloud';
import { Config, google, JournalFactoryServiceClient } from '../../../src/cloud';
import { SubProjectDAO } from '../../../src/services/subproject';
import { TenantDAO, TenantGroups, TenantModel } from '../../../src/services/tenant';
import { TenantHandler } from '../../../src/services/tenant/handler';
import { TenantOP } from '../../../src/services/tenant/optype';
import { TenantParser } from '../../../src/services/tenant/parser';
import { Response } from '../../../src/shared';
import { Tx } from '../utils';
import sinon from 'sinon';
export class TestTenantSVC {
......@@ -105,7 +104,7 @@ export class TestTenantSVC {
Tx.testExp(async (done: any, expReq: expRequest, expRes: expResponse) => {
expReq.query.datapartition = 'datapartition';
this.sandbox.stub(TenantDAO, 'getAll').resolves([{name:'tenant01', default_acls:'x', esd:'datapartition.domain.com', gcpid:'any'}])
this.sandbox.stub(TenantDAO, 'getAll').resolves([{ name: 'tenant01', default_acls: 'x', esd: 'datapartition.domain.com', gcpid: 'any' }])
this.sandbox.stub(Auth, 'isUserRegistered').resolves();
Tx.checkTrue((await TenantHandler.getTenantSDPath(expReq)) === Config.SDPATHPREFIX + 'tenant01', done);
});
......@@ -233,13 +232,14 @@ export class TestTenantSVC {
Tx.sectionInit('tenant parser');
Tx.testExp(async (done: any, expReq: expRequest, expRes: expResponse) => {
expReq.query.datapartition = 'datapartition';
Tx.testExp((done: any, expReq: expRequest, expRes: expResponse) => {
expReq.query.datapartition = 'tenant-a';
expReq.params.tenantid = 'tenant-a';
expReq.body.esd = 'esd';
expReq.body.esd = 'tenant-a.evt.group.com';
expReq.body.gcpid = 'gcpid';
expReq.body.default_acls = 'userdatalakeadmin@group.com';
await TenantParser.create(expReq);
expReq.body.default_acls = 'userdatalakeadmin@tenant-a.evt.group.com';
Config.CLOUDPROVIDER = "google"
TenantParser.create(expReq);
done();
});
}
......@@ -251,21 +251,11 @@ export class TestTenantSVC {
Tx.testExp(async (done: any, expReq: expRequest, expRes: expResponse) => {
expReq.query.datapartition = 'datapartition';
this.tenant.default_acls = 'authgroup@dp.com';
const result = TenantGroups.adminGroup(this.tenant);
Tx.checkTrue(result === 'authgroup@dp.com', done);
});
Tx.testExp(async (done: any, expReq: expRequest, expRes: expResponse) => {
expReq.query.datapartition = 'datapartition';
this.tenant.default_acls = undefined;
const result = TenantGroups.adminGroup(this.tenant);
const expectedString = 'service.seistore.' + Config.SERVICE_ENV + '.tenant-a.admin@esd';
Tx.checkTrue(result === expectedString, done);
});
}
}
......@@ -17,6 +17,9 @@
import { Request as expRequest, Response as expResponse } from 'express';
import sinon from 'sinon';
import { Auth, AuthGroups } from '../../../src/auth';
import { JournalFactoryTenantClient } from '../../../src/cloud';
import { Config } from '../../../src/cloud/config';
import { google } from '../../../src/cloud/providers';
import { SubProjectDAO, SubprojectGroups, SubProjectModel } from '../../../src/services/subproject';
import { TenantDAO } from '../../../src/services/tenant';
import { UserHandler } from '../../../src/services/user/handler';
......@@ -36,6 +39,17 @@ export class TestUserSVC {
beforeEach(() => {
this.spy = sinon.createSandbox();
this.spy.stub(Response, 'writeMetric').returns();
this.subproject = {
name: 'subproject-test',
admin: 'admin@xyz.com',
acls: {
admins: [],
viewers: []
}
} as SubProjectModel
this.journal = this.spy.createStubInstance(google.DatastoreDAO);
Config.CLOUDPROVIDER = 'google';
});
afterEach(() => { this.spy.restore(); });
......@@ -50,6 +64,8 @@ export class TestUserSVC {
}
private static spy: sinon.SinonSandbox;
private static subproject: SubProjectModel;
private static journal: any;
private static add() {
......@@ -62,6 +78,8 @@ export class TestUserSVC {
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(AuthGroups, 'addUserToGroup');
this.spy.stub(UserHandler, 'doNotThrowIfNotMember' as never).resolves();
this.spy.stub(JournalFactoryTenantClient, 'get').returns(this.journal)
this.spy.stub(SubProjectDAO, 'get').resolves(this.subproject)
await UserHandler.handler(expReq, expRes, UserOP.Add);
Tx.check200(expRes.statusCode, done);
});
......@@ -73,6 +91,8 @@ export class TestUserSVC {
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(AuthGroups, 'addUserToGroup');
this.spy.stub(UserHandler, 'doNotThrowIfNotMember' as never).resolves();
this.spy.stub(JournalFactoryTenantClient, 'get').returns(this.journal)
this.spy.stub(SubProjectDAO, 'get').resolves(this.subproject)
await UserHandler.handler(expReq, expRes, UserOP.Add);
Tx.check200(expRes.statusCode, done);
});
......@@ -84,7 +104,9 @@ export class TestUserSVC {
this.spy.stub(AuthGroups, 'removeUserFromGroup').resolves();
this.spy.stub(AuthGroups, 'addUserToGroup');
this.spy.stub(UserHandler, 'doNotThrowIfNotMember' as never).resolves();
this.spy.stub(TenantDAO, 'get').resolves({name: 'tenant-a', esd: 'esd', gcpid: 'gcpid'} as any);
this.spy.stub(TenantDAO, 'get').resolves({ name: 'tenant-a', esd: 'esd', gcpid: 'gcpid' } as any);
this.spy.stub(JournalFactoryTenantClient, 'get').returns(this.journal)
this.spy.stub(SubProjectDAO, 'get').resolves(this.subproject)
await UserHandler.handler(expReq, expRes, UserOP.Add);
Tx.check200(expRes.statusCode, done);
});
......@@ -93,6 +115,8 @@ export class TestUserSVC {
expReq.body.email = 'user@user.com';
expReq.body.path = 'sd://tnx';
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(JournalFactoryTenantClient, 'get').returns(this.journal)
this.spy.stub(SubProjectDAO, 'get').resolves(this.subproject)
await UserHandler.handler(expReq, expRes, UserOP.Add);
Tx.check400(expRes.statusCode, done);
});
......@@ -102,6 +126,8 @@ export class TestUserSVC {
{ email: '', sdPath: { tenant: 'tnx01', subproject: 'spx' } as SDPathModel, groupRole: 'none' });
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(Response, 'writeError');
this.spy.stub(SubProjectDAO, 'get').resolves(this.subproject)
this.spy.stub(JournalFactoryTenantClient, 'get').returns(this.journal)
await UserHandler.handler(expReq, expRes, UserOP.Add);
done();
});
......@@ -141,7 +167,11 @@ export class TestUserSVC {
expReq.body.email = 'user2@user.com';
expReq.body.path = 'sd://tnx/spx';
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(JournalFactoryTenantClient, 'get').returns(this.journal)
this.spy.stub(AuthGroups, 'removeUserFromGroup');
this.subproject.acls.admins = ["group1", "group2"]
this.subproject.acls.viewers = ["vgroup1", "vgroup2"]
this.spy.stub(SubProjectDAO, 'get').resolves(this.subproject)
await UserHandler.handler(expReq, expRes, UserOP.Remove);
Tx.check200(expRes.statusCode, done);
});
......@@ -150,6 +180,8 @@ export class TestUserSVC {
expReq.body.email = 'user2@user.com';
expReq.body.path = 'sd://tnx';
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(JournalFactoryTenantClient, 'get').returns(this.journal)
this.spy.stub(SubProjectDAO, 'get').resolves(this.subproject)
await UserHandler.handler(expReq, expRes, UserOP.Remove);
Tx.check400(expRes.statusCode, done);
});
......@@ -158,6 +190,8 @@ export class TestUserSVC {
expReq.body.email = 'user@user.com';
expReq.body.path = 'sd://tnx/spx';
this.spy.stub(Response, 'writeError');
this.spy.stub(JournalFactoryTenantClient, 'get').returns(this.journal)
this.spy.stub(SubProjectDAO, 'get').resolves(this.subproject)
await UserHandler.handler(expReq, expRes, UserOP.Remove);
done();
});
......@@ -189,6 +223,7 @@ export class TestUserSVC {
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(Auth, 'isUserAuthorized');
this.spy.stub(AuthGroups, 'listUsersInGroup').resolves([{ email: 'userx' }] as never);
this.spy.stub(SubProjectDAO, 'get').resolves(this.subproject)
await UserHandler.handler(expReq, expRes, UserOP.List);
Tx.check200(expRes.statusCode, done);
});
......@@ -218,7 +253,7 @@ export class TestUserSVC {
const prefix = SubprojectGroups.groupPrefix('tnx', 'spx');
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(AuthGroups, 'getUserGroups').resolves([{ name: prefix + '.abc' }] as any);
this.spy.stub(SubProjectDAO, 'list').resolves([{name: 'spx'} as SubProjectModel ] as any)
this.spy.stub(SubProjectDAO, 'list').resolves([{ name: 'spx' } as SubProjectModel] as any)
await UserHandler.handler(expReq, expRes, UserOP.Roles);
Tx.check200(expRes.statusCode, done);
});
......@@ -228,7 +263,7 @@ export class TestUserSVC {
const prefix = SubprojectGroups.groupPrefix('tnx', 'spx');
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(AuthGroups, 'getUserGroups').resolves([{ name: prefix + '.abc.abc' }] as any);
this.spy.stub(SubProjectDAO, 'list').resolves([{name: 'spx'} as SubProjectModel ] as any)
this.spy.stub(SubProjectDAO, 'list').resolves([{ name: 'spx' } as SubProjectModel] as any)
await UserHandler.handler(expReq, expRes, UserOP.Roles);
Tx.check200(expRes.statusCode, done);
});
......@@ -237,7 +272,7 @@ export class TestUserSVC {
expReq.query.sdpath = 'sd://tnx';
this.spy.stub(TenantDAO, 'get').resolves({} as any);
this.spy.stub(AuthGroups, 'getUserGroups').resolves([] as any);
this.spy.stub(SubProjectDAO, 'list').resolves([{name: 'spx'} as SubProjectModel ] as any)
this.spy.stub(SubProjectDAO, 'list').resolves([{ name: 'spx' } as SubProjectModel] as any)
await UserHandler.handler(expReq, expRes, UserOP.Roles);
Tx.check200(expRes.statusCode, done);
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment