Beispiel #1
0
func GetSiteDetails(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var w site.Website
	var err error
	idStr := params["id"]
	id, err := strconv.Atoi(idStr)

	if err != nil {
		apierror.GenerateError("Trouble getting site ID", err, rw, req)
	}
	w.ID = id

	err = w.GetDetails(dtx)
	if err != nil {
		apierror.GenerateError("Trouble getting site details", err, rw, req)
	}
	return encoding.Must(enc.Encode(w))
}
Beispiel #2
0
func SaveSite(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params) string {
	var m site.Website
	var err error
	idStr := params["id"]
	if idStr != "" {
		m.ID, err = strconv.Atoi(idStr)
		err = m.Get()
		if err != nil {
			apierror.GenerateError("Trouble getting website", err, rw, req)
		}
	}

	//json
	requestBody, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError("Trouble reading request body for saving website", err, rw, req)
	}
	err = json.Unmarshal(requestBody, &m)
	if err != nil {
		apierror.GenerateError("Trouble unmarshalling request body for saving website", err, rw, req)
	}
	//create or update
	if m.ID > 0 {
		err = m.Update()
	} else {
		err = m.Create()
	}

	if err != nil {
		apierror.GenerateError("Trouble saving website", err, rw, req)
	}
	return encoding.Must(enc.Encode(m))
}
Beispiel #3
0
func DeleteSite(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params) string {
	var err error
	var m site.Website

	idStr := params["id"]

	id, err := strconv.Atoi(idStr)
	if err != nil {
		apierror.GenerateError("Trouble getting website ID", err, rw, req)
	}
	m.ID = id
	err = m.Delete()
	if err != nil {
		apierror.GenerateError("Trouble getting website", err, rw, req)
	}

	return encoding.Must(enc.Encode(m))
}
Beispiel #4
0
func TestSite(t *testing.T) {

	dtx, err := apicontextmock.Mock()
	if err != nil {
		t.Log(err)
	}

	Convey("Site Contents", t, func() {
		var c site.Content
		var contents site.Contents
		var cr site.ContentRevision
		c.WebsiteId = dtx.WebsiteID
		qs := make(url.Values, 0)
		qs.Add("key", dtx.APIKey)

		c.Title = "test content - controller test"
		cr.Text = "test revision - controller test"
		c.ContentRevisions = append(c.ContentRevisions, cr)

		response := httprunner.ParameterizedJsonRequest("POST", "/site/content", "/site/content", &qs, c, SaveContent)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

		c.Active = true
		response = httprunner.ParameterizedJsonRequest("PUT", "/site/content/:id", "/site/content/"+strconv.Itoa(c.Id), &qs, c, SaveContent)
		So(response.Code, ShouldEqual, 200)

		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/content/:id", "/site/content/"+strconv.Itoa(c.Id), &qs, nil, GetContent)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/content", "/site/content", &qs, nil, GetAllContents)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &contents), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/content/:id/revisions", "/site/content/"+strconv.Itoa(c.Id)+"/revisions", &qs, nil, GetContentRevisions)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("DELETE", "/site/content/:id", "/site/content/"+strconv.Itoa(c.Id), &qs, nil, DeleteContent)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &c), ShouldEqual, nil)

	})

	Convey("Site Menu", t, func() {
		var m site.Menu
		var menus site.Menus
		m.WebsiteId = dtx.WebsiteID
		qs := make(url.Values, 0)
		qs.Add("key", dtx.APIKey)

		m.Name = "test menu - controller test"

		response := httprunner.ParameterizedJsonRequest("POST", "/site/menu", "/site/menu", &qs, m, SaveMenu)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

		m.Active = true
		response = httprunner.ParameterizedJsonRequest("PUT", "/site/menu/:id", "/site/menu/"+strconv.Itoa(m.Id), &qs, m, SaveMenu)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/menu/:id", "/site/menu/"+strconv.Itoa(m.Id), &qs, nil, GetMenu)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/menu", "/site/menu", &qs, nil, GetAllMenus)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &menus), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/menu/contents/:id", "/site/menu/contents/"+strconv.Itoa(m.Id), &qs, nil, GetMenuWithContents)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("DELETE", "/site/menu/:id", "/site/menu/"+strconv.Itoa(m.Id), &qs, nil, DeleteMenu)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &m), ShouldEqual, nil)

	})

	Convey("Site Menu", t, func() {
		var s site.Website
		qs := make(url.Values, 0)
		qs.Add("key", dtx.APIKey)

		s.Description = "test site - controller test"

		response := httprunner.ParameterizedJsonRequest("POST", "/site", "/site", &qs, s, SaveSite)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &s), ShouldEqual, nil)

		s.Url = "www.controllertest.com"

		response = httprunner.ParameterizedJsonRequest("PUT", "/site/:id", "/site/"+strconv.Itoa(s.ID), &qs, s, SaveSite)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &s), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/site/:id/details", "/site/"+strconv.Itoa(s.ID)+"/details", &qs, nil, GetSiteDetails)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &s), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("DELETE", "/site/:id", "/site/"+strconv.Itoa(s.ID), &qs, nil, DeleteSite)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &s), ShouldEqual, nil)

	})

	_ = apicontextmock.DeMock(dtx)
}