File size: 3,725 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
package smoke

import (
	"encoding/json"
	"fmt"
	"testing"

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

// TestGraphQLUpdateRotation tests that all steps like creating and updating rotations are carried out without any errors.
func TestGraphQLUpdateRotation(t *testing.T) {
	t.Parallel()

	const sql = `
	insert into users (id, name, email)
	values
		({{uuid "u1"}}, 'bob', 'joe'),
		({{uuid "u2"}}, 'ben', 'josh');
`

	h := harness.NewHarness(t, sql, "ids-to-uuids")
	defer h.Close()

	u1UUID := h.UUID("u1")
	u2UUID := h.UUID("u2")

	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)
		}
	}

	var sched struct {
		CreateSchedule struct {
			ID      string
			Name    string
			Targets []struct {
				Target struct {
					ID string
				}
			}
		}
	}
	doQL(fmt.Sprintf(`
		mutation {
			createSchedule(
				input: {
					name: "default"
					description: "default testing"
					timeZone: "America/Chicago"
					targets: {
						newRotation: {
							name: "old name"
							description: "old description"
							timeZone: "America/Chicago"
							start: "2020-02-04T12:08:25-06:00"
							type: daily
							shiftLength: 6
							userIDs: ["%s"]
						}
						rules: {
							start: "00:00"
							end: "23:00"
							weekdayFilter: [true, true, true, true, true, true, true]
						}
					}
				}
			) {
				id
				name
				targets {
					target {
						id
					}
				}
			}
		}
	`, u1UUID), &sched)

	rotationID := sched.CreateSchedule.Targets[0].Target.ID

	doQL(fmt.Sprintf(`
		mutation {
			updateRotation(input:{
				id: "%s",
				name: "new name",
				description: "new description"
				timeZone: "America/New_York"
				start: "1997-11-26T12:08:25-05:00"
				type: hourly
				shiftLength: 12
				activeUserIndex: 0
				userIDs: ["%s", "%s"]
			})
		}
	
	`, rotationID, u1UUID, u2UUID), nil)

	var newSched struct {
		Schedule struct {
			ID      string
			Name    string
			Targets []struct {
				ScheduleID string
				Target     struct{ ID string }
			}
		}
	}
	doQL(fmt.Sprintf(`
		query {
			schedule(id: "%s") {
				targets {
					target {
						id
					}
				}
			}
		}
	
	`, sched.CreateSchedule.ID), &newSched)

	if len(newSched.Schedule.Targets) != 1 {
		t.Errorf("got %d rotations; want 1", len(newSched.Schedule.Targets))
	}

	var updatedRotation struct {
		Rotation struct {
			Name            string
			Description     string
			TimeZone        string
			Start           string
			Type            string
			ShiftLength     int
			ActiveUserIndex int
			Users           []struct {
				ID string
			}
		}
	}
	doQL(fmt.Sprintf(`
		query{
		rotation(id: "%s"){
			name
			description
			timeZone
			start
			type
			shiftLength
			activeUserIndex
			users {
				id
			}
		}
	}`, rotationID), &updatedRotation)

	assert.Equal(t, "new name", updatedRotation.Rotation.Name)
	assert.Equal(t, "new description", updatedRotation.Rotation.Description)
	assert.Equal(t, "America/New_York", updatedRotation.Rotation.TimeZone)
	assert.Equal(t, "1997-11-26T17:08:00Z", updatedRotation.Rotation.Start) // truncate to minute
	assert.Equal(t, "hourly", updatedRotation.Rotation.Type)
	assert.Equal(t, 12, updatedRotation.Rotation.ShiftLength)
	assert.Equal(t, 0, updatedRotation.Rotation.ActiveUserIndex)
	assert.Equal(t, u1UUID, updatedRotation.Rotation.Users[0].ID)
	assert.Equal(t, u2UUID, updatedRotation.Rotation.Users[1].ID)
}