|
| 1 | +package flow |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gofrs/uuid" |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + |
| 13 | + "github.com/ory/kratos/identity" |
| 14 | + "github.com/ory/kratos/schema" |
| 15 | + "github.com/ory/kratos/text" |
| 16 | + "github.com/ory/kratos/ui/container" |
| 17 | + "github.com/ory/kratos/ui/node" |
| 18 | + "github.com/ory/kratos/x" |
| 19 | + "github.com/ory/x/httpx" |
| 20 | + "github.com/ory/x/logrusx" |
| 21 | + "github.com/ory/x/otelx" |
| 22 | +) |
| 23 | + |
| 24 | +type testCSRFTokenGenerator struct{} |
| 25 | + |
| 26 | +func (t *testCSRFTokenGenerator) GenerateCSRFToken(_ *http.Request) string { |
| 27 | + return "csrf_token_value" |
| 28 | +} |
| 29 | + |
| 30 | +// testFlow is a minimalistic flow implementation to satisfy interface and is used only in tests. |
| 31 | +type testFlow struct { |
| 32 | + // ID represents the flow's unique ID. |
| 33 | + // |
| 34 | + // required: true |
| 35 | + ID uuid.UUID `json:"id" faker:"-" db:"id" rw:"r"` |
| 36 | + |
| 37 | + // Type represents the flow's type which can be either "api" or "browser", depending on the flow interaction. |
| 38 | + // |
| 39 | + // required: true |
| 40 | + Type Type `json:"type" db:"type" faker:"flow_type"` |
| 41 | + |
| 42 | + // RequestURL is the initial URL that was requested from Ory Kratos. It can be used |
| 43 | + // to forward information contained in the URL's path or query for example. |
| 44 | + // |
| 45 | + // required: true |
| 46 | + RequestURL string `json:"request_url" db:"request_url"` |
| 47 | + |
| 48 | + // UI contains data which must be shown in the user interface. |
| 49 | + // |
| 50 | + // required: true |
| 51 | + UI *container.Container `json:"ui" db:"ui"` |
| 52 | +} |
| 53 | + |
| 54 | +func (t *testFlow) GetID() uuid.UUID { |
| 55 | + return t.ID |
| 56 | +} |
| 57 | + |
| 58 | +func (t *testFlow) GetType() Type { |
| 59 | + return t.Type |
| 60 | +} |
| 61 | + |
| 62 | +func (t *testFlow) GetRequestURL() string { |
| 63 | + return t.RequestURL |
| 64 | +} |
| 65 | + |
| 66 | +func (t *testFlow) AppendTo(url *url.URL) *url.URL { |
| 67 | + return AppendFlowTo(url, t.ID) |
| 68 | +} |
| 69 | + |
| 70 | +func (t *testFlow) GetUI() *container.Container { |
| 71 | + return t.UI |
| 72 | +} |
| 73 | + |
| 74 | +func newTestFlow(r *http.Request, flowType Type) Flow { |
| 75 | + id := x.NewUUID() |
| 76 | + requestURL := x.RequestURL(r).String() |
| 77 | + ui := &container.Container{ |
| 78 | + Method: "POST", |
| 79 | + Action: "/test", |
| 80 | + } |
| 81 | + |
| 82 | + ui.Nodes.Append(node.NewInputField("traits.username", nil, node.PasswordGroup, node.InputAttributeTypeText, node.WithRequiredInputAttribute)) |
| 83 | + ui.Nodes.Append(node.NewInputField("traits.password", nil, node.PasswordGroup, node.InputAttributeTypePassword, node.WithRequiredInputAttribute)) |
| 84 | + |
| 85 | + return &testFlow{ |
| 86 | + ID: id, |
| 87 | + UI: ui, |
| 88 | + RequestURL: requestURL, |
| 89 | + Type: flowType, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func prepareTraits(username, password string) identity.Traits { |
| 94 | + payload := struct { |
| 95 | + Username string `json:"username"` |
| 96 | + Password string `json:"password"` |
| 97 | + }{username, password} |
| 98 | + |
| 99 | + data, _ := json.Marshal(payload) |
| 100 | + return data |
| 101 | +} |
| 102 | + |
| 103 | +func TestHandleHookError(t *testing.T) { |
| 104 | + r := &http.Request{URL: &url.URL{RawQuery: ""}} |
| 105 | + logger := logrusx.New("kratos", "test", logrusx.ForceLevel(logrus.FatalLevel)) |
| 106 | + l := &x.SimpleLoggerWithClient{L: logger, C: httpx.NewResilientClient(), T: otelx.NewNoop(logger, &otelx.Config{ServiceName: "kratos"})} |
| 107 | + csrf := testCSRFTokenGenerator{} |
| 108 | + f := newTestFlow(r, TypeBrowser) |
| 109 | + tr := prepareTraits("foo", "bar") |
| 110 | + |
| 111 | + t.Run("case=fill_in_traits", func(t *testing.T) { |
| 112 | + ve := schema.NewValidationListError([]*schema.ValidationError{schema.NewHookValidationError("traits.username", "invalid username", text.Messages{})}) |
| 113 | + |
| 114 | + err := HandleHookError(nil, r, f, tr, node.PasswordGroup, ve, l, &csrf) |
| 115 | + assert.ErrorIs(t, err, ve) |
| 116 | + if assert.NotEmpty(t, f.GetUI()) { |
| 117 | + ui := f.GetUI() |
| 118 | + assert.Len(t, ui.Nodes, 3) |
| 119 | + assert.ElementsMatch(t, ui.Nodes, |
| 120 | + node.Nodes{ |
| 121 | + &node.Node{Type: node.Input, Group: node.PasswordGroup, Attributes: &node.InputAttributes{Name: "traits.username", Type: node.InputAttributeTypeText, FieldValue: "foo", Required: true}, Meta: &node.Meta{}}, |
| 122 | + &node.Node{Type: node.Input, Group: node.PasswordGroup, Attributes: &node.InputAttributes{Name: "traits.password", Type: node.InputAttributeTypePassword, FieldValue: "bar", Required: true}, Meta: &node.Meta{}}, |
| 123 | + &node.Node{Type: node.Input, Group: node.DefaultGroup, Attributes: &node.InputAttributes{Name: "csrf_token", Type: node.InputAttributeTypeHidden, FieldValue: "csrf_token_value", Required: true}}, |
| 124 | + }) |
| 125 | + } |
| 126 | + }) |
| 127 | + |
| 128 | + t.Run("case=unmarshal_fail", func(t *testing.T) { |
| 129 | + ve := schema.NewValidationListError([]*schema.ValidationError{schema.NewHookValidationError("traits.username", "invalid username", text.Messages{})}) |
| 130 | + |
| 131 | + err := HandleHookError(nil, r, f, []byte("garbage"), node.PasswordGroup, ve, l, &csrf) |
| 132 | + var jsonErr *json.SyntaxError |
| 133 | + assert.ErrorAs(t, err, &jsonErr) |
| 134 | + }) |
| 135 | +} |
0 commit comments