File size: 2,275 Bytes
d6f631f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package httpdriver

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/openmeterio/openmeter/api"
	"github.com/openmeterio/openmeter/openmeter/notification"
	"github.com/openmeterio/openmeter/pkg/models"
)

func invoiceEvent(invoice *notification.InvoicePayload) notification.Event {
	return notification.Event{
		NamespacedID: models.NamespacedID{ID: "event-id"},
		CreatedAt:    time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
		Type:         notification.EventTypeInvoiceCreated,
		Payload: notification.EventPayload{
			EventPayloadMeta: notification.EventPayloadMeta{
				Type:    notification.EventTypeInvoiceCreated,
				Version: notification.EventPayloadVersionCurrent,
			},
			Invoice: invoice,
		},
	}
}

func TestFromEventAsInvoiceCreatedPayload(t *testing.T) {
	t.Run("passes through api.Invoice into Data", func(t *testing.T) {
		invoice := api.Invoice{Id: "inv-1", Currency: "USD"}
		event := invoiceEvent(&notification.InvoicePayload{Invoice: invoice})

		got, err := FromEventAsInvoiceCreatedPayload(event)
		require.NoError(t, err)

		assert.Equal(t, "event-id", got.Id)
		assert.Equal(t, api.NotificationEventInvoiceCreatedPayloadTypeInvoiceCreated, got.Type)
		assert.Equal(t, invoice, got.Data)
	})

	t.Run("returns error when invoice payload is nil", func(t *testing.T) {
		event := invoiceEvent(nil)

		_, err := FromEventAsInvoiceCreatedPayload(event)
		require.Error(t, err)
	})
}

func TestFromEventAsInvoiceUpdatedPayload(t *testing.T) {
	t.Run("passes through api.Invoice into Data", func(t *testing.T) {
		invoice := api.Invoice{Id: "inv-2", Currency: "EUR"}
		event := invoiceEvent(&notification.InvoicePayload{Invoice: invoice})
		event.Type = notification.EventTypeInvoiceUpdated
		event.Payload.Type = notification.EventTypeInvoiceUpdated

		got, err := FromEventAsInvoiceUpdatedPayload(event)
		require.NoError(t, err)

		assert.Equal(t, "event-id", got.Id)
		assert.Equal(t, api.NotificationEventInvoiceUpdatedPayloadTypeInvoiceUpdated, got.Type)
		assert.Equal(t, invoice, got.Data)
	})

	t.Run("returns error when invoice payload is nil", func(t *testing.T) {
		event := invoiceEvent(nil)

		_, err := FromEventAsInvoiceUpdatedPayload(event)
		require.Error(t, err)
	})
}