import { BaseRequestOptions, Http, RequestMethod, RequestOptions, Response, ResponseOptions, XHRBackend } from '@angular/http'; import { MockBackend, MockConnection } from '@angular/http/testing'; import Web3 from 'web3'; import ipfsAPI from 'ipfs-api'; import { Buffer } from 'buffer'; import Readable from 'readable-stream'; import { LoginCustom } from './login-custom'; export function mockBackEndFactory( backend: MockBackend, options: BaseRequestOptions, realBackend: XHRBackend ) { var web3; var GBMasterContractAddress; var GBMasterAbi; var companyName; var masterAddress = ''; // array in local storage for registered users let users: any[] = JSON.parse(localStorage.getItem('users')) || []; // fake token let token: string = 'token'; console.log('Init'); // if(window["web3"]!=undefined) // { // web3 = new Web3(window['web3'].currentProvider); // console.log(web3); // web3.eth.defaultAccount=web3.eth.coinbase; // var memberAddress=web3.eth.accounts[0]; // GBMasterContractAddress="0x073aba43e7da8a6f70acf4b7792fc90c5f197975"; // GBMasterAbi=[{"constant":true,"inputs":[],"name":"getGBTCAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getByteCodeAndAbi","outputs":[{"name":"byteCode","type":"string"},{"name":"abiHash","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_memberAddress","type":"address"}],"name":"isAuthorizedGBOwner","outputs":[{"name":"auth","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getSVAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getAllDappLength","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_masterOrtokenAddress","type":"address"}],"name":"getGovBlocksUserDetails2","outputs":[{"name":"dappName","type":"bytes32"},{"name":"masterContractAddress","type":"address"},{"name":"dappTokenAddress","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_gbUserName","type":"bytes32"}],"name":"getGovBlocksUserDetails1","outputs":[{"name":"GbUserName","type":"bytes32"},{"name":"masterContractAddress","type":"address"},{"name":"dappTokenAddress","type":"address"},{"name":"allContractsbyteCodeHash","type":"string"},{"name":"allCcontractsAbiHash","type":"string"},{"name":"versionNo","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_gbUserName","type":"bytes32"}],"name":"getGovBlocksUserDetails","outputs":[{"name":"GbUserName","type":"bytes32"},{"name":"masterContractAddress","type":"address"},{"name":"allContractsbyteCodeHash","type":"string"},{"name":"allCcontractsAbiHash","type":"string"},{"name":"versionNo","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getGOVAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getGDAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_gbIndex","type":"uint256"}],"name":"getAllDappById","outputs":[{"name":"_gbUserName","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getGBTandGBTC","outputs":[{"name":"_GBTController","type":"address"},{"name":"_GBToken","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getAllDappArray","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_memberAddress","type":"address"}],"name":"getDappUser","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"authGBOwner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getGBTAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_index","type":"uint256"}],"name":"getGovBlocksUserDetailsByIndex","outputs":[{"name":"index","type":"uint256"},{"name":"GbUserName","type":"bytes32"},{"name":"MasterContractAddress","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_GBTControllerAddress","type":"address"},{"name":"_GBTAddress","type":"address"}],"name":"GovBlocksMasterInit","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_memberAddress","type":"address"}],"name":"changeAuthorizedGB","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_gbUserName","type":"bytes32"},{"name":"_memberAddress","type":"address"},{"name":"_memberRoleId","type":"uint256"},{"name":"_typeOf","type":"uint8"}],"name":"updateMemberRoleGB","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_GBTControllerAddress","type":"address"},{"name":"_GBTokenAddress","type":"address"}],"name":"changeAllAddress1","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_GBTConrollerAddress","type":"address"}],"name":"updateGBTControllerAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_GDAddress","type":"address"},{"name":"_SVAddress","type":"address"},{"name":"_GOVAddress","type":"address"}],"name":"changeAllAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_memberAddress","type":"address"},{"name":"_hash","type":"string"}],"name":"setDappUser","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_gbUserName","type":"bytes32"},{"name":"_categoryId","type":"uint256"},{"name":"_categoryData","type":"string"}],"name":"updateCategoryGB","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_gbUserName","type":"bytes32"},{"name":"_dappTokenAddress","type":"address"}],"name":"addGovBlocksUser","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_gbUserName","type":"bytes32"},{"name":"_descHash","type":"string"}],"name":"addNewCategoryGB","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_gbUserName","type":"bytes32"},{"name":"_newRoleName","type":"bytes32"},{"name":"_newDescHash","type":"string"}],"name":"addNewMemberRoleGB","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_GBTContractAddress","type":"address"}],"name":"updateGBTAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_gbUserName","type":"bytes32"},{"name":"_typeOf","type":"bytes16"},{"name":"_value","type":"uint256"}],"name":"configureGlobalParameters","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_byteCodeHash","type":"string"},{"name":"_abiHash","type":"string"}],"name":"setByteCodeAndAbi","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_gbUserName","type":"bytes32"},{"name":"_newMasterAddress","type":"address"}],"name":"changeDappMasterAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_gbUserName","type":"bytes32"},{"name":"_dappTokenAddress","type":"address"}],"name":"changeDappTokenAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]; // } // configure fake backend backend.connections.subscribe((connection: MockConnection) => { // wrap in timeout to simulate server api call setTimeout(() => { if (window['web3'] != undefined) { web3 = new Web3(window['web3'].currentProvider); console.log(web3); web3.eth.defaultAccount = web3.eth.coinbase; var memberAddress = web3.eth.accounts[0]; var GBMasteAddress = { 4: '0xca27deb069d83629db00579ab0f0d80cd119a79b', 42: '0xfd49ec86aaff8a312273cbebc3f48cc4c067c04a', 31: '0x26847806987d45cdb482987f2027b05eef14d061', 96: '0xd62d9f828928661808a4700ea747e8474b236961' }; // var GBMasteAddress ={4: "0xb3936a1ff0ecab45244bff9ef4f2e7e9a7a26971", 42: "0x3eac5a2c4d67468b89d2b2983e68c00ed7eee56c"}; web3.version.getNetwork((err, netId) => { GBMasterContractAddress = GBMasteAddress[netId]; }); // GBMasterContractAddress="0xbc7e609bc10b22c206b4fd2dc90b5637697cad0d"; GBMasterAbi = [ { constant: true, inputs: [], name: 'getGBTCAddress', outputs: [{ name: '', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [ { name: '_GDAddress', type: 'address' }, { name: '_SVAddress', type: 'address' }, { name: '_GOVAddress', type: 'address' } ], name: 'changeAllAddress', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [], name: 'getAllDappLength', outputs: [{ name: '', type: 'uint256' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [{ name: '_memberAddress', type: 'address' }], name: 'changeAuthorizedGB', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [{ name: '_gbUserName', type: 'bytes32' }], name: 'getGovBlocksUserDetails', outputs: [ { name: 'GbUserName', type: 'bytes32' }, { name: 'masterContractAddress', type: 'address' }, { name: 'allContractsbyteCodeHash', type: 'string' }, { name: 'allCcontractsAbiHash', type: 'string' }, { name: 'versionNo', type: 'uint256' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'getGOVAddress', outputs: [{ name: '', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [ { name: '_gbUserName', type: 'bytes32' }, { name: '_memberAddress', type: 'address' }, { name: '_memberRoleId', type: 'uint256' }, { name: '_typeOf', type: 'uint8' } ], name: 'updateMemberRoleGB', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [{ name: '_gbUserName', type: 'bytes32' }], name: 'getDappDescHash', outputs: [{ name: '', type: 'string' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [{ name: '_gbUserName', type: 'bytes32' }], name: 'getDappMasterAddress', outputs: [{ name: 'masterAddress', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [{ name: '_memberAddress', type: 'address' }], name: 'isAuthorizedGBOwner', outputs: [{ name: 'auth', type: 'uint256' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [ { name: '_GBTControllerAddress', type: 'address' }, { name: '_GBTAddress', type: 'address' } ], name: 'GovBlocksMasterInit', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [{ name: '_Address', type: 'address' }], name: 'getGovBlocksUserDetails2', outputs: [ { name: 'dappName', type: 'bytes32' }, { name: 'masterContractAddress', type: 'address' }, { name: 'dappTokenAddress', type: 'address' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'getDappUser', outputs: [{ name: '', type: 'string' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [{ name: '_gbUserName', type: 'bytes32' }], name: 'getDappTokenAddress', outputs: [{ name: 'tokenAddres', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [{ name: '_GBTConrollerAddress', type: 'address' }], name: 'updateGBTControllerAddress', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_gbUserName', type: 'bytes32' }, { name: '_categoryId', type: 'uint256' }, { name: '_categoryData', type: 'string' } ], name: 'updateCategoryGB', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_gbUserName', type: 'bytes32' }, { name: '_dappDescriptionHash', type: 'string' } ], name: 'changeDappDescHash', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [], name: 'getGBTandGBTC', outputs: [ { name: '_GBTController', type: 'address' }, { name: '_GBToken', type: 'address' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'owner', outputs: [{ name: '', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [{ name: '_gbUserName', type: 'bytes32' }], name: 'getGovBlocksUserDetails1', outputs: [ { name: 'GbUserName', type: 'bytes32' }, { name: 'masterContractAddress', type: 'address' }, { name: 'dappTokenAddress', type: 'address' }, { name: 'allContractsbyteCodeHash', type: 'string' }, { name: 'allCcontractsAbiHash', type: 'string' }, { name: 'versionNo', type: 'uint256' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'getSVAddress', outputs: [{ name: '', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'getAllDappArray', outputs: [{ name: '', type: 'bytes32[]' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [ { name: '_gbUserName', type: 'bytes32' }, { name: '_descHash', type: 'string' } ], name: 'addNewCategoryGB', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [], name: 'authGBOwner', outputs: [{ name: '', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [ { name: '_GBTControllerAddress', type: 'address' }, { name: '_GBTokenAddress', type: 'address' } ], name: 'changeAllAddress1', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [{ name: '_index', type: 'uint256' }], name: 'getGovBlocksUserDetailsByIndex', outputs: [ { name: 'index', type: 'uint256' }, { name: 'GbUserName', type: 'bytes32' }, { name: 'MasterContractAddress', type: 'address' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [], name: 'getGDAddress', outputs: [{ name: '', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [ { name: '_gbUserName', type: 'bytes32' }, { name: '_typeOf', type: 'bytes16' }, { name: '_value', type: 'uint256' } ], name: 'configureGlobalParameters', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [{ name: '_hash', type: 'string' }], name: 'setDappUser', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_byteCodeHash', type: 'string' }, { name: '_abiHash', type: 'string' } ], name: 'setByteCodeAndAbi', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_gbUserName', type: 'bytes32' }, { name: '_newMasterAddress', type: 'address' } ], name: 'changeDappMasterAddress', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [], name: 'getGBTAddress', outputs: [{ name: '', type: 'address' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [ { name: '_gbUserName', type: 'bytes32' }, { name: '_dappTokenAddress', type: 'address' } ], name: 'changeDappTokenAddress', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [ { name: '_gbUserName', type: 'bytes32' }, { name: '_dappTokenAddress', type: 'address' }, { name: '_dappDescriptionHash', type: 'string' } ], name: 'addGovBlocksUser', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [], name: 'getByteCodeAndAbi', outputs: [ { name: 'byteCode', type: 'string' }, { name: 'abiHash', type: 'string' } ], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [ { name: '_gbUserName', type: 'bytes32' }, { name: '_newRoleName', type: 'bytes32' }, { name: '_newDescHash', type: 'string' } ], name: 'addNewMemberRoleGB', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: false, inputs: [{ name: '_newOwner', type: 'address' }], name: 'transferOwnership', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' }, { constant: true, inputs: [{ name: '_contractAddress', type: 'address' }], name: 'getDappNameByAddress', outputs: [{ name: '', type: 'bytes32' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: true, inputs: [{ name: '_gbIndex', type: 'uint256' }], name: 'getAllDappById', outputs: [{ name: '_gbUserName', type: 'bytes32' }], payable: false, stateMutability: 'view', type: 'function' }, { constant: false, inputs: [{ name: '_GBTContractAddress', type: 'address' }], name: 'updateGBTAddress', outputs: [], payable: false, stateMutability: 'nonpayable', type: 'function' } ]; if ( connection.request.url.endsWith('/api/authenticate') && connection.request.method === RequestMethod.Post ) { // get parameters from post request let params = JSON.parse(connection.request.getBody()); // console.log(params); var hash; web3.version.getNetwork((err, netId) => { GBMasterContractAddress = GBMasteAddress[netId]; connection.mockRespond( new Response( new ResponseOptions({ status: 200, body: { masterAddress: '', userName: 'GOVBLOCKS', GBMasterAddress: GBMasterContractAddress, // email: "govblocks@govblocks.io", email: '', companyName: '', authorized: true, token: token } }) ) ); }); // web3.eth.contract(GBMasterAbi).at(GBMasterContractAddress,function(error,result){ // let ans=result.getDappUser(function(error1,result1){ // // var defaultAddress="0x0000000000000000000000000000000000000000"; // var hashDetails=getDataFromHash(result1); // var present=false; // if(hashDetails!=""){ // for(let i=0;i { // // return user.email === params.email && user.password === params.password; // return 1 == params.email; // }); // default account // if (params.email === 'demo@demo.com' && params.password === 'demo') { // filteredUsers[0] = { // fullName: 'Demo', // email: 'demo@demo.com', // password: 'demo', // }; // } // if (params.email == 1) { // filteredUsers[0] = { // fullName: 'Demo', // email: 'demo@demo.com', // password: 'demo', // }; // } // if ((masterAddress!="0x0000000000000000000000000000000000000000")&&(masterAddress!="")) { // // if login details are valid return 200 OK with user details and fake jwt token // let user = filteredUsers; // connection.mockRespond(new Response(new ResponseOptions({ // status: 200, // body: { // id: user, // fullName: companyName, // email: companyName, // token: token // } // }))); // } else { // // else return 400 bad request // connection.mockError(new Error('GovBlocks Username not found')); // } // if (filteredUsers.length) { // // if login details are valid return 200 OK with user details and fake jwt token // let user = filteredUsers[0]; // connection.mockRespond(new Response(new ResponseOptions({ // status: 200, // body: { // id: user.id, // fullName: user.fullName, // email: user.email, // token: token // } // }))); // } else { // // else return 400 bad request // connection.mockError(new Error('Email or password is incorrect')); // } return; } } // authenticate // get users if ( connection.request.url.endsWith('/api/users') && connection.request.method === RequestMethod.Get ) { // check for fake auth token in header and return users if valid, this security // is implemented server side in a real application if ( connection.request.headers.get('Authorization') === 'Bearer ' + token ) { connection.mockRespond( new Response(new ResponseOptions({ status: 200, body: users })) ); } else { // return 401 not authorised if token is null or invalid connection.mockRespond( new Response(new ResponseOptions({ status: 401 })) ); } return; } // get user by id if ( connection.request.url.match(/\/api\/users\/\d+$/) && connection.request.method === RequestMethod.Get ) { // check for fake auth token in header and return user if valid, this security is implemented server side in a real application if ( connection.request.headers.get('Authorization') === 'Bearer ' + token ) { // find user by id in users array let urlParts = connection.request.url.split('/'); let id = parseInt(urlParts[urlParts.length - 1]); let matchedUsers = users.filter(user => { return user.id === id; }); let user = matchedUsers.length ? matchedUsers[0] : null; // respond 200 OK with user connection.mockRespond( new Response(new ResponseOptions({ status: 200, body: user })) ); } else { // return 401 not authorised if token is null or invalid connection.mockRespond( new Response(new ResponseOptions({ status: 401 })) ); } return; } // create user if ( connection.request.url.endsWith('/api/users') && connection.request.method === RequestMethod.Post ) { // get new user object from post body let newUser = JSON.parse(connection.request.getBody()); // validation // let duplicateUser = users.filter(user => { // return user.email === newUser.email; // }).length; // if (duplicateUser) { // return connection.mockError(new Error('User "' + newUser.email + '" is already registered')); // } // return connection.mockError(new Error('Error registering company')); console.log(newUser); // save new user newUser.id = users.length + 1; users.push(newUser); localStorage.setItem('users', JSON.stringify(users)); // respond 200 OK connection.mockRespond( new Response(new ResponseOptions({ status: 200 })) ); return; } // delete user if ( connection.request.url.match(/\/api\/users\/\d+$/) && connection.request.method === RequestMethod.Delete ) { // check for fake auth token in header and return user if valid, this security is implemented server side in a real application if ( connection.request.headers.get('Authorization') === 'Bearer ' + token ) { // find user by id in users array let urlParts = connection.request.url.split('/'); let id = parseInt(urlParts[urlParts.length - 1]); for (let i = 0; i < users.length; i++) { let user = users[i]; if (user.id === id) { // delete user users.splice(i, 1); localStorage.setItem('users', JSON.stringify(users)); break; } } // respond 200 OK connection.mockRespond( new Response(new ResponseOptions({ status: 200 })) ); } else { // return 401 not authorised if token is null or invalid connection.mockRespond( new Response(new ResponseOptions({ status: 401 })) ); } return; } // token verify if ( connection.request.url.endsWith('/api/verify') && connection.request.method === RequestMethod.Get ) { // check for fake auth token in header and return users if valid, this security // is implemented server side in a real application if ( connection.request.headers.get('Authorization') === 'Bearer ' + token ) { connection.mockRespond( new Response( new ResponseOptions({ status: 200, body: { status: 'ok' } }) ) ); } else { // return 401 not authorised if token is null or invalid connection.mockRespond( new Response(new ResponseOptions({ status: 401 })) ); } return; } // forgot password if ( connection.request.url.endsWith('/api/forgot-password') && connection.request.method === RequestMethod.Post ) { // get parameters from post request let params = JSON.parse(connection.request.getBody()); // find if any user matches login credentials let filteredUsers = users.filter(user => { return user.email === params.email; }); if (filteredUsers.length) { // in real world, if email is valid, send email change password link let user = filteredUsers[0]; connection.mockRespond( new Response(new ResponseOptions({ status: 200 })) ); } else { // else return 400 bad request connection.mockError( new Error('User with this email does not exist') ); } return; } // pass through any requests not handled above let realHttp = new Http(realBackend, options); let requestOptions = new RequestOptions({ method: connection.request.method, headers: connection.request.headers, body: connection.request.getBody(), url: connection.request.url, withCredentials: connection.request.withCredentials, responseType: connection.request.responseType }); realHttp.request(connection.request.url, requestOptions).subscribe( (response: Response) => { connection.mockRespond(response); }, (error: any) => { connection.mockError(error); } ); }, 500); }); return new Http(backend, options); } function getDataFromHash(hash) { if (hash == '') { return hash; } else { console.log(hash); var XMLHttp = new XMLHttpRequest(); XMLHttp.open('GET', 'https://ipfs.govblocks.io/ipfs/' + hash, false); XMLHttp.send(null); // // // this gives the json containing the details - title, description, voting type var hashDetails = JSON.parse(XMLHttp.responseText); console.log(hashDetails); return hashDetails; } } function timeout(hash) { setTimeout(() => { console.log('Test'); var boundFunction = this.web3.eth.getTransactionReceipt( hash, function(error, result) { console.log(error); console.log(result); if (result != null) this.connection.mockRespond( new Response(new ResponseOptions({ status: 200 })) ); else this.timeout(hash); }.bind(this) ); }, 10 * 1000); } export let fakeBackendProvider = { // use fake backend in place of Http service for backend-less development provide: Http, deps: [MockBackend, BaseRequestOptions, XHRBackend], useFactory: mockBackEndFactory };