github.1485827954.workers.dev/newrelic/newrelic-client-go@v1.1.0/pkg/notifications/destinations_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package notifications
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  
    13  	"github.com/newrelic/newrelic-client-go/pkg/ai"
    14  	"github.com/newrelic/newrelic-client-go/pkg/nrtime"
    15  )
    16  
    17  var (
    18  	timestampString = "2022-07-10T11:10:43.123715Z"
    19  	timestamp       = nrtime.DateTime(timestampString)
    20  	user            = "test-user"
    21  	accountId       = 1
    22  	id              = "7463c367-6d61-416b-9aac-47f4a285fe5a"
    23  
    24  	testCreateDestinationResponseJSON = `{
    25  	 "aiNotificationsCreateDestination": {
    26  		  "destination": {
    27  			"accountId": 1,
    28  			"active": true,
    29  			"auth": {
    30  			  "authType": "BASIC",
    31  			  "user": "test-user"
    32  			},
    33  			"createdAt": "2022-07-10T11:10:43.123715Z",
    34  			"id": "7463c367-6d61-416b-9aac-47f4a285fe5a",
    35  			"isUserAuthenticated": false,
    36  			"lastSent": "2022-07-10T11:10:43.123715Z",
    37  			"name": "test-notification-destination-1",
    38  			"properties": [
    39  			  {
    40  				"displayValue": null,
    41  				"key": "email",
    42  				"label": null,
    43  				"value": "test@newrelic.com"
    44  			  }
    45  			],
    46  			"status": "DEFAULT",
    47  			"type": "EMAIL",
    48  			"updatedAt": "2022-07-10T11:10:43.123715Z",
    49  			"updatedBy": 1547846
    50  		  },
    51  		  "error": null,
    52  		  "errors": []
    53  		}
    54  	}`
    55  
    56  	testDeleteDestinationResponseJSON = `{
    57  		"aiNotificationsDeleteDestination": {
    58  		  "error": null,
    59  		  "errors": [],
    60  		  "ids": [
    61  			"7463c367-6d61-416b-9aac-47f4a285fe5a"
    62  		  ]
    63  		}
    64  	}`
    65  
    66  	testGetDestinationResponseJSON = `{
    67      "actor": {
    68        "account": {
    69          "aiNotifications": {
    70            "destinations": {
    71              "entities": [
    72                {
    73                  "accountId": 1,
    74                  "active": true,
    75                  "auth": {
    76                    "authType": "BASIC",
    77                    "user": "test-user"
    78                  },
    79                  "createdAt": "2022-07-10T11:10:43.123715Z",
    80                  "id": "7463c367-6d61-416b-9aac-47f4a285fe5a",
    81                  "isUserAuthenticated": false,
    82                  "lastSent": "2022-07-10T11:10:43.123715Z",
    83                  "name": "test-notification-destination-1",
    84                  "properties": [
    85                    {
    86                      "displayValue": null,
    87                      "key": "email",
    88                      "label": null,
    89                      "value": "test@newrelic.com"
    90                    }
    91                  ],
    92                  "status": "DEFAULT",
    93                  "type": "EMAIL",
    94                  "updatedAt": "2022-07-10T11:10:43.123715Z",
    95                  "updatedBy": 1547846
    96                }
    97              ],
    98              "error": null,
    99              "errors": [],
   100              "nextCursor": null,
   101              "totalCount": 1
   102  			}
   103            }
   104          }
   105        }
   106  	}`
   107  )
   108  
   109  func TestCreateDestination(t *testing.T) {
   110  	t.Parallel()
   111  	respJSON := fmt.Sprintf(`{ "data":%s }`, testCreateDestinationResponseJSON)
   112  	notifications := newMockResponse(t, respJSON, http.StatusCreated)
   113  
   114  	destinationInput := AiNotificationsDestinationInput{
   115  		Type: AiNotificationsDestinationTypeTypes.EMAIL,
   116  		Name: "test-notification-destination-1",
   117  		Properties: []AiNotificationsPropertyInput{
   118  			{
   119  				Key:   "email",
   120  				Value: "test@newrelic.com",
   121  			},
   122  		},
   123  		Auth: &AiNotificationsCredentialsInput{
   124  			Basic: AiNotificationsBasicAuthInput{
   125  				User:     user,
   126  				Password: "Pass",
   127  			},
   128  			Type: AiNotificationsAuthTypeTypes.BASIC,
   129  		},
   130  	}
   131  
   132  	auth := ai.AiNotificationsAuth{
   133  		AuthType: "BASIC",
   134  		User:     user,
   135  	}
   136  	auth.ImplementsAiNotificationsAuth()
   137  
   138  	expected := &AiNotificationsDestinationResponse{
   139  		Destination: AiNotificationsDestination{
   140  			AccountID:           accountId,
   141  			Active:              true,
   142  			Auth:                auth,
   143  			CreatedAt:           timestamp,
   144  			ID:                  id,
   145  			IsUserAuthenticated: false,
   146  			LastSent:            timestamp,
   147  			Name:                "test-notification-destination-1",
   148  			Properties: []AiNotificationsProperty{
   149  				{
   150  					DisplayValue: "",
   151  					Key:          "email",
   152  					Label:        "",
   153  					Value:        "test@newrelic.com",
   154  				},
   155  			},
   156  			Status:    AiNotificationsDestinationStatusTypes.DEFAULT,
   157  			Type:      AiNotificationsDestinationTypeTypes.EMAIL,
   158  			UpdatedAt: timestamp,
   159  			UpdatedBy: 1547846,
   160  		},
   161  		Errors: []ai.AiNotificationsError{},
   162  	}
   163  
   164  	actual, err := notifications.AiNotificationsCreateDestination(accountId, destinationInput)
   165  
   166  	assert.NoError(t, err)
   167  	assert.NotNil(t, actual)
   168  	assert.Equal(t, expected, actual)
   169  }
   170  
   171  func TestGetDestinations(t *testing.T) {
   172  	t.Parallel()
   173  	respJSON := fmt.Sprintf(`{ "data":%s }`, testGetDestinationResponseJSON)
   174  	notifications := newMockResponse(t, respJSON, http.StatusOK)
   175  
   176  	auth := ai.AiNotificationsAuth{
   177  		AuthType: "BASIC",
   178  		User:     user,
   179  	}
   180  	auth.ImplementsAiNotificationsAuth()
   181  
   182  	expected := &AiNotificationsDestinationsResponse{
   183  		Entities: []AiNotificationsDestination{
   184  			{
   185  				AccountID:           accountId,
   186  				Active:              true,
   187  				Auth:                auth,
   188  				CreatedAt:           timestamp,
   189  				ID:                  id,
   190  				IsUserAuthenticated: false,
   191  				LastSent:            timestamp,
   192  				Name:                "test-notification-destination-1",
   193  				Properties: []AiNotificationsProperty{
   194  					{
   195  						DisplayValue: "",
   196  						Key:          "email",
   197  						Label:        "",
   198  						Value:        "test@newrelic.com",
   199  					},
   200  				},
   201  				Status:    AiNotificationsDestinationStatusTypes.DEFAULT,
   202  				Type:      AiNotificationsDestinationTypeTypes.EMAIL,
   203  				UpdatedAt: timestamp,
   204  				UpdatedBy: 1547846,
   205  			},
   206  		},
   207  		Errors:     []AiNotificationsResponseError{},
   208  		Error:      AiNotificationsResponseError{},
   209  		NextCursor: "",
   210  		TotalCount: 1,
   211  	}
   212  
   213  	filters := ai.AiNotificationsDestinationFilter{
   214  		ID: id,
   215  	}
   216  	sorter := AiNotificationsDestinationSorter{}
   217  
   218  	actual, err := notifications.GetDestinations(accountId, "", filters, sorter)
   219  
   220  	assert.NoError(t, err)
   221  	assert.NotNil(t, actual)
   222  	assert.Equal(t, expected, actual)
   223  }
   224  
   225  func TestDeleteDestination(t *testing.T) {
   226  	t.Parallel()
   227  	respJSON := fmt.Sprintf(`{ "data":%s }`, testDeleteDestinationResponseJSON)
   228  	notifications := newMockResponse(t, respJSON, http.StatusOK)
   229  
   230  	expected := &AiNotificationsDeleteResponse{
   231  		IDs:    []string{id},
   232  		Errors: []AiNotificationsResponseError{},
   233  	}
   234  
   235  	actual, err := notifications.AiNotificationsDeleteDestination(accountId, id)
   236  
   237  	assert.NoError(t, err)
   238  	assert.NotNil(t, actual)
   239  	assert.Equal(t, expected, actual)
   240  }