From ba11f6428cddad38caba6242239ae465270a9eaf Mon Sep 17 00:00:00 2001 From: Jordan Wright Date: Tue, 25 Aug 2015 21:03:12 -0500 Subject: [PATCH] Can now add/delete pages Fixed test for importing a site - Now actually performs the right test. --- controllers/api.go | 3 ++- controllers/api_test.go | 8 +++++--- static/js/app/landing_pages.js | 23 +++++++++++++---------- static/js/app/users.js | 3 +-- static/js/gophish.js | 20 ++++++++++---------- templates/landing_pages.html | 8 +++----- 6 files changed, 34 insertions(+), 31 deletions(-) diff --git a/controllers/api.go b/controllers/api.go index d281d032..92ce6484 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -283,6 +283,7 @@ func API_Pages(w http.ResponseWriter, r *http.Request) { JSONResponse(w, models.Response{Success: false, Message: "Invalid request"}, http.StatusBadRequest) return } + // Check to make sure the name is unique _, err = models.GetPageByName(p.Name, ctx.Get(r, "user_id").(int64)) if err != gorm.RecordNotFound { JSONResponse(w, models.Response{Success: false, Message: "Page name already in use"}, http.StatusConflict) @@ -293,7 +294,7 @@ func API_Pages(w http.ResponseWriter, r *http.Request) { p.UserId = ctx.Get(r, "user_id").(int64) err = models.PostPage(&p) if err != nil { - JSONResponse(w, models.Response{Success: false, Message: "Error inserting page"}, http.StatusInternalServerError) + JSONResponse(w, models.Response{Success: false, Message: err.Error()}, http.StatusInternalServerError) return } JSONResponse(w, p, http.StatusCreated) diff --git a/controllers/api_test.go b/controllers/api_test.go index 79169704..d3ec75a7 100644 --- a/controllers/api_test.go +++ b/controllers/api_test.go @@ -2,8 +2,8 @@ package controllers import ( "bytes" + "encoding/json" "fmt" - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -45,6 +45,7 @@ func (s *ControllersSuite) TestSiteImportBaseHref() { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, h) })) + hr := fmt.Sprintf("\n", ts.URL) defer ts.Close() resp, err := http.Post(fmt.Sprintf("%s/api/import/site?api_key=%s", as.URL, s.ApiKey), "application/json", bytes.NewBuffer([]byte(fmt.Sprintf(` @@ -55,9 +56,10 @@ func (s *ControllersSuite) TestSiteImportBaseHref() { `, ts.URL)))) s.Nil(err) defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + cs := cloneResponse{} + err = json.NewDecoder(resp.Body).Decode(&cs) s.Nil(err) - fmt.Printf("%s", body) + s.Equal(cs.HTML, hr) } func (s *ControllersSuite) TearDownSuite() { diff --git a/static/js/app/landing_pages.js b/static/js/app/landing_pages.js index 849b3004..d14a0188 100644 --- a/static/js/app/landing_pages.js +++ b/static/js/app/landing_pages.js @@ -19,7 +19,7 @@ function save(idx){ }) } else { // Submit the page - api.landing_pages.post(page) + api.pages.post(page) .success(function(data){ successFlash("Page added successfully!") load() @@ -37,9 +37,9 @@ function dismiss(){ $("#html_editor").val("") } -function deleteTemplate(idx){ +function deletePage(idx){ if (confirm("Delete " + pages[idx].name + "?")){ - api.landing_pageId.delete(pages[idx].id) + api.pageId.delete(pages[idx].id) .success(function(data){ successFlash(data.message) load() @@ -79,22 +79,25 @@ function edit(idx){ } function load(){ +/* + load() - Loads the current pages using the API +*/ $("#pagesTable").hide() $("#emptyMessage").hide() $("#loading").show() - api.landing_pages.get() + api.pages.get() .success(function(ps){ pages = ps $("#loading").hide() if (pages.length > 0){ $("#pagesTable").show() - pagesTable = $("#templateTable").DataTable(); + pagesTable = $("#pagesTable").DataTable(); pagesTable.clear() $.each(pages, function(i, page){ pagesTable.row.add([ page.name, moment(page.modified_date).format('MMMM Do YYYY, h:mm:ss a'), - "
\