github.com/newrelic/newrelic-client-go@v1.1.0/pkg/alerts/channels_integration_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package alerts
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/newrelic/newrelic-client-go/internal/serialization"
    12  )
    13  
    14  func TestIntegrationChannel(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	var (
    18  		testChannelEmail = Channel{
    19  			Name: "integration-test-email",
    20  			Type: ChannelTypes.Email,
    21  			Configuration: ChannelConfiguration{
    22  				Recipients:            "devtoolkittest@newrelic.com",
    23  				IncludeJSONAttachment: "true",
    24  			},
    25  			Links: ChannelLinks{
    26  				PolicyIDs: []int{},
    27  			},
    28  		}
    29  
    30  		testChannelOpsGenie = Channel{
    31  			Name: "integration-test-opsgenie",
    32  			Type: ChannelTypes.OpsGenie,
    33  			Configuration: ChannelConfiguration{
    34  				APIKey:     "abc123",
    35  				Teams:      "dev-toolkit",
    36  				Tags:       "tag1,tag2",
    37  				Recipients: "devtoolkittest@newrelic.com",
    38  			},
    39  			Links: ChannelLinks{
    40  				PolicyIDs: []int{},
    41  			},
    42  		}
    43  
    44  		testChannelSlack = Channel{
    45  			Name: "integration-test-slack",
    46  			Type: ChannelTypes.Slack,
    47  			Configuration: ChannelConfiguration{
    48  				URL:     "https://example-org.slack.com",
    49  				Channel: "test-channel",
    50  			},
    51  			Links: ChannelLinks{
    52  				PolicyIDs: []int{},
    53  			},
    54  		}
    55  
    56  		testChannelVictorops = Channel{
    57  			Name: "integration-test-victorops",
    58  			Type: ChannelTypes.VictorOps,
    59  			Configuration: ChannelConfiguration{
    60  				Key:      "abc123",
    61  				RouteKey: "/route-name",
    62  			},
    63  			Links: ChannelLinks{
    64  				PolicyIDs: []int{},
    65  			},
    66  		}
    67  
    68  		testChannelWebhook = Channel{
    69  			Name: "integration-test-webhook",
    70  			Type: ChannelTypes.Webhook,
    71  			Configuration: ChannelConfiguration{
    72  				BaseURL:     "https://test.com",
    73  				PayloadType: "application/json",
    74  				Headers: serialization.MapStringInterface{
    75  					"x-test-header": "test-header",
    76  				},
    77  				Payload: serialization.MapStringInterface{
    78  					"account_id": "123",
    79  				},
    80  			},
    81  			Links: ChannelLinks{
    82  				PolicyIDs: []int{},
    83  			},
    84  		}
    85  
    86  		testChannelWebhookEmptyHeadersAndPayload = Channel{
    87  			Name: "integration-test-webhook-empty-headers-and-payload",
    88  			Type: ChannelTypes.Webhook,
    89  			Configuration: ChannelConfiguration{
    90  				BaseURL: "https://test.com",
    91  			},
    92  			Links: ChannelLinks{
    93  				PolicyIDs: []int{},
    94  			},
    95  		}
    96  
    97  		testChannelWebhookWeirdHeadersAndPayload = Channel{
    98  			Name: "integration-test-webhook-weird-headers-and-payload",
    99  			Type: ChannelTypes.Webhook,
   100  			Configuration: ChannelConfiguration{
   101  				BaseURL: "https://test.com",
   102  				Headers: serialization.MapStringInterface{
   103  					"": "",
   104  				},
   105  				Payload: serialization.MapStringInterface{
   106  					"": "",
   107  				},
   108  				PayloadType: "application/json",
   109  			},
   110  			Links: ChannelLinks{
   111  				PolicyIDs: []int{},
   112  			},
   113  		}
   114  
   115  		// Currently the v2 API has minimal validation on the data
   116  		// structure for Headers and Payload, so we need to test
   117  		// as many scenarios as possible.
   118  		testChannelWebhookComplexHeadersPayload = Channel{
   119  			Name: "integration-test-webhook",
   120  			Type: ChannelTypes.Webhook,
   121  			Configuration: ChannelConfiguration{
   122  				BaseURL:     "https://test.com",
   123  				PayloadType: "application/json",
   124  				Headers: serialization.MapStringInterface{
   125  					"x-test-header": "test-header",
   126  					"object": map[string]interface{}{
   127  						"key": "value",
   128  						"nestedObject": map[string]interface{}{
   129  							"k": "v",
   130  						},
   131  					},
   132  				},
   133  				Payload: serialization.MapStringInterface{
   134  					"account_id": "123",
   135  					"array":      []interface{}{"string", 2},
   136  					"object": map[string]interface{}{
   137  						"key": "value",
   138  						"nestedObject": map[string]interface{}{
   139  							"k": "v",
   140  						},
   141  					},
   142  				},
   143  			},
   144  			Links: ChannelLinks{
   145  				PolicyIDs: []int{},
   146  			},
   147  		}
   148  
   149  		channels = []Channel{
   150  			testChannelEmail,
   151  			testChannelOpsGenie,
   152  			testChannelSlack,
   153  			testChannelVictorops,
   154  			testChannelWebhook,
   155  			testChannelWebhookEmptyHeadersAndPayload,
   156  			testChannelWebhookWeirdHeadersAndPayload,
   157  			testChannelWebhookComplexHeadersPayload,
   158  		}
   159  	)
   160  
   161  	client := newIntegrationTestClient(t)
   162  
   163  	for _, channel := range channels {
   164  		// Test: Create
   165  		created, err := client.CreateChannel(channel)
   166  
   167  		require.NoError(t, err)
   168  		require.NotNil(t, created)
   169  
   170  		// Test: Read
   171  		read, err := client.GetChannel(created.ID)
   172  
   173  		require.NoError(t, err)
   174  		require.NotNil(t, read)
   175  
   176  		// Test: Delete
   177  		deleted, err := client.DeleteChannel(read.ID)
   178  
   179  		require.NoError(t, err)
   180  		require.NotNil(t, deleted)
   181  	}
   182  }