Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(notifications): add name to filter #1014

Merged
merged 5 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/ai/types_ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func UnmarshalAiNotificationsErrorInterface(b []byte) (*AiNotificationsErrorInte
type AiNotificationsDestinationFilter struct {
// id
ID string `json:"id,omitempty"`
// Name
Name string `json:"name,omitempty"`
}

// SecureValue - The `SecureValue` scalar represents a secure value, ie a password, an API key, etc.
Expand Down
55 changes: 55 additions & 0 deletions pkg/notifications/destinations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
user = "test-user"
accountId = 1
id = "7463c367-6d61-416b-9aac-47f4a285fe5a"
name = "test-notification-destination-1"

testCreateDestinationResponseJSON = `{
"aiNotificationsCreateDestination": {
Expand Down Expand Up @@ -222,6 +223,60 @@ func TestGetDestinations(t *testing.T) {
assert.Equal(t, expected, actual)
}

func TestGetDestinationsByName(t *testing.T) {
t.Parallel()
respJSON := fmt.Sprintf(`{ "data":%s }`, testGetDestinationResponseJSON)
notifications := newMockResponse(t, respJSON, http.StatusOK)

auth := ai.AiNotificationsAuth{
AuthType: "BASIC",
User: user,
}
auth.ImplementsAiNotificationsAuth()

expected := &AiNotificationsDestinationsResponse{
Entities: []AiNotificationsDestination{
{
AccountID: accountId,
Active: true,
Auth: auth,
CreatedAt: timestamp,
ID: id,
IsUserAuthenticated: false,
LastSent: timestamp,
Name: "test-notification-destination-1",
Properties: []AiNotificationsProperty{
{
DisplayValue: "",
Key: "email",
Label: "",
Value: "[email protected]",
},
},
Status: AiNotificationsDestinationStatusTypes.DEFAULT,
Type: AiNotificationsDestinationTypeTypes.EMAIL,
UpdatedAt: timestamp,
UpdatedBy: 1547846,
},
},
Errors: []AiNotificationsResponseError{},
Error: AiNotificationsResponseError{},
NextCursor: "",
TotalCount: 1,
}

filters := ai.AiNotificationsDestinationFilter{
Name: name,
}
sorter := AiNotificationsDestinationSorter{}

actual, err := notifications.GetDestinations(accountId, "", filters, sorter)

assert.NoError(t, err)
assert.NotNil(t, actual)
assert.Equal(t, expected, actual)
}

func TestDeleteDestination(t *testing.T) {
t.Parallel()
respJSON := fmt.Sprintf(`{ "data":%s }`, testDeleteDestinationResponseJSON)
Expand Down
82 changes: 80 additions & 2 deletions pkg/notifications/notifications_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
mock "github.com/newrelic/newrelic-client-go/v2/pkg/testhelpers"
)

func TestNotificationMutationDestination(t *testing.T) {
func TestNotificationMutationDestinationById(t *testing.T) {
t.Parallel()

n := newIntegrationTestClient(t)
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestNotificationMutationDestination(t *testing.T) {
require.NotEmpty(t, createResult.Destination.Auth)
require.Equal(t, ai.AiNotificationsAuthType("TOKEN"), createResult.Destination.Auth.AuthType)

// Test: Get Destination
// Test: Get Destination by id
filters := ai.AiNotificationsDestinationFilter{
ID: createResult.Destination.ID,
}
Expand Down Expand Up @@ -93,6 +93,84 @@ func TestNotificationMutationDestination(t *testing.T) {
require.NotNil(t, deleteResult)
}

func TestNotificationMutationDestinationByName(t *testing.T) {
lzaga-newrelic marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

n := newIntegrationTestClient(t)

accountID, err := mock.GetTestAccountID()
if err != nil {
t.Skipf("%s", err)
}

// Create a destination to work with in this test
testIntegrationDestinationNameRandStr := mock.RandSeq(5)
destination := AiNotificationsDestinationInput{}
destination.Type = AiNotificationsDestinationTypeTypes.WEBHOOK
destination.Properties = []AiNotificationsPropertyInput{
{
Key: "url",
Value: "https://webhook.site/94193c01-4a81-4782-8f1b-554d5230395b",
Label: "",
DisplayValue: "",
},
}
destination.Auth = &AiNotificationsCredentialsInput{
Type: AiNotificationsAuthTypeTypes.TOKEN,
Token: AiNotificationsTokenAuthInput{
Token: "Token",
Prefix: "Bearer",
},
}
destination.Name = fmt.Sprintf("test-notifications-destination-%s", testIntegrationDestinationNameRandStr)

// Test: Create
createResult, err := n.AiNotificationsCreateDestination(accountID, destination)
require.NoError(t, err)
require.NotNil(t, createResult)
require.NotEmpty(t, createResult.Destination.Auth)
require.Equal(t, ai.AiNotificationsAuthType("TOKEN"), createResult.Destination.Auth.AuthType)
lzaga-newrelic marked this conversation as resolved.
Show resolved Hide resolved

// Test: Get Destination by name
filtersByName := ai.AiNotificationsDestinationFilter{
lzaga-newrelic marked this conversation as resolved.
Show resolved Hide resolved
Name: createResult.Destination.Name,
}
sorter := AiNotificationsDestinationSorter{}
getDestinationByNameResult, err := n.GetDestinations(accountID, "", filtersByName, sorter)
require.NoError(t, err)
require.NotNil(t, getDestinationByNameResult)
assert.Equal(t, 1, getDestinationByNameResult.TotalCount)

// Test: Update Destination
updateDestination := AiNotificationsDestinationUpdate{}
updateDestination.Active = false
updateDestination.Properties = []AiNotificationsPropertyInput{
{
Key: "url",
Value: "https://webhook.site/94193c01-4a81-4782-8f1b-554d5230395b",
Label: "",
DisplayValue: "",
},
}
updateDestination.Auth = &AiNotificationsCredentialsInput{
Type: AiNotificationsAuthTypeTypes.TOKEN,
Token: AiNotificationsTokenAuthInput{
Token: "TokenUpdate",
Prefix: "BearerUpdate",
},
}
updateDestination.Name = fmt.Sprintf("test-notifications-update-destination-%s", testIntegrationDestinationNameRandStr)

updateDestinationResult, err := n.AiNotificationsUpdateDestination(accountID, updateDestination, createResult.Destination.ID)
require.NoError(t, err)
require.NotNil(t, updateDestinationResult)
lzaga-newrelic marked this conversation as resolved.
Show resolved Hide resolved

// Test: Delete
deleteResult, err := n.AiNotificationsDeleteDestination(accountID, createResult.Destination.ID)
require.NoError(t, err)
require.NotNil(t, deleteResult)
pranav-new-relic marked this conversation as resolved.
Show resolved Hide resolved
}

func TestNotificationMutationChannel(t *testing.T) {
t.Parallel()

Expand Down