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

	// swagger:route POST /projects/{id}/posts project post NewProjectPost
	//
	// Creates a new post on the specified project board
	//
	// Consumes:
	// - application/json
	//
	//	Produces:
	//	- application/json
	//
	//	Security:
	//		oauth: project_messages:write
	//
	//	Responses:
	//		default: apiResponse

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

		// Read a rest.Message from the body request.
		message := rest.NewMessage{}
		if err := c.Bind(&message); err != nil {
			return err
		}

		// Create a nerdz.ProjectPost from the message
		// and current context.
		post := nerdz.ProjectPost{}
		post.Message = message.Message
		post.Lang = message.Lang
		post.To = c.Get("project").(*nerdz.Project).ID()

		// Send it
		me := c.Get("me").(*nerdz.User)
		if err := me.Add(&post); err != nil {
			return err
		}
		// Extract the TO from the new post and return
		// selected fields.
		return rest.SelectFields(post.GetTO(me), c)
	}
}
Ejemplo n.º 2
0
func TestAddEditDeleteProjectPost(t *testing.T) {
	var post nerdz.ProjectPost

	myProject := me.Projects()[0]
	post.To = myProject.Counter
	post.Message = "BEST ADMIN EVER :>\nHello!"
	post.Lang = "en"

	if err := me.Add(&post); err != nil {
		t.Fatalf("No errors should occur whie adding a post to a project of mine, but got: %v", err)
	}

	post.Message = "WORST ADMIN EVER :<\a <- some random character"
	if err := me.Edit(&post); err != nil {
		t.Fatalf("Project Post edit should work, but failed with error: %s\n", err)
	}

	if err := me.Delete(&post); err != nil {
		t.Fatalf("Delete failed with error: %s", err.Error())
	}
}