fix: ๐Ÿ› callback with query string

This commit is contained in:
apotdevin 2021-02-03 07:13:04 +01:00
parent e7cba59d36
commit 476db5d755
No known key found for this signature in database
GPG key ID: 4403F1DFBE779457
5 changed files with 140 additions and 1 deletions

View file

@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`LNURL Resolvers getBitcoinPrice failure 1`] = `
Object {
"data": null,
"errors": Array [
[GraphQLError: ProblemWithdrawingFromLnUrlService],
],
"extensions": undefined,
"http": Object {
"headers": Headers {
Symbol(map): Object {},
},
},
}
`;
exports[`LNURL Resolvers getBitcoinPrice success 1`] = `
Object {
"data": Object {
"lnUrlWithdraw": "requestId",
},
"errors": undefined,
"extensions": undefined,
"http": Object {
"headers": Headers {
Symbol(map): Object {},
},
},
}
`;

View file

@ -0,0 +1,94 @@
import testServer from 'server/tests/testServer';
import fetchMock from 'jest-fetch-mock';
import { GraphQLError } from 'graphql';
import { WITHDRAW_LN_URL } from 'src/graphql/mutations/lnUrl';
jest.mock('ln-service');
describe('LNURL Resolvers', () => {
beforeEach(() => {
fetchMock.resetMocks();
});
describe('getBitcoinPrice', () => {
test('success', async () => {
fetchMock.mockResponseOnce(JSON.stringify({ status: 'SUCCESS' }));
const { mutate } = testServer();
const res = await mutate({
mutation: WITHDRAW_LN_URL,
variables: {
callback: 'https://domain.com',
amount: 1000,
k1: 'random',
description: 'ln-withdraw',
},
});
expect(res.errors).toBe(undefined);
expect(fetchMock).toBeCalledWith(
'https://domain.com?k1=random&pr=boltEncodedRequest',
undefined
);
expect(res).toMatchSnapshot();
});
test('success with callback that has query string', async () => {
fetchMock.mockResponseOnce(JSON.stringify({ status: 'SUCCESS' }));
const { mutate } = testServer();
const res = await mutate({
mutation: WITHDRAW_LN_URL,
variables: {
callback: 'https://domain.com?user=123456',
amount: 1000,
k1: 'random',
description: 'ln-withdraw',
},
});
expect(res.errors).toBe(undefined);
expect(fetchMock).toBeCalledWith(
'https://domain.com?user=123456&k1=random&pr=boltEncodedRequest',
undefined
);
});
test('success but not able to withdraw', async () => {
fetchMock.mockResponseOnce(JSON.stringify({ status: 'ERROR' }));
const { mutate } = testServer();
const res = await mutate({
mutation: WITHDRAW_LN_URL,
variables: {
callback: 'https://domain.com',
amount: 1000,
k1: 'random',
description: 'ln-withdraw',
},
});
expect(res.errors).toStrictEqual([
new GraphQLError('ProblemWithdrawingFromLnUrlService'),
]);
});
test('failure', async () => {
fetchMock.mockRejectOnce(new Error('Error'));
const { mutate } = testServer();
const res = await mutate({
mutation: WITHDRAW_LN_URL,
variables: {
callback: 'domain.com',
amount: 1000,
k1: 'random',
description: 'ln-withdraw',
},
});
expect(res.errors).toStrictEqual([
new GraphQLError('ProblemWithdrawingFromLnUrlService'),
]);
expect(res).toMatchSnapshot();
});
});
});

View file

@ -213,7 +213,10 @@ export const lnUrlResolvers = {
createInvoice({ lnd, tokens: amount, description })
);
const finalUrl = `${callback}?k1=${k1}&pr=${info.request}`;
// If the callback url already has an initial query '?' identifier we don't need to add it again.
const initialIdentifier = callback.indexOf('?') != -1 ? '&' : '?';
const finalUrl = `${callback}${initialIdentifier}k1=${k1}&pr=${info.request}`;
try {
const response = await fetchWithProxy(finalUrl);

View file

@ -103,3 +103,7 @@ export const verifyMessage = jest
export const getPublicKey = jest
.fn()
.mockReturnValue(Promise.resolve(res.getPublicKeyResponse));
export const createInvoice = jest
.fn()
.mockReturnValue(Promise.resolve(res.createInvoiceResponse));

View file

@ -763,3 +763,10 @@ export const verifyMessageResponse = {
export const getPublicKeyResponse = {
public_key: 'public_key',
};
export const createInvoiceResponse = {
request: 'boltEncodedRequest',
created_at: '',
id: 'requestId',
secret: 'secretString',
};