|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "math/big" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/Jetlum/WalletAlertService/mock" |
| 10 | + "github.com/Jetlum/WalletAlertService/models" |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + "github.com/ethereum/go-ethereum/core/types" |
| 13 | + "github.com/ethereum/go-ethereum/crypto" |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +func TestIntegration(t *testing.T) { |
| 18 | + if testing.Short() { |
| 19 | + t.Skip("Skipping integration test in short mode") |
| 20 | + } |
| 21 | + |
| 22 | + // Setup test context with timeout |
| 23 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 24 | + defer cancel() |
| 25 | + |
| 26 | + userPref := models.UserPreference{ |
| 27 | + UserID: "test@example.com", |
| 28 | + WalletAddress: "0xto", |
| 29 | + MinEtherValue: "500000000000000000", // 0.5 ETH |
| 30 | + EmailNotification: true, |
| 31 | + } |
| 32 | + |
| 33 | + mockUserPrefRepo := &mock.MockUserPreferenceRepository{ |
| 34 | + GetMatchingPreferencesFunc: func(e *models.Event) ([]models.UserPreference, error) { |
| 35 | + return []models.UserPreference{userPref}, nil |
| 36 | + }, |
| 37 | + } |
| 38 | + |
| 39 | + // Create mock Ethereum client |
| 40 | + mockClient := mock.NewMockEthClient() |
| 41 | + |
| 42 | + // Create and use fromAddress in assertions |
| 43 | + privateKey, err := crypto.GenerateKey() |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("Failed to generate private key: %v", err) |
| 46 | + } |
| 47 | + fromAddress := crypto.PubkeyToAddress(privateKey.PublicKey) |
| 48 | + |
| 49 | + mockEventRepo := &mock.MockEventRepository{ |
| 50 | + CreateFunc: func(e *models.Event) error { |
| 51 | + assert.Equal(t, fromAddress.Hex(), e.FromAddress, "From address should match") |
| 52 | + return nil |
| 53 | + }, |
| 54 | + } |
| 55 | + // Create mock transaction (unsigned) |
| 56 | + mockTx := types.NewTransaction( |
| 57 | + 0, |
| 58 | + common.HexToAddress("0xto"), |
| 59 | + big.NewInt(2000000000000000000), // 2 ETH (above threshold) |
| 60 | + 21000, |
| 61 | + big.NewInt(1), |
| 62 | + nil, |
| 63 | + ) |
| 64 | + |
| 65 | + // Sign the transaction |
| 66 | + chainID := big.NewInt(1) // Use the same chain ID in the mock client |
| 67 | + signer := types.LatestSignerForChainID(chainID) |
| 68 | + signedTx, err := types.SignTx(mockTx, signer, privateKey) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("Failed to sign transaction: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + // Update NetworkIDFunc to return the correct chain ID |
| 74 | + mockClient.NetworkIDFunc = func(ctx context.Context) (*big.Int, error) { |
| 75 | + return chainID, nil |
| 76 | + } |
| 77 | + |
| 78 | + // Fix block.WithBody call |
| 79 | + mockClient.BlockByHashFunc = func(ctx context.Context, hash common.Hash) (*types.Block, error) { |
| 80 | + header := &types.Header{ |
| 81 | + Number: big.NewInt(1), |
| 82 | + ParentHash: common.HexToHash("0x123"), |
| 83 | + Time: uint64(time.Now().Unix()), |
| 84 | + } |
| 85 | + |
| 86 | + block := types.NewBlockWithHeader(header) |
| 87 | + return block.WithBody(types.Body{ |
| 88 | + Transactions: []*types.Transaction{signedTx}, |
| 89 | + Uncles: []*types.Header{}, |
| 90 | + }), nil |
| 91 | + } |
| 92 | + |
| 93 | + // Use mock NFT detector |
| 94 | + mockNFTDetector := &mock.MockNFTDetector{ |
| 95 | + IsNFTTransactionFunc: func(tx *types.Transaction) bool { |
| 96 | + return false |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + emailSent := false |
| 101 | + mockEmailNotification := &mock.MockEmailNotification{ |
| 102 | + SendFunc: func(event *models.Event, userPref *models.UserPreference) error { |
| 103 | + emailSent = true |
| 104 | + // Add assertions if needed |
| 105 | + return nil |
| 106 | + }, |
| 107 | + } |
| 108 | + |
| 109 | + // Create mock header |
| 110 | + mockHeader := &types.Header{ |
| 111 | + Number: big.NewInt(1), |
| 112 | + } |
| 113 | + |
| 114 | + done := make(chan bool) |
| 115 | + go func() { |
| 116 | + processBlock( |
| 117 | + mockClient, |
| 118 | + mockHeader, |
| 119 | + mockNFTDetector, |
| 120 | + mockEmailNotification, |
| 121 | + mockEventRepo, |
| 122 | + mockUserPrefRepo, |
| 123 | + ) |
| 124 | + done <- true |
| 125 | + }() |
| 126 | + |
| 127 | + select { |
| 128 | + case <-ctx.Done(): |
| 129 | + t.Fatal("Test timed out") |
| 130 | + case <-done: |
| 131 | + assert.True(t, emailSent, "Email notification should have been sent") |
| 132 | + } |
| 133 | +} |
0 commit comments