diff --git a/internal/events/event.go b/internal/events/event.go index 32bb35d5..fb86aaa1 100644 --- a/internal/events/event.go +++ b/internal/events/event.go @@ -14,6 +14,7 @@ type MockEventParameters struct { ToUserName string IsAnonymous bool IsGift bool + GiftUser string EventStatus string SubscriptionStatus string ItemID string diff --git a/internal/events/trigger/trigger_event.go b/internal/events/trigger/trigger_event.go index 35373911..41afe552 100644 --- a/internal/events/trigger/trigger_event.go +++ b/internal/events/trigger/trigger_event.go @@ -134,6 +134,7 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event` CharityCurrentValue: p.CharityCurrentValue, CharityTargetValue: p.CharityTargetValue, ClientID: p.ClientID, + GiftUser: p.GiftUser, } e, err := types.GetByTriggerAndTransportAndVersion(p.Event, p.Transport, p.Version) diff --git a/internal/events/types/subscribe/sub_event.go b/internal/events/types/subscribe/sub_event.go index 6dff04c8..6f8d21b2 100644 --- a/internal/events/types/subscribe/sub_event.go +++ b/internal/events/types/subscribe/sub_event.go @@ -42,9 +42,14 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven params.IsGift = true } + if params.GiftUser != "" { + params.FromUserID = params.GiftUser + params.IsGift = true // make doubly sure it's set to to in cases of "twitch event trigger channel.subscribe -g 1" + } + switch params.Transport { case models.TransportWebhook, models.TransportWebSocket: - body := *&models.EventsubResponse{ + body := &models.EventsubResponse{ Subscription: models.EventsubSubscription{ ID: params.ID, Status: params.SubscriptionStatus, diff --git a/internal/events/types/subscribe/sub_event_test.go b/internal/events/types/subscribe/sub_event_test.go index 3382a34c..80f49e29 100644 --- a/internal/events/types/subscribe/sub_event_test.go +++ b/internal/events/types/subscribe/sub_event_test.go @@ -18,7 +18,7 @@ var tierTwo = "2000" func TestEventSub(t *testing.T) { a := test_setup.SetupTestEnv(t) - params := *&events.MockEventParameters{ + params := events.MockEventParameters{ FromUserID: fromUser, ToUserID: toUser, Transport: models.TransportWebhook, @@ -39,10 +39,35 @@ func TestEventSub(t *testing.T) { a.Equal(tierTwo, body.Event.Tier, "Expected tier %v, got %v", tierTwo, body.Event.Tier) } +func TestGiftLogic(t *testing.T) { + a := test_setup.SetupTestEnv(t) + + params := events.MockEventParameters{ + ToUserID: toUser, + Transport: models.TransportWebhook, + SubscriptionStatus: "enabled", + Trigger: "channel.subscribe", + Tier: tierTwo, + GiftUser: "1", + } + + r, err := Event{}.GenerateEvent(params) + a.Nil(err) + + var body models.SubEventSubResponse + err = json.Unmarshal(r.JSON, &body) + a.Nil(err) + + a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID) + a.Equal(tierTwo, body.Event.Tier, "Expected tier %v, got %v", tierTwo, body.Event.Tier) + a.Equal("1", body.Event.UserID, "Expected user ID %v, got %v", "1", body.Event.UserID) + a.Equal(true, body.Event.IsGift, "Expected is_gift to be true") +} + func TestFakeTransport(t *testing.T) { a := test_setup.SetupTestEnv(t) - params := *&events.MockEventParameters{ + params := events.MockEventParameters{ FromUserID: fromUser, ToUserID: toUser, Transport: "fake_transport", diff --git a/internal/mock_api/endpoints/drops/entitlements.go b/internal/mock_api/endpoints/drops/entitlements.go index bd36465c..5ac518a3 100644 --- a/internal/mock_api/endpoints/drops/entitlements.go +++ b/internal/mock_api/endpoints/drops/entitlements.go @@ -97,6 +97,7 @@ func getEntitlements(w http.ResponseWriter, r *http.Request) { Cursor: dbr.Cursor, } } + bytes, err := json.Marshal(apiResponse) w.Write(bytes) } @@ -170,6 +171,10 @@ func patchEntitlements(w http.ResponseWriter, r *http.Request) { } } - bytes, _ := json.Marshal(resp) + apiResponse := models.APIResponse{ + Data: resp, + } + + bytes, _ := json.Marshal(apiResponse) w.Write(bytes) }