Skip to content

Commit

Permalink
fix: 🐛 callback with query string
Browse files Browse the repository at this point in the history
  • Loading branch information
apotdevin committed Feb 3, 2021
1 parent e7cba59 commit 476db5d
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 1 deletion.
31 changes: 31 additions & 0 deletions server/schema/lnurl/__snapshots__/lnurl.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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 {},
},
},
}
`;
94 changes: 94 additions & 0 deletions server/schema/lnurl/lnurl.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
5 changes: 4 additions & 1 deletion server/schema/lnurl/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions server/tests/__mocks__/ln-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
7 changes: 7 additions & 0 deletions server/tests/lnServiceResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};

0 comments on commit 476db5d

Please sign in to comment.