File size: 5,232 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
package smoke
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"testing"
"time"
"github.com/target/goalert/test/smoke/harness"
)
// TestGraphQLAlert tests that all steps up to, and including, generating
// an alert via GraphQL result in notifications going out.
//
// Specifically, mutations tested include:
// - createContactMethod
// - createNotificationRule
// - createSchedule
// - updateSchedule
// - addRotationParticipant
// - createEscalationPolicy
// - createEscalationPolicyStep
// - createService
// - createAlert
func TestGraphQLAlert(t *testing.T) {
t.Parallel()
const sql = `
insert into users (id, name, email)
values
({{uuid "u1"}}, 'bob', 'joe'),
({{uuid "u2"}}, 'ben', 'josh');
`
loc, err := time.LoadLocation("America/Chicago")
if err != nil {
t.Fatal("failed to load America/Chicago tzdata:", err)
}
h := harness.NewHarness(t, sql, "ids-to-uuids")
defer h.Close()
doQL := func(query string, res interface{}) {
g := h.GraphQLQuery2(query)
for _, err := range g.Errors {
t.Error("GraphQL Error:", err.Message)
}
if len(g.Errors) > 0 {
t.Fatal("errors returned from GraphQL")
}
t.Log("Response:", string(g.Data))
if res == nil {
return
}
err := json.Unmarshal(g.Data, &res)
if err != nil {
t.Fatal("failed to parse response:", err)
}
}
uid1, uid2 := h.UUID("u1"), h.UUID("u2")
phone1, phone2 := h.Phone("u1"), h.Phone("u2")
var cm1, cm2 struct {
CreateUserContactMethod struct {
ID string
}
}
createCM := func(userID, phone string, cm interface{}) {
t.Helper()
doQL(fmt.Sprintf(`
mutation {
createUserContactMethod(input:{
userID: "%s",
name: "default",
type: SMS,
value: "%s"
}) {
id
}
}
`, userID, phone), cm)
}
createCM(uid1, phone1, &cm1)
createCM(uid2, phone2, &cm2)
sendCMVerification := func(cmID string) {
doQL(fmt.Sprintf(`
mutation {
sendContactMethodVerification(input:{
contactMethodID: "%s"
})
}
`, cmID), nil)
}
sendCMVerification(cm1.CreateUserContactMethod.ID)
sendCMVerification(cm2.CreateUserContactMethod.ID)
msg1 := h.Twilio(t).Device(phone1).ExpectSMS("verification")
msg2 := h.Twilio(t).Device(phone2).ExpectSMS("verification")
digits := func(r rune) rune {
if r >= '0' && r <= '9' {
return r
}
return -1
}
codeStr1 := strings.Map(digits, msg1.Body())
codeStr2 := strings.Map(digits, msg2.Body())
code1, _ := strconv.Atoi(codeStr1)
code2, _ := strconv.Atoi(codeStr2)
verifyCM := func(cmID string, code int) {
doQL(fmt.Sprintf(`
mutation {
verifyContactMethod(input:{
contactMethodID: "%s",
code: %d
})
}
`, cmID, code), nil)
}
verifyCM(cm1.CreateUserContactMethod.ID, code1)
verifyCM(cm2.CreateUserContactMethod.ID, code2)
createUserNR := func(userID string, cmID string) {
doQL(fmt.Sprintf(`
mutation {
createUserNotificationRule(input:{
userID: "%s",
contactMethodID: "%s",
delayMinutes: 0
}){
id
}
}
`, userID, cmID), nil)
}
createUserNR(uid1, cm1.CreateUserContactMethod.ID)
createUserNR(uid2, cm2.CreateUserContactMethod.ID)
var sched struct {
CreateSchedule struct {
ID string
Name string
Targets []struct {
ScheduleID string
Target struct{ ID string }
}
}
}
doQL(fmt.Sprintf(`
mutation {
createSchedule(
input: {
name: "default"
description: "default testing"
timeZone: "America/Chicago"
targets: {
newRotation: {
name: "foobar"
timeZone: "America/Chicago"
start: "%s"
type: daily
}
rules: {
start: "00:00"
end: "23:00"
weekdayFilter: [true, true, true, true, true, true, true]
}
}
}
) {
id
name
targets {
scheduleID
target {
id
}
}
}
}
`, time.Now().Add(-time.Hour).In(loc).Format(time.RFC3339)), &sched)
if len(sched.CreateSchedule.Targets) != 1 {
t.Errorf("got %d schedule targets; want 1", len(sched.CreateSchedule.Targets))
}
rotID := sched.CreateSchedule.Targets[0].Target.ID
doQL(fmt.Sprintf(`
mutation {
updateRotation(input:{
id: "%s",
userIDs: ["%s"]
})
}
`, rotID, uid1), nil)
var esc struct{ CreateEscalationPolicy struct{ ID string } }
doQL(`
mutation {
createEscalationPolicy(input:{
repeat: 0,
name: "default"
}){id}
}
`, &esc)
var step struct {
CreateEscalationPolicyStep struct{ Step struct{ ID string } }
}
doQL(fmt.Sprintf(`
mutation {
createEscalationPolicyStep(input:{
delayMinutes: 60,
escalationPolicyID: "%s",
targets: [{id: "%s", type: user}, {id: "%s", type: schedule}]
}){
id
}
}
`, esc.CreateEscalationPolicy.ID, uid2, sched.CreateSchedule.ID), &step)
var svc struct{ CreateService struct{ ID string } }
doQL(fmt.Sprintf(`
mutation {
createService(input:{
name: "default",
escalationPolicyID: "%s"
}){id}
}
`, esc.CreateEscalationPolicy.ID), &svc)
// finally.. we can create the alert
doQL(fmt.Sprintf(`
mutation {
createAlert(input:{
summary: "brok",
serviceID: "%s"
}){id}
}
`, svc.CreateService.ID), nil)
h.Twilio(t).Device(phone1).ExpectSMS()
h.Twilio(t).Device(phone2).ExpectSMS()
}
|