-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from twitchdev/feature/auth-and-raid-topics
added revoke+raid topics and fixed template
- Loading branch information
Showing
13 changed files
with
480 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
internal/events/types/authorization_revoke/authorization_revoke.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
93 changes: 93 additions & 0 deletions
93
internal/events/types/authorization_revoke/authorization_revoke_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
} |
Oops, something went wrong.