2019-03-27 03:19:18 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2020-02-02 03:44:50 +00:00
|
|
|
"fmt"
|
2019-03-27 03:19:18 +00:00
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2020-02-02 03:44:50 +00:00
|
|
|
"path/filepath"
|
2019-03-27 03:19:18 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-06-20 03:03:51 +00:00
|
|
|
"github.com/gophish/gophish/auth"
|
2019-03-27 03:19:18 +00:00
|
|
|
"github.com/gophish/gophish/config"
|
|
|
|
"github.com/gophish/gophish/models"
|
|
|
|
)
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
// testContext is the data required to test API related functions
|
|
|
|
type testContext struct {
|
2019-03-27 03:19:18 +00:00
|
|
|
apiKey string
|
|
|
|
config *config.Config
|
|
|
|
adminServer *httptest.Server
|
|
|
|
phishServer *httptest.Server
|
2020-02-02 03:44:50 +00:00
|
|
|
origPath string
|
2019-03-27 03:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
func setupTest(t *testing.T) *testContext {
|
|
|
|
wd, _ := os.Getwd()
|
|
|
|
fmt.Println(wd)
|
2019-03-27 03:19:18 +00:00
|
|
|
conf := &config.Config{
|
|
|
|
DBName: "sqlite3",
|
|
|
|
DBPath: ":memory:",
|
|
|
|
MigrationsPath: "../db/db_sqlite3/migrations/",
|
|
|
|
}
|
2020-02-02 03:44:50 +00:00
|
|
|
abs, _ := filepath.Abs("../db/db_sqlite3/migrations/")
|
|
|
|
fmt.Printf("in controllers_test.go: %s\n", abs)
|
2019-03-27 03:19:18 +00:00
|
|
|
err := models.Setup(conf)
|
|
|
|
if err != nil {
|
2020-02-02 03:44:50 +00:00
|
|
|
t.Fatalf("error setting up database: %v", err)
|
2019-03-27 03:19:18 +00:00
|
|
|
}
|
2020-02-02 03:44:50 +00:00
|
|
|
ctx := &testContext{}
|
|
|
|
ctx.config = conf
|
|
|
|
ctx.adminServer = httptest.NewUnstartedServer(NewAdminServer(ctx.config.AdminConf).server.Handler)
|
|
|
|
ctx.adminServer.Config.Addr = ctx.config.AdminConf.ListenURL
|
|
|
|
ctx.adminServer.Start()
|
2019-03-27 03:19:18 +00:00
|
|
|
// Get the API key to use for these tests
|
|
|
|
u, err := models.GetUser(1)
|
2020-06-20 03:03:51 +00:00
|
|
|
// Reset the temporary password for the admin user to a value we control
|
|
|
|
hash, err := auth.GeneratePasswordHash("gophish")
|
|
|
|
u.Hash = hash
|
|
|
|
models.PutUser(&u)
|
2020-02-02 03:44:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error getting first user from database: %v", err)
|
|
|
|
}
|
2020-12-07 14:56:05 +00:00
|
|
|
|
|
|
|
// Create a second user to test account locked status
|
|
|
|
u2 := models.User{Username: "houdini", Hash: hash, AccountLocked: true}
|
|
|
|
models.PutUser(&u2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating new user: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
ctx.apiKey = u.ApiKey
|
2019-03-27 03:19:18 +00:00
|
|
|
// Start the phishing server
|
2020-02-02 03:44:50 +00:00
|
|
|
ctx.phishServer = httptest.NewUnstartedServer(NewPhishingServer(ctx.config.PhishConf).server.Handler)
|
|
|
|
ctx.phishServer.Config.Addr = ctx.config.PhishConf.ListenURL
|
|
|
|
ctx.phishServer.Start()
|
2019-03-27 03:19:18 +00:00
|
|
|
// Move our cwd up to the project root for help with resolving
|
|
|
|
// static assets
|
2020-02-02 03:44:50 +00:00
|
|
|
origPath, _ := os.Getwd()
|
|
|
|
ctx.origPath = origPath
|
2019-03-27 03:19:18 +00:00
|
|
|
err = os.Chdir("../")
|
2020-02-02 03:44:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error changing directories to setup asset discovery: %v", err)
|
|
|
|
}
|
|
|
|
createTestData(t)
|
|
|
|
return ctx
|
2019-03-27 03:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
func tearDown(t *testing.T, ctx *testContext) {
|
|
|
|
// Tear down the admin and phishing servers
|
|
|
|
ctx.adminServer.Close()
|
|
|
|
ctx.phishServer.Close()
|
|
|
|
// Reset the path for the next test
|
|
|
|
os.Chdir(ctx.origPath)
|
2019-03-27 03:19:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 03:44:50 +00:00
|
|
|
func createTestData(t *testing.T) {
|
2019-03-27 03:19:18 +00:00
|
|
|
// Add a group
|
|
|
|
group := models.Group{Name: "Test Group"}
|
|
|
|
group.Targets = []models.Target{
|
2023-04-28 10:36:22 +00:00
|
|
|
{BaseRecipient: models.BaseRecipient{Email: "test1@example.com", FirstName: "First", LastName: "Example"}},
|
|
|
|
{
|
|
|
|
Id: 0,
|
|
|
|
BaseRecipient: models.BaseRecipient{Email: "test2@example.com", FirstName: "Second", LastName: "Example"},
|
|
|
|
},
|
2019-03-27 03:19:18 +00:00
|
|
|
}
|
|
|
|
group.UserId = 1
|
|
|
|
models.PostGroup(&group)
|
|
|
|
|
|
|
|
// Add a template
|
2020-02-02 03:44:50 +00:00
|
|
|
template := models.Template{Name: "Test Template"}
|
|
|
|
template.Subject = "Test subject"
|
|
|
|
template.Text = "Text text"
|
|
|
|
template.HTML = "<html>Test</html>"
|
|
|
|
template.UserId = 1
|
|
|
|
models.PostTemplate(&template)
|
2019-03-27 03:19:18 +00:00
|
|
|
|
|
|
|
// Add a landing page
|
|
|
|
p := models.Page{Name: "Test Page"}
|
|
|
|
p.HTML = "<html>Test</html>"
|
|
|
|
p.UserId = 1
|
|
|
|
models.PostPage(&p)
|
|
|
|
|
|
|
|
// Add a sending profile
|
|
|
|
smtp := models.SMTP{Name: "Test Page"}
|
|
|
|
smtp.UserId = 1
|
|
|
|
smtp.Host = "example.com"
|
|
|
|
smtp.FromAddress = "test@test.com"
|
|
|
|
models.PostSMTP(&smtp)
|
|
|
|
|
|
|
|
// Setup and "launch" our campaign
|
|
|
|
// Set the status such that no emails are attempted
|
|
|
|
c := models.Campaign{Name: "Test campaign"}
|
|
|
|
c.UserId = 1
|
2020-02-02 03:44:50 +00:00
|
|
|
c.Template = template
|
2019-03-27 03:19:18 +00:00
|
|
|
c.Page = p
|
|
|
|
c.SMTP = smtp
|
|
|
|
c.Groups = []models.Group{group}
|
|
|
|
models.PostCampaign(&c, c.UserId)
|
|
|
|
c.UpdateStatus(models.CampaignEmailsSent)
|
|
|
|
}
|