File size: 2,876 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
package smoke

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"regexp"
	"testing"

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

var (

	// example: 2025-12-13T16:06:38.918293-06:00
	isoRx     = regexp.MustCompile(`"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(Z|[-+]\d{2}:\d{2})"`)
	urlHostRx = regexp.MustCompile(`"http://[^/]+`)
)

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

	const sql = `
		insert into users (id, name, email)
		values
			({{uuid "user"}}, 'bob', 'joe');
		insert into schedules (id, name, time_zone, description) 
		values
			({{uuid "schedId"}},'sched', 'America/Chicago', 'test description here');
	`
	h := harness.NewHarness(t, sql, "calendar-subscriptions")
	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)
		}
	}

	var cs struct{ CreateUserCalendarSubscription struct{ URL string } }

	const mut = `
		mutation {
			createUserCalendarSubscription (input: {
				name: "%s",
				reminderMinutes: [%d]
				scheduleID: "%s",
			}) {
				url
			}
		}
	`

	// create subscription
	doQL(fmt.Sprintf(mut, "foobar", 5, h.UUID("schedId")), &cs)

	u, err := url.Parse(cs.CreateUserCalendarSubscription.URL)
	assert.NoError(t, err)
	assert.Contains(t, u.Path, "/api/v2/calendar")

	resp, err := http.Get(cs.CreateUserCalendarSubscription.URL)
	require.NoError(t, err)
	require.Equal(t, 200, resp.StatusCode, "serve iCalendar")

	data, err := io.ReadAll(resp.Body)
	require.NoError(t, err)

	assert.Equal(t, "BEGIN:VCALENDAR\r\nPRODID:-//GoAlert//dev//EN\r\nVERSION:2.0\r\nCALSCALE:GREGORIAN\r\nMETHOD:PUBLISH\r\nEND:VCALENDAR\r\n", string(data))

	req, err := http.NewRequest("GET", cs.CreateUserCalendarSubscription.URL, nil)
	require.NoError(t, err)
	req.Header.Set("Accept", "application/json")
	resp, err = http.DefaultClient.Do(req)
	require.NoError(t, err)
	require.Equal(t, 200, resp.StatusCode, "serve JSON")
	data, err = io.ReadAll(resp.Body)
	require.NoError(t, err)

	data = isoRx.ReplaceAll(data, []byte(`"2021-01-01T00:00:00Z"`))
	data = urlHostRx.ReplaceAll(data, []byte(`"http://TEST_HOST`))

	expected := fmt.Sprintf(`
{
"AppName": "GoAlert",
"AppVersion": "dev",
"Start": "2021-01-01T00:00:00Z",
"End": "2021-01-01T00:00:00Z",
"ScheduleID": "%s",
"ScheduleName": "sched",
"ScheduleURL": "http://TEST_HOST/schedules/%s",
"Shifts":[],
"Type": "calendar-subscription/v1"
}
`, h.UUID("schedId"), h.UUID("schedId"))

	assert.JSONEq(t, expected, string(data))
}