Skip to content

Commit

Permalink
Merge pull request #30 from twitchdev/feature/auth-and-raid-topics
Browse files Browse the repository at this point in the history
added revoke+raid topics and fixed template
  • Loading branch information
lleadbet authored Feb 20, 2021
2 parents e0cd933 + 19719f3 commit 0e96cd1
Show file tree
Hide file tree
Showing 13 changed files with 480 additions and 28 deletions.
54 changes: 28 additions & 26 deletions docs/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ 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. |
| `cheer` | Only usable with the `eventsub` transport, shows Cheers from chat. |
| `transaction` | Bits in Extensions transactions events. |
| `add-reward` | Channel Points EventSub event for a Custom Reward being added. |
| `update-reward` | Channel Points EventSub event for a Custom Reward being updated. |
| `remove-reward` | Channel Points EventSub event for a Custom Reward being removed. |
| `add-redemption` | Channel Points EventSub event for a redemption being performed. |
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |


| 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. |
| `cheer` | Only usable with the `eventsub` transport, shows Cheers from chat. |
| `transaction` | Bits in Extensions transactions events. |
| `add-reward` | Channel Points EventSub event for a Custom Reward being added. |
| `update-reward` | Channel Points EventSub event for a Custom Reward being updated. |
| `remove-reward` | Channel Points EventSub event for a Custom Reward being removed. |
| `add-redemption` | Channel Points EventSub event for a redemption being performed. |
| `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. |

**Flags**

Expand Down Expand Up @@ -95,18 +95,20 @@ 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. |
| `cheer` | Only usable with the `eventsub` transport, shows Cheers from chat. |
| `transaction` | Bits in Extensions transactions events. |
| `add-reward` | Channel Points EventSub event for a Custom Reward being added. |
| `update-reward` | Channel Points EventSub event for a Custom Reward being updated. |
| `remove-reward` | Channel Points EventSub event for a Custom Reward being removed. |
| `add-redemption` | Channel Points EventSub event for a redemption being performed. |
| `update-redemption` | Channel Points EventSub event for a redemption being updated. |
| 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. |
| `cheer` | Only usable with the `eventsub` transport, shows Cheers from chat. |
| `transaction` | Bits in Extensions transactions events. |
| `add-reward` | Channel Points EventSub event for a Custom Reward being added. |
| `update-reward` | Channel Points EventSub event for a Custom Reward being updated. |
| `remove-reward` | Channel Points EventSub event for a Custom Reward being removed. |
| `add-redemption` | Channel Points EventSub event for a redemption being performed. |
| `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. |

**Flags**

Expand Down
5 changes: 4 additions & 1 deletion internal/events/types/_template/_event_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var triggerMapping = map[string]map[string]string{

type Event struct{}

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

Expand Down Expand Up @@ -70,3 +70,6 @@ func (e Event) ValidTrigger(t string) bool {
}
return false
}
func (e Event) GetTopic(transport string, trigger string) string {
return triggerMapping[transport][trigger]
}
6 changes: 6 additions & 0 deletions internal/events/types/_template/_event_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,9 @@ func TestValidTransport(t *testing.T) {
r = Event{}.ValidTransport("noteventsub")
a.Equal(false, r)
}
func TestGetTopic(t *testing.T) {
a := util.SetupTestEnv(t)

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

import (
"encoding/json"
"errors"
"time"

"github.com/spf13/viper"
"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{"revoke"}

var triggerMapping = map[string]map[string]string{
models.TransportEventSub: {
"trigger_keyword": "revoke",
},
}

type Event struct{}

func (e Event) GenerateEvent(params events.MockEventParameters) (events.MockEventResponse, error) {
var event []byte
var err error
clientID := viper.GetString("ClientID")

// if not configured, generate a random one
if clientID == "" {
clientID = util.RandomClientID()
}
switch params.Transport {
case models.TransportEventSub:
body := &models.AuthorizationRevokeEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
Status: "enabled",
Type: triggerMapping[params.Transport][params.Trigger],
Version: "1",
Condition: models.EventsubCondition{
ClientID: clientID,
},
Transport: models.EventsubTransport{
Method: "webhook",
Callback: "null",
},
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
Event: models.AuthorizationRevokeEvent{
ClientID: clientID,
UserID: params.FromUserID,
UserLogin: params.FromUserName,
UserName: params.FromUserName,
},
}
event, err = json.Marshal(body)
if err != nil {
return events.MockEventResponse{}, err
}
case models.TransportWebSub:
return events.MockEventResponse{}, errors.New("Websub is unsupported for authorization revoke events")
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]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package authorization_revoke

import (
"encoding/json"
"testing"

"github.com/spf13/viper"
"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)

viper.Set("ClientID", "1234")
params := *&events.MockEventParameters{
FromUserID: fromUser,
ToUserID: toUser,
Transport: models.TransportEventSub,
Trigger: "subscribe",
}

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

var body models.AuthorizationRevokeEventSubResponse // replace with actual value
err = json.Unmarshal(r.JSON, &body)
a.Nil(err)

a.NotEmpty(body.Event.ClientID)
a.Equal(body.Event.ClientID, body.Subscription.Condition.ClientID)
a.Equal("1234", body.Event.ClientID)
}
func TestWebSub(t *testing.T) {
a := util.SetupTestEnv(t)

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

_, err := Event{}.GenerateEvent(params)
a.NotNil(err)

// write tests here for websub
}
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("revoke")
a.Equal(true, r)

r = Event{}.ValidTrigger("fake_revoke")
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, "revoke")
a.NotNil(r)
}
93 changes: 93 additions & 0 deletions internal/events/types/raid/raid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package raid

import (
"encoding/json"
"errors"
"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{"raid"}

var triggerMapping = map[string]map[string]string{
models.TransportEventSub: {
"raid": "channel.raid",
},
}

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.RaidEventSubResponse{
Subscription: models.EventsubSubscription{
ID: params.ID,
Status: "enabled",
Type: triggerMapping[params.Transport][params.Trigger],
Version: "beta",
Condition: models.EventsubCondition{
ToBroadcasterUserID: params.ToUserID,
},
Transport: models.EventsubTransport{
Method: "webhook",
Callback: "null",
},
CreatedAt: util.GetTimestamp().Format(time.RFC3339Nano),
},
Event: models.RaidEvent{
ToBroadcasterUserID: params.ToUserID,
ToBroadcasterUserLogin: params.ToUserName,
ToBroadcasterUserName: params.ToUserName,
FromBroadcasterUserID: params.FromUserID,
FromBroadcasterUserLogin: params.FromUserName,
FromBroadcasterUserName: params.FromUserName,
Viewers: util.RandomViewerCount(),
},
}
event, err = json.Marshal(body)
if err != nil {
return events.MockEventResponse{}, err
}
case models.TransportWebSub:
return events.MockEventResponse{}, errors.New("Raids are unsupported for websub")
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]
}
Loading

0 comments on commit 0e96cd1

Please sign in to comment.