Ejemplo n.º 1
0
// View handles pad render request
func (pc *Pad) View(w *json.Encoder, r *json.Decoder, h *jsonapi.HTTP) {
	res := new(Response)
	path := h.Request.URL.Path
	args := PathArg(path, "/api/pad/")
	if len(args) != 1 {
		log.Printf("Invalid path for /pad/pad: %s, %#v", path, args)
		res.Fail(path + " is invalid").Do(w)
		return
	}

	pid, err := strconv.Atoi(args[0])
	if err != nil {
		res.Fail(args[0] + " is not integer").Do(w)
		return
	}

	p, err := model.LoadPad(pid)
	if err != nil {
		log.Printf("Cannot load pad#%d from db: %s", pid, err)
		res.Failf("Cannot load pad#%d from database.", pid).Do(w)
		return
	}

	p.Sort()
	res.Ok(p).Do(w)
}
Ejemplo n.º 2
0
func realTestEdit(t *testing.T, pid int, tags []string, coops []int, cookie string) {
	pad, err := model.LoadPad(pid)
	if err != nil {
		t.Fatalf("Cannot load pad to edit: %s", err)
	}
	t.Logf("Test pad modification with tags %#v and coops %#v", tags, coops)
	ts := tags
	if tags == nil {
		ts = []string{}
	}
	c := coops
	if coops == nil {
		c = []int{}
	}
	randomTitle := fmt.Sprintf("randomTitle-%d", rand.Int())
	uri := fmt.Sprintf("/api/edit/%d", pad.ID)
	param := map[string]interface{}{
		"title":      randomTitle,
		"content":    pad.Content,
		"tags":       tags,
		"cooperator": coops,
		"version":    pad.Version,
	}

	resp, err := jsonapi.HandlerTest(pc().Edit).PostJSON(uri, cookie, param)
	if err != nil {
		t.Fatalf("Cannot save pad modification to db: %s", err)
	}
	if !testResult(resp.Body, true) {
		t.Errorf("/apd/edit returns false: %s", resp.Body.String())
	}
	actual, err := model.LoadPad(pad.ID)
	if err != nil {
		t.Fatalf("Cannot load just updated pad#%d: %s", pad.ID, err)
	}
	if !reflect.DeepEqual(actual.Tags, ts) {
		t.Errorf("Expected tags %v, got %v", tags, pad.Tags)
	}
	if !reflect.DeepEqual(actual.Cooperators, c) {
		t.Errorf("Expected coops %v, got %v", coops, pad.Cooperators)
	}

}
Ejemplo n.º 3
0
// Delete handles requests to delete a pad
func (pc *Pad) Delete(w *json.Encoder, r *json.Decoder, h *jsonapi.HTTP) {
	res := new(Response)
	u, err := Me(pc.SF.Get(h.Request))
	if err != nil {
		res.Err(1, "Not logged in").Do(w)
		return
	}

	args := PathArg(h.Request.URL.Path, "/api/delete/")
	if len(args) != 1 {
		res.Err(2, "No such pad").Do(w)
		return
	}

	pid, err := strconv.Atoi(args[0])
	if err != nil {
		res.Err(2, "No such pad").Do(w)
		return
	}

	p, err := model.LoadPad(pid)
	if err != nil {
		res.Err(2, "No such pad").Do(w)
		return
	}

	if p.UID != u.ID {
		res.Err(3, "Not owner").Do(w)
		return
	}

	if err := p.Delete(pc.DB); err != nil {
		log.Printf("Cannot delete pad#%d: %s", p.ID, err)
		res.Err(4, "Cannot delete from database").Do(w)
		return
	}

	res.Ok(nil).Do(w)
}
Ejemplo n.º 4
0
func realTestCreate(t *testing.T, tags []string, coops []int) {
	t.Logf("Test pad creation with tags %v and coops %v", tags, coops)
	randomString := rand.Int()
	randomTitle := fmt.Sprintf("pm create-%d", randomString)
	param := map[string]interface{}{
		"title":      randomTitle,
		"content":    fmt.Sprintf("test pm create-%d", randomString),
		"tags":       tags,
		"cooperator": coops,
	}
	sess := session(pc().SF, t)
	sess.Login(pmTestUser)
	sess.Save()
	if err := sess.Err(); err != nil {
		t.Fatalf("Failed to login for creating pad: %s", err)
	}
	cookie := sess.Cookie()

	resp, err := jsonapi.HandlerTest(pc().Create).PostJSON("/api/create", cookie, param)
	if err != nil {
		t.Fatalf("While getting response of pad creation: %s", err)
	}

	if !testResult(resp.Body, true) {
		t.Fatalf("Returns error when creating pad: %s", resp.Body.String())
	}

	data, ok := testHasData(resp.Body, "id")
	if !ok {
		t.Fatalf("No pad returned from creating pad: %s", resp.Body.String())
	}

	var pid int
	switch v := data.(type) {
	case float64, float32:
		pid = int(reflect.ValueOf(v).Float())
	case int64, int32, int16, int8, int:
		pid = int(reflect.ValueOf(v).Int())
	default:
		t.Fatalf("returned pid is not integer: %#v", data)
	}

	pad, err := model.LoadPad(pid)
	if err != nil {
		t.Fatalf("Failed to save created pad into db: %s", err)
	}
	if pad.Title != randomTitle {
		t.Errorf("Expected title [%s], got [%s]", randomTitle, pad.Title)
	}
	ts := tags
	if tags == nil {
		ts = []string{}
	}
	if !reflect.DeepEqual(pad.Tags, ts) {
		t.Errorf("Expected tags %v, got %v", tags, pad.Tags)
	}
	c := coops
	if coops == nil {
		c = []int{}
	}
	if !reflect.DeepEqual(pad.Cooperators, c) {
		t.Errorf("Expected coops %v, got %v", coops, pad.Cooperators)
	}
}
Ejemplo n.º 5
0
// Edit handles requests to edit a pad
func (pc *Pad) Edit(w *json.Encoder, r *json.Decoder, h *jsonapi.HTTP) {
	res := new(Response)
	u, err := Me(pc.SF.Get(h.Request))
	if err != nil {
		res.Err(1, "Not logged in").Do(w)
		return
	}

	args := PathArg(h.Request.URL.Path, "/api/edit/")
	if len(args) != 1 {
		res.Err(2, "No such pad").Do(w)
		return
	}

	pid, err := strconv.Atoi(args[0])
	if err != nil {
		res.Err(2, "No such pad").Do(w)
		return
	}

	p, err := model.LoadPad(pid)
	if err != nil {
		res.Err(2, "No such pad").Do(w)
		return
	}

	if p.UID != u.ID {
		var isCoop bool
		for _, c := range p.Cooperators {
			if c == u.ID {
				isCoop = true
				break
			}
		}
		if !isCoop {
			res.Err(3, "Not cooperator").Do(w)
			return

		}
	}

	var data EditRequest
	if err := r.Decode(&data); err != nil {
		res.Err(4, "Data error").Do(w)
		return
	}

	p.Title = data.Title
	p.Content = data.Content
	p.Tags = data.Tags
	p.Cooperators = data.Coops
	p.Version = data.Version

	if p.UID != u.ID && p.CoopModified() {
		res.Err(3, "Not owner").Do(w)
		return
	}

	err = p.Save(pc.DB)
	if err != nil {
		switch v := err.(type) {
		case model.VersionError:
			res.Err(5, "Version mismatch").Do(w)
		default:
			log.Printf("Error saving pad#%d: %s", p.ID, v)
			res.Err(4, "Unable to save pad into db").Do(w)
		}
		return
	}

	res.Ok(map[string]int{"code": 0}).Do(w)
}