2017-09-24 22:50:58 +00:00
|
|
|
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() {
|
2018-12-15 21:42:32 +00:00
|
|
|
conf := &config.Config{
|
|
|
|
DBName: "sqlite3",
|
|
|
|
DBPath: ":memory:",
|
|
|
|
MigrationsPath: "../db/db_sqlite3/migrations/",
|
|
|
|
}
|
|
|
|
err := models.Setup(conf)
|
2017-09-24 22:50:58 +00:00
|
|
|
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{
|
2018-06-09 02:20:52 +00:00
|
|
|
BaseRecipient: models.BaseRecipient{
|
|
|
|
FirstName: "John",
|
|
|
|
LastName: "Doe",
|
|
|
|
Email: "johndoe@example.com",
|
|
|
|
},
|
2017-09-24 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|