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

ui/profile: display proper errors in send test dialog #2734

Merged
merged 7 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 7 additions & 1 deletion notification/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,16 @@ func (s *Store) Code(ctx context.Context, id string) (int, error) {
}

func (s *Store) SendContactMethodTest(ctx context.Context, id string) error {
_, err := s.cmUserID(ctx, id)
cmUserID, err := s.cmUserID(ctx, id)
if err != nil {
return err
}

// if the contact method user id does not match the current user id, return an error
if cmUserID != permission.UserID(ctx) {
return validation.NewFieldError("ContactMethod", "contact method does not belong to user")
}

tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions test/smoke/twilioenablebysms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func TestTwilioEnableBySMS(t *testing.T) {
t.Parallel()

sqlQuery := `
insert into users (id, name, email)
insert into users (id, name, email, role)
values
({{uuid "user"}}, 'bob', 'joe');
({{uuid "user"}}, 'bob', 'joe', 'admin');
insert into user_contact_methods (id, user_id, name, type, value, disabled)
values
({{uuid "cm1"}}, {{uuid "user"}}, 'personal', 'SMS', {{phone "1"}}, true),
Expand All @@ -30,7 +30,7 @@ func TestTwilioEnableBySMS(t *testing.T) {
defer h.Close()

doQL := func(query string, expectErr bool) {
g := h.GraphQLQuery2(query)
g := h.GraphQLQueryUserT(t, h.UUID("user"), query)
if expectErr {
if len(g.Errors) == 0 {
t.Fatal("expected error")
Expand Down
6 changes: 3 additions & 3 deletions test/smoke/twilioenablebyvoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func TestTwilioEnablebyVoice(t *testing.T) {
t.Parallel()

sqlQuery := `
insert into users (id, name, email)
insert into users (id, name, email, role)
values
({{uuid "user"}}, 'bob', 'joe');
({{uuid "user"}}, 'bob', 'joe', 'admin');
insert into user_contact_methods (id, user_id, name, type, value, disabled)
values
({{uuid "cm1"}}, {{uuid "user"}}, 'personal', 'SMS', {{phone "1"}}, true),
Expand All @@ -30,7 +30,7 @@ func TestTwilioEnablebyVoice(t *testing.T) {
defer h.Close()

doQL := func(query string, expectErr bool) {
g := h.GraphQLQuery2(query)
g := h.GraphQLQueryUserT(t, h.UUID("user"), query)
if expectErr {
if len(g.Errors) == 0 {
t.Fatal("expected error")
Expand Down
6 changes: 3 additions & 3 deletions test/smoke/twiliosmsverification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func TestTwilioSMSVerification(t *testing.T) {
t.Parallel()

sqlQuery := `
insert into users (id, name, email)
insert into users (id, name, email, role)
values
({{uuid "user"}}, 'bob', 'joe');
({{uuid "user"}}, 'bob', 'joe', 'admin');
insert into user_contact_methods (id, user_id, name, type, value, disabled)
values
({{uuid "cm1"}}, {{uuid "user"}}, 'personal', 'SMS', {{phone "1"}}, true);
Expand All @@ -30,7 +30,7 @@ func TestTwilioSMSVerification(t *testing.T) {
defer h.Close()

doQL := func(query string) {
g := h.GraphQLQuery2(query)
g := h.GraphQLQueryUserT(t, h.UUID("user"), query)
for _, err := range g.Errors {
t.Error("GraphQL Error:", err.Message)
}
Expand Down
6 changes: 3 additions & 3 deletions test/smoke/twiliotestsms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func TestTwilioSMS(t *testing.T) {
t.Parallel()

sqlQuery := `
insert into users (id, name, email)
insert into users (id, name, email, role)
values
({{uuid "user"}}, 'bob', 'joe');
({{uuid "user"}}, 'bob', 'joe', 'admin');
insert into user_contact_methods (id, user_id, name, type, value)
values
({{uuid "cm1"}}, {{uuid "user"}}, 'personal', 'SMS', {{phone "1"}});
Expand All @@ -23,7 +23,7 @@ func TestTwilioSMS(t *testing.T) {
defer h.Close()

doQL := func(query string) {
g := h.GraphQLQuery2(query)
g := h.GraphQLQueryUserT(t, h.UUID("user"), query)
for _, err := range g.Errors {
t.Error("GraphQL Error:", err.Message)
}
Expand Down
38 changes: 38 additions & 0 deletions test/smoke/twiliotestsmsnotcmowner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package smoke

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
"github.com/target/goalert/test/smoke/harness"
)

// TestTwilioSMS checks that a test SMS is processed.
func TestTwilioSMSNotCMOwner(t *testing.T) {
t.Parallel()

sqlQuery := `
insert into users (id, name, email)
values
({{uuid "user"}}, 'bob', 'joe');
insert into user_contact_methods (id, user_id, name, type, value)
values
({{uuid "cm1"}}, {{uuid "user"}}, 'personal', 'SMS', {{phone "1"}});
`
h := harness.NewHarness(t, sqlQuery, "add-verification-code")
defer h.Close()

doQL := func(query string) {
g := h.GraphQLQuery2(query)
require.Len(t, g.Errors, 1, "errors returned from GraphQL")
require.Equal(t, "contact method does not belong to user", g.Errors[0].Message)
}
cm1 := h.UUID("cm1")

doQL(fmt.Sprintf(`
mutation {
testContactMethod(id: "%s")
}
`, cm1))
}
6 changes: 3 additions & 3 deletions test/smoke/twiliotestvoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func TestTwilioVoice(t *testing.T) {
t.Parallel()

sqlQuery := `
insert into users (id, name, email)
insert into users (id, name, email, role)
values
({{uuid "user"}}, 'bob', 'joe');
({{uuid "user"}}, 'bob', 'joe', 'admin');
insert into user_contact_methods (id, user_id, name, type, value)
values
({{uuid "cm1"}}, {{uuid "user"}}, 'personal', 'VOICE', {{phone "1"}});
Expand All @@ -23,7 +23,7 @@ func TestTwilioVoice(t *testing.T) {
defer h.Close()

doQL := func(query string) {
g := h.GraphQLQuery2(query)
g := h.GraphQLQueryUserT(t, h.UUID("user"), query)
for _, err := range g.Errors {
t.Error("GraphQL Error:", err.Message)
}
Expand Down
38 changes: 38 additions & 0 deletions test/smoke/twiliotestvoicenotcmowner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package smoke

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"
"github.com/target/goalert/test/smoke/harness"
)

// TestTwilioSMS checks that a test SMS is processed.
func TestTwilioVoiceNotCMOwner(t *testing.T) {
t.Parallel()

sqlQuery := `
insert into users (id, name, email)
values
({{uuid "user"}}, 'bob', 'joe');
insert into user_contact_methods (id, user_id, name, type, value)
values
({{uuid "cm1"}}, {{uuid "user"}}, 'personal', 'VOICE', {{phone "1"}});
`
h := harness.NewHarness(t, sqlQuery, "add-verification-code")
defer h.Close()

doQL := func(query string) {
g := h.GraphQLQuery2(query)
require.Len(t, g.Errors, 1, "errors returned from GraphQL")
require.Equal(t, "contact method does not belong to user", g.Errors[0].Message)
}
cm1 := h.UUID("cm1")

doQL(fmt.Sprintf(`
mutation {
testContactMethod(id: "%s")
}
`, cm1))
}
6 changes: 3 additions & 3 deletions test/smoke/twiliovoiceverification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func TestTwilioVoiceVerification(t *testing.T) {
t.Parallel()

sqlQuery := `
insert into users (id, name, email)
insert into users (id, name, email, role)
values
({{uuid "user"}}, 'bob', 'joe');
({{uuid "user"}}, 'bob', 'joe', 'admin');
insert into user_contact_methods (id, user_id, name, type, value, disabled)
values
({{uuid "cm2"}}, {{uuid "user"}}, 'personal', 'VOICE', {{phone "1"}}, true);
Expand All @@ -44,7 +44,7 @@ func TestTwilioVoiceVerification(t *testing.T) {
defer h.Close()

doQL := func(query string) {
g := h.GraphQLQuery2(query)
g := h.GraphQLQueryUserT(t, h.UUID("user"), query)
for _, err := range g.Errors {
t.Error("GraphQL Error:", err.Message)
}
Expand Down
44 changes: 19 additions & 25 deletions web/src/app/users/SendTestDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState, MouseEvent } from 'react'
import { useQuery, useMutation, gql } from '@apollo/client'
import React, { useEffect, MouseEvent, useRef } from 'react'
import { gql, useQuery, useMutation } from 'urql'

import Spinner from '../loading/components/Spinner'

Expand Down Expand Up @@ -43,44 +43,42 @@ export default function SendTestDialog(
): JSX.Element {
const { title = 'Test Delivery Status', onClose, messageID } = props

const [now] = useState(DateTime.local())
const [sendTestStatus, sendTest] = useMutation(mutation)

const [sendTest, sendTestStatus] = useMutation(mutation, {
const [{ data, fetching, error }] = useQuery({
query,
variables: {
id: messageID,
},
requestPolicy: 'network-only',
})

const { data, loading, error } = useQuery(query, {
variables: {
id: messageID,
},
fetchPolicy: 'network-only',
})

const now = useRef(DateTime.utc())
const status = data?.userContactMethod?.lastTestMessageState?.status ?? ''
const cmDestValue = data?.userContactMethod?.formattedValue ?? ''
const cmType: ContactMethodType = data?.userContactMethod?.type ?? ''
const lastTestVerifyAt = data?.userContactMethod?.lastTestVerifyAt ?? ''
const timeSinceLastVerified = now.diff(DateTime.fromISO(lastTestVerifyAt))
const timeSinceLastVerified = now.current.diff(
DateTime.fromISO(lastTestVerifyAt),
)
const fromValue =
data?.userContactMethod?.lastTestMessageState?.formattedSrcValue ?? ''
const errorMessage = error?.message ?? ''
const errorMessage = (error?.message || sendTestStatus.error?.message) ?? ''

useEffect(() => {
if (loading || error || sendTestStatus.called) {
if (fetching || errorMessage || sendTestStatus.data) {
return
}
if (
data?.userContactMethod?.lastTestMessageState == null ||
data?.lastTestMessageState == null ||
!(timeSinceLastVerified.as('seconds') < 60)
) {
sendTest()
sendTest({ id: messageID })
}
}, [lastTestVerifyAt, loading])
}, [lastTestVerifyAt, fetching])

let details
if (sendTestStatus.called && lastTestVerifyAt > now.toISO()) {
if (sendTestStatus.data && lastTestVerifyAt > now.current.toISO()) {
details = data?.userContactMethod?.lastTestMessageState?.details ?? ''
}

Expand All @@ -95,8 +93,6 @@ export default function SendTestDialog(
}
}

if (loading || sendTestStatus.loading) return <Spinner text='Loading...' />

const msg = (): string => {
switch (cmType) {
case 'SMS':
Expand All @@ -119,6 +115,9 @@ export default function SendTestDialog(
<DialogContentText>
GoAlert is sending a test {msg()}.
</DialogContentText>
{(sendTestStatus.fetching || (!details && !errorMessage)) && (
<Spinner text='Sending Test...' />
)}
{fromValue && (
<DialogContentText>
The test message was sent from {fromValue}.
Expand All @@ -129,11 +128,6 @@ export default function SendTestDialog(
{toTitleCase(details)}
</DialogContentText>
)}
{!details && (
<DialogContentText color='error'>
Couldn't send a message yet, please try again after about a minute.
</DialogContentText>
)}
</DialogContent>

{errorMessage && <DialogContentError error={errorMessage} />}
Expand Down