2015-08-24 01:42:47 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-08-26 02:03:12 +00:00
|
|
|
"encoding/json"
|
2015-08-24 01:42:47 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gorilla/handlers"
|
2016-01-10 17:03:17 +00:00
|
|
|
"github.com/gophish/gophish/config"
|
|
|
|
"github.com/gophish/gophish/models"
|
2015-08-24 01:42:47 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ControllersSuite is a suite of tests to cover API related functions
|
|
|
|
type ControllersSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
ApiKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
// as is the Admin Server for our API calls
|
|
|
|
var as *httptest.Server = httptest.NewUnstartedServer(handlers.CombinedLoggingHandler(os.Stdout, CreateAdminRouter()))
|
|
|
|
|
|
|
|
func (s *ControllersSuite) SetupSuite() {
|
|
|
|
config.Conf.DBPath = ":memory:"
|
|
|
|
err := models.Setup()
|
|
|
|
if err != nil {
|
|
|
|
s.T().Fatalf("Failed creating database: %v", err)
|
|
|
|
}
|
|
|
|
s.Nil(err)
|
|
|
|
// Setup the admin server for use in testing
|
2016-01-17 16:45:13 +00:00
|
|
|
as.Config.Addr = config.Conf.AdminConf.ListenURL
|
2015-08-24 01:42:47 +00:00
|
|
|
as.Start()
|
|
|
|
// Get the API key to use for these tests
|
|
|
|
u, err := models.GetUser(1)
|
|
|
|
s.Nil(err)
|
|
|
|
s.ApiKey = u.ApiKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) TestSiteImportBaseHref() {
|
|
|
|
h := "<html><head></head><body><img src=\"/test.png\"/></body></html>"
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Fprintln(w, h)
|
|
|
|
}))
|
2015-08-26 02:03:12 +00:00
|
|
|
hr := fmt.Sprintf("<html><head><base href=\"%s\"/></head><body><img src=\"/test.png\"/>\n</body></html>", ts.URL)
|
2015-08-24 01:42:47 +00:00
|
|
|
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(`
|
|
|
|
{
|
|
|
|
"url" : "%s",
|
|
|
|
"include_resources" : false
|
|
|
|
}
|
|
|
|
`, ts.URL))))
|
|
|
|
s.Nil(err)
|
|
|
|
defer resp.Body.Close()
|
2015-08-26 02:03:12 +00:00
|
|
|
cs := cloneResponse{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&cs)
|
2015-08-24 01:42:47 +00:00
|
|
|
s.Nil(err)
|
2015-08-26 02:03:12 +00:00
|
|
|
s.Equal(cs.HTML, hr)
|
2015-08-24 01:42:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) TearDownSuite() {
|
|
|
|
// Tear down the admin server
|
|
|
|
as.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestControllerSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ControllersSuite))
|
|
|
|
}
|