generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implemented Queue (#36) * Chore : Updated .env.example * Feat : Added rabbitmq * Feat : Added exponential retry method * Feat: Added DTO for message * Bug : Fixed ExponentialBackoffRetry function * Feat : Added ExponentialBackoffRetry helper * Feat : Added Queue code * Test : Updated Test * Feat : Updated config to load queue name * Feat : Added QUEUE_URL in env * Doc : Updated readme * Chore : Updated order * Chore : Fixed broken test and added error handling * Feat : Added queue handler (#37) * Chore : Updated .env.example * Feat : Added rabbitmq * Feat : Added exponential retry method * Feat: Added DTO for message * Bug : Fixed ExponentialBackoffRetry function * Feat : Added ExponentialBackoffRetry helper * Feat : Added Queue code * Test : Updated Test * Feat : Updated config to load queue name * Feat : Added QUEUE_URL in env * Doc : Updated readme * Chore : Updated order * Chore : Fixed broken test and added error handling * Feat : Added queue handler * Add QUEUE_URL and QUEUE_NAME env (#39) * Chore : Updated .env.example * Feat : Added rabbitmq * Feat : Added exponential retry method * Feat: Added DTO for message * Bug : Fixed ExponentialBackoffRetry function * Feat : Added ExponentialBackoffRetry helper * Feat : Added Queue code * Test : Updated Test * Feat : Updated config to load queue name * Feat : Added QUEUE_URL in env * Doc : Updated readme * Chore : Updated order * Chore : Fixed broken test and added error handling * Feat : Added queue handler * chore: add queuename and url env --------- Co-authored-by: Joy <[email protected]> * replace env to vars (#41) * Feat/listening command (#43) * Feat : Added listening command * Feat : Added helper to DataPacket * Feat : Updated SendMessage Code * Test : Fixed test for DataPacket * Feat : Added listening service * Chore : Updated function declaration * Chore : Updated InteractionResponseData * Feat : Addded end-to-end implementation of listening command * Feat : Build common package for discord * WIP : Creating methods for discord * Test : Added test for commandHandler.MainHandler in QueueHandler * Test : Added test for QueueHandler * Test : Added test for MainHandler * Test : Added test * Test : Added test * Chore : Removed consoles * Chore : Removed fmt * Chore : Minor change * Chore : Minor change * feat : Moved register command flow within main.go (#45) * Feat : Moved register command flow within main.go * Refactor : Updated vars & functions name * Update commands/register/register.go Co-authored-by: Yash Raj <[email protected]> * Chore : Minor change * Chore : Minor change --------- Co-authored-by: Amit Prakash <[email protected]> Co-authored-by: Yash Raj <[email protected]> --------- Co-authored-by: Prakash Choudhary <[email protected]> Co-authored-by: Amit Prakash <[email protected]> Co-authored-by: Yash Raj <[email protected]>
- Loading branch information
1 parent
5b190d7
commit 59d2e4e
Showing
34 changed files
with
1,160 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
PORT = 8999 | ||
DISCORD_PUBLIC_KEY = "<DISCORD_PUBLIC_KEY>" | ||
PORT = "<PORT>" # Default :8999 | ||
DISCORD_PUBLIC_KEY = "<DISCORD_PUBLIC_KEY>" | ||
GUILD_ID = "<DISCORD_GUILD_ID>" | ||
BOT_TOKEN = "<BOT_TOKEN>" | ||
DISCORD_QUEUE = "<DISCORD_QUEUE>" # Default :DISCORD_QUEUE | ||
QUEUE_URL = "<QUEUE_URL>" # Default :amqp://localhost |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/Real-Dev-Squad/discord-service/utils" | ||
) | ||
|
||
func (s *CommandHandler) listeningHandler() error { | ||
metaData := s.discordMessage.MetaData | ||
nickName := metaData["nickname"] | ||
if metaData["value"] == "true" { | ||
nickName = fmt.Sprintf("%s%s%s", utils.NICKNAME_PREFIX, nickName, utils.NICKNAME_SUFFIX) | ||
} else { | ||
nickName = strings.TrimPrefix(strings.TrimSuffix(nickName, utils.NICKNAME_SUFFIX), utils.NICKNAME_PREFIX) | ||
} | ||
return UpdateNickName(s.discordMessage.UserID, nickName) | ||
} |
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,63 @@ | ||
package handlers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Real-Dev-Squad/discord-service/dtos" | ||
_ "github.com/Real-Dev-Squad/discord-service/tests/helpers" | ||
"github.com/Real-Dev-Squad/discord-service/utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type MockCommandHandler struct { | ||
discordMessage *dtos.DataPacket | ||
} | ||
|
||
func TestListeningHandler(t *testing.T) { | ||
|
||
t.Run("should update nickname with prefix and suffix if value is true", func(t *testing.T) { | ||
|
||
dataPacket := &dtos.DataPacket{ | ||
UserID: "userID", | ||
MetaData: map[string]string{ | ||
"nickname": "testNick", | ||
"value": "true", | ||
}, | ||
} | ||
|
||
handler := &CommandHandler{discordMessage: dataPacket} | ||
err := handler.listeningHandler() | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("should update nickname without prefix and suffix if value is false", func(t *testing.T) { | ||
|
||
dataPacket := &dtos.DataPacket{ | ||
UserID: "userID", | ||
MetaData: map[string]string{ | ||
"nickname": utils.NICKNAME_PREFIX + "testNick" + utils.NICKNAME_SUFFIX, | ||
"value": "false", | ||
}, | ||
} | ||
|
||
handler := &CommandHandler{discordMessage: dataPacket} | ||
err := handler.listeningHandler() | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("should return error if UpdateNickName fails", func(t *testing.T) { | ||
|
||
dataPacket := &dtos.DataPacket{ | ||
UserID: "userID", | ||
MetaData: map[string]string{ | ||
"nickname": "testNick", | ||
"value": "true", | ||
}, | ||
} | ||
|
||
handler := &CommandHandler{discordMessage: dataPacket} | ||
err := handler.listeningHandler() | ||
assert.Error(t, err) | ||
assert.Equal(t, "websocket: close 4004: Authentication failed.", err.Error()) | ||
}) | ||
} |
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,71 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/Real-Dev-Squad/discord-service/config" | ||
"github.com/Real-Dev-Squad/discord-service/dtos" | ||
"github.com/Real-Dev-Squad/discord-service/models" | ||
"github.com/bwmarrin/discordgo" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type CommandHandler struct { | ||
discordMessage *dtos.DataPacket | ||
} | ||
|
||
var CS = CommandHandler{} | ||
|
||
func MainHandler(dataPacket []byte) func() error { | ||
packetData := &dtos.DataPacket{} | ||
err := packetData.FromByte(dataPacket) | ||
if err != nil { | ||
logrus.Errorf("Failed to unmarshal data send by queue: %v", err) | ||
return nil | ||
} | ||
CS.discordMessage = packetData | ||
switch packetData.CommandName { | ||
case "listening": | ||
return CS.listeningHandler | ||
default: | ||
logrus.Warn("Invalid Command Received: ", packetData.CommandName) | ||
return nil | ||
} | ||
} | ||
|
||
type DiscordSession struct { | ||
session *discordgo.Session | ||
} | ||
|
||
var NewDiscord = discordgo.New | ||
var CreateSession = func() (*discordgo.Session, error) { | ||
session, err := NewDiscord("Bot " + config.AppConfig.BOT_TOKEN) | ||
if err != nil { | ||
logrus.Errorf("Cannot create a new Discord session: %v", err) | ||
return nil, err | ||
} | ||
openSession := &models.SessionWrapper{Session: session} | ||
err = openSession.Open() | ||
if err != nil { | ||
logrus.Errorf("Cannot open the session: %v", err) | ||
return nil, err | ||
} | ||
return session, nil | ||
} | ||
|
||
func UpdateNickName(userId string, newNickName string) error { | ||
if len(newNickName) > 32 { | ||
logrus.Error("Must be 32 or fewer in length.") | ||
return errors.New("Must be 32 or fewer in length.") | ||
} | ||
session, err := CreateSession() | ||
if err != nil { | ||
return err | ||
} | ||
err = session.GuildMemberNickname(config.AppConfig.GUILD_ID, userId, newNickName) | ||
if err != nil { | ||
logrus.Errorf("Cannot update nickname: %v", err) | ||
return nil | ||
} | ||
return nil | ||
} |
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,79 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/Real-Dev-Squad/discord-service/dtos" | ||
_ "github.com/Real-Dev-Squad/discord-service/tests/helpers" | ||
"github.com/bwmarrin/discordgo" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMainHandler(t *testing.T) { | ||
t.Run("should return listeningHandler for 'listening' command", func(t *testing.T) { | ||
dataPacket := &dtos.DataPacket{ | ||
CommandName: "listening", | ||
} | ||
data, err := dataPacket.ToByte() | ||
assert.NoError(t, err) | ||
|
||
handler := MainHandler(data) | ||
assert.NotNil(t, handler) | ||
}) | ||
t.Run("should return nil for invalid data", func(t *testing.T) { | ||
invalidData := []byte(`{"invalid": "data"}`) | ||
handler := MainHandler(invalidData) | ||
assert.Nil(t, handler) | ||
}) | ||
} | ||
|
||
func TestCreateSession(t *testing.T) { | ||
t.Run("should fail if NewDiscord returns an error", func(t *testing.T) { | ||
originalNewDiscord := NewDiscord | ||
defer func() { NewDiscord = originalNewDiscord }() | ||
NewDiscord = func(token string) (s *discordgo.Session, err error) { | ||
return nil, errors.New("testing error") | ||
} | ||
_, err := CreateSession() | ||
assert.Error(t, err) | ||
}) | ||
t.Run("should initiate open session if NewDiscord returns no error", func(t *testing.T) { | ||
originalNewDiscord := NewDiscord | ||
defer func() { NewDiscord = originalNewDiscord }() | ||
NewDiscord = func(token string) (s *discordgo.Session, err error) { | ||
return &discordgo.Session{}, nil | ||
} | ||
assert.Panics(t, func() { CreateSession() }) | ||
}) | ||
} | ||
|
||
func mockCreateSession() (*discordgo.Session, error) { | ||
return &discordgo.Session{}, nil | ||
} | ||
func TestUpdateNickName(t *testing.T) { | ||
|
||
var originalCreateSession = CreateSession | ||
defer func() { CreateSession = originalCreateSession }() | ||
|
||
t.Run("should return error if newNickName is longer than 32 characters", func(t *testing.T) { | ||
err := UpdateNickName("userID", "ThisIsAVeryLongNicknameThatExceedsTheLimit") | ||
assert.Error(t, err) | ||
assert.Equal(t, "Must be 32 or fewer in length.", err.Error()) | ||
}) | ||
|
||
t.Run("should return error if CreateSession fails", func(t *testing.T) { | ||
CreateSession = func() (*discordgo.Session, error) { | ||
return nil, errors.New("failed to create session") | ||
} | ||
err := UpdateNickName("userID", "validNickname") | ||
assert.Error(t, err) | ||
assert.Equal(t, "failed to create session", err.Error()) | ||
}) | ||
t.Run("should hit GuildMemberNickname if CreateSession succeeds", func(t *testing.T) { | ||
CreateSession = mockCreateSession | ||
assert.Panics(t, func() { UpdateNickName("userID", "validNickname") }) | ||
|
||
}) | ||
|
||
} |
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
Oops, something went wrong.