mirror of
https://github.com/volatiletech/authboss.git
synced 2025-03-19 21:28:00 +02:00
Fix the tests in defaults package
- Make the default responder aggregate data, this allows aggregation of data from various middlewares like csrf or extra data for the layout without having to have an extra callback function like LayoutDataMaker or CRSFMaker
This commit is contained in:
parent
b33e47a97c
commit
2db3a3f782
@ -8,41 +8,17 @@ import (
|
|||||||
|
|
||||||
// Responder helps respond to http requests
|
// Responder helps respond to http requests
|
||||||
type Responder struct {
|
type Responder struct {
|
||||||
// CRSFHandler creates csrf tokens for inclusion on rendered forms
|
|
||||||
CSRFMaker CSRFMaker
|
|
||||||
// CRSFName is the name of the field that will include the token
|
|
||||||
CSRFName string
|
|
||||||
|
|
||||||
Renderer authboss.Renderer
|
Renderer authboss.Renderer
|
||||||
}
|
}
|
||||||
|
|
||||||
// CSRFMaker returns an opaque string when handed a request and response
|
// Respond to an HTTP request. It's main job is to merge data that comes in from
|
||||||
// to be included in the data as a
|
// various middlewares via the context with the data sent by the controller and render that.
|
||||||
type CSRFMaker func(w http.ResponseWriter, r *http.Request) string
|
|
||||||
|
|
||||||
// Respond to an HTTP request. Renders templates, flash messages, does CSRF
|
|
||||||
// and writes the headers out.
|
|
||||||
func (r *Responder) Respond(w http.ResponseWriter, req *http.Request, code int, templateName string, data authboss.HTMLData) error {
|
func (r *Responder) Respond(w http.ResponseWriter, req *http.Request, code int, templateName string, data authboss.HTMLData) error {
|
||||||
data.MergeKV(
|
ctxData := req.Context().Value(authboss.CTXKeyData)
|
||||||
r.CSRFName, r.CSRFMaker(w, req),
|
if ctxData != nil {
|
||||||
)
|
data.Merge(ctxData.(authboss.HTMLData))
|
||||||
|
|
||||||
/*
|
|
||||||
TODO(aarondl): Add middlewares for accumulating eventual view data using contexts
|
|
||||||
if a.LayoutDataMaker != nil {
|
|
||||||
data.Merge(a.LayoutDataMaker(w, req))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flashSuccess := authboss.FlashSuccess(w, req)
|
|
||||||
flashError := authboss.FlashError(w, req)
|
|
||||||
if len(flashSuccess) != 0 {
|
|
||||||
data.MergeKV(authboss.FlashSuccessKey, flashSuccess)
|
|
||||||
}
|
|
||||||
if len(flashError) != 0 {
|
|
||||||
data.MergeKV(authboss.FlashErrorKey, flashError)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
rendered, mime, err := r.Renderer.Render(req.Context(), templateName, data)
|
rendered, mime, err := r.Renderer.Render(req.Context(), templateName, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -20,24 +20,34 @@ func (t testRenderer) Render(ctx context.Context, name string, data authboss.HTM
|
|||||||
return t.Callback(ctx, name, data)
|
return t.Callback(ctx, name, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testJSONRender(ctx context.Context, name string, data authboss.HTMLData) ([]byte, string, error) {
|
||||||
|
b, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b, "application/json", nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestResponder(t *testing.T) {
|
func TestResponder(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
renderer := testRenderer{
|
renderer := testRenderer{
|
||||||
Callback: func(ctx context.Context, name string, data authboss.HTMLData) ([]byte, string, error) {
|
Callback: testJSONRender,
|
||||||
return nil, "", nil
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
responder := Responder{
|
responder := Responder{
|
||||||
Renderer: renderer,
|
Renderer: renderer,
|
||||||
CSRFName: "csrf",
|
|
||||||
CSRFMaker: func(w http.ResponseWriter, r *http.Request) string { return "csrftoken" },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
r := httptest.NewRequest("GET", "/", nil)
|
r := httptest.NewRequest("GET", "/", nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
r = r.WithContext(context.WithValue(context.Background(), authboss.CTXKeyData, authboss.HTMLData{
|
||||||
|
"csrfname": "csrf",
|
||||||
|
"csrftoken": "12345",
|
||||||
|
}))
|
||||||
|
|
||||||
err := responder.Respond(w, r, http.StatusCreated, "some_template.tpl", authboss.HTMLData{"auth_happy": true})
|
err := responder.Respond(w, r, http.StatusCreated, "some_template.tpl", authboss.HTMLData{"auth_happy": true})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
@ -52,9 +62,8 @@ func TestResponder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
expectData := authboss.HTMLData{
|
expectData := authboss.HTMLData{
|
||||||
"csrfName": "xsrf",
|
"csrfname": "csrf",
|
||||||
"csrfToken": "xsrftoken",
|
"csrftoken": "12345",
|
||||||
"hello": "world",
|
|
||||||
"auth_happy": true,
|
"auth_happy": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,9 +80,7 @@ func TestRedirector(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
renderer := testRenderer{
|
renderer := testRenderer{
|
||||||
Callback: func(ctx context.Context, name string, data authboss.HTMLData) ([]byte, string, error) {
|
Callback: testJSONRender,
|
||||||
return nil, "", nil
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
redir := Redirector{
|
redir := Redirector{
|
||||||
@ -102,7 +109,7 @@ func TestRedirector(t *testing.T) {
|
|||||||
|
|
||||||
var gotData map[string]string
|
var gotData map[string]string
|
||||||
if err := json.Unmarshal(w.Body.Bytes(), &gotData); err != nil {
|
if err := json.Unmarshal(w.Body.Bytes(), &gotData); err != nil {
|
||||||
t.Error(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if got := gotData["status"]; got != "success" {
|
if got := gotData["status"]; got != "success" {
|
||||||
@ -120,9 +127,7 @@ func TestResponseRedirectAPIFollowRedir(t *testing.T) {
|
|||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
renderer := testRenderer{
|
renderer := testRenderer{
|
||||||
Callback: func(ctx context.Context, name string, data authboss.HTMLData) ([]byte, string, error) {
|
Callback: testJSONRender,
|
||||||
return nil, "", nil
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
redir := Redirector{
|
redir := Redirector{
|
||||||
@ -151,7 +156,7 @@ func TestResponseRedirectAPIFollowRedir(t *testing.T) {
|
|||||||
|
|
||||||
var gotData map[string]string
|
var gotData map[string]string
|
||||||
if err := json.Unmarshal(w.Body.Bytes(), &gotData); err != nil {
|
if err := json.Unmarshal(w.Body.Bytes(), &gotData); err != nil {
|
||||||
t.Error(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if got := gotData["status"]; got != "failure" {
|
if got := gotData["status"]; got != "failure" {
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,12 +40,29 @@ func TestRouter(t *testing.T) {
|
|||||||
delete = string(b)
|
delete = string(b)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
wr := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest("GET", "/test", strings.NewReader("testget"))
|
||||||
|
r.ServeHTTP(wr, req)
|
||||||
if get != wantGet {
|
if get != wantGet {
|
||||||
t.Error("want:", wantGet, "got:", get)
|
t.Error("want:", wantGet, "got:", get)
|
||||||
}
|
}
|
||||||
|
if len(post) != 0 || len(delete) != 0 {
|
||||||
|
t.Error("should be empty:", post, delete)
|
||||||
|
}
|
||||||
|
|
||||||
|
wr = httptest.NewRecorder()
|
||||||
|
req = httptest.NewRequest("POST", "/test", strings.NewReader("testpost"))
|
||||||
|
r.ServeHTTP(wr, req)
|
||||||
if post != wantPost {
|
if post != wantPost {
|
||||||
t.Error("want:", wantPost, "got:", post)
|
t.Error("want:", wantPost, "got:", post)
|
||||||
}
|
}
|
||||||
|
if len(delete) != 0 {
|
||||||
|
t.Error("should be empty:", delete)
|
||||||
|
}
|
||||||
|
|
||||||
|
wr = httptest.NewRecorder()
|
||||||
|
req = httptest.NewRequest("DELETE", "/test", strings.NewReader("testdelete"))
|
||||||
|
r.ServeHTTP(wr, req)
|
||||||
if delete != wantDelete {
|
if delete != wantDelete {
|
||||||
t.Error("want:", wantDelete, "got:", delete)
|
t.Error("want:", wantDelete, "got:", delete)
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,6 @@ func (h HTTPFormValidator) Validate(r *http.Request) authboss.ErrorList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(h.ConfirmFields)-1; i += 2 {
|
for i := 0; i < len(h.ConfirmFields)-1; i += 2 {
|
||||||
fmt.Println(h.ConfirmFields)
|
|
||||||
main := r.FormValue(h.ConfirmFields[i])
|
main := r.FormValue(h.ConfirmFields[i])
|
||||||
if len(main) == 0 {
|
if len(main) == 0 {
|
||||||
continue
|
continue
|
||||||
|
Loading…
x
Reference in New Issue
Block a user