From 07f8b11b20219ad1e4c8e98a688b57e792769afe Mon Sep 17 00:00:00 2001 From: Joy Date: Wed, 20 Nov 2024 02:32:03 +0530 Subject: [PATCH] Refactor : Simplified existing code --- controllers/baseHandler_test.go | 2 -- service/helloService.go | 2 +- service/helloService_test.go | 10 ++++++++-- service/main.go | 1 - service/main_test.go | 11 ++--------- utils/responseGenerator.go | 11 +++++++++++ 6 files changed, 22 insertions(+), 15 deletions(-) create mode 100644 utils/responseGenerator.go diff --git a/controllers/baseHandler_test.go b/controllers/baseHandler_test.go index 0062835..0f0b7b7 100644 --- a/controllers/baseHandler_test.go +++ b/controllers/baseHandler_test.go @@ -58,8 +58,6 @@ func TestHomeHandler(t *testing.T) { assert.Nil(t, err) assert.Equal(t, float64(discordgo.InteractionResponseChannelMessageWithSource), response["type"]) - data := response["data"].(map[string]interface{}) - assert.Equal(t, "Hey there! Congratulations, you just executed your first slash command", data["content"]) }) t.Run("Should return 200 when interaction type is unknown", func(t *testing.T) { diff --git a/service/helloService.go b/service/helloService.go index 5d13850..33a3530 100644 --- a/service/helloService.go +++ b/service/helloService.go @@ -11,7 +11,7 @@ func (s *CommandService) HelloService(response http.ResponseWriter, request *htt messageResponse := &discordgo.InteractionResponse{ Type: discordgo.InteractionResponseChannelMessageWithSource, Data: &discordgo.InteractionResponseData{ - Content: "Hey there! Congratulations, you just executed your first slash command", + Content: utils.ResponseGenerator.HelloResponse(s.discordMessage.Member.User.ID), }, } utils.Success.NewDiscordResponse(response, "Success", messageResponse) diff --git a/service/helloService_test.go b/service/helloService_test.go index 3e2c267..85938fd 100644 --- a/service/helloService_test.go +++ b/service/helloService_test.go @@ -1,6 +1,7 @@ package service import ( + "bytes" "encoding/json" "net/http" "net/http/httptest" @@ -8,22 +9,27 @@ import ( "github.com/Real-Dev-Squad/discord-service/fixtures" _ "github.com/Real-Dev-Squad/discord-service/tests/helpers" + "github.com/Real-Dev-Squad/discord-service/utils" "github.com/bwmarrin/discordgo" "github.com/stretchr/testify/assert" ) func TestHelloService(t *testing.T) { + t.Run("should return a success response with a message", func(t *testing.T) { w := httptest.NewRecorder() - r, _ := http.NewRequest("GET", "/", nil) + jsonBytes, _ := json.Marshal(fixtures.HelloCommand) + r, _ := http.NewRequest("POST", "/", bytes.NewBuffer(jsonBytes)) + CS.discordMessage = fixtures.HelloCommand CS.HelloService(w, r) + assert.Equal(t, http.StatusOK, w.Code) var response discordgo.InteractionResponse err := json.NewDecoder(w.Body).Decode(&response) assert.NoError(t, err) assert.Equal(t, discordgo.InteractionResponseChannelMessageWithSource, response.Type) - assert.Equal(t, "Hey there! Congratulations, you just executed your first slash command", response.Data.Content) + assert.Equal(t, utils.ResponseGenerator.HelloResponse(fixtures.HelloCommand.Member.User.ID), response.Data.Content) }) } diff --git a/service/main.go b/service/main.go index 91e5fd5..93f6bc8 100644 --- a/service/main.go +++ b/service/main.go @@ -15,7 +15,6 @@ var CS = CommandService{} func MainService(discordMessage *dtos.DiscordMessage) func(response http.ResponseWriter, request *http.Request) { CS.discordMessage = discordMessage switch discordMessage.Data.Name { - case "hello": return CS.HelloService diff --git a/service/main_test.go b/service/main_test.go index 012c9fa..b9553cf 100644 --- a/service/main_test.go +++ b/service/main_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/Real-Dev-Squad/discord-service/dtos" + "github.com/Real-Dev-Squad/discord-service/fixtures" _ "github.com/Real-Dev-Squad/discord-service/tests/helpers" "github.com/bwmarrin/discordgo" "github.com/stretchr/testify/assert" @@ -13,15 +14,7 @@ import ( func TestMainService(t *testing.T) { t.Run("should return HelloService when command name is hello", func(t *testing.T) { - discordMessage := &dtos.DiscordMessage{ - Data: &dtos.Data{ - GuildId: "876543210987654321", - ApplicationCommandInteractionData: discordgo.ApplicationCommandInteractionData{ - Name: "hello", - }, - }, - } - handler := MainService(discordMessage) + handler := MainService(fixtures.HelloCommand) w := httptest.NewRecorder() r, _ := http.NewRequest("GET", "/", nil) handler(w, r) diff --git a/utils/responseGenerator.go b/utils/responseGenerator.go new file mode 100644 index 0000000..c1c88ea --- /dev/null +++ b/utils/responseGenerator.go @@ -0,0 +1,11 @@ +package utils + +import "fmt" + +type responseGenerator struct{} + +func (r *responseGenerator) HelloResponse(userId string) string { + return fmt.Sprintf("Hey there <@%s>! Congratulations, you just executed your first slash command", userId) +} + +var ResponseGenerator = &responseGenerator{}