-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
selfservice_recovery.go
98 lines (81 loc) · 3.13 KB
/
selfservice_recovery.go
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
// nolint
package testhelpers
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ory/viper"
"github.com/ory/x/pointerx"
"github.com/ory/kratos/driver"
"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/internal/httpclient/client/common"
"github.com/ory/kratos/internal/httpclient/models"
"github.com/ory/kratos/selfservice/flow/recovery"
"github.com/ory/kratos/x"
)
func NewRecoveryUITestServer(t *testing.T) *httptest.Server {
router := httprouter.New()
router.GET("/recovery", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.WriteHeader(http.StatusNoContent)
})
router.GET("/settings", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.WriteHeader(http.StatusAccepted)
})
ts := httptest.NewServer(router)
t.Cleanup(ts.Close)
viper.Set(configuration.ViperKeySelfServiceSettingsURL, ts.URL+"/settings")
viper.Set(configuration.ViperKeySelfServiceRecoveryUI, ts.URL+"/recovery")
return ts
}
func NewRecoveryUIFlowEchoServer(t *testing.T, reg driver.Registry) *httptest.Server {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
e, err := reg.RecoveryFlowPersister().GetRecoveryFlow(r.Context(), x.ParseUUID(r.URL.Query().Get("flow")))
require.NoError(t, err)
reg.Writer().Write(w, r, e)
}))
viper.Set(configuration.ViperKeySelfServiceRecoveryUI, ts.URL+"/recovery-ts")
t.Cleanup(ts.Close)
return ts
}
func GetRecoveryRequest(t *testing.T, client *http.Client, ts *httptest.Server) *common.GetSelfServiceRecoveryFlowOK {
publicClient := NewSDKClient(ts)
res, err := client.Get(ts.URL + recovery.RouteInitBrowserFlow)
require.NoError(t, err)
require.NoError(t, res.Body.Close())
rs, err := publicClient.Common.GetSelfServiceRecoveryFlow(
common.NewGetSelfServiceRecoveryFlowParams().WithHTTPClient(client).
WithID(res.Request.URL.Query().Get("flow")),
)
require.NoError(t, err, "%s", res.Request.URL.String())
assert.Empty(t, rs.Payload.Active)
return rs
}
func RecoverySubmitForm(
t *testing.T,
f *models.RecoveryFlowMethodConfig,
hc *http.Client,
values url.Values,
) (string, *common.GetSelfServiceRecoveryFlowOK) {
require.NotEmpty(t, f.Action)
res, err := hc.PostForm(pointerx.StringR(f.Action), values)
require.NoError(t, err)
defer res.Body.Close()
b, err := ioutil.ReadAll(res.Body)
require.NoError(t, err)
assert.EqualValues(t, http.StatusNoContent, res.StatusCode, "%s", b)
assert.Equal(t, viper.GetString(configuration.ViperKeySelfServiceRecoveryUI), res.Request.URL.Scheme+"://"+res.Request.URL.Host+res.Request.URL.Path, "should end up at the settings URL, used: %s", pointerx.StringR(f.Action))
rs, err := NewSDKClientFromURL(viper.GetString(configuration.ViperKeyPublicBaseURL)).Common.GetSelfServiceRecoveryFlow(
common.NewGetSelfServiceRecoveryFlowParams().WithHTTPClient(hc).
WithID(res.Request.URL.Query().Get("flow")),
)
require.NoError(t, err)
body, err := json.Marshal(rs.Payload)
require.NoError(t, err)
return string(body), rs
}