Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/fix-osx-scope-issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet committed Mar 8, 2021
2 parents 1c7d1db + 55791b6 commit 26f6908
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 23 deletions.
50 changes: 27 additions & 23 deletions docs/event.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# Events

- [Events](#events)
- [Description](#description)
- [Trigger](#trigger)
- [Retrigger](#retrigger)
- [Verify-Subscription](#verify-subscription)

## Description
## Description

The `event` product contains commands to trigger mock events for local webhook testing or migration.
The `event` product contains commands to trigger mock events for local webhook testing or migration.

## Trigger

Used to either create or send mock events for use with local webhooks testing.


**Args**

| Argument | Description |
|---------------------|------------------------------------------------------------------------------------------------------------|
| ------------------- | ---------------------------------------------------------------------------------------------------------- |
| `subscribe` | A standard subscription event. Triggers a basic tier 1 sub. |
| `unsubscribe` | A standard unsubscribe event. Triggers a basic tier 1 sub. |
| `gift` | A gifted subscription event. Triggers a basic tier 1 sub. |
Expand All @@ -30,11 +30,13 @@ Used to either create or send mock events for use with local webhooks testing.
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |
| `raid` | Channel Raid event with a random viewer count. |
| `revoke` | User authorization revoke event. Uses local Client as set in `twitch configure` or generates one randomly. |
| `streamup` | Only usable with the `eventsub` transport, a stream online event. |
| `streamdown` | Only usable with the `eventsub` transport, a stream offline event. |

**Flags**

| Flag | Shorthand | Description | Example | Required? (Y/N) |
|---------------------|-----------|----------------------------------------------------------------------------------------------------------------------------|-------------------------------------------|-----------------|
| ------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------- | --------------- |
| `--forward-address` | `-F` | Web server address for where to send mock events. | `-F https://localhost:8080` | N |
| `--transport` | `-T` | The method used to send events. Default is eventsub, but can send using websub. | `-T websub` | N |
| `--to-user` | `-t` | Denotes the receiver's TUID of the event, usually the broadcaster. | `-t 44635596` | N |
Expand All @@ -44,7 +46,7 @@ Used to either create or send mock events for use with local webhooks testing.
| `--count` | `-c` | Count of events to fire. This can be used to simulate an influx of subscriptions. | `-c 100` | N |
| `--anonymous` | `-a` | If the event is anonymous. Only applies to `gift` and `cheer` events. | `-a` | N |
| `--status` | `-S` | Status of the event object, currently applies to channel points redemptions. | `-S fulfilled` | N |
| `--item-id` | `-i` | Manually set the ID of the event payload item (for example the reward ID in redemption events). | `-i 032e4a6c-4aef-11eb-a9f5-1f703d1f0b92` | N |
| `--item-id` | `-i` | Manually set the ID of the event payload item (for example the reward ID in redemption events). | `-i 032e4a6c-4aef-11eb-a9f5-1f703d1f0b92` | N |
| `--cost` | `-C` | Amount of bits or channel points redeemed/used in the event. | `-C 250` | N |

**Examples**
Expand All @@ -56,9 +58,9 @@ twitch event trigger cheer -f 1234 -t 4567 // generates JSON for a cheer event f

## Retrigger

Allows previous events to be refired based on the event ID. The ID is noted within the event itself, such as in the "subscription" payload of standard webhooks.
Allows previous events to be refired based on the event ID. The ID is noted within the event itself, such as in the "subscription" payload of standard webhooks.

For example, for:
For example, for:

```json
{
Expand All @@ -69,19 +71,19 @@ For example, for:
}
}
```
The resulting ID would be `713f3254-0178-9757-7439-d779400c0999`.

The resulting ID would be `713f3254-0178-9757-7439-d779400c0999`.

**Args**
None

**Flags**

| Flag | Shorthand | Description | Example | Required? (Y/N) |
|---------------------|-----------|----------------------------------------------------------------------------------------------------------------------------|-----------------------------|-----------------|
| `--forward-address` | `-F` | Web server address for where to send mock events. | `-F https://localhost:8080` | N |
| `--id` | `-i` | The ID of the event to refire. | `-i <id>` | Y |
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC. | `-s testsecret` | N |

| Flag | Shorthand | Description | Example | Required? (Y/N) |
| ------------------- | --------- | ---------------------------------------------------------------------------- | --------------------------- | --------------- |
| `--forward-address` | `-F` | Web server address for where to send mock events. | `-F https://localhost:8080` | N |
| `--id` | `-i` | The ID of the event to refire. | `-i <id>` | Y |
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC. | `-s testsecret` | N |

**Examples**

Expand All @@ -91,12 +93,12 @@ twitch event retrigger -i "713f3254-0178-9757-7439-d779400c0999" -F https://loca

## Verify-Subscription

Allows you to test if your webserver responds to subscription requests properly.
Allows you to test if your webserver responds to subscription requests properly.

**Args**

| Argument | Description |
|---------------------|------------------------------------------------------------------------------------------------------------|
| ------------------- | ---------------------------------------------------------------------------------------------------------- |
| `subscribe` | A standard subscription event. Triggers a basic tier 1 sub. |
| `unsubscribe` | A standard unsubscribe event. Triggers a basic tier 1 sub. |
| `gift` | A gifted subscription event. Triggers a basic tier 1 sub. |
Expand All @@ -109,17 +111,19 @@ Allows you to test if your webserver responds to subscription requests properly.
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |
| `raid` | Channel Raid event with a random viewer count. |
| `revoke` | User authorization revoke event. Uses local Client as set in `twitch configure` or generates one randomly. |
| `streamup` | Only usable with the `eventsub` transport, a stream online event. |
| `streamdown` | Only usable with the `eventsub` transport, a stream offline event. |

**Flags**

| Flag | Shorthand | Description | Example | Required? (Y/N) |
|---------------------|-----------|----------------------------------------------------------------------------------------------------------------------------|-----------------------------|-----------------|
| `--forward-address` | `-F` | Web server address for where to send mock subscription. | `-F https://localhost:8080` | Y |
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC. | `-s testsecret` | N |
| `--transport` | `-T` | The method used to send events. Default is eventsub, but can send using websub. | `-T websub` | N |
| Flag | Shorthand | Description | Example | Required? (Y/N) |
| ------------------- | --------- | ------------------------------------------------------------------------------- | --------------------------- | --------------- |
| `--forward-address` | `-F` | Web server address for where to send mock subscription. | `-F https://localhost:8080` | Y |
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC. | `-s testsecret` | N |
| `--transport` | `-T` | The method used to send events. Default is eventsub, but can send using websub. | `-T websub` | N |

**Examples**

```sh
twitch event verify-subscription cheer -F https://localhost:8080/ // triggers a fake "cheer" EventSub subscription and validates if localhost responds properly
```
```
89 changes: 89 additions & 0 deletions internal/events/types/streamdown/streamdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package streamdown

import (
"encoding/json"
"time"

"github.com/twitchdev/twitch-cli/internal/events"
"github.com/twitchdev/twitch-cli/internal/models"
"github.com/twitchdev/twitch-cli/internal/util"
)

var transportsSupported = map[string]bool{
models.TransportWebSub: false,
models.TransportEventSub: true,
}

var triggerSupported = []string{"streamdown"}

var triggerMapping = map[string]map[string]string{
models.TransportWebSub: {
"streamdown": "channel.update",
},
models.TransportEventSub: {
"streamdown": "stream.offline",
},
}

type Event struct{}

func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) {
var event []byte
var err error

switch params.Transport {
case models.TransportEventSub:
body := &models.EventsubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
Status: "enabled",
Type: triggerMapping[params.Transport][params.Trigger],
Version: "1",
Condition: models.EventsubCondition{
BroadcasterUserID: params.ToUserID,
},
Transport: models.EventsubTransport{
Method: "webhook",
Callback: "null",
},
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
Event: models.StreamDownEventSubEvent{
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
},
}
event, err = json.Marshal(body)
if err != nil {
return events.MockEventResponse{}, err
}
default:
return events.MockEventResponse{}, nil
}

return events.MockEventResponse{
ID: params.ID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
}, nil
}

func (e Event) ValidTransport(t string) bool {
return transportsSupported[t]
}

func (e Event) ValidTrigger(t string) bool {
for _, ts := range triggerSupported {
if ts == t {
return true
}
}
return false
}
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
75 changes: 75 additions & 0 deletions internal/events/types/streamdown/streamdown_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package streamdown

import (
"encoding/json"
"testing"

"github.com/twitchdev/twitch-cli/internal/events"
"github.com/twitchdev/twitch-cli/internal/models"
"github.com/twitchdev/twitch-cli/internal/util"
)

var fromUser = "1234"
var toUser = "4567"

func TestEventSub(t *testing.T) {
a := util.SetupTestEnv(t)

params := *&events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: models.TransportEventSub,
Trigger: "streamdown",
}

r, err := Event{}.GenerateEvent(params)
a.Nil(err)

var body models.StreamDownEventSubResponse
err = json.Unmarshal(r.JSON, &body)
a.Nil(err)

// write actual tests here (making sure you set appropriate values and the like) for eventsub
}

func TestFakeTransport(t *testing.T) {
a := util.SetupTestEnv(t)

params := *&events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: "fake_transport",
Trigger: "unsubscribe",
}

r, err := Event{}.GenerateEvent(params)
a.Nil(err)
a.Empty(r)
}
func TestValidTrigger(t *testing.T) {
a := util.SetupTestEnv(t)

r := Event{}.ValidTrigger("streamdown")
a.Equal(true, r)

r = Event{}.ValidTrigger("notgift")
a.Equal(false, r)
}

func TestValidTransport(t *testing.T) {
a := util.SetupTestEnv(t)

r := Event{}.ValidTransport(models.TransportEventSub)
a.Equal(true, r)

r = Event{}.ValidTransport("noteventsub")
a.Equal(false, r)
}
func TestGetTopic(t *testing.T) {
a := util.SetupTestEnv(t)

r := Event{}.GetTopic(models.TransportEventSub, "streamdown")
a.NotNil(r)
}
92 changes: 92 additions & 0 deletions internal/events/types/streamup/streamup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package streamup

import (
"encoding/json"
"time"

"github.com/twitchdev/twitch-cli/internal/events"
"github.com/twitchdev/twitch-cli/internal/models"
"github.com/twitchdev/twitch-cli/internal/util"
)

var transportsSupported = map[string]bool{
models.TransportWebSub: false,
models.TransportEventSub: true,
}

var triggerSupported = []string{"streamup"}

var triggerMapping = map[string]map[string]string{
models.TransportWebSub: {
"streamup": "channel.update",
},
models.TransportEventSub: {
"streamup": "stream.online",
},
}

type Event struct{}

func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) {
var event []byte
var err error

switch params.Transport {
case models.TransportEventSub:
body := &models.EventsubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
Status: "enabled",
Type: triggerMapping[params.Transport][params.Trigger],
Version: "1",
Condition: models.EventsubCondition{
BroadcasterUserID: params.ToUserID,
},
Transport: models.EventsubTransport{
Method: "webhook",
Callback: "null",
},
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
Event: models.StreamUpEventSubEvent{
ID: util.RandomUserID(),
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
Type: "live",
StartedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
}
event, err = json.Marshal(body)
if err != nil {
return events.MockEventResponse{}, err
}
default:
return events.MockEventResponse{}, nil
}

return events.MockEventResponse{
ID: params.ID,
JSON: event,
FromUser: params.FromUserID,
ToUser: params.ToUserID,
}, nil
}

func (e Event) ValidTransport(t string) bool {
return transportsSupported[t]
}

func (e Event) ValidTrigger(t string) bool {
for _, ts := range triggerSupported {
if ts == t {
return true
}
}
return false
}
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}

0 comments on commit 26f6908

Please sign in to comment.