Skip to content

Commit

Permalink
refactor: grecaptcha v21 (#204)
Browse files Browse the repository at this point in the history
* refactor: adding grecaptcha to useFaucet inputs

* docs: reverting to un-linted docs

* refactor: grecaptcha v21

* docs: update year

* docs: fix _

* docs: adding comments for skipped test

---------

Co-authored-by: Cameron Gilbert <[email protected]>
  • Loading branch information
CalicoNino and cgilbe27 authored Aug 14, 2023
1 parent 19404d0 commit 08af259
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ See [HACKING.md](https://github.com/NibiruChain/ts-sdk/blob/main/HACKING.md) for

This software is licensed under the MIT license. See [LICENSE](https://github.com/NibiruChain/ts-sdk/blob/main/LICENSE) for full disclosure.

© 2022 Nibi, Inc.
© 2023 Nibi, Inc.

<p align="center">
<img src="https://raw.githubusercontent.com/NibiruChain/ts-sdk/main/img/nibi-logo-onwhite.png" style="width:200px">
Expand Down
1 change: 1 addition & 0 deletions examples/01_new-wallet-and-faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ async function runExample() {
await useFaucet({
address,
chain: TEST_CHAIN,
grecaptcha: "GOOGLE_RECATPCHA_TOKEN",
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/nibijs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ See [HACKING.md](https://github.com/NibiruChain/ts-sdk/blob/main/HACKING.md) for

This software is licensed under the MIT license. See [LICENSE](https://github.com/NibiruChain/ts-sdk/blob/main/LICENSE) for full disclosure.

© 2022 Nibi, Inc.
© 2023 Nibi, Inc.

<p align="center">
<img src="https://raw.githubusercontent.com/NibiruChain/ts-sdk/main/img/nibi-logo-onwhite.png" style="width:200px">
Expand Down
2 changes: 1 addition & 1 deletion packages/nibijs/docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ See [HACKING.md](https://github.com/NibiruChain/ts-sdk/blob/main/HACKING.md) for

This software is licensed under the MIT license. See [LICENSE](https://github.com/NibiruChain/ts-sdk/blob/main/LICENSE) for full disclosure.

© 2022 Nibi, Inc.
© 2023 Nibi, Inc.

<p align="center">
<img src="https://raw.githubusercontent.com/NibiruChain/ts-sdk/main/img/nibi-logo-onwhite.png" style="width:200px">
Expand Down
6 changes: 4 additions & 2 deletions packages/nibijs/src/chain/useFaucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export async function useFaucet({
address,
chain,
amts,
grecaptcha,
}: {
address: string
chain: Chain
amts?: { nibi: number; nusd: number; usdt: number }
grecaptcha: string
}): Promise<Response> {
if (!amts) {
// default values
Expand All @@ -30,7 +32,7 @@ export async function useFaucet({

// Execute faucet request
console.info(
`Requesting funds from faucet @ ${faucetUrl}:
`Requesting funds from faucet @ ${faucetUrl}:
Coins: ${coins}
Address: ${address}
`
Expand All @@ -43,7 +45,7 @@ export async function useFaucet({
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ address, coins }),
body: JSON.stringify({ address, coins, grecaptcha }),
})
.catch((err) => {
console.error(err)
Expand Down
22 changes: 16 additions & 6 deletions packages/nibijs/src/test/faucet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import {
} from "../tx"
import { TEST_CHAIN, TEST_MNEMONIC } from "./helpers"

test("faucet utility works", async () => {
// We can't create a test token even with faked recaptcha site
// and secret tokens. This not only would require a setup to generate
// a token from the UI, but to also create a fake backend
// recaptcha check with a test secret.
// In this case, we can skip the test and check it manually.
// eslint-disable-next-line jest/no-disabled-tests
test.skip("faucet utility works", async () => {
const wallet: WalletHD = await newRandomWallet()
const [{ address: toAddr }] = await wallet.getAccounts()

Expand All @@ -39,6 +45,7 @@ test("faucet utility works", async () => {
const faucetResp = await useFaucet({
address: toAddr,
chain: TEST_CHAIN,
grecaptcha: "",
})
expect(faucetResp.ok).toBeTruthy()

Expand All @@ -61,6 +68,7 @@ describe("useFaucet", () => {
feeDenom: "",
}

const grecaptcha = "TEST_GRECAPTCHA_TOKEN"
const address = "0x1234567890"
const expectedUrl = "https://faucet.shortName-1.nibiru.fi/"
const mockedFetch = jest.fn(
Expand All @@ -72,7 +80,7 @@ describe("useFaucet", () => {
window.fetch = mockedFetch

test("should request funds from faucet with default amounts", async () => {
await useFaucet({ address, chain })
await useFaucet({ address, chain, grecaptcha })

const expectedCoins = ["11000000unibi", "100000000unusd", "100000000uusdt"]

Expand All @@ -82,13 +90,13 @@ describe("useFaucet", () => {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ address, coins: expectedCoins }),
body: JSON.stringify({ address, coins: expectedCoins, grecaptcha }),
})
})

test("should request funds from faucet with custom amounts", async () => {
const amts = { nibi: 5, nusd: 50, usdt: 50 }
await useFaucet({ address, chain, amts })
await useFaucet({ address, chain, amts, grecaptcha })

const expectedCoins = ["5000000unibi", "50000000unusd", "50000000uusdt"]

Expand All @@ -98,7 +106,7 @@ describe("useFaucet", () => {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ address, coins: expectedCoins }),
body: JSON.stringify({ address, coins: expectedCoins, grecaptcha }),
})
})

Expand All @@ -109,7 +117,9 @@ describe("useFaucet", () => {
.mockImplementationOnce(() => Promise.reject(new Error(errorMessage)))
window.fetch = mockedFetchError

await expect(useFaucet({ address, chain })).rejects.toThrow(errorMessage)
await expect(useFaucet({ address, chain, grecaptcha })).rejects.toThrow(
errorMessage
)
expect(mockedFetchError).toHaveBeenCalledTimes(1)
})

Expand Down

0 comments on commit 08af259

Please sign in to comment.