component-dbs-core
Version:
DTO objects shared among device backend
397 lines • 22.2 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const identityDto_1 = require("../identity/dtos/identityDto");
const testing_1 = require("@nestjs/testing");
const config_1 = require("@nestjs/config");
const envService_1 = require("../common_services/config/envService");
const common_1 = require("@nestjs/common");
const ts_mockito_1 = require("ts-mockito");
const auth0Service_1 = require("./auth0Service");
const rxjs_1 = require("rxjs");
const smsService_1 = require("./smsService");
const jwtValidator_1 = __importStar(require("./utils/jwt/jwtValidator"));
const jwtLib_1 = __importDefault(require("./utils/jwt/jwtLib"));
const testCases_1 = require("../identity/service/testcases/testCases");
jest.mock('./utils/jwt/jwtLib');
describe('Auth0 service test suite', () => {
let underTest;
let envService;
let snsService;
let app;
//TEST PARAMETERS
let MockHttpService = ts_mockito_1.mock(common_1.HttpService);
let mHttpService;
let allEnvs;
beforeAll(() => __awaiter(void 0, void 0, void 0, function* () {
jest.resetModules();
allEnvs = process.env;
setTestEnvironmentVariables();
app = yield testing_1.Test.createTestingModule({
imports: [
config_1.ConfigModule.forRoot({
isGlobal: true,
}),
],
providers: [envService_1.EnvironmentService],
}).compile();
envService = app.get(envService_1.EnvironmentService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
}));
afterAll(() => {
process.env = allEnvs;
});
test('should send sms when identityPhoneRegister called', () => __awaiter(void 0, void 0, void 0, function* () {
const MockSns = ts_mockito_1.mock(smsService_1.SmsService);
ts_mockito_1.when(MockSns.sendSMS(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new Promise((resolve, rej) => resolve()));
mHttpService = ts_mockito_1.instance(MockHttpService);
const mSNS = ts_mockito_1.instance(MockSns);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, mSNS, new jwtValidator_1.default(new jwtLib_1.default(envService)));
let res = yield underTest.phoneRegister('+38073199001');
expect(res).toEqual(expect.objectContaining({
codeSent: true,
validUntil: expect.any(String),
}));
}));
test('should fail sending sms when identityPhoneRegister called', () => __awaiter(void 0, void 0, void 0, function* () {
const MockSns = ts_mockito_1.mock(smsService_1.SmsService);
ts_mockito_1.when(MockSns.sendSMS(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenThrow(new Error('Invalid phone number'));
mHttpService = ts_mockito_1.instance(MockHttpService);
const mSNS = ts_mockito_1.instance(MockSns);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, mSNS, new jwtValidator_1.default(new jwtLib_1.default(envService)));
expect(underTest.phoneRegister('+38073199001')).rejects.toThrowError();
}));
test('should send correct data on identityForgotPassword', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: '',
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
const response = yield underTest.forgotPassword('email@email.com');
const [url, options] = ts_mockito_1.capture(MockHttpService.post).last();
expect(options).toMatchObject({ email: 'email@email.com' });
expect(url).toEqual(envService.auth0UrlForgotPwd);
expect(response).toBe(true);
}));
test('should handle error on identityForgotPassword', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.error({
data: 'error',
status: 400,
statusText: '',
headers: [],
config: {},
});
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
const result = yield underTest.forgotPassword('email@email.com');
expect(result).toBe(false);
}));
test('should send correct data on identityChangePassword', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: { access_token: '' },
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
ts_mockito_1.when(MockHttpService.patch(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: '',
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
let response = yield underTest.changePassword(new identityDto_1.ChangePasswordInputDto('kisParol123!', 'userId'));
const [url, options] = ts_mockito_1.capture(MockHttpService.patch).last();
expect(url).toEqual(`${envService.auth0UrlChangePwd}userId`);
expect(options).toMatchObject({ password: 'kisParol123!' });
expect(response).toBe(true);
}));
test('should handle error response on identityChangePassword', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: { access_token: '' },
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
ts_mockito_1.when(MockHttpService.patch(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.error({
data: 'error',
status: 400,
statusText: '',
headers: [],
config: {},
});
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
let response = yield underTest.changePassword(new identityDto_1.ChangePasswordInputDto('kisParol123!', 'userId'));
const [url, options] = ts_mockito_1.capture(MockHttpService.patch).last();
expect(url).toEqual(`${envService.auth0UrlChangePwd}userId`);
expect(options).toMatchObject({ password: 'kisParol123!' });
expect(response).toBe(false);
}));
test('should perform successful identitySignUp', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: { access_token: '' },
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: { access_token: '1', refresh_token: '2', id_token: '3' },
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
let response = yield underTest.createUser(new identityDto_1.IdentitySignUpInputDto('email@mail.com', 'kisParol123!', '1234'));
const [url, options] = ts_mockito_1.capture(MockHttpService.post).last();
expect(url).toEqual(envService.auth0UrlCreateUser);
expect(options).toMatchObject({
email: 'email@mail.com',
password: 'kisParol123!',
});
expect(response).toBe(true);
}));
test('should perform successful identityLogin', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: { access_token: '1', refresh_token: '2', id_token: '3' },
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
let response = yield underTest.getIdentity(new identityDto_1.IdentityLoginInputDto('email@mail.com', 'kisParol123!', '1234'));
const [url, options] = ts_mockito_1.capture(MockHttpService.post).last();
expect(url).toEqual(envService.auth0UrlGetToken);
expect(options).toMatchObject({
username: 'email@mail.com',
password: 'kisParol123!',
grant_type: 'password',
});
expect(response).toMatchObject({
valid: true,
accessToken: '1',
refreshToken: '2',
idToken: '3',
});
}));
test('should update tokens based on refresToken', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: { access_token: '1', refresh_token: '2', id_token: '3' },
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
let response = yield underTest.getIdentity(new identityDto_1.CheckAccessTokenInputDto('1', '2', '3'));
const [url, options] = ts_mockito_1.capture(MockHttpService.post).last();
expect(url).toEqual(envService.auth0UrlGetToken);
expect(options).toHaveProperty('grant_type');
expect(options.grant_type).toEqual('refresh_token');
expect(response).toMatchObject({
valid: true,
accessToken: '1',
refreshToken: '2',
idToken: '3',
});
}));
test('should handle error on getIdentity', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.error({
message: 'Login error',
status: 400,
response: { data: { error_description: 'Incorrect credentials' } },
statusText: '',
headers: [],
config: {},
});
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
expect(underTest.getIdentity(new identityDto_1.IdentityLoginInputDto('email@mail.com', 'kisParol123!', '1234'))).rejects.toMatchObject(new Error('Login error. Reason:Incorrect credentials'));
}));
test('should handle internal error on getIdentity', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.error({
message: 'Login error',
status: 400,
statusText: '',
headers: [],
config: {},
});
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
expect(underTest.getIdentity(new identityDto_1.IdentityLoginInputDto('email@mail.com', 'kisParol123!', '1234'))).rejects.toMatchObject(new Error('Login error'));
}));
test('should handle error on createUser', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: { access_token: '' },
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.error({
message: 'Signup error',
status: 400,
response: { data: { message: 'User exists' } },
statusText: '',
headers: [],
config: {},
});
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
expect(underTest.createUser(new identityDto_1.IdentitySignUpInputDto('email@mail.com', 'kisParol123!', '1234'))).rejects.toMatchObject(new Error('Signup error. Reason:User exists'));
}));
test('should handle internal error on createUser', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: { access_token: '' },
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.error({
message: 'Signup error',
status: 400,
statusText: '',
headers: [],
config: {},
});
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
expect(underTest.createUser(new identityDto_1.IdentitySignUpInputDto('email@mail.com', 'kisParol123!', '1234'))).rejects.toMatchObject(new Error('Signup error'));
}));
test('should call jwt validate function', () => __awaiter(void 0, void 0, void 0, function* () {
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
expect(underTest.checkAccessToken({
testCaseCode: testCases_1.IdentityTestCase.CHECKACCESSTOKEN_VALID,
})).resolves.toMatchObject(jwtValidator_1.JWT_CHECK_RESULT.VALID_TOKEN);
}));
test('should call revoke token api', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReturn(new rxjs_1.Observable((observer) => {
observer.next({
data: {},
status: 200,
statusText: '',
headers: [],
config: {},
});
observer.complete();
}));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
yield underTest.revokeRefreshToken('refresh-token');
const [url, options] = ts_mockito_1.capture(MockHttpService.post).last();
expect(url).toEqual(envService.auth0UrlRevokeToken);
expect(options).toHaveProperty('token');
expect(options.token).toEqual('refresh-token');
}));
test('should handle error when revokeToken fail', () => __awaiter(void 0, void 0, void 0, function* () {
ts_mockito_1.when(MockHttpService.post(ts_mockito_1.anything(), ts_mockito_1.anything(), ts_mockito_1.anything())).thenReject(new Error('message'));
mHttpService = ts_mockito_1.instance(MockHttpService);
underTest = new auth0Service_1.Auth0Service(envService, mHttpService, new smsService_1.SmsService(), new jwtValidator_1.default(new jwtLib_1.default(envService)));
expect(underTest.revokeRefreshToken('refresh-token')).rejects.toMatchObject(new Error('message'));
}));
});
function setTestEnvironmentVariables() {
process.env.DEVICE_TABLE = 'Devices';
process.env.DEVICE_MODELSERIAL_GSI = 'modelSerialGsi';
process.env.AUTH0_CLIENT_ID = 'client_id';
process.env.AUTH0_CLIENT_SECRET = 'client_secret';
process.env.AUTH0_TENANT = 'auth_tenant';
process.env.AUTH0_MANAGEMENT_AUDIENCE = 'https://auth0.com/api/v2/';
process.env.AUTH0_AUDIENCE = 'myapi';
process.env.AUTH0_JWT_MAX_AGE = '86400';
process.env.AUTH0_JWT_ISSUER = 'https://auth0.com/';
process.env.AUTH0_PATH_CREATE_USER = 'https://auth0.com/api/v2/users';
process.env.AUTH0_PATH_GET_TOKEN = 'https://auth0.com/oauth/token';
process.env.AUTH0_PATH_FORGOT_PWD = 'https://auth0.com/dbconnections/change_password';
process.env.AUTH0_PATH_CHANGE_PWD = 'https://auth0.com/api/v2/users/';
process.env.AUTH0_PATH_JWT_WELLKNOWN = 'https://auth0.com/.well-known/jwks.json';
process.env.AUTH0_PATH_REVOKE_TOKEN = 'https://auth0.com/oauth/revoke';
}
//# sourceMappingURL=auth0Service.spec.js.map