package recover import ( "bytes" "fmt" "html/template" "log" "net/http" "net/http/httptest" "regexp" "strings" "testing" "time" "gopkg.in/authboss.v0" "gopkg.in/authboss.v0/internal/mocks" ) const ( testURLBase64Token = "MTIzNA==" testStdBase64Token = "gdyb21LQTcIANtvYMT7QVQ==" ) func testSetup() (r *Recover, s *mocks.MockStorer, l *bytes.Buffer) { s = mocks.NewMockStorer() l = &bytes.Buffer{} ab := authboss.New() ab.Layout = template.Must(template.New("").Parse(`{{template "authboss" .}}`)) ab.LayoutHTMLEmail = template.Must(template.New("").Parse(`{{template "authboss" .}}`)) ab.LayoutTextEmail = template.Must(template.New("").Parse(`{{template "authboss" .}}`)) ab.Storer = s ab.XSRFName = "xsrf" ab.XSRFMaker = func(_ http.ResponseWriter, _ *http.Request) string { return "xsrfvalue" } ab.PrimaryID = authboss.StoreUsername ab.LogWriter = l r = &Recover{} if err := r.Initialize(ab); err != nil { panic(err) } return r, s, l } func testRequest(ab *authboss.Authboss, 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(ab, postFormValues...) ctx.SessionStorer = sessionStorer return ctx, httptest.NewRecorder(), r, sessionStorer } func TestRecover(t *testing.T) { t.Parallel() r, _, _ := testSetup() storage := r.Storage() if storage[r.PrimaryID] != authboss.String { t.Error("Expected storage KV:", r.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) { t.Parallel() rec, _, _ := testSetup() ctx, w, r, _ := testRequest(rec.Authboss, "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, `