Skip to content

Commit

Permalink
Added check for unique block id
Browse files Browse the repository at this point in the history
  • Loading branch information
sspaink committed May 11, 2020
1 parent 0f98367 commit d988ff3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
ErrRTMGoodbye = errorsx.String("goodbye detected")
ErrRTMDeadman = errorsx.String("deadman switch triggered")
ErrParametersMissing = errorsx.String("received empty parameters")
ErrBlockIDNotUnique = errorsx.String("Block ID needs to be unique")
ErrInvalidConfiguration = errorsx.String("invalid configuration")
ErrMissingHeaders = errorsx.String("missing headers")
ErrExpiredTimestamp = errorsx.String("timestamp is too old")
Expand Down
22 changes: 22 additions & 0 deletions views.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ func (api *Client) OpenView(triggerID string, view ModalViewRequest) (*ViewRespo
return api.OpenViewContext(context.Background(), triggerID, view)
}

// ValidateUniqueBlockID will verify if each input block has a unique block ID if set
func ValidateUniqueBlockID(view ModalViewRequest) bool {

uniqueBlockID := map[string]bool{}

for _, b := range view.Blocks.BlockSet {
if inputBlock, ok := b.(*InputBlock); ok {
if _, ok := uniqueBlockID[inputBlock.BlockID]; ok {
return false
}
uniqueBlockID[inputBlock.BlockID] = true
}
}

return true
}

// OpenViewContext opens a view for a user with a custom context.
func (api *Client) OpenViewContext(
ctx context.Context,
Expand All @@ -159,6 +176,11 @@ func (api *Client) OpenViewContext(
if triggerID == "" {
return nil, ErrParametersMissing
}

if !ValidateUniqueBlockID(view) {
return nil, ErrBlockIDNotUnique
}

req := openViewRequest{
TriggerID: triggerID,
View: view,
Expand Down
44 changes: 33 additions & 11 deletions views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,39 @@ func TestSlack_OpenView(t *testing.T) {
api := New("testing-token", OptionAPIURL("http://"+serverAddr+"/"))

cases := []struct {
caseName string
triggerID string
rawResp string
expectedResp *ViewResponse
expectedErr error
caseName string
triggerID string
modalViewRequest ModalViewRequest
rawResp string
expectedResp *ViewResponse
expectedErr error
}{
{
caseName: "pass empty trigger_id",
triggerID: "",
caseName: "pass empty trigger_id",
triggerID: "",
modalViewRequest: ModalViewRequest{},
rawResp: "",
expectedResp: nil,
expectedErr: ErrParametersMissing,
},
{
caseName: "raise an error for not having a unique block id",
triggerID: "dummy_trigger_id",
modalViewRequest: ModalViewRequest{
Blocks: Blocks{
BlockSet: []Block{
&InputBlock{
BlockID: "example",
},
&InputBlock{
BlockID: "example",
},
},
},
},
rawResp: "",
expectedResp: nil,
expectedErr: ErrParametersMissing,
expectedErr: ErrBlockIDNotUnique,
},
{
caseName: "raise an error from Slack API",
Expand All @@ -60,8 +81,9 @@ func TestSlack_OpenView(t *testing.T) {
expectedErr: dummySlackErr,
},
{
caseName: "success",
triggerID: "dummy_trigger_id",
caseName: "success",
triggerID: "dummy_trigger_id",
modalViewRequest: ModalViewRequest{},
rawResp: `{
"ok": true,
"view": {
Expand Down Expand Up @@ -126,7 +148,7 @@ func TestSlack_OpenView(t *testing.T) {
t.Run(c.caseName, func(t *testing.T) {
h.rawResponse = c.rawResp

resp, err := api.OpenView(c.triggerID, ModalViewRequest{})
resp, err := api.OpenView(c.triggerID, c.modalViewRequest)
if c.expectedErr == nil && err != nil {
t.Errorf("unexpected error: %s\n", err)
return
Expand Down

0 comments on commit d988ff3

Please sign in to comment.