Exemple #1
0
func TestUploader(t *testing.T) {
	mgodb.Setup("localhost", "tenpu_test")
	db := mgodb.NewDatabase("localhost", "tenpu_test")
	mgodb.CollectionDo(tenpu.CollectionName, func(c *mgo.Collection) {
		c.DropCollection()
	})

	st := gridfs.NewStorage()

	http.HandleFunc("/postupload", tenpu.MakeUploader("OwnerId", "posts", st))
	http.HandleFunc("/load", tenpu.MakeFileLoader("id", st))
	ts := httptest.NewServer(http.DefaultServeMux)
	defer ts.Close()

	req, _ := http.NewRequest("POST", ts.URL+"/postupload", strings.NewReader(multipartContent))
	req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	b, _ := ioutil.ReadAll(res.Body)
	strb := string(b)
	if !strings.Contains(strb, "4facead362911fa23c000001") {
		t.Errorf("%+v", strb)
	}

	dbc := tenpu.DatabaseClient{Database: db}
	atts := dbc.Attachments("4facead362911fa23c000001")
	if len(atts) != 2 {
		t.Errorf("%+v", atts[0])
	}

	res, err = http.Get(ts.URL + "/load?id=" + atts[0].Id)
	if err != nil {
		panic(err)
	}

	b, _ = ioutil.ReadAll(res.Body)
	strb = string(b)
	if strb != "the file content a\n" {
		t.Errorf("%+v", strb)
	}
}
Exemple #2
0
func TestUploadWithoutOwnerId(t *testing.T) {
	mgodb.Setup("localhost", "tenpu_test")

	http.HandleFunc("/errorpostupload", tenpu.MakeUploader("OwnerId", "posts", gridfs.NewStorage()))
	ts := httptest.NewServer(http.DefaultServeMux)
	defer ts.Close()

	req, _ := http.NewRequest("POST", ts.URL+"/errorpostupload", strings.NewReader(noOwnerIdPostContent))
	req.Header.Set("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarySHaDkk90eMKgsVUj")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	b, _ := ioutil.ReadAll(res.Body)
	strb := string(b)
	if !strings.Contains(strb, "ownerId required") {
		t.Errorf("%+v", strb)
	}

}
Exemple #3
0
func Mux() (mux *http.ServeMux) {

	p := pat.New()
	sessionMW := mango.Sessions("f908b1c425062e95d30b8d30de7123458", "duoerl", &mango.CookieOptions{Path: "/", MaxAge: 3600 * 24 * 7})

	rendererMW := middlewares.ProduceRenderer()
	authenMW := middlewares.AuthenticateUser()
	hardAuthenMW := middlewares.HardAuthenUser()
	rHtml, rJson := middlewares.RespondHtml(), middlewares.RespondJson()

	mainLayoutMW := middlewares.ProduceLayout(middlewares.MAIN_LAYOUT)
	mainStack := new(mango.Stack)
	mainStack.Middleware(mangogzip.Zipper, mangolog.Logger, sessionMW, authenMW, mainLayoutMW, rendererMW, rHtml)

	mainAjaxStack := new(mango.Stack)
	mainAjaxStack.Middleware(mangogzip.Zipper, mangolog.Logger, sessionMW, authenMW, rJson)

	hardAuthenStack := new(mango.Stack)
	hardAuthenStack.Middleware(mangogzip.Zipper, mangolog.Logger, sessionMW, hardAuthenMW, mainLayoutMW, rendererMW, rHtml)

	// User
	p.Get("/login", mainStack.HandlerFunc(sessions.LoginPage))
	p.Post("/login", mainStack.HandlerFunc(sessions.LoginAction))
	p.Get("/signup", mainStack.HandlerFunc(sessions.SignupPage))
	p.Post("/signup", mainStack.HandlerFunc(sessions.SignupAction))
	p.Get("/logout", mainStack.HandlerFunc(sessions.Logout))

	p.Post("/user/update", hardAuthenStack.HandlerFunc(users.Update))
	p.Get("/user/edit", hardAuthenStack.HandlerFunc(users.Edit))
	p.Get("/user/:id", mainStack.HandlerFunc(users.Show))

	// User post
	p.Post("/post/create", mainAjaxStack.HandlerFunc(posts.Create))

	// Brand
	p.Get("/brands", mainStack.HandlerFunc(brands.Index))
	p.Get("/brand/new", mainStack.HandlerFunc(brands.New))
	p.Post("/brand/create", mainStack.HandlerFunc(brands.Create))
	p.Get("/brand/:id", mainStack.HandlerFunc(brands.Show))
	p.Get("/brand/:id/edit", mainStack.HandlerFunc(brands.Edit))
	p.Post("/brand/update", mainStack.HandlerFunc(brands.Update))

	// Follow brand
	p.Post("/brand/follow", mainStack.HandlerFunc(followbrands.Create))
	p.Post("/brand/unfollow", mainStack.HandlerFunc(followbrands.Delete))

	// Product
	p.Get("/products", mainStack.HandlerFunc(products.Index))
	p.Get("/product/new", mainStack.HandlerFunc(products.New))
	p.Post("/product/create", mainStack.HandlerFunc(products.Create))
	p.Get("/product/:id", mainStack.HandlerFunc(products.Show))
	p.Get("/product/:id/edit", mainStack.HandlerFunc(products.Edit))
	p.Post("/product/update", mainStack.HandlerFunc(products.Update))

	// Notes
	p.Get("/note/new", mainStack.HandlerFunc(notes.New))
	p.Get("/note/:id", mainStack.HandlerFunc(notes.Show))
	p.Post("/note/create", mainStack.HandlerFunc(notes.Create))

	// Review
	p.Post("/review/create", mainStack.HandlerFunc(reviews.Create))
	p.Post("/review/like", mainAjaxStack.HandlerFunc(reviews.Like))

	// Wish Item
	p.Post("/wish_item/add", mainAjaxStack.HandlerFunc(wishitems.Create))
	p.Post("/wish_item/remove", mainAjaxStack.HandlerFunc(wishitems.Delete))

	// Own Item
	p.Post("/own_item/add", mainAjaxStack.HandlerFunc(ownitems.Create))
	p.Post("/own_item/remove", mainAjaxStack.HandlerFunc(ownitems.Delete))

	// News
	p.Get("/news/:id", mainStack.HandlerFunc(news.Show))
	p.Post("/news/create", mainStack.HandlerFunc(news.Create))
	p.Get("/news/:id/edit", mainStack.HandlerFunc(news.Edit))
	p.Post("/news/update", mainStack.HandlerFunc(news.Update))

	// For admin in the futrue
	p.Get("/admin/categories", mainStack.HandlerFunc(categories.Index))
	p.Post("/admin/category/create", mainStack.HandlerFunc(categories.Create))
	p.Post("/admin/efficacy/create", mainStack.HandlerFunc(efficacies.Create))

	// News for admin
	p.Get("/admin/news/new", mainStack.HandlerFunc(news.New))

	// For Image upload
	imageUploader := tenpu.MakeUploader(images.TheImageMaker)
	imageLoader := tenpu.MakeFileLoader(images.TheImageMaker)

	p.Post("/upload/:category/:uid", imageUploader)
	p.Get("/img/:id/:name", imageLoader)

	p.Get("/", mainStack.HandlerFunc(feeds.Index))
	mux = http.NewServeMux()
	mux.HandleFunc("/favicon.ico", filterUrl)
	mux.Handle("/", p)
	mux.Handle("/public/", http.FileServer(http.Dir(".")))

	train.ConfigureHttpHandler(mux)
	return
}
Exemple #4
0
func TestThumbnailLoader(t *testing.T) {

	mgodb.Setup("localhost", "tenpu_test")
	mgodb.DropCollections(collectionName, thumbnailsCollectionName)

	m := &maker{}
	_, meta, _, _ := m.MakeForUpload(nil)

	http.HandleFunc("/thumbpostupload", tenpu.MakeUploader(m))
	http.HandleFunc("/thumbload", thumbnails.MakeLoader(&thumbnails.Configuration{
		Maker: m,
		ThumbnailStorageMaker: &ThumbnailStorageMaker{},
		ThumbnailSpecs: []*thumbnails.ThumbnailSpec{
			{Name: "icon", Width: 100},
		},
	}))

	ts := httptest.NewServer(http.DefaultServeMux)
	defer ts.Close()

	var err error

	s := integrationtest.NewSession()
	res := integrationtest.Must(s.PostMultipart(ts.URL+"/thumbpostupload", func(w *multipart.Writer) {
		w.WriteField("OwnerId", "my12345")
		p1, err := w.CreateFormFile("t", "t.jpg")
		if err != nil {
			panic(err)
		}
		tf, err1 := os.Open("t.jpg")
		if err1 != nil {
			panic(err1)
		}
		defer tf.Close()

		io.Copy(p1, tf)
	}))

	b, _ := ioutil.ReadAll(res.Body)
	strb := string(b)
	if !strings.Contains(strb, "my12345") {
		t.Errorf("%+v", strb)
	}

	atts := meta.Attachments("my12345")
	if len(atts) != 1 {
		t.Errorf("%+v", atts)
	}

	res, err = http.Get(ts.URL + fmt.Sprintf("/thumbload?id=%s&thumb=icon", atts[0].Id))
	if err != nil {
		panic(err)
	}

	f, err2 := os.OpenFile("thumbGenerated.jpg", os.O_CREATE|os.O_RDWR, 0666)
	if err2 != nil {
		panic(err2)
	}

	defer f.Close()

	io.Copy(f, res.Body)

	http.Get(ts.URL + fmt.Sprintf("/thumbload?id=%s&thumb=icon", atts[0].Id))

	var thumbs []thumbnails.Thumbnail
	mgodb.FindAll(thumbnailsCollectionName, bson.M{}, &thumbs)
	if len(thumbs) != 1 {
		t.Errorf("%+v", thumbs)
	}
}