github.com/lunarobliq/gophish@v0.8.1-0.20230523153303-93511002234d/controllers/controllers_test.go (about)

     1  package controllers
     2  
     3  import (
     4  	"fmt"
     5  	"net/http/httptest"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/gophish/gophish/auth"
    11  	"github.com/gophish/gophish/config"
    12  	"github.com/gophish/gophish/models"
    13  )
    14  
    15  // testContext is the data required to test API related functions
    16  type testContext struct {
    17  	apiKey      string
    18  	config      *config.Config
    19  	adminServer *httptest.Server
    20  	phishServer *httptest.Server
    21  	origPath    string
    22  }
    23  
    24  func setupTest(t *testing.T) *testContext {
    25  	wd, _ := os.Getwd()
    26  	fmt.Println(wd)
    27  	conf := &config.Config{
    28  		DBName:         "sqlite3",
    29  		DBPath:         ":memory:",
    30  		MigrationsPath: "../db/db_sqlite3/migrations/",
    31  	}
    32  	abs, _ := filepath.Abs("../db/db_sqlite3/migrations/")
    33  	fmt.Printf("in controllers_test.go: %s\n", abs)
    34  	err := models.Setup(conf)
    35  	if err != nil {
    36  		t.Fatalf("error setting up database: %v", err)
    37  	}
    38  	ctx := &testContext{}
    39  	ctx.config = conf
    40  	ctx.adminServer = httptest.NewUnstartedServer(NewAdminServer(ctx.config.AdminConf).server.Handler)
    41  	ctx.adminServer.Config.Addr = ctx.config.AdminConf.ListenURL
    42  	ctx.adminServer.Start()
    43  	// Get the API key to use for these tests
    44  	u, err := models.GetUser(1)
    45  	// Reset the temporary password for the admin user to a value we control
    46  	hash, err := auth.GeneratePasswordHash("gophish")
    47  	u.Hash = hash
    48  	models.PutUser(&u)
    49  	if err != nil {
    50  		t.Fatalf("error getting first user from database: %v", err)
    51  	}
    52  
    53  	// Create a second user to test account locked status
    54  	u2 := models.User{Username: "houdini", Hash: hash, AccountLocked: true}
    55  	models.PutUser(&u2)
    56  	if err != nil {
    57  		t.Fatalf("error creating new user: %v", err)
    58  	}
    59  
    60  	ctx.apiKey = u.ApiKey
    61  	// Start the phishing server
    62  	ctx.phishServer = httptest.NewUnstartedServer(NewPhishingServer(ctx.config.PhishConf).server.Handler)
    63  	ctx.phishServer.Config.Addr = ctx.config.PhishConf.ListenURL
    64  	ctx.phishServer.Start()
    65  	// Move our cwd up to the project root for help with resolving
    66  	// static assets
    67  	origPath, _ := os.Getwd()
    68  	ctx.origPath = origPath
    69  	err = os.Chdir("../")
    70  	if err != nil {
    71  		t.Fatalf("error changing directories to setup asset discovery: %v", err)
    72  	}
    73  	createTestData(t)
    74  	return ctx
    75  }
    76  
    77  func tearDown(t *testing.T, ctx *testContext) {
    78  	// Tear down the admin and phishing servers
    79  	ctx.adminServer.Close()
    80  	ctx.phishServer.Close()
    81  	// Reset the path for the next test
    82  	os.Chdir(ctx.origPath)
    83  }
    84  
    85  func createTestData(t *testing.T) {
    86  	// Add a group
    87  	group := models.Group{Name: "Test Group"}
    88  	group.Targets = []models.Target{
    89  		models.Target{BaseRecipient: models.BaseRecipient{Email: "test1@example.com", FirstName: "First", LastName: "Example"}},
    90  		models.Target{BaseRecipient: models.BaseRecipient{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}},
    91  	}
    92  	group.UserId = 1
    93  	models.PostGroup(&group)
    94  
    95  	// Add a template
    96  	template := models.Template{Name: "Test Template"}
    97  	template.Subject = "Test subject"
    98  	template.Text = "Text text"
    99  	template.HTML = "<html>Test</html>"
   100  	template.UserId = 1
   101  	models.PostTemplate(&template)
   102  
   103  	// Add a landing page
   104  	p := models.Page{Name: "Test Page"}
   105  	p.HTML = "<html>Test</html>"
   106  	p.UserId = 1
   107  	models.PostPage(&p)
   108  
   109  	// Add a sending profile
   110  	smtp := models.SMTP{Name: "Test Page"}
   111  	smtp.UserId = 1
   112  	smtp.Host = "example.com"
   113  	smtp.FromAddress = "test@test.com"
   114  	models.PostSMTP(&smtp)
   115  
   116  	// Setup and "launch" our campaign
   117  	// Set the status such that no emails are attempted
   118  	c := models.Campaign{Name: "Test campaign"}
   119  	c.UserId = 1
   120  	c.Template = template
   121  	c.Page = p
   122  	c.SMTP = smtp
   123  	c.Groups = []models.Group{group}
   124  	models.PostCampaign(&c, c.UserId)
   125  	c.UpdateStatus(models.CampaignEmailsSent)
   126  }