Пример #1
0
func TestEditPad(t *testing.T) {
	sess := session(pc().SF, t)
	sess.Login(pmTestUser)
	sess.Save()
	if err := sess.Err(); err != nil {
		t.Fatalf("Failed to login for modifing pad: %s", err)
	}
	cookie := sess.Cookie()
	pad, err := model.NewPad(db, pmTestUser.ID, "pm edit", "test pm edit", nil, nil)
	if err != nil {
		t.Fatalf("Cannot create pad for later modify: %s", err)
	}
	type params struct {
		t []string
		c []int
	}

	arr := []params{
		{t: []string{"tag1"}, c: nil},
		{t: nil, c: []int{pmCoop.ID}},
		{t: []string{}, c: []int{}},
		{t: []string{"tag1"}, c: []int{}},
		{t: []string{}, c: []int{pmCoop.ID}},
		{t: []string{"tag1"}, c: []int{pmCoop.ID}},
		{t: nil, c: nil},
		{t: nil, c: nil}, // this line tests if we did not modify anything.
	}
	for _, v := range arr {
		realTestEdit(t, pad.ID, v.t, v.c, cookie)
	}
}
Пример #2
0
// Create hadles requests to create pad
func (pc *Pad) Create(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
	}

	if !validEditor(pc.Config, u) {
		res.Err(3, "Not valid editor").Do(w)
		return
	}

	var data CreateRequest
	if err := r.Decode(&data); err != nil {
		log.Printf("Failed to parse postdata: %s", err)
		res.Err(2, "Data error").Do(w)
		return
	}

	p, err := model.NewPad(pc.DB, u.ID, data.Title, data.Content, data.Tags, data.Coops)
	if err != nil {
		log.Printf("Failed creating pad: %s", err)
		res.Err(2, "Database error").Do(w)
		return
	}

	resData := map[string]int{
		"code": 0,
		"id":   p.ID,
	}
	res.Ok(resData).Do(w)
}
Пример #3
0
func TestPadVersionMismatch(t *testing.T) {
	pad, err := model.NewPad(db, pvTestUser.ID, "pm version", "test pm version", nil, nil)
	if err != nil {
		t.Fatalf("Cannot create pad for version testing: %s", err)
	}
	sess := session(pc().SF, t)
	sess.Login(pvTestUser)
	sess.Save()
	if err := sess.Err(); err != nil {
		t.Fatalf("Failed to login for testing version: %s", err)
	}
	cookie := sess.Cookie()
	uri := fmt.Sprintf("/api/edit/%d", pad.ID)
	param := map[string]interface{}{
		"title":   "pm version modified",
		"content": "content",
		"version": pad.Version + 1,
	}

	fn := func() {
		resp, err := jsonapi.HandlerTest(pc().Edit).PostJSON(uri, cookie, param)
		if err != nil {
			t.Fatalf("Cannot run /api/edit for version test: %s", err)
		}

		if !testResult(resp.Body, false) {
			t.Errorf("Version (%d) mismatch but pad is updates: %s", param["version"].(int), resp.Body.String())
		}
		code, ok := testHasData(resp.Body, "code")
		if !ok {
			t.Errorf("Not returning code wher version mismatch: %s", resp.Body.String())
		}
		var c int
		switch v := code.(type) {
		case float32, float64:
			c = int(reflect.ValueOf(v).Float())
		case int64, int32, int16, int8, int:
			c = int(reflect.ValueOf(v).Int())
		default:
			t.Fatalf("Returned code is not integer: %#v", code)
		}
		if c != 5 {
			t.Errorf("Error code is not 5: %d", c)
		}
	}

	fn()
	param["version"] = pad.Version - 1
	fn()
}
Пример #4
0
func TestPads(t *testing.T) {
	p, err := model.NewPad(db, padTestUser.ID, "list pad", "test list pad", []string{}, []int{})
	if err != nil {
		t.Fatalf("Cannot create pad for testing list: %s", err)
	}

	resp, err := jsonapi.HandlerTest(pc().List).Get("/api/pads", "")
	if err != nil {
		t.Fatalf("While getting response of /api/pads: %s", err)
	}

	if !testResult(resp.Body, true) {
		t.Fatalf("/api/pads resurns an error: %s", resp.Body.String())
	}
	if !testArrayHas(p.ID, resp.Body) {
		t.Fatalf("Cannot find pad#%d in response of /api/pads: %s", p.ID, resp.Body.String())
	}
}
Пример #5
0
func TestPad(t *testing.T) {
	p, err := model.NewPad(db, padTestUser.ID, "pad", "test pad", []string{}, []int{})
	if err != nil {
		t.Fatalf("Cannot create pad for later testing: %s", err)
	}

	resp, err := jsonapi.HandlerTest(pc().View).Get(fmt.Sprintf("/api/pad/%d", p.ID), "")
	if err != nil {
		t.Fatalf("While getting response of /api/pad/%d: %s", p.ID, err)
	}

	if !testResult(resp.Body, true) {
		t.Fatalf("/api/pad/%d returns an error: %s", p.ID, resp.Body.String())
	}

	if !testData(p.ID, resp.Body, "id") {
		t.Fatalf("/api/pad/%d returns wrong data: %s", p.ID, resp.Body.String())
	}
}
Пример #6
0
func TestDeletePad(t *testing.T) {
	sess := session(pc().SF, t)
	sess.Login(pmTestUser)
	sess.Save()
	if err := sess.Err(); err != nil {
		t.Fatalf("Failed to login for deleting pad: %s", err)
	}
	cookie := sess.Cookie()
	pad, err := model.NewPad(db, pmTestUser.ID, "pm del", "test pm del", nil, nil)
	if err != nil {
		t.Fatalf("Cannot create pad for later deletion: %s", err)
	}

	resp, err := jsonapi.HandlerTest(pc().Delete).Get(fmt.Sprintf("/api/delete/%d", pad.ID), cookie)
	if err != nil {
		t.Fatalf("Error occured when delete pad: %s", err)
	}

	if !testResult(resp.Body, true) {
		t.Errorf("Failed to delete pad#%d: returned %s", pad.ID, resp.Body.String())
	}
}
Пример #7
0
func init() {
	var err error
	peAclUser, err = model.NewUser("test pe user", "*****@*****.**", "https://patrolavia.com/logo128.png")
	if err != nil {
		log.Fatalf("Cannot create user for testing pad edit acl: %s", err)
	}

	peAclCoop, err = model.NewUser("test pe coop", "*****@*****.**", "https://patrolavia.com/logo128.png")
	if err != nil {
		log.Fatalf("Cannot create coop for testing pad edit acl: %s", err)
	}

	peAclGuest, err = model.NewUser("test pe guest", "*****@*****.**", "https://patrolavia.com/logo128.png")
	if err != nil {
		log.Fatalf("Cannot create guest for testing pad edit acl: %s", err)
	}

	peAclPad, err = model.NewPad(db, peAclUser.ID, "pe acl", "test pe acl", nil, []int{peAclCoop.ID})
	if err != nil {
		log.Fatalf("Cannot create pad for testing pad edit act: %s", err)
	}

	peAclURI = fmt.Sprintf("/api/edit/%d", peAclPad.ID)
}