MCPcopy
hub / github.com/ory/kratos / TestHandler

Function TestHandler

courier/handler_test.go:34–285  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

32var defaultPageToken = courier.Message{}.DefaultPageToken().Encrypt(nil)
33
34func TestHandler(t *testing.T) {
35 ctx := context.Background()
36 conf, reg := pkg.NewFastRegistryWithMocks(t)
37 // Start kratos server
38 publicTS, adminTS := testhelpers.NewKratosServerWithCSRF(t, reg)
39
40 tss := []struct {
41 name string
42 s *httptest.Server
43 }{
44 {
45 name: "public",
46 s: publicTS,
47 },
48 {
49 name: "admin",
50 s: adminTS,
51 },
52 }
53
54 mockServerURL := urlx.ParseOrPanic(publicTS.URL)
55 conf.MustSet(ctx, config.ViperKeyAdminBaseURL, adminTS.URL)
56 conf.MustSet(ctx, config.ViperKeyPublicBaseURL, mockServerURL.String())
57
58 get := func(t *testing.T, base *httptest.Server, href string, expectCode int) gjson.Result {
59 t.Helper()
60 res, err := base.Client().Get(base.URL + href)
61 require.NoError(t, err)
62 body, err := io.ReadAll(res.Body)
63 require.NoError(t, err)
64 require.NoError(t, res.Body.Close())
65
66 assert.EqualValuesf(t, expectCode, res.StatusCode, "%s", body)
67 return gjson.ParseBytes(body)
68 }
69
70 getList := func(t *testing.T, tsName string, qs string) gjson.Result {
71 t.Helper()
72 href := courier.AdminRouteListMessages + qs
73 ts := adminTS
74
75 if tsName == "public" {
76 href = httprouterx.AdminPrefix + href
77 ts = publicTS
78 }
79
80 parsed := get(t, ts, href, http.StatusOK)
81 require.Truef(t, parsed.IsArray(), "%s", parsed.Raw)
82 return parsed
83 }
84
85 t.Run("case=should return an empty list of messages", func(t *testing.T) {
86 for _, name := range []string{"public", "admin"} {
87 t.Run("endpoint="+name, func(t *testing.T) {
88 parsed := getList(t, name, "")
89 assert.Len(t, parsed.Array(), 0)
90 })
91 }

Callers

nothing calls this directly

Calls 13

NewFastRegistryWithMocksFunction · 0.92
NewKratosServerWithCSRFFunction · 0.92
MustSetMethod · 0.80
StringMethod · 0.65
ClientMethod · 0.65
CloseMethod · 0.65
RunMethod · 0.65
LenMethod · 0.65
AddMessageMethod · 0.65
CourierPersisterMethod · 0.65
SetMessageStatusMethod · 0.65
RecordDispatchMethod · 0.65

Tested by

no test coverage detected