func GetMenuWithContents(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string { var m site.Menu var err error idStr := params["id"] id, err := strconv.Atoi(idStr) if err == nil { //Thar be an Id int m.Id = id err = m.Get(dtx) } else { //Thar be a name m.Name = idStr err = m.GetByName(dtx) } if err != nil { apierror.GenerateError("Trouble getting site menu", err, rw, req) return "" } err = m.GetContents() if err != nil { apierror.GenerateError("Trouble getting site menu with contents", err, rw, req) return "" } return encoding.Must(enc.Encode(m)) }
func SaveMenu(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string { var m site.Menu var err error idStr := params["id"] if idStr != "" { m.Id, err = strconv.Atoi(idStr) err = m.Get(dtx) if err != nil { apierror.GenerateError("Trouble getting site menu ID", err, rw, req) } } //json requestBody, err := ioutil.ReadAll(req.Body) if err != nil { apierror.GenerateError("Trouble reading request body for saving site menu", err, rw, req) } err = json.Unmarshal(requestBody, &m) if err != nil { apierror.GenerateError("Trouble unmarshalling request body for saving site menu", err, rw, req) } //create or update if m.Id > 0 { err = m.Update() } else { err = m.Create() } if err != nil { apierror.GenerateError("Trouble saving site menu", err, rw, req) } return encoding.Must(enc.Encode(m)) }
func DeleteMenu(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string { var err error var m site.Menu idStr := params["id"] id, err := strconv.Atoi(idStr) if err != nil { apierror.GenerateError("Trouble getting site menu ID", err, rw, req) } m.Id = id err = m.Delete() if err != nil { apierror.GenerateError("Trouble deleting site menu", err, rw, req) } return encoding.Must(enc.Encode(m)) }
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) }