~bigbes/core-go

ref: ba01be8449eadd60ffae7ae28f27247bbefa8bcc core-go/webhooks/legacy_test.go -rw-r--r-- 3.3 KiB
ba01be84 — Conrad Hoffmann server: replace deprecated gqlgen code 1 year, 1 month ago
                                                                                
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
package webhooks

import (
	"context"
	"database/sql/driver"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"time"

	"github.com/DATA-DOG/go-sqlmock"
	sq "github.com/Masterminds/squirrel"
	"github.com/stretchr/testify/assert"
	"github.com/vaughan0/go-ini"

	"git.sr.ht/~sircmpwn/core-go/crypto"
	"git.sr.ht/~sircmpwn/core-go/database"
)

func init() {
	conf, err := ini.Load(strings.NewReader(`
[webhooks]
private-key=ebzsjPaN6E13ln/FeNWly1C92q6bVMVdOnDo1HPl5fc=

[sr.ht]
network-key=tbuG-7Vh44vrDq1L_HKWkHnWrDOtJhEkPKPiauaLeuk=
internal-ipnet=127.0.0.1/24,::1/64`))
	if err != nil {
		panic(err)
	}
	crypto.InitCrypto(conf)
}

type argContains struct {
	matches []string
}

func ArgMatchesAll(matches ...string) *argContains {
	return &argContains{matches}
}

func (ac *argContains) Match(v driver.Value) bool {
	str, ok := v.(string)
	if !ok {
		return false
	}
	for _, match := range ac.matches {
		if !strings.Contains(str, match) {
			return false
		}
	}
	return true
}

func TestDelivery(t *testing.T) {
	called := make(chan struct{})
	srv := httptest.NewServer(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			defer r.Body.Close()

			assert.Equal(t, r.Method, http.MethodPost)
			assert.Equal(t, r.URL.Path, "/webhook")

			assert.NotEqual(t, "", r.Header.Get("X-Webhook-Delivery"))
			assert.Equal(t, "profile:update", r.Header.Get("X-Webhook-Event"))
			assert.Equal(t, "application/json", r.Header.Get("Content-Type"))

			b, err := ioutil.ReadAll(r.Body)
			assert.Nil(t, err)
			assert.Equal(t, `{"hello": "world"}`, string(b))

			nonce := r.Header.Get("X-Payload-Nonce")
			signature := r.Header.Get("X-Payload-Signature")
			assert.True(t, crypto.VerifyWebhook(b, nonce, signature))

			w.Write([]byte("Thanks!"))
			close(called)
		}))
	defer srv.Close()

	db, mock, err := sqlmock.New()
	if err != nil {
		panic(err)
	}
	ctx := database.Context(context.Background(), db)
	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
	defer cancel()

	// Lookup phase
	mock.ExpectBegin()
	mock.ExpectQuery(`SELECT .* FROM user_webhook_subscription sub`).
		WillReturnRows(sqlmock.NewRows([]string{
			"sub.id", "sub.created", "sub.url", "sub.events",
		}).AddRow(1337, time.Now().UTC(),
						srv.URL+"/webhook", "profile:update")).
		WithArgs(42, sqlmock.AnyArg()) // Any => events LIKE %profile:update%
	mock.ExpectCommit()
	// Schedule phase
	mock.ExpectBegin()
	mock.ExpectQuery(`INSERT INTO user_webhook_delivery`).
		WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(4096))
	mock.ExpectCommit()
	// Delivery phase
	mock.ExpectBegin()
	mock.ExpectExec(`UPDATE user_webhook_delivery`).
		WithArgs("Thanks!", 200,
			sqlmock.AnyArg(), // Response headers
			ArgMatchesAll(
				"X-Payload-Signature",
				"X-Payload-Nonce",
				"X-Webhook-Event",
				"X-Webhook-Delivery",
			), // Final request headers
			4096).
		WillReturnResult(sqlmock.NewResult(1, 1))
	mock.ExpectCommit()

	queue := NewLegacyQueue(make(ini.File))
	queue.Queue.Start(ctx, 1)
	q := sq.
		Select().
		From("user_webhook_subscription sub").
		Where(`sub.user_id = ?`, 42)
	queue.Schedule(ctx, q, "user", "profile:update", []byte(`{"hello": "world"}`))

	select {
	case <-called:
		queue.Queue.Shutdown()
		assert.Nil(t, mock.ExpectationsWereMet())
	case <-ctx.Done():
		t.Fatal("webhook url endpoint not called")
	}
}