Ejemplo n.º 1
0
func TestAddEditDeleteUserPostComment(t *testing.T) {
	postList := *me.Postlist(nerdz.PostlistOptions{N: 1})
	existingPost := postList[0].(*nerdz.UserPost)

	var comment nerdz.UserPostComment
	comment.Message = "Nice <html>"
	comment.Hpid = existingPost.Hpid

	if err := me.Add(&comment); err != nil {
		t.Fatalf("Add failed: %s", err)
	}

	comment.Message = "LOL EDIT"

	// Should fail, because of flood limits
	if err := me.Edit(&comment); err == nil {
		t.Fatalf("Edit should fail, but succeded")
	}

	// Wait 5 second to avoid flood limit (db side)
	time.Sleep(6000 * time.Millisecond)
	if err := me.Edit(&comment); err != nil {
		t.Fatalf("Edit comment failed with error: %s", err)
	}

	if err := me.Delete(&comment); err != nil {
		t.Fatalf("Delete comment with hcid %v shoud work, but got error: %v", comment.Hcid, err)
	}
}
Ejemplo n.º 2
0
func TestAddEditDeleteUserPostComment(t *testing.T) {
	existingPost := me.Postlist(&nerdz.PostlistOptions{N: 1}).([]nerdz.UserPost)[0]

	var comment nerdz.UserPostComment
	comment.Message = "Nice <html>"
	comment.Hpid = existingPost.Hpid

	if err := me.Add(&comment); err != nil {
		t.Errorf("Add failed: %s", err)
	}

	comment.Message = "LOL EDIT"
	// Wait 5 second to avoid flood limit (db side)
	time.Sleep(5000 * time.Millisecond)
	if err := me.Edit(&comment); err != nil {
		t.Errorf("Edit comment failed with error: %s", err)
	}

	if err := me.Delete(&comment); err != nil {
		t.Errorf("Delete comment with hcid %v shoud work, but got error: %v", comment.Hcid, err)
	}
}
Ejemplo n.º 3
0
// NewPostComment handles the request and creates a new post
func NewPostComment() echo.HandlerFunc {

	// swagger:route POST /users/{id}/posts/{pid}/comments user post comment NewUserPostComment
	//
	// Creates a new post on the specified user board
	//
	// Consumes:
	// - application/json
	//
	//	Produces:
	//	- application/json
	//
	//	Security:
	//		oauth: profile_comments:write
	//
	//	Responses:
	//		default: apiResponse

	return func(c echo.Context) error {
		if !rest.IsGranted("profile_comments:write", c) {
			return rest.InvalidScopeResponse("profile_comments:write", c)
		}

		// Read a rest.NewMessage from the body request.
		message := rest.NewMessage{}
		if err := c.Bind(&message); err != nil {
			errstr := err.Error()
			c.JSON(http.StatusBadRequest, &rest.Response{
				Data:         nil,
				HumanMessage: errstr,
				Message:      errstr,
				Status:       http.StatusBadRequest,
				Success:      false,
			})
			return errors.New(errstr)
		}

		// Create a nerdz.UserPostComment from the message
		// and current context.
		comment := nerdz.UserPostComment{}
		comment.Message = message.Message
		comment.Lang = message.Lang
		comment.To = c.Get("other").(*nerdz.User).ID()
		comment.Hpid = c.Get("post").(*nerdz.UserPost).ID()

		// Send it
		me := c.Get("me").(*nerdz.User)
		if err := me.Add(&comment); err != nil {
			errstr := err.Error()
			c.JSON(http.StatusBadRequest, &rest.Response{
				Data:         nil,
				HumanMessage: errstr,
				Message:      errstr,
				Status:       http.StatusBadRequest,
				Success:      false,
			})
			return errors.New(errstr)
		}
		// Extract the TO from the new post and return
		// selected fields.
		return rest.SelectFields(comment.GetTO(me), c)
	}
}