Skip to content

Commit

Permalink
add smoketest for calsub return data
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercactapus committed Dec 13, 2024
1 parent ec0ba4b commit c8a5fc8
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
3 changes: 3 additions & 0 deletions calsub/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func (s *Store) ServeICalData(w http.ResponseWriter, req *http.Request) {
UserURL: cfg.CallbackURL("/users/" + s.UserID),
})
}
if len(data.Shifts) == 0 {
data.Shifts = []JSONShiftV1{}
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(data)
if errutil.HTTPError(ctx, w, err) {
Expand Down
113 changes: 113 additions & 0 deletions test/smoke/calendarsubscriptionjson_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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))
}

0 comments on commit c8a5fc8

Please sign in to comment.