Skip to content

Commit

Permalink
Merge branch 'main' into feature/api-mock
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet authored Jun 14, 2021
2 parents 9721fd8 + 150e1e1 commit 4ca0cd3
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 47 deletions.
22 changes: 14 additions & 8 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ var (
secret string
status string
itemID string
itemName string
cost int64
count int
streamTitle string
description string
)

var eventCmd = &cobra.Command{
Expand Down Expand Up @@ -58,7 +59,7 @@ var verifyCmd = &cobra.Command{
Args: cobra.MaximumNArgs(1),
ValidArgs: events.ValidTriggers(),
Run: verifyCmdRun,
Example: `twitch verify-subscribe subscribe`,
Example: `twitch event verify-subscription subscribe`,
}

var retriggerCmd = &cobra.Command{
Expand All @@ -73,18 +74,22 @@ func init() {
eventCmd.AddCommand(triggerCmd, retriggerCmd, verifyCmd)

// trigger flags
// flags for forwarding functionality/changing payloads
triggerCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
triggerCmd.Flags().StringVarP(&transport, "transport", "T", "eventsub", fmt.Sprintf("Preferred transport method for event. Defaults to Webhooks 2/EventSub.\nSupported values: %s", events.ValidTransports()))
triggerCmd.Flags().StringVarP(&transport, "transport", "T", "eventsub", fmt.Sprintf("Preferred transport method for event. Defaults to /EventSub.\nSupported values: %s", events.ValidTransports()))
triggerCmd.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC.")

// per-topic flags
triggerCmd.Flags().StringVarP(&toUser, "to-user", "t", "", "User ID of the receiver of the event. For example, the user that receives a follow. In most contexts, this is the broadcaster.")
triggerCmd.Flags().StringVarP(&fromUser, "from-user", "f", "", "User ID of the user sending the event, for example the user following another user.")
triggerCmd.Flags().StringVarP(&giftUser, "gift-user", "g", "", "Used only for \"gift\" events. Denotes the User ID of the gifting user.")
triggerCmd.Flags().BoolVarP(&isAnonymous, "anonymous", "a", false, "Denotes if the event is anonymous. Only applies to Gift and Sub events.")
triggerCmd.Flags().IntVarP(&count, "count", "c", 1, "Count of events to events. This will simulate a sub gift, or large number of cheers.")
triggerCmd.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC.")
triggerCmd.Flags().StringVarP(&status, "status", "S", "", "Status of the event object, currently applies to channel points redemptions.")
triggerCmd.Flags().StringVarP(&itemID, "item-id", "i", "", "Manually set the ID of the event payload item (for example the reward ID in redemption events).")
triggerCmd.Flags().StringVarP(&itemID, "item-id", "i", "", "Manually set the ID of the event payload item (for example the reward ID in redemption events). For stream events, this is the game ID.")
triggerCmd.Flags().StringVarP(&itemName, "item-name", "n", "", "Manually set the name of the event payload item (for example the reward ID in redemption events). For stream events, this is the game title.")
triggerCmd.Flags().Int64VarP(&cost, "cost", "C", 0, "Amount of bits or channel points redeemed/used in the event.")
triggerCmd.Flags().StringVarP(&streamTitle, "description", "d", "", "Title the stream should be updated with.")
triggerCmd.Flags().StringVarP(&description, "description", "d", "", "Title the stream should be updated with.")

// retrigger flags
retriggerCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
Expand All @@ -94,7 +99,7 @@ func init() {

// verify-subscription flags
verifyCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
verifyCmd.Flags().StringVarP(&transport, "transport", "T", "eventsub", fmt.Sprintf("Preferred transport method for event. Defaults to Webhooks 2/EventSub.\nSupported values: %s", events.ValidTransports()))
verifyCmd.Flags().StringVarP(&transport, "transport", "T", "eventsub", fmt.Sprintf("Preferred transport method for event. Defaults to EventSub.\nSupported values: %s", events.ValidTransports()))
verifyCmd.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC.")
verifyCmd.MarkFlagRequired("forward-address")
}
Expand Down Expand Up @@ -127,7 +132,8 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
Status: status,
ItemID: itemID,
Cost: cost,
StreamTitle: streamTitle,
Description: description,
ItemName: itemName,
})

if err != nil {
Expand Down
30 changes: 16 additions & 14 deletions docs/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,22 @@ Used to either create or send mock events for use with local webhooks testing.

**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 |
| `--from-user` | `-f` | Denotes the sender's TUID of the event, for example the user that follows another user or the subscriber to a broadcaster. | `-f 44635596` | N |
| `--gift-user` | `-g` | Used only for subcription-based events, denotes the gifting user ID | `-g 44635596` | N |
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC. | `-s testsecret` | N |
| `--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 |
| `--cost` | `-C` | Amount of bits or channel points redeemed/used in the event. | `-C 250` | N |
| `--description` | `-d` | Title the stream should be updated/started with. | `-d Awesome new title!` | 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 |
| `--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 |
| `--from-user` | `-f` | Denotes the sender's TUID of the event, for example the user that follows another user or the subscriber to a broadcaster. | `-f 44635596` | N |
| `--gift-user` | `-g` | Used only for subcription-based events, denotes the gifting user ID | `-g 44635596` | N |
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC. | `-s testsecret` | N |
| `--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 or game in stream events). | `-i 032e4a6c-4aef-11eb-a9f5-1f703d1f0b92` | N |
| `--item-name` | `-n` | Manually set the name of the event payload item (for example the reward ID in redemption events or game name in stream events). | `-n "Science & Technology"` | N |
| `--cost` | `-C` | Amount of bits or channel points redeemed/used in the event. | `-C 250` | N |
| `--description` | `-d` | Title the stream should be updated/started with. | `-d Awesome new title!` | N |


**Examples**

Expand Down
3 changes: 2 additions & 1 deletion internal/events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ type MockEventParameters struct {
IsGift bool
Status string
ItemID string
ItemName string
Cost int64
IsPermanent bool
StreamTitle string
Description string
}

type MockEventResponse struct {
Expand Down
6 changes: 4 additions & 2 deletions internal/events/trigger/trigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type TriggerParameters struct {
Secret string
Verbose bool
Count int
StreamTitle string
Description string
ItemName string
}

type TriggerResponse struct {
Expand Down Expand Up @@ -63,7 +64,8 @@ func Fire(p TriggerParameters) (string, error) {
Cost: p.Cost,
Status: p.Status,
ItemID: p.ItemID,
StreamTitle: p.StreamTitle,
Description: p.Description,
ItemName: p.ItemName,
}

e, err := types.GetByTriggerAndTransport(p.Event, p.Transport)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
params.Cost = 150
}

if params.ItemName == "" {
params.ItemName = "Test Reward from CLI"
}

switch params.Transport {
case models.TransportEventSub:
body := *&models.RedemptionEventSubResponse{
Expand Down Expand Up @@ -75,7 +79,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
Status: params.Status,
Reward: models.RedemptionReward{
ID: params.ItemID,
Title: "Test Reward from CLI",
Title: params.ItemName,
Cost: params.Cost,
Prompt: "Redeem Your Test Reward from CLI",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestEventSub(t *testing.T) {
Status: "tested",
ItemID: "12345678-1234-abcd-5678-000000000000",
Cost: 1337,
ItemName: "Testing",
}

r, err := Event{}.GenerateEvent(params)
Expand All @@ -39,6 +40,7 @@ func TestEventSub(t *testing.T) {
a.Equal(params.Status, body.Event.Status)
a.Equal(params.Cost, body.Event.Reward.Cost)
a.Equal(params.ItemID, body.Event.Reward.ID)
a.Equal(params.ItemName, body.Event.Reward.Title)

params = events.MockEventParameters{
Transport: models.TransportEventSub,
Expand Down
8 changes: 6 additions & 2 deletions internal/events/types/channel_points_reward/reward_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
params.Cost = 150
}

if params.ItemName == "" {
params.ItemName = "Test Reward from CLI"
}

switch params.Transport {
case models.TransportEventSub:
body := models.EventsubResponse{
Expand All @@ -64,7 +68,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
IsEnabled: true,
IsPaused: false,
IsInStock: true,
Title: "Test Reward from CLI",
Title: params.ItemName,
Cost: params.Cost,
Prompt: "Redeem Your Test Reward from CLI",
IsUserInputRequired: true,
Expand All @@ -81,7 +85,7 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
},
GlobalCooldown: models.RewardGlobalCooldown{
IsEnabled: true,
Value: 300,
Seconds: 300,
},
BackgroundColor: "#c0ffee",
Image: models.RewardImage{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestEventSub(t *testing.T) {
Status: "tested",
ItemID: "12345678-1234-abcd-5678-000000000000",
Cost: 1337,
ItemName: "Testing",
}

r, err := Event{}.GenerateEvent(params)
Expand All @@ -36,6 +37,7 @@ func TestEventSub(t *testing.T) {

a.Equal(toUser, body.Event.BroadcasterUserID, "Expected to user %v, got %v", toUser, body.Event.BroadcasterUserID)
a.Equal(params.Cost, body.Event.Cost, "Expected cost %v, got %v", params.Cost, body.Event.Cost)
a.Equal(params.ItemName, body.Event.Title)
}

func TestWebSub(t *testing.T) {
Expand Down
26 changes: 16 additions & 10 deletions internal/events/types/stream_change/stream_change_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ var triggerSupported = []string{"stream-change"}

var triggerMapping = map[string]map[string]string{
models.TransportWebSub: {
"stream_change": "streams",
"stream-change": "streams",
},
models.TransportEventSub: {
"stream_change": "channel.update",
"stream-change": "channel.update",
},
}

Expand All @@ -34,8 +34,14 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
var event []byte
var err error

if params.StreamTitle == "" {
params.StreamTitle = "Example title from the CLI!"
if params.Description == "" {
params.Description = "Example title from the CLI!"
}
if params.ItemID == "" {
params.ItemID = "509658"
}
if params.ItemName == "" {
params.ItemName = "Just Chatting"
}

switch params.Transport {
Expand All @@ -61,11 +67,11 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
StreamTitle: params.StreamTitle,
StreamTitle: params.Description,
StreamLanguage: "en",
StreamCategoryID: "509658",
StreamCategoryName: "Just Chatting",
IsMature: "true",
StreamCategoryID: params.ItemID,
StreamCategoryName: params.ItemName,
IsMature: false,
},
}
event, err = json.Marshal(body)
Expand All @@ -80,10 +86,10 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
BroadcasterUserID: params.ToUserID,
BroadcasterUserLogin: params.ToUserName,
BroadcasterUserName: params.ToUserName,
StreamCategoryID: "509658",
StreamCategoryID: params.ItemID,
StreamCategoryName: "Just Chatting",
StreamType: "live",
StreamTitle: params.StreamTitle,
StreamTitle: params.Description,
StreamViewerCount: 9848,
StreamStartedAt: util.GetTimestamp().Format(time.RFC3339),
StreamLanguage: "en",
Expand Down
10 changes: 7 additions & 3 deletions internal/events/types/stream_change/stream_change_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestEventSub(t *testing.T) {
ToUserID: toUser,
Transport: models.TransportEventSub,
Trigger: "stream_change",
ItemID: "1234",
}

r, err = Event{}.GenerateEvent(params)
Expand All @@ -50,19 +51,21 @@ func TestEventSub(t *testing.T) {

a.Equal(toUser, body.Event.BroadcasterUserID, "Expected Stream Channel %v, got %v", toUser, body.Event.BroadcasterUserID)
a.Equal("Example title from the CLI!", body.Event.StreamTitle, "Expected new stream title, got %v", body.Event.StreamTitle)
a.Equal("1234", body.Event.StreamCategoryID)
}

func TestWebSubStreamChange(t *testing.T) {
a := test_setup.SetupTestEnv(t)

newStreamTitle := "Awesome new title from the CLI!"

params := *&events.MockEventParameters{
params := events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: models.TransportWebSub,
Trigger: "stream-change",
StreamTitle: newStreamTitle,
Description: newStreamTitle,
ItemID: "1234",
}

r, err := Event{}.GenerateEvent(params)
Expand All @@ -75,11 +78,12 @@ func TestWebSubStreamChange(t *testing.T) {
// write tests here for websub
a.Equal(toUser, body.Data[0].BroadcasterUserID, "Expected Stream Channel %v, got %v", toUser, body.Data[0].BroadcasterUserID)
a.Equal(newStreamTitle, body.Data[0].StreamTitle, "Expected new stream title, got %v", body.Data[0].StreamTitle)
a.Equal("1234", body.Data[0].StreamCategoryID)
}
func TestFakeTransport(t *testing.T) {
a := test_setup.SetupTestEnv(t)

params := *&events.MockEventParameters{
params := events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: "fake_transport",
Expand Down
12 changes: 8 additions & 4 deletions internal/events/types/streamup/streamup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
var event []byte
var err error

if params.StreamTitle == "" {
params.StreamTitle = "Example title from the CLI!"
if params.Description == "" {
params.Description = "Example title from the CLI!"
}

if params.ItemID == "" {
params.ItemID = "509658"
}

switch params.Transport {
Expand Down Expand Up @@ -76,9 +80,9 @@ func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEven
UserID: params.ToUserID,
UserLogin: params.ToUserName,
UserName: params.ToUserName,
GameID: "509658",
GameID: params.ItemID,
Type: "live",
Title: params.StreamTitle,
Title: params.Description,
ViewerCount: util.RandomViewerCount(),
StartedAt: util.GetTimestamp().Format(time.RFC3339),
Language: "en",
Expand Down
2 changes: 1 addition & 1 deletion internal/models/reward.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type RewardMax struct {

type RewardGlobalCooldown struct {
IsEnabled bool `json:"is_enabled"`
Value int64 `json:"value"`
Seconds int64 `json:"seconds"`
}

type RewardImage struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/models/stream_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ChannelUpdateEventSubEvent struct {
StreamLanguage string `json:"language"`
StreamCategoryID string `json:"category_id"`
StreamCategoryName string `json:"category_name"`
IsMature string `json:"is_mature"`
IsMature bool `json:"is_mature"`
}

type StreamChangeWebSubResponse struct {
Expand Down

0 comments on commit 4ca0cd3

Please sign in to comment.