github.com/newrelic/newrelic-client-go@v1.1.0/pkg/notifications/notifications_integration_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package notifications 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/newrelic/newrelic-client-go/pkg/ai" 14 15 mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers" 16 ) 17 18 func TestNotificationMutationDestination(t *testing.T) { 19 t.Parallel() 20 21 n := newIntegrationTestClient(t) 22 23 accountID, err := mock.GetTestAccountID() 24 if err != nil { 25 t.Skipf("%s", err) 26 } 27 28 // Create a destination to work with in this test 29 testIntegrationDestinationNameRandStr := mock.RandSeq(5) 30 destination := AiNotificationsDestinationInput{} 31 destination.Type = AiNotificationsDestinationTypeTypes.WEBHOOK 32 destination.Properties = []AiNotificationsPropertyInput{ 33 { 34 Key: "url", 35 Value: "https://webhook.site/94193c01-4a81-4782-8f1b-554d5230395b", 36 Label: "", 37 DisplayValue: "", 38 }, 39 } 40 destination.Auth = &AiNotificationsCredentialsInput{ 41 Type: AiNotificationsAuthTypeTypes.TOKEN, 42 Token: AiNotificationsTokenAuthInput{ 43 Token: "Token", 44 Prefix: "Bearer", 45 }, 46 } 47 destination.Name = fmt.Sprintf("test-notifications-destination-%s", testIntegrationDestinationNameRandStr) 48 49 // Test: Create 50 createResult, err := n.AiNotificationsCreateDestination(accountID, destination) 51 require.NoError(t, err) 52 require.NotNil(t, createResult) 53 require.NotEmpty(t, createResult.Destination.Auth) 54 require.Equal(t, ai.AiNotificationsAuthType("TOKEN"), createResult.Destination.Auth.AuthType) 55 56 // Test: Get Destination 57 filters := ai.AiNotificationsDestinationFilter{ 58 ID: createResult.Destination.ID, 59 } 60 sorter := AiNotificationsDestinationSorter{} 61 getDestinationResult, err := n.GetDestinations(accountID, "", filters, sorter) 62 require.NoError(t, err) 63 require.NotNil(t, getDestinationResult) 64 assert.Equal(t, 1, getDestinationResult.TotalCount) 65 66 // Test: Update Destination 67 updateDestination := AiNotificationsDestinationUpdate{} 68 updateDestination.Active = false 69 updateDestination.Properties = []AiNotificationsPropertyInput{ 70 { 71 Key: "url", 72 Value: "https://webhook.site/94193c01-4a81-4782-8f1b-554d5230395b", 73 Label: "", 74 DisplayValue: "", 75 }, 76 } 77 updateDestination.Auth = &AiNotificationsCredentialsInput{ 78 Type: AiNotificationsAuthTypeTypes.TOKEN, 79 Token: AiNotificationsTokenAuthInput{ 80 Token: "TokenUpdate", 81 Prefix: "BearerUpdate", 82 }, 83 } 84 updateDestination.Name = fmt.Sprintf("test-notifications-update-destination-%s", testIntegrationDestinationNameRandStr) 85 86 updateDestinationResult, err := n.AiNotificationsUpdateDestination(accountID, updateDestination, createResult.Destination.ID) 87 require.NoError(t, err) 88 require.NotNil(t, updateDestinationResult) 89 90 // Test: Delete 91 deleteResult, err := n.AiNotificationsDeleteDestination(accountID, createResult.Destination.ID) 92 require.NoError(t, err) 93 require.NotNil(t, deleteResult) 94 } 95 96 func TestNotificationMutationChannel(t *testing.T) { 97 t.Parallel() 98 99 n := newIntegrationTestClient(t) 100 101 accountID, err := mock.GetTestAccountID() 102 if err != nil { 103 t.Skipf("%s", err) 104 } 105 106 // Create a destination to work with in this test 107 testIntegrationDestinationNameRandStr := mock.RandSeq(5) 108 destination := AiNotificationsDestinationInput{} 109 destination.Type = AiNotificationsDestinationTypeTypes.WEBHOOK 110 destination.Properties = []AiNotificationsPropertyInput{ 111 { 112 Key: "url", 113 Value: "https://webhook.site/94193c01-4a81-4782-8f1b-554d5230395b", 114 Label: "", 115 DisplayValue: "", 116 }, 117 } 118 destination.Auth = &AiNotificationsCredentialsInput{ 119 Type: AiNotificationsAuthTypeTypes.TOKEN, 120 Token: AiNotificationsTokenAuthInput{ 121 Token: "Token", 122 Prefix: "Bearer", 123 }, 124 } 125 destination.Name = fmt.Sprintf("test-notifications-destination-%s", testIntegrationDestinationNameRandStr) 126 127 // Test: Create Destination 128 createDestinationResult, err := n.AiNotificationsCreateDestination(accountID, destination) 129 require.NoError(t, err) 130 require.NotNil(t, createDestinationResult) 131 132 destinationId := createDestinationResult.Destination.ID 133 134 // Create a channel to work with in this test 135 testIntegrationChannelNameRandStr := mock.RandSeq(5) 136 channel := AiNotificationsChannelInput{} 137 channel.Type = AiNotificationsChannelTypeTypes.WEBHOOK 138 channel.Product = AiNotificationsProductTypes.IINT 139 channel.Properties = []AiNotificationsPropertyInput{ 140 { 141 Key: "headers", 142 Value: "{}", 143 Label: "Custom headers", 144 DisplayValue: "", 145 }, 146 { 147 Key: "payload", 148 Value: "{\\n\\t\\\"id\\\": \\\"test\\\"\\n}", 149 Label: "Payload Template", 150 DisplayValue: "", 151 }, 152 } 153 channel.DestinationId = destinationId 154 channel.Name = fmt.Sprintf("test-notifications-channel-%s", testIntegrationChannelNameRandStr) 155 156 // Test: Create Channel 157 createResult, err := n.AiNotificationsCreateChannel(accountID, channel) 158 require.NoError(t, err) 159 require.NotNil(t, createResult) 160 161 // Test: Get Channel 162 filters := ai.AiNotificationsChannelFilter{ 163 ID: createResult.Channel.ID, 164 } 165 sorter := AiNotificationsChannelSorter{} 166 167 getChannelResult, err := n.GetChannels(accountID, "", filters, sorter) 168 require.NoError(t, err) 169 require.NotNil(t, getChannelResult) 170 assert.Equal(t, 1, getChannelResult.TotalCount) 171 172 // Test: Update Channel 173 updateChannel := AiNotificationsChannelUpdate{} 174 updateChannel.Active = false 175 updateChannel.Properties = []AiNotificationsPropertyInput{ 176 { 177 Key: "headers", 178 Value: "{}", 179 Label: "Custom headers", 180 DisplayValue: "", 181 }, 182 { 183 Key: "payload", 184 Value: "{\\n\\t\\\"id\\\": \\\"test-update\\\"\\n}", 185 Label: "Payload Template", 186 DisplayValue: "", 187 }, 188 } 189 updateChannel.Name = fmt.Sprintf("test-notifications-update-channel-%s", testIntegrationChannelNameRandStr) 190 191 updateChannelResult, err := n.AiNotificationsUpdateChannel(accountID, updateChannel, createResult.Channel.ID) 192 require.NoError(t, err) 193 require.NotNil(t, updateChannelResult) 194 195 // Test: Delete Channel 196 deleteResult, err := n.AiNotificationsDeleteChannel(accountID, createResult.Channel.ID) 197 require.NoError(t, err) 198 require.NotNil(t, deleteResult) 199 200 // Test: Delete Destination 201 deleteDestinationResult, err := n.AiNotificationsDeleteDestination(accountID, destinationId) 202 require.NoError(t, err) 203 require.NotNil(t, deleteDestinationResult) 204 }