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

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

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

type calSub struct {
	Name            string
	Disabled        bool
	ScheduleID      string
	ReminderMinutes []int
}
type calSubWithID struct {
	ID string
	calSub
}

// TestGraphQLCalendarSubscriptions tests operations on calendar subscriptions API
func TestGraphQLCalendarSubscriptions(t *testing.T) {
	t.Parallel()

	sql := `
		insert into users (id, name, email, role) 
		values ({{uuid "user"}}, 'bob', 'joe', 'admin');

		insert into schedules (id, name, time_zone) 
		values ({{uuid "sched1"}}, 'default', 'America/Chicago');

		insert into user_calendar_subscriptions (id, name, user_id, config, schedule_id)
		values ({{uuid "cs1"}}, 'test1', {{uuid "user"}}, '{ "ReminderMinutes": [2, 4, 8, 16]}', {{uuid "sched1"}});

	`

	h := harness.NewHarness(t, sql, "calendar-subscriptions")
	defer h.Close()

	doQL := func(t *testing.T, 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)
		}
	}

	// create
	var csCreate struct {
		CreateUserCalendarSubscription calSubWithID
	}
	doQL(t, fmt.Sprintf(`
		mutation {
		  createUserCalendarSubscription(input: {
			name: "%s"
			scheduleID: "%s"
			reminderMinutes: [%d]
			disabled: %v
		  }) {
			id
			name
			scheduleID
			reminderMinutes
		  }
		}
	`, "Name 1", h.UUID("sched1"), 32, false), &csCreate)
	assert.Equal(t, calSub{
		Name:            "Name 1",
		ScheduleID:      h.UUID("sched1"),
		ReminderMinutes: []int{32},
	}, csCreate.CreateUserCalendarSubscription.calSub)
	assert.NotEmpty(t, csCreate.CreateUserCalendarSubscription.ID)

	// update
	doQL(t, fmt.Sprintf(`
		mutation {
		  updateUserCalendarSubscription(input: {
			id: "%s"
			name: "%s"
			disabled: %v
		  })
		}
	`, csCreate.CreateUserCalendarSubscription.ID, "updated", true), nil)

	// find one
	var csFindOne struct {
		UserCalendarSubscription calSubWithID
	}
	doQL(t, fmt.Sprintf(`
		query {
			userCalendarSubscription(id: "%s") {
				id
				name
				disabled
			}
		}
	`, csCreate.CreateUserCalendarSubscription.ID), &csFindOne)
	assert.Equal(t, calSub{
		Name:     "updated",
		Disabled: true,
	}, csFindOne.UserCalendarSubscription.calSub)
	assert.Equal(t, csCreate.CreateUserCalendarSubscription.ID, csFindOne.UserCalendarSubscription.ID)

	var csFindMany struct {
		User struct {
			CalendarSubscriptions []calSub
		}
	}

	// find many
	doQL(t, `
		query{
			user {
				calendarSubscriptions {
				  id
				  name
				  reminderMinutes
				  scheduleID
				  disabled
				}
		    }
		}
	`, &csFindMany)
	assert.Equal(t, []calSub{{
		Name:            "updated",
		Disabled:        true,
		ReminderMinutes: []int{32},
		ScheduleID:      h.UUID("sched1"),
	}}, csFindMany.User.CalendarSubscriptions)

	// delete
	var delete struct {
		DeleteAll bool
	}
	doQL(t, fmt.Sprintf(`
		mutation {
			deleteAll(input: [{
				id: "%s"
				type: calendarSubscription
			}])
		}
	`, csCreate.CreateUserCalendarSubscription.ID), &delete)
	assert.True(t, delete.DeleteAll)

	// ensure delete happened
	doQL(t, `
		query{
			user {
				calendarSubscriptions {
				  id
				  name
				  reminderMinutes
				  scheduleID
				  disabled
				}
		    }
		}
	`, &csFindMany)
	assert.Equal(t, []calSub{}, csFindMany.User.CalendarSubscriptions)
}