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