gophish/util/util_test.go
Jordan Wright 47f0049c30
Refactor servers (#1321)
* Refactoring servers to support custom workers and graceful shutdown.
* Refactoring workers to support custom mailers.
* Refactoring mailer to be an interface, with proper instances instead of a single global instance
* Cleaning up a few things. Locking maillogs for campaigns set to launch immediately to prevent a race condition.
* Cleaning up API middleware to be simpler
* Moving template parameters to separate struct
* Changed LoadConfig to return config object
* Cleaned up some error handling, removing uninitialized global error in models package
* Changed static file serving to use the unindexed package
2018-12-15 15:42:32 -06:00

78 lines
1.7 KiB
Go

package util
import (
"bytes"
"fmt"
"mime/multipart"
"net/http"
"reflect"
"testing"
"github.com/gophish/gophish/config"
"github.com/gophish/gophish/models"
"github.com/stretchr/testify/suite"
)
type UtilSuite struct {
suite.Suite
}
func (s *UtilSuite) SetupSuite() {
conf := &config.Config{
DBName: "sqlite3",
DBPath: ":memory:",
MigrationsPath: "../db/db_sqlite3/migrations/",
}
err := models.Setup(conf)
if err != nil {
s.T().Fatalf("Failed creating database: %v", err)
}
s.Nil(err)
}
func buildCSVRequest(csvPayload string) (*http.Request, error) {
csvHeader := "First Name,Last Name,Email\n"
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("files[]", "example.csv")
if err != nil {
return nil, err
}
part.Write([]byte(csvHeader))
part.Write([]byte(csvPayload))
err = writer.Close()
if err != nil {
return nil, err
}
r, err := http.NewRequest("POST", "http://127.0.0.1", body)
if err != nil {
return nil, err
}
r.Header.Set("Content-Type", writer.FormDataContentType())
return r, nil
}
func (s *UtilSuite) TestParseCSVEmail() {
expected := models.Target{
BaseRecipient: models.BaseRecipient{
FirstName: "John",
LastName: "Doe",
Email: "johndoe@example.com",
},
}
csvPayload := fmt.Sprintf("%s,%s,<%s>", expected.FirstName, expected.LastName, expected.Email)
r, err := buildCSVRequest(csvPayload)
s.Nil(err)
got, err := ParseCSV(r)
s.Nil(err)
s.Equal(len(got), 1)
if !reflect.DeepEqual(expected, got[0]) {
s.T().Fatalf("Incorrect targets received. Expected: %#v\nGot: %#v", expected, got)
}
}
func TestUtilSuite(t *testing.T) {
suite.Run(t, new(UtilSuite))
}