github.com/someshkoli/terratest@v0.41.1/modules/slack/validate.go (about)

     1  package slack
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/gruntwork-io/terratest/modules/testing"
    10  	"github.com/slack-go/slack"
    11  )
    12  
    13  // ValidateExpectedSlackMessage validates whether a message containing the expected text was posted in the given channel
    14  // ID, looking back historyLimit messages up to the given duration. For example, if you set (15*time.Minute) as the
    15  // lookBack parameter with historyLimit set to 50, then this will look back the last 50 messages, up to 15 minutes ago.
    16  // This expects a slack token to be provided. This returns MessageNotFoundErr when there is no match.
    17  // For the purposes of matching, this only checks the following blocks:
    18  // - Section block text
    19  // - Header block text
    20  // All other blocks are ignored in the validation.
    21  // NOTE: This only looks for bot posted messages.
    22  func ValidateExpectedSlackMessageE(
    23  	t testing.TestingT,
    24  	token,
    25  	channelID,
    26  	expectedText string,
    27  	historyLimit int,
    28  	lookBack time.Duration,
    29  ) error {
    30  	lookBackTime := time.Now().Add(-1 * lookBack)
    31  	slackClt := slack.New(token)
    32  	params := slack.GetConversationHistoryParameters{
    33  		ChannelID: channelID,
    34  		Limit:     historyLimit,
    35  		Oldest:    strconv.FormatInt(lookBackTime.Unix(), 10),
    36  	}
    37  
    38  	resp, err := slackClt.GetConversationHistory(&params)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	for _, msg := range resp.Messages {
    44  		if checkMessageContainsText(msg.Msg, expectedText) {
    45  			return nil
    46  		}
    47  
    48  		if msg.SubMessage != nil {
    49  			if checkMessageContainsText(*msg.SubMessage, expectedText) {
    50  				return nil
    51  			}
    52  		}
    53  	}
    54  	return fmt.Errorf("still no message")
    55  }
    56  
    57  func checkMessageContainsText(msg slack.Msg, expectedText string) bool {
    58  	// If this message is not a bot message, ignore.
    59  	if msg.Type != slack.MsgSubTypeBotMessage && msg.BotID == "" {
    60  		return false
    61  	}
    62  
    63  	// Check message text
    64  	if strings.Contains(msg.Text, expectedText) {
    65  		return true
    66  	}
    67  
    68  	// Check attachments
    69  	for _, attachment := range msg.Attachments {
    70  		if strings.Contains(attachment.Text, expectedText) {
    71  			return true
    72  		}
    73  	}
    74  
    75  	// Check blocks
    76  	for _, block := range msg.Blocks.BlockSet {
    77  		switch block.BlockType() {
    78  		case slack.MBTSection:
    79  			sectionBlk := block.(*slack.SectionBlock)
    80  			if sectionBlk.Text != nil && strings.Contains(sectionBlk.Text.Text, expectedText) {
    81  				return true
    82  			}
    83  		case slack.MBTHeader:
    84  			headerBlk := block.(*slack.HeaderBlock)
    85  			if headerBlk.Text != nil && strings.Contains(headerBlk.Text.Text, expectedText) {
    86  				return true
    87  			}
    88  		}
    89  	}
    90  
    91  	return false
    92  }
    93  
    94  type MessageNotFoundErr struct{}
    95  
    96  func (err MessageNotFoundErr) Error() string {
    97  	return "Could not find the expected text in any of the messages posted in the given channel."
    98  }