package recover import ( "bytes" "fmt" "html/template" "net/http" "net/http/httptest" "regexp" "strings" "testing" "gopkg.in/authboss.v0" "gopkg.in/authboss.v0/internal/mocks" ) func testSetup() (r *Recover, s *mocks.MockStorer, l *bytes.Buffer) { s = mocks.NewMockStorer() l = &bytes.Buffer{} authboss.Cfg = authboss.NewConfig() authboss.Cfg.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`)) authboss.Cfg.LayoutEmail = template.Must(template.New("").Parse(`{{template "authboss" .}}`)) authboss.Cfg.Storer = s authboss.Cfg.XSRFName = "xsrf" authboss.Cfg.XSRFMaker = func(_ http.ResponseWriter, _ *http.Request) string { return "xsrfvalue" } authboss.Cfg.PrimaryID = authboss.StoreUsername authboss.Cfg.LogWriter = l r = &Recover{} if err := r.Initialize(); err != nil { panic(err) } return r, s, l } func testRequest(method string, postFormValues ...string) (*authboss.Context, *httptest.ResponseRecorder, *http.Request, authboss.ClientStorerErr) { r, err := http.NewRequest(method, "", nil) if err != nil { panic(err) } sessionStorer := mocks.NewMockClientStorer() ctx := mocks.MockRequestContext(postFormValues...) ctx.SessionStorer = sessionStorer return ctx, httptest.NewRecorder(), r, sessionStorer } func TestRecover(t *testing.T) { r, _, _ := testSetup() storage := r.Storage() if storage[authboss.Cfg.PrimaryID] != authboss.String { t.Error("Expected storage KV:", authboss.Cfg.PrimaryID, authboss.String) } if storage[authboss.StoreEmail] != authboss.String { t.Error("Expected storage KV:", authboss.StoreEmail, authboss.String) } if storage[authboss.StorePassword] != authboss.String { t.Error("Expected storage KV:", authboss.StorePassword, authboss.String) } if storage[StoreRecoverToken] != authboss.String { t.Error("Expected storage KV:", StoreRecoverToken, authboss.String) } if storage[StoreRecoverTokenExpiry] != authboss.String { t.Error("Expected storage KV:", StoreRecoverTokenExpiry, authboss.String) } routes := r.Routes() if routes["/recover"] == nil { t.Error("Expected route '/recover' with handleFunc") } if routes["/recover/complete"] == nil { t.Error("Expected route '/recover/complete' with handleFunc") } } func TestRecover_startHandlerFunc_GET(t *testing.T) { rec, _, _ := testSetup() ctx, w, r, _ := testRequest("GET") if err := rec.startHandlerFunc(ctx, w, r); err != nil { t.Error("Unexpected error:", err) } if w.Code != http.StatusOK { t.Error("Unexpected status:", w.Code) } body := w.Body.String() if !strings.Contains(body, `