github.com/merlinepedra/gophish1@v0.9.0/controllers/api/api_test.go (about)

     1  package api
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"os"
    10  	"testing"
    11  
    12  	"github.com/gophish/gophish/config"
    13  	"github.com/gophish/gophish/models"
    14  	"github.com/stretchr/testify/suite"
    15  )
    16  
    17  type APISuite struct {
    18  	suite.Suite
    19  	apiKey    string
    20  	config    *config.Config
    21  	apiServer *Server
    22  	admin     models.User
    23  }
    24  
    25  func (s *APISuite) SetupSuite() {
    26  	conf := &config.Config{
    27  		DBName:         "sqlite3",
    28  		DBPath:         ":memory:",
    29  		MigrationsPath: "../../db/db_sqlite3/migrations/",
    30  	}
    31  	err := models.Setup(conf)
    32  	if err != nil {
    33  		s.T().Fatalf("Failed creating database: %v", err)
    34  	}
    35  	s.config = conf
    36  	s.Nil(err)
    37  	// Get the API key to use for these tests
    38  	u, err := models.GetUser(1)
    39  	s.Nil(err)
    40  	s.apiKey = u.ApiKey
    41  	s.admin = u
    42  	// Move our cwd up to the project root for help with resolving
    43  	// static assets
    44  	err = os.Chdir("../")
    45  	s.Nil(err)
    46  	s.apiServer = NewServer()
    47  }
    48  
    49  func (s *APISuite) TearDownTest() {
    50  	campaigns, _ := models.GetCampaigns(1)
    51  	for _, campaign := range campaigns {
    52  		models.DeleteCampaign(campaign.Id)
    53  	}
    54  	// Cleanup all users except the original admin
    55  	users, _ := models.GetUsers()
    56  	for _, user := range users {
    57  		if user.Id == 1 {
    58  			continue
    59  		}
    60  		err := models.DeleteUser(user.Id)
    61  		s.Nil(err)
    62  	}
    63  }
    64  
    65  func (s *APISuite) SetupTest() {
    66  	// Add a group
    67  	group := models.Group{Name: "Test Group"}
    68  	group.Targets = []models.Target{
    69  		models.Target{BaseRecipient: models.BaseRecipient{Email: "test1@example.com", FirstName: "First", LastName: "Example"}},
    70  		models.Target{BaseRecipient: models.BaseRecipient{Email: "test2@example.com", FirstName: "Second", LastName: "Example"}},
    71  	}
    72  	group.UserId = 1
    73  	models.PostGroup(&group)
    74  
    75  	// Add a template
    76  	t := models.Template{Name: "Test Template"}
    77  	t.Subject = "Test subject"
    78  	t.Text = "Text text"
    79  	t.HTML = "<html>Test</html>"
    80  	t.UserId = 1
    81  	models.PostTemplate(&t)
    82  
    83  	// Add a landing page
    84  	p := models.Page{Name: "Test Page"}
    85  	p.HTML = "<html>Test</html>"
    86  	p.UserId = 1
    87  	models.PostPage(&p)
    88  
    89  	// Add a sending profile
    90  	smtp := models.SMTP{Name: "Test Page"}
    91  	smtp.UserId = 1
    92  	smtp.Host = "example.com"
    93  	smtp.FromAddress = "test@test.com"
    94  	models.PostSMTP(&smtp)
    95  
    96  	// Setup and "launch" our campaign
    97  	// Set the status such that no emails are attempted
    98  	c := models.Campaign{Name: "Test campaign"}
    99  	c.UserId = 1
   100  	c.Template = t
   101  	c.Page = p
   102  	c.SMTP = smtp
   103  	c.Groups = []models.Group{group}
   104  	models.PostCampaign(&c, c.UserId)
   105  	c.UpdateStatus(models.CampaignEmailsSent)
   106  }
   107  
   108  func (s *APISuite) TestSiteImportBaseHref() {
   109  	h := "<html><head></head><body><img src=\"/test.png\"/></body></html>"
   110  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   111  		fmt.Fprintln(w, h)
   112  	}))
   113  	hr := fmt.Sprintf("<html><head><base href=\"%s\"/></head><body><img src=\"/test.png\"/>\n</body></html>", ts.URL)
   114  	defer ts.Close()
   115  	req := httptest.NewRequest(http.MethodPost, "/api/import/site",
   116  		bytes.NewBuffer([]byte(fmt.Sprintf(`
   117  			{
   118  				"url" : "%s",
   119  				"include_resources" : false
   120  			}
   121  		`, ts.URL))))
   122  	req.Header.Set("Content-Type", "application/json")
   123  	response := httptest.NewRecorder()
   124  	s.apiServer.ImportSite(response, req)
   125  	cs := cloneResponse{}
   126  	err := json.NewDecoder(response.Body).Decode(&cs)
   127  	s.Nil(err)
   128  	s.Equal(cs.HTML, hr)
   129  }
   130  
   131  func TestAPISuite(t *testing.T) {
   132  	suite.Run(t, new(APISuite))
   133  }