Exemplo n.º 1
0
func TestAddEditDeleteUserPost(t *testing.T) {
	var post nerdz.UserPost

	// New post on my board (To = 0)
	post.Message = "All right"
	if err := me.Add(&post); err != nil {
		t.Fatalf("Add user post should work but, got: %v", err)
	}

	if post.Language() != me.Language() {
		t.Fatalf("User language should have been used, but instead %v has", post.Language())
	}

	if err := me.Delete(&post); err != nil {
		t.Fatalf("Delete with hpid %v shoud work, but got error: %v", post.Hpid, err)
	}

	post.Message = "All right2"
	post.Lang = "en"

	if err := me.Add(&post); err != nil {
		t.Fatalf("Add with ID should work but, got: %v", err)
	}

	post.Message = "Post updated -> :D\nwow JA JA JA"
	post.Lang = "fu"
	// Language "fu" does not exists, this edit should fail
	if err := me.Edit(&post); err == nil {
		t.Fatalf("Edit post language and message not failed! - %v", err)
	}

	post.Lang = "de"
	if err := me.Edit(&post); err != nil {
		t.Fatalf("This edit shold work but got %s", err)
	}

	oldHpid := post.Hpid
	post.Hpid = 0 //default value for uint64
	if err := me.Delete(&post); err == nil {
		t.Fatalf("Delete with hpid 0 should fail")
	}

	post.Hpid = oldHpid
	if err := me.Delete(&post); err != nil {
		t.Fatalf("Delete a valid post should work")
	}

}
Exemplo n.º 2
0
// NewPost handles the request and creates a new post
func NewPost() echo.HandlerFunc {

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

	return func(c echo.Context) error {
		if !rest.IsGranted("profile_messages:write", c) {
			return rest.InvalidScopeResponse("profile_messages: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.UserPost from the message
		// and current context.
		post := nerdz.UserPost{}
		post.Message = message.Message
		post.Lang = message.Lang
		post.To = c.Get("other").(*nerdz.User).ID()

		// Send it
		me := c.Get("me").(*nerdz.User)
		if err := me.Add(&post); 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(post.GetTO(me), c)
	}
}