Beispiel #1
0
func TestAdminEveryAction(t *testing.T) {
	r := &TestRenderer{}
	h := &Admin{
		Session:  session,
		Renderer: r,
	}
	h.Register(T6{}, "admin_test.T6", nil)

	//cowboy type assertions
	defer func() {
		if err := recover(); err != nil {
			t.Fatalf("Error while attempting to do everything: %s", err)
		}
	}()

	c := session.DB("admin_test").C("T6")

	Post(t, h, "/create/admin_test.T6/", url.Values{
		"X": {"2"},
		"Y": {"new"},
		"Z": {"false"},
	})
	id := r.Last().Params.(CreateContext).Form.context.Values["ID"].(string)

	//lets check it exists in the database
	if n, err := c.Find(d{"_id": bson.ObjectIdHex(id)}).Count(); n != 1 || err != nil {
		t.Fatalf("Expected %d objects in the database. Got %d.\nError: %v", 1, n, err)
	}

	//make sure we can get it out from the web
	Get(t, h, fmt.Sprintf("/detail/admin_test.T6/%s", id))
	obj1 := r.Last().Params.(DetailContext).Object.(*T6)
	if obj1.X != 2 || obj1.Y != "new" || obj1.Z != false {
		t.Fatalf("Expected %v\nGot %v\n", T6{"", 2, "new", false}, obj1)
	}

	//time to update
	Post(t, h, fmt.Sprintf("/update/admin_test.T6/%s", id), url.Values{
		"X": {"20"},
		"Y": {"newt"},
		"Z": {"true"},
	})
	obj2 := r.Last().Params.(UpdateContext).Object.(*T6)
	if obj2.X != 20 || obj2.Y != "newt" || obj2.Z != true {
		t.Fatalf("Expected %v\nGot %v\n", T6{"", 20, "newt", true}, obj1)
	}

	//now lets delete it
	Get(t, h, fmt.Sprintf("/delete/admin_test.T6/%s?_sure=yes", id))
	if n, err := c.Find(d{"_id": bson.ObjectIdHex(id)}).Count(); n != 0 || err != nil {
		t.Fatalf("Expected %d objects in the database. Got %d.\nError: %v", 0, n, err)
	}
}
Beispiel #2
0
func TestReverseCustomObj(t *testing.T) {
	h := &Admin{
		Routes: map[string]string{
			"create": "/1/",
			"detail": "/2/",
			"list":   "/3/",
			"delete": "/4/",
			"update": "/5/",
			"auth":   "/6/",
			"index":  "/",
		},
		Session: session,
	}
	r := Reverser{h}
	h.Register(T{}, "admin_test.T", nil)

	var x T = T{bson.ObjectIdHex("ffffffffffffffffffffffff")}

	if c, e := r.CreateObj(x), "/1/admin_test.T"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
	if c, e := r.DetailObj(x), "/2/admin_test.T/ffffffffffffffffffffffff"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
	if c, e := r.ListObj(x), "/3/admin_test.T"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
	if c, e := r.DeleteObj(x), "/4/admin_test.T/ffffffffffffffffffffffff"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
	if c, e := r.UpdateObj(x), "/5/admin_test.T/ffffffffffffffffffffffff"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
}
Beispiel #3
0
func (self *Backend) ImageVersionReader(id string) (reader io.ReadCloser, ctype string, err error) {
	file, err := self.gridFS.OpenId(bson.ObjectIdHex(id))
	if err == mgo.ErrNotFound {
		return nil, "", media.ErrInvalidImageID(id)
	} else if err != nil {
		return nil, "", err
	}
	return file, file.ContentType(), nil
}
Beispiel #4
0
func (self *Backend) LoadImage(id string) (*media.Image, error) {
	doc, err := self.images.DocumentWithID(bson.ObjectIdHex(id))
	if err != nil {
		return nil, err
	}
	image := &doc.(*ImageDoc).Image
	image.Init()
	return image, nil
}
Beispiel #5
0
func (self *Ref) SetString(str string) error {
	switch len(str) {
	case 0, 12:
		self.ID = bson.ObjectId(str)
	case 24:
		self.ID = bson.ObjectIdHex(str)
	default:
		return errs.Format("Invalid string for bson.ObjectId: '%s'", str)
	}
	return nil
}
Beispiel #6
0
func TestAdminPostUpdate(t *testing.T) {
	r := &TestRenderer{}
	h := &Admin{
		Session:  session,
		Renderer: r,
	}
	h.Register(T6{}, "admin_test.T6", nil)
	var w *TestResponseWriter

	//revert to original after
	defer session.DB("admin_test").C("T6").Update(d{"_id": bson.ObjectIdHex("4f0ee3600888a1b6646199bd")}, d{"x": 20, "y": "foo", "z": true})

	w = Post(t, h, "/update/admin_test.T6/4f0ee3600888a1b6646199bd", url.Values{
		"X": {"30"},
		"Y": {"foob"},
		"Z": {"false"},
	})
	if w.Status != http.StatusOK {
		t.Fatalf("Wrong return type on create. expected 200 got %d", w.Status)
	}
	if r.Last().Type != "Update" {
		t.Fatalf("Wrong Renderer called. Expected Update got %s", r.Last().Type)
	}

	w = Get(t, h, "/detail/admin_test.T6/4f0ee3600888a1b6646199bd")
	if w.Status != http.StatusOK {
		t.Fatalf("Wrong return type on Detail. Expected 200 got %d", w.Status)
	}
	if r.Last().Type != "Detail" {
		t.Fatalf("Wrong Renderer called. Expected Detail got %s", r.Last().Type)
	}

	//cowboy type assertions
	defer func() {
		if err := recover(); err != nil {
			t.Fatalf("Error checking conditions: %s", err)
		}
	}()

	//compare values in the object to known values
	obj := r.Last().Params.(DetailContext).Object.(*T6)
	if obj.X != 30 {
		t.Fatalf("X: Expected %d. Got %d.", 30, obj.X)
	}
	if obj.Y != "foob" {
		t.Fatalf("Y: Expected %s. Got %s.", "foob", obj.X)
	}
	if obj.Z != false {
		t.Fatalf("Z: Expected %v. Got %v.", false, obj.X)
	}
}
Beispiel #7
0
func TestReverseIdFor(t *testing.T) {
	h := &Admin{Session: session}
	r := Reverser{h}
	h.Register(T{}, "admin_test.T", nil)

	var x T = T{bson.ObjectIdHex("ffffffffffffffffffffffff")}

	if r.idFor(x) != "ffffffffffffffffffffffff" {
		t.Fatalf("Expected %q. Got %q.", "ffffffffffffffffffffffff", r.idFor(x))
	}

	if r.idFor(&x) != "ffffffffffffffffffffffff" {
		t.Fatalf("Expected %q. Got %q.", "ffffffffffffffffffffffff", r.idFor(x))
	}
}
Beispiel #8
0
//Presents the detail view for an object in a collection
func (a *Admin) detail(w http.ResponseWriter, req *http.Request) {
	coll, id := parseRequest(req.URL.Path)

	//ensure we have both a collection key and an id
	if coll == "" || id == "" {
		a.Renderer.NotFound(w, req)
		return
	}

	//make sure we know about the requested collection
	if !a.hasType(coll) {
		a.Renderer.NotFound(w, req)
		return
	}

	c, t := a.collFor(coll), a.newType(coll)

	//load into T
	if err := c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(t); err != nil {
		if err.Error() == "Document not found" {
			a.Renderer.NotFound(w, req)
			return
		}
		a.Renderer.InternalError(w, req, err)
		return
	}

	//create the values for the template
	ctx, err := generateContext(t, nil)
	if err != nil {
		a.Renderer.InternalError(w, req, err)
		return
	}

	a.Renderer.Detail(w, req, DetailContext{
		BaseContext: a.baseContext(req),
		Collection:  coll,
		Object:      t,
		Form: Form{
			object:  t,
			context: ctx,
			logger:  a.logger,
		},
	})
}
Beispiel #9
0
func (self *Backend) SaveImage(image *media.Image) error {
	if image.ID == "" {
		doc := self.images.NewDocument().(*ImageDoc)
		doc.Image = *image
		id, err := self.images.Insert(doc)
		if err != nil {
			return err
		}
		image.ID.Set(id.Hex())
		return nil
	}

	id := bson.ObjectIdHex(image.ID.Get())
	doc := self.images.NewDocument().(*ImageDoc)
	doc.SetObjectId(id)
	doc.Image = *image
	doc.Image.ID = ""
	return self.images.Update(id, doc)
}
Beispiel #10
0
func TestReverseDefaultObj(t *testing.T) {
	h := &Admin{Session: session}
	r := Reverser{h}
	h.Register(T{}, "admin_test.T", nil)

	var x T = T{bson.ObjectIdHex("ffffffffffffffffffffffff")}

	if c, e := r.CreateObj(x), "/create/admin_test.T"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
	if c, e := r.DetailObj(x), "/detail/admin_test.T/ffffffffffffffffffffffff"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
	if c, e := r.ListObj(x), "/list/admin_test.T"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
	if c, e := r.DeleteObj(x), "/delete/admin_test.T/ffffffffffffffffffffffff"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
	if c, e := r.UpdateObj(x), "/update/admin_test.T/ffffffffffffffffffffffff"; c != e {
		t.Errorf("Expected %q. Got %q.", e, c)
	}
}
Beispiel #11
0
func TestAdminPostCreate(t *testing.T) {
	r := &TestRenderer{}
	h := &Admin{
		Session:  session,
		Renderer: r,
	}
	h.Register(T6{}, "admin_test.T6", nil)
	var w *TestResponseWriter

	w = Post(t, h, "/create/admin_test.T6/", url.Values{
		"X": {"20"},
		"Y": {"foo"},
		"Z": {"true"},
	})
	if w.Status != http.StatusOK {
		t.Fatalf("Wrong return type on create. Expected 200 got %d", w.Status)
	}
	if r.Last().Type != "Create" {
		t.Fatalf("Wrong Renderer called. Expected Create got %s", r.Last().Type)
	}

	//lets check all the context values
	params := r.Last().Params.(CreateContext)
	if !params.Success {
		t.Fatalf("Unsucessful create.\n%s", params)
	}
	if !params.Attempted {
		t.Fatalf("Attempted false. Expected true.\n%s", params)
	}

	values := params.Form.context.Values

	defer session.DB("admin_test").C("T6").Remove(d{"_id": bson.ObjectIdHex(values["ID"].(string))})

	if values["X"] != "20" {
		t.Fatalf("X: Expected %q. Got %q.", "20", values["X"])
	}

	if values["Y"] != "foo" {
		t.Fatalf("Y: Expected %q. Got %q.", "foo", values["X"])
	}

	if values["Z"] != "true" {
		t.Fatalf("Z: Expected %q. Got %q.", "true", values["X"])
	}

	//lets ask detail about values["ID"]
	w = Get(t, h, fmt.Sprintf("/detail/admin_test.T6/%s", values["ID"]))
	if w.Status != http.StatusOK {
		t.Fatalf("Wrong return type on Detail. Expected 200 got %d", w.Status)
	}
	if r.Last().Type != "Detail" {
		t.Fatalf("Wrong Renderer called. Expected Detail got %s", r.Last().Type)
	}

	//cowboy type assertions
	defer func() {
		if err := recover(); err != nil {
			t.Fatalf("Error checking conditions: %s", err)
		}
	}()

	//compare values in the object to known values
	obj := r.Last().Params.(DetailContext).Object.(*T6)
	if obj.X != 20 {
		t.Fatalf("X: Expected %d. Got %d.", 20, obj.X)
	}
	if obj.Y != "foo" {
		t.Fatalf("Y: Expected %s. Got %s.", "foo", obj.X)
	}
	if obj.Z != true {
		t.Fatalf("Z: Expected %v. Got %v.", true, obj.X)
	}

	//delete the item from the database
}
Beispiel #12
0
func FindByID(id string) (userDoc interface{}, found bool, err error) {
	return Config.Collection.TryDocumentWithID(bson.ObjectIdHex(id))
}
Beispiel #13
0
//Presents a handler that updates an object and shows the results of the update
func (a *Admin) update(w http.ResponseWriter, req *http.Request) {
	coll, id := parseRequest(req.URL.Path)

	//ensure we have both an id and a collection
	if coll == "" || id == "" {
		a.Renderer.NotFound(w, req)
		return
	}

	//make sure we know about the requested collection
	if !a.hasType(coll) {
		a.Renderer.NotFound(w, req)
		return
	}

	c, t := a.collFor(coll), a.newType(coll)

	//grab the data
	if err := c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(t); err != nil {
		if err.Error() == "Document not found" {
			a.Renderer.NotFound(w, req)
			return
		}
		a.Renderer.InternalError(w, req, err)
		return
	}

	var attempted, success bool
	var errors map[string]interface{}
	if req.Method == "POST" {
		attempted = true

		var err error
		errors, err = performLoading(req, t)
		if err != nil {
			a.Renderer.InternalError(w, req, err)
			return
		}
		if errors != nil && len(errors) > 0 {
			goto render
		}

		if err := c.Update(bson.M{"_id": bson.ObjectIdHex(id)}, t); err != nil {
			a.Renderer.InternalError(w, req, err)
			return
		}
		success = true
	}

render:
	var form = Form{
		object: t,
		logger: a.logger,
	}
	if ctx, err := generateContext(t, errors); err != nil {
		a.Renderer.InternalError(w, req, err)
		return
	} else {
		form.context = ctx
	}

	a.Renderer.Update(w, req, UpdateContext{
		BaseContext: a.baseContext(req),
		Collection:  coll,
		Object:      t,
		Attempted:   attempted,
		Success:     success,
		Form:        form,
	})
}
Beispiel #14
0
//Presents the delete view for an object in a collection
func (a *Admin) delete(w http.ResponseWriter, req *http.Request) {
	coll, id := parseRequest(req.URL.Path)

	//ensure we have both a collection and an id
	if coll == "" || id == "" {
		a.Renderer.NotFound(w, req)
		return
	}

	//make sure we know about the requested collection
	if !a.hasType(coll) {
		a.Renderer.NotFound(w, req)
		return
	}

	c, t := a.collFor(coll), a.newType(coll)

	//load into T
	if err := c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(t); err != nil {
		if err.Error() == "Document not found" {
			a.Renderer.NotFound(w, req)
			return
		}
		a.Renderer.InternalError(w, req, err)
		return
	}

	//check if they're sure they want to delete
	req.ParseForm()

	var attempted, success bool
	var err error

	if req.Form.Get("_sure") == "yes" {
		attempted = true

		err = c.Remove(bson.M{"_id": bson.ObjectIdHex(id)})
		success = err == nil
	}

	//create the values for the template
	ctx, err := generateContext(t, nil)
	if err != nil {
		a.Renderer.InternalError(w, req, err)
		return
	}

	a.Renderer.Delete(w, req, DeleteContext{
		BaseContext: a.baseContext(req),
		Collection:  coll,
		Object:      t,
		Attempted:   attempted,
		Success:     success,
		Error:       err,
		Form: Form{
			object:  t,
			context: ctx,
			logger:  a.logger,
		},
	})
}