Skip to content

Commit

Permalink
Parse string or boolean for SlashCommand.IsEnterpriseInstall
Browse files Browse the repository at this point in the history
  • Loading branch information
kpaulisse committed Feb 20, 2024
1 parent 2a9c35c commit 9a23f7a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
34 changes: 34 additions & 0 deletions slash.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package slack

import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)

// SlashCommand contains information about a request of the slash command
Expand Down Expand Up @@ -55,3 +58,34 @@ func (s SlashCommand) ValidateToken(verificationTokens ...string) bool {
}
return false
}

// UnmarshalJSON handles is_enterprise_install being either a boolean or a
// string when parsing JSON from various payloads
func (s *SlashCommand) UnmarshalJSON(data []byte) error {
type SlashCommandCopy SlashCommand
scopy := &struct {
*SlashCommandCopy
IsEnterpriseInstall interface{} `json:"is_enterprise_install"`
}{
SlashCommandCopy: (*SlashCommandCopy)(s),
}

if err := json.Unmarshal(data, scopy); err != nil {
return err
}

switch rawValue := scopy.IsEnterpriseInstall.(type) {
case string:
b, err := strconv.ParseBool(rawValue)
if err != nil {
return fmt.Errorf("parsing boolean for is_enterprise_install: %w", err)
}
s.IsEnterpriseInstall = b
case bool:
s.IsEnterpriseInstall = rawValue
default:
return fmt.Errorf("wrong data type for is_enterprise_install: %T", scopy.IsEnterpriseInstall)
}

return nil
}
68 changes: 68 additions & 0 deletions slash_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slack

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -99,3 +100,70 @@ func TestSlash_ServeHTTP(t *testing.T) {
resp.Body.Close()
}
}

func TestSlash_UnmarshalJSON(t *testing.T) {
tests := []struct {
body string
wantIsEnterpriseInstall bool
wantToken string
wantUnmarshalError string
}{
{
body: `{"token":"blahblah","is_enterprise_install":"false"}`,
wantIsEnterpriseInstall: false,
wantToken: "blahblah",
wantUnmarshalError: "",
},
{
body: `{"token":"blahblah","is_enterprise_install":false}`,
wantIsEnterpriseInstall: false,
wantToken: "blahblah",
wantUnmarshalError: "",
},
{
body: `{"token":"blahblah","is_enterprise_install":"true"}`,
wantIsEnterpriseInstall: true,
wantToken: "blahblah",
wantUnmarshalError: "",
},
{
body: `{"token":"blahblah","is_enterprise_install":true}`,
wantIsEnterpriseInstall: true,
wantToken: "blahblah",
wantUnmarshalError: "",
},
{
body: `{"token":"blahblah","is_enterprise_install":42}`,
wantUnmarshalError: "wrong data type for is_enterprise_install: float64",
},
{
body: `{"token":"blahblah","is_enterprise_install":"unconvertable to bool"}`,
wantUnmarshalError: "parsing boolean for is_enterprise_install: strconv.ParseBool: parsing \"unconvertable to bool\": invalid syntax",
},
}

for i, test := range tests {
var result SlashCommand

err := json.Unmarshal([]byte(test.body), &result)
if err != nil {
if err.Error() != test.wantUnmarshalError {
t.Errorf("%d: Got error %v, want error %q", i, err, test.wantUnmarshalError)
}
continue
}

if test.wantUnmarshalError != "" {
t.Errorf("%d: Got no error, want error %q", i, test.wantUnmarshalError)
continue
}

if result.IsEnterpriseInstall != test.wantIsEnterpriseInstall {
t.Errorf("%d: Got IsEnterpriseInstall %v, want IsEnterpriseInstall %v", i, result.IsEnterpriseInstall, test.wantIsEnterpriseInstall)
}

if result.Token != test.wantToken {
t.Errorf("%d: Got Token %v, want Token %v", i, result.Token, test.wantToken)
}
}
}

0 comments on commit 9a23f7a

Please sign in to comment.