Skip to content

Commit

Permalink
req header fix and unit tests for simplexSell
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamboster committed Nov 18, 2024
1 parent 1810557 commit 4c33a02
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/bitcore-wallet-service/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5997,9 +5997,12 @@ export class WalletService implements IWalletService {
const headers = {
'Content-Type': 'application/json',
Authorization: 'ApiKey ' + API_KEY,
'x-country-code': 'LT', // TODO: send the real country code
};

if (req.body.userCountry && typeof req.body.userCountry === 'string') {
headers['x-country-code'] = req.body.userCountry.toUpperCase();
}

let qs = [];
qs.push('base_currency=' + req.body.base_currency);
qs.push('base_amount=' + req.body.base_amount);
Expand Down Expand Up @@ -6095,7 +6098,7 @@ export class WalletService implements IWalletService {
const appSellRefId = keys.APP_SELL_REF_ID;

if (
!checkRequired(req.body, ['referer_url', 'return_url']) &&
!checkRequired(req.body, ['referer_url', 'return_url']) ||
!checkRequired(req.body.txn_details, ['quote_id'])
) {
return reject(new ClientError("Simplex's request missing arguments"));
Expand All @@ -6104,9 +6107,12 @@ export class WalletService implements IWalletService {
const headers = {
'Content-Type': 'application/json',
Authorization: 'ApiKey ' + API_KEY,
'x-country-code': 'LT', // TODO: send the real country code
};

if (req.body.userCountry && typeof req.body.userCountry === 'string') {
headers['x-country-code'] = req.body.userCountry.toUpperCase();
}

this.request.post(
API + '/v3/initiate-sell/widget',
{
Expand Down
148 changes: 148 additions & 0 deletions packages/bitcore-wallet-service/test/integration/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11113,6 +11113,10 @@ describe('Wallet service', function() {
moonpay: {
disabled: false,
removed: false
},
simplex: {
disabled: false,
removed: false
}
},
swapCrypto: {
Expand Down Expand Up @@ -13234,6 +13238,83 @@ describe('Wallet service', function() {
});
});

describe.only('#simplexGetSellQuote', () => {
beforeEach(() => {
req = {
headers: {},
body: {
env: 'sandbox',
userCountry: 'LT',
base_currency: 'BTC',
base_amount: 1000000,
quote_currency: 'EUR',
pp_payment_method: 'sepa'
}
}
});

it('should work properly if req is OK', async () => {
server.request = fakeRequest;
try {
const data = await server.simplexGetSellQuote(req);
should.exist(data);
} catch (err) {
should.not.exist(err);
}
});

it('should work properly if req is OK for web', async () => {
req.body.context = 'web';
server.request = fakeRequest;
try {
const data = await server.simplexGetSellQuote(req);
should.exist(data);
} catch (err) {
should.not.exist(err);
}
});

it('should return error if get returns error', async () => {
const fakeRequest2 = {
get: (_url, _opts, _cb) => { return _cb(new Error('Error'), null) },
};

server.request = fakeRequest2;
try {
const data = await server.simplexGetSellQuote(req);
should.not.exist(data);
} catch (err) {
should.exist(err);
err.message.should.equal('Error');
};
});

it('should return error if there is some missing arguments', async () => {
delete req.body.base_amount;
server.request = fakeRequest;
try {
const data = await server.simplexGetSellQuote(req);
should.not.exist(data);
} catch (err) {
should.exist(err);
err.message.should.equal('Simplex\'s request missing arguments');
}
});

it('should return error if simplex is commented in config', async () => {
config.simplex = undefined;

server.request = fakeRequest;
try {
const data = await server.simplexGetSellQuote(req);
should.not.exist(data);
} catch (err) {
should.exist(err);
err.message.should.equal('Simplex missing credentials');
}
});
});

describe('#simplexPaymentRequest', () => {
beforeEach(() => {
req = {
Expand Down Expand Up @@ -13303,6 +13384,73 @@ describe('Wallet service', function() {
});
});

describe.only('#simplexSellPaymentRequest', () => {
beforeEach(() => {
req = {
headers: {},
body: {
env: 'production',
userCountry: 'LT',
referer_url: 'https://referer_url.com/',
return_url: 'https://return_url.com/',
txn_details: {quote_id: 'quote_id_1'},
},
ip: '1.2.3.4'
}

fakeRequest = {
post: (_url, _opts, _cb) => { return _cb(null, { body: {} }) },
};
});

it('should work properly if req is OK', () => {
server.request = fakeRequest;
server.simplexSellPaymentRequest(req).then(data => {
should.exist(data);
}).catch(err => {
should.not.exist(err);
});
});

it('should return error if post returns error', () => {
const fakeRequest2 = {
post: (_url, _opts, _cb) => { return _cb(new Error('Error'), null) },
};

server.request = fakeRequest2;
server.simplexSellPaymentRequest(req).then(data => {
should.not.exist(data);
}).catch(err => {
should.exist(err);
err.message.should.equal('Error');
});
});

it('should return error if there is some missing arguments', () => {
delete req.body.return_url;

server.request = fakeRequest;
server.simplexSellPaymentRequest(req).then(data => {
should.not.exist(data);
}).catch(err => {
should.exist(err);
err.message.should.equal('Simplex\'s request missing arguments');
});
});

it('should return error if simplex is commented in config', () => {
config.simplex = undefined;

server.request = fakeRequest;
server.simplexSellPaymentRequest(req).then(data => {
should.not.exist(data);
}).catch(err => {
should.exist(err);
err.message.should.equal('Simplex missing credentials');
});
});
});

describe('#simplexGetEvents', () => {
beforeEach(() => {
req = {
Expand Down

0 comments on commit 4c33a02

Please sign in to comment.