@@ -82,7 +82,7 @@ func TestFaucet_Serve_ValidRequests(t *testing.T) {
8282
8383 var (
8484 gasFee = std .MustParseCoin ("1ugnot" )
85- sendAmount = std .MustParseCoins (config .DefaultSendAmount )
85+ sendAmount = std .MustParseCoins (config .DefaultMaxSendAmount )
8686 )
8787
8888 var (
@@ -295,7 +295,7 @@ func TestFaucet_Serve_InvalidRequests(t *testing.T) {
295295
296296 var (
297297 gasFee = std .MustParseCoin ("1ugnot" )
298- sendAmount = std .MustParseCoins (config .DefaultSendAmount )
298+ sendAmount = std .MustParseCoins (config .DefaultMaxSendAmount )
299299 )
300300
301301 var (
@@ -569,7 +569,7 @@ func TestFaucet_Serve_NoFundedAccounts(t *testing.T) {
569569
570570 var (
571571 gasFee = std .MustParseCoin ("1ugnot" )
572- sendAmount = std .MustParseCoins (config .DefaultSendAmount )
572+ sendAmount = std .MustParseCoins (config .DefaultMaxSendAmount )
573573 )
574574
575575 var (
@@ -713,3 +713,100 @@ func TestFaucet_Serve_NoFundedAccounts(t *testing.T) {
713713 // Validate the broadcast tx
714714 assert .Nil (t , capturedTxs )
715715}
716+
717+ func TestFaucet_Serve_InvalidSendAmount (t * testing.T ) {
718+ t .Parallel ()
719+
720+ // Extract the default send amount
721+ maxSendAmount := std .MustParseCoins (config .DefaultMaxSendAmount )
722+
723+ testTable := []struct {
724+ name string
725+ sendAmount std.Coins
726+ }{
727+ {
728+ "invalid send amount" ,
729+ std .NewCoins (std .NewCoin ("atom" , 10 )),
730+ },
731+ {
732+ "excessive send amount" ,
733+ maxSendAmount .Add (std .MustParseCoins ("100ugnot" )),
734+ },
735+ }
736+
737+ for _ , testCase := range testTable {
738+ testCase := testCase
739+
740+ t .Run (testCase .name , func (t * testing.T ) {
741+ t .Parallel ()
742+
743+ var (
744+ validAddress = crypto .MustAddressFromString ("g155n659f89cfak0zgy575yqma64sm4tv6exqk99" )
745+ gasFee = std .MustParseCoin ("1ugnot" )
746+
747+ singleInvalidRequest = Request {
748+ To : validAddress .String (),
749+ Amount : testCase .sendAmount .String (),
750+ }
751+ )
752+
753+ encodedSingleInvalidRequest , err := json .Marshal (
754+ singleInvalidRequest ,
755+ )
756+ require .NoError (t , err )
757+
758+ getFaucetURL := func (address string ) string {
759+ return fmt .Sprintf ("http://%s" , address )
760+ }
761+
762+ // Create a new faucet with default params
763+ cfg := config .DefaultConfig ()
764+ cfg .ListenAddress = fmt .Sprintf ("127.0.0.1:%d" , getFreePort (t ))
765+ cfg .MaxSendAmount = maxSendAmount .String ()
766+
767+ f , err := NewFaucet (
768+ static .New (gasFee , 100000 ),
769+ & mockClient {},
770+ WithConfig (cfg ),
771+ )
772+
773+ require .NoError (t , err )
774+ require .NotNil (t , f )
775+
776+ // Start the faucet
777+ ctx , cancelFn := context .WithCancel (context .Background ())
778+ defer cancelFn ()
779+
780+ g , gCtx := errgroup .WithContext (ctx )
781+
782+ g .Go (func () error {
783+ return f .Serve (gCtx )
784+ })
785+
786+ url := getFaucetURL (f .config .ListenAddress )
787+
788+ // Wait for the faucet to be started
789+ waitForServer (t , url )
790+
791+ // Execute the request
792+ respRaw , err := http .Post (
793+ url ,
794+ jsonMimeType ,
795+ bytes .NewBuffer (encodedSingleInvalidRequest ),
796+ )
797+ require .NoError (t , err )
798+
799+ respBytes , err := io .ReadAll (respRaw .Body )
800+ require .NoError (t , err )
801+
802+ response := decodeResponse [Response ](t , respBytes )
803+
804+ assert .Contains (t , response .Error , errInvalidSendAmount .Error ())
805+ assert .Equal (t , unableToHandleRequest , response .Result )
806+
807+ // Stop the faucet and wait for it to finish
808+ cancelFn ()
809+ assert .NoError (t , g .Wait ())
810+ })
811+ }
812+ }
0 commit comments