github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/tests/integrations/chain_tx_test.go (about)

     1  package integrations
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/onsi/gomega"
     7  
     8  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/clients/applet_mgr"
     9  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/tests/requires"
    10  	"github.com/machinefi/w3bstream/pkg/depends/kit/statusx"
    11  	"github.com/machinefi/w3bstream/pkg/errors/status"
    12  	"github.com/machinefi/w3bstream/pkg/modules/blockchain"
    13  	"github.com/machinefi/w3bstream/pkg/types"
    14  )
    15  
    16  func TestChainTxAPIs(t *testing.T) {
    17  	var (
    18  		ctx         = requires.Context()
    19  		client      = requires.AuthClient()
    20  		projectName = "test_project"
    21  		chainTxID   types.SFID
    22  		baseReq     = applet_mgr.CreateChainTx{}
    23  	)
    24  
    25  	baseReq.ProjectName = projectName
    26  	baseReq.CreateChainTxReq.ChainID = 4690
    27  	baseReq.CreateChainTxReq.EventType = "DEFAULT"
    28  	baseReq.CreateChainTxReq.TxAddress = "85cfc682ccbf276cc80a1242e69e1d1a8ab9295465c6fc9e4d3f433363ec3ccd"
    29  
    30  	t.Logf("random a project name: %s", projectName)
    31  
    32  	t.Run("ChainTx", func(t *testing.T) {
    33  		t.Run("#CreateChainTx", func(t *testing.T) {
    34  			t.Run("#Success", func(t *testing.T) {
    35  
    36  				// create project without user defined config(database/env)
    37  				{
    38  					req := &applet_mgr.CreateProject{}
    39  					req.CreateReq.Name = projectName
    40  
    41  					rsp, _, err := client.CreateProject(req)
    42  
    43  					NewWithT(t).Expect(err).To(BeNil())
    44  					NewWithT(t).Expect(rsp.Name).To(Equal(projectName))
    45  				}
    46  
    47  				// init blockchain config
    48  				{
    49  					err := blockchain.InitChainDB(ctx)
    50  					NewWithT(t).Expect(err).To(BeNil())
    51  				}
    52  
    53  				// create chain tx monitor
    54  				{
    55  					req := baseReq
    56  					rsp, _, err := client.CreateChainTx(&req)
    57  
    58  					NewWithT(t).Expect(err).To(BeNil())
    59  					chainTxID = rsp.ChainTxID
    60  				}
    61  
    62  				// check chain tx is created
    63  				{
    64  					_, err := blockchain.GetChainTxBySFID(ctx, chainTxID)
    65  					NewWithT(t).Expect(err).To(BeNil())
    66  				}
    67  
    68  				// remove chain tx
    69  				{
    70  					req := &applet_mgr.RemoveChainTx{
    71  						ProjectName: projectName,
    72  						ChainTxID:   chainTxID,
    73  					}
    74  					_, err := client.RemoveChainTx(req)
    75  					NewWithT(t).Expect(err).To(BeNil())
    76  				}
    77  
    78  				// check chain tx is removed
    79  				{
    80  					_, err := blockchain.GetChainTxBySFID(ctx, chainTxID)
    81  					requires.CheckError(t, err, status.ChainTxNotFound)
    82  				}
    83  
    84  				// remove project
    85  				{
    86  					req := &applet_mgr.RemoveProject{ProjectName: projectName}
    87  					_, err := client.RemoveProject(req)
    88  					NewWithT(t).Expect(err).To(BeNil())
    89  				}
    90  			})
    91  			t.Run("#InvalidChainTxParam", func(t *testing.T) {
    92  				// chain tx is empty
    93  				{
    94  					req := baseReq
    95  					req.CreateChainTxReq.TxAddress = ""
    96  
    97  					_, _, err := client.CreateChainTx(&req)
    98  					requires.CheckError(t, err, &statusx.StatusErr{
    99  						Key: "badRequest",
   100  					})
   101  				}
   102  
   103  				// chain id is empty
   104  				{
   105  					req := baseReq
   106  					req.CreateChainTxReq.ChainID = 0
   107  					_, _, err := client.CreateChainTx(&req)
   108  					requires.CheckError(t, err, &statusx.StatusErr{
   109  						Key: "badRequest",
   110  					})
   111  				}
   112  			})
   113  		})
   114  	})
   115  }