-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
158 lines (138 loc) · 4.22 KB
/
main_test.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
// import (
// "context"
// "encoding/json"
// "log/slog"
// "net/http"
// "net/http/httptest"
// "os"
// "sync"
// "testing"
// "time"
// "github.com/matryer/is"
// "github.com/hotosm/central-webhook/db"
// "github.com/hotosm/central-webhook/parser"
// )
// func TestSetupWebhook(t *testing.T) {
// dbUri := os.Getenv("CENTRAL_WEBHOOK_DB_URI")
// if len(dbUri) == 0 {
// dbUri = "postgresql://odk:odk@db:5432/odk?sslmode=disable"
// }
// is := is.New(t)
// wg := sync.WaitGroup{}
// log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
// Level: slog.LevelDebug,
// }))
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel()
// dbPool, err := db.InitPool(ctx, log, dbUri)
// is.NoErr(err)
// defer dbPool.Close()
// conn, err := dbPool.Acquire(ctx)
// is.NoErr(err)
// defer conn.Release()
// // Create test tables
// conn.Exec(ctx, `DROP TABLE IF EXISTS entity_defs;`)
// conn.Exec(ctx, `DROP TABLE IF EXISTS audits;`)
// createTables := []string{
// `CREATE TABLE IF NOT EXISTS entity_defs (
// id SERIAL PRIMARY KEY,
// "entityId" INT,
// "createdAt" TIMESTAMPTZ,
// "current" BOOL,
// "data" JSONB,
// "creatorId" INT,
// "label" TEXT
// );`,
// `CREATE TABLE IF NOT EXISTS audits (
// "actorId" INT,
// action VARCHAR,
// details JSONB
// );`,
// }
// for _, sql := range createTables {
// _, err := conn.Exec(ctx, sql)
// is.NoErr(err)
// }
// // Insert an entity record
// log.Info("inserting entity details record")
// _, err = conn.Exec(ctx, `
// INSERT INTO public.entity_defs (
// id, "entityId","createdAt","current","data","creatorId","label"
// ) VALUES (
// 1001,
// 900,
// '2025-01-10 16:23:40.073',
// true,
// '{"status": "0", "task_id": "26", "version": "1"}',
// 5,
// 'Task 26 Feature 904487737'
// );
// `)
// is.NoErr(err)
// log.Info("entity record inserted")
// // Mock webhook server
// webhookReceived := make(chan bool, 1)
// mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// defer r.Body.Close()
// var payload parser.ProcessedEvent
// err := json.NewDecoder(r.Body).Decode(&payload)
// is.NoErr(err)
// log.Info("payload received", "payload", payload)
// is.Equal(payload.ID, "xxx") // Validate Entity ID
// // Convert the payload.Data to map[string]string for comparison
// actualData, ok := payload.Data.(map[string]interface{})
// is.True(ok) // Ensure the type assertion succeeded
// expectedData := map[string]interface{}{
// "status": "0",
// "task_id": "26",
// "version": "1",
// }
// is.Equal(actualData, expectedData) // Validate Entity data
// webhookReceived <- true
// w.Header().Set("Content-Type", "application/json")
// w.WriteHeader(http.StatusOK)
// }))
// defer mockServer.Close()
// // Start webhook listener
// wg.Add(1)
// go func() {
// defer wg.Done()
// log.Info("starting webhook listener")
// err := SetupWebhook(log, ctx, dbPool, nil, mockServer.URL, mockServer.URL, mockServer.URL)
// if err != nil && ctx.Err() == nil {
// log.Error("webhook listener error", "error", err)
// }
// }()
// // Wait for the listener to initialize
// log.Info("waiting for listener to initialize")
// time.Sleep(300 * time.Millisecond) // Wait for the listener to be fully set up
// // Insert an audit log to trigger the webhook
// log.Info("inserting audit log")
// _, err = conn.Exec(ctx, `
// INSERT INTO audits ("actorId", action, details)
// VALUES (
// 1,
// 'entity.update.version',
// '{"entityDefId": 1001, "entityId": 1000, "entity": {"uuid": "xxx", "dataset": "test"}}'
// );
// `)
// is.NoErr(err)
// // Wait for webhook response or timeout
// select {
// case <-webhookReceived:
// log.Info("webhook received successfully")
// case <-time.After(3 * time.Second):
// t.Fatalf("test timed out waiting for webhook")
// }
// // Allow some time for final webhook processing
// time.Sleep(100 * time.Millisecond)
// // Cleanup
// log.Info("cleaning up...")
// cancel()
// wg.Wait()
// conn.Exec(ctx, `DROP TABLE IF EXISTS entity_defs;`)
// conn.Exec(ctx, `DROP TABLE IF EXISTS audits;`)
// conn.Release()
// dbPool.Close()
// }