mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-10-13 18:50:47 +02:00
add tests to dashboard
This commit is contained in:
1 parent
7ad60ebecd
commit
e089e0cc7d
5 files changed
+161
-36
No files matched your search
@@ -250,11 +250,13 @@ func (c *Campaign) GetStats(g *gin.Context) {
|
||||
}
|
||||
// parse request
|
||||
companyID := companyIDFromRequestQuery(g)
|
||||
includeTestCampaigns := g.Query("includeTest") == "true"
|
||||
// get
|
||||
stats, err := c.CampaignService.GetStats(
|
||||
g.Request.Context(),
|
||||
session,
|
||||
companyID,
|
||||
includeTestCampaigns,
|
||||
)
|
||||
// handle responses
|
||||
if ok := c.handleErrors(g, err); !ok {
|
||||
@@ -303,6 +305,7 @@ func (c *Campaign) GetAllWithinDates(g *gin.Context) {
|
||||
}
|
||||
// parse request
|
||||
companyID := companyIDFromRequestQuery(g)
|
||||
includeTestCampaigns := g.Query("includeTest") == "true"
|
||||
queryArgs, ok := c.handleQueryArgs(g)
|
||||
if !ok {
|
||||
return
|
||||
@@ -330,6 +333,7 @@ func (c *Campaign) GetAllWithinDates(g *gin.Context) {
|
||||
&repository.CampaignOption{
|
||||
QueryArgs: queryArgs,
|
||||
WithCampaignTemplate: true,
|
||||
IncludeTestCampaigns: includeTestCampaigns,
|
||||
},
|
||||
)
|
||||
// handle responses
|
||||
@@ -349,6 +353,7 @@ func (c *Campaign) GetAllActive(g *gin.Context) {
|
||||
}
|
||||
// parse request
|
||||
companyID := companyIDFromRequestQuery(g)
|
||||
includeTestCampaigns := g.Query("includeTest") == "true"
|
||||
queryArgs, ok := c.handleQueryArgs(g)
|
||||
if !ok {
|
||||
return
|
||||
@@ -367,6 +372,7 @@ func (c *Campaign) GetAllActive(g *gin.Context) {
|
||||
QueryArgs: queryArgs,
|
||||
WithCompany: true,
|
||||
WithCampaignTemplate: true,
|
||||
IncludeTestCampaigns: includeTestCampaigns,
|
||||
},
|
||||
)
|
||||
// handle responses
|
||||
@@ -385,6 +391,7 @@ func (c *Campaign) GetAllUpcoming(g *gin.Context) {
|
||||
}
|
||||
// parse request
|
||||
companyID := companyIDFromRequestQuery(g)
|
||||
includeTestCampaigns := g.Query("includeTest") == "true"
|
||||
queryArgs, ok := c.handleQueryArgs(g)
|
||||
if !ok {
|
||||
return
|
||||
@@ -403,6 +410,7 @@ func (c *Campaign) GetAllUpcoming(g *gin.Context) {
|
||||
QueryArgs: queryArgs,
|
||||
WithCompany: true,
|
||||
WithCampaignTemplate: true,
|
||||
IncludeTestCampaigns: includeTestCampaigns,
|
||||
},
|
||||
)
|
||||
// handle responses
|
||||
@@ -421,6 +429,7 @@ func (c *Campaign) GetAllFinished(g *gin.Context) {
|
||||
}
|
||||
// parse request
|
||||
companyID := companyIDFromRequestQuery(g)
|
||||
includeTestCampaigns := g.Query("includeTest") == "true"
|
||||
queryArgs, ok := c.handleQueryArgs(g)
|
||||
if !ok {
|
||||
return
|
||||
@@ -439,6 +448,7 @@ func (c *Campaign) GetAllFinished(g *gin.Context) {
|
||||
QueryArgs: queryArgs,
|
||||
WithCompany: true,
|
||||
WithCampaignTemplate: true,
|
||||
IncludeTestCampaigns: includeTestCampaigns,
|
||||
},
|
||||
)
|
||||
// handle responses
|
||||
|
||||
@@ -59,6 +59,7 @@ type CampaignOption struct {
|
||||
WithRecipientGroupCount bool
|
||||
WithAllowDeny bool
|
||||
WithDenyPage bool
|
||||
IncludeTestCampaigns bool
|
||||
}
|
||||
|
||||
// CampaignEventOption is options for preloading
|
||||
@@ -74,6 +75,14 @@ type Campaign struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
// applyTestCampaignFilter conditionally applies the is_test filter based on options
|
||||
func (r *Campaign) applyTestCampaignFilter(db *gorm.DB, options *CampaignOption) *gorm.DB {
|
||||
if !options.IncludeTestCampaigns {
|
||||
db = db.Where("is_test = false")
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// load preloads the campaign repository
|
||||
func (r *Campaign) load(db *gorm.DB, options *CampaignOption) *gorm.DB {
|
||||
if options.WithCompany {
|
||||
@@ -435,8 +444,8 @@ func (r *Campaign) GetAllActive(
|
||||
if err != nil {
|
||||
return result, errs.Wrap(err)
|
||||
}
|
||||
// Filter out test campaigns
|
||||
db = db.Where("is_test = false")
|
||||
// Apply test campaign filter based on options
|
||||
db = r.applyTestCampaignFilter(db, options)
|
||||
|
||||
var dbCampaigns []database.Campaign
|
||||
res := db.
|
||||
@@ -486,8 +495,8 @@ func (r *Campaign) GetAllUpcoming(
|
||||
if err != nil {
|
||||
return result, errs.Wrap(err)
|
||||
}
|
||||
// Filter out test campaigns
|
||||
db = db.Where("is_test = false")
|
||||
// Apply test campaign filter based on options
|
||||
db = r.applyTestCampaignFilter(db, options)
|
||||
|
||||
var dbCampaigns []database.Campaign
|
||||
res := db.
|
||||
@@ -537,8 +546,8 @@ func (r *Campaign) GetAllFinished(
|
||||
if err != nil {
|
||||
return result, errs.Wrap(err)
|
||||
}
|
||||
// Filter out test campaigns
|
||||
db = db.Where("is_test = false")
|
||||
// Apply test campaign filter based on options
|
||||
db = r.applyTestCampaignFilter(db, options)
|
||||
|
||||
var dbCampaigns []database.Campaign
|
||||
res := db.
|
||||
@@ -860,8 +869,8 @@ func (r *Campaign) GetAllCampaignWithinDates(
|
||||
|
||||
var dbCampaigns []database.Campaign
|
||||
|
||||
// Filter out test campaigns
|
||||
db = db.Where("is_test = false")
|
||||
// Apply test campaign filter based on options
|
||||
db = r.applyTestCampaignFilter(db, options)
|
||||
|
||||
// Query campaigns that:
|
||||
// 1. Are self-managed (no send_start_at)
|
||||
@@ -1304,18 +1313,21 @@ func (r *Campaign) AnonymizeCampaignEventsByRecipientID(
|
||||
|
||||
// GetActiveCount get the number running campaigns
|
||||
// if no company ID is selected it gets the global count including all companies
|
||||
func (r *Campaign) GetActiveCount(ctx context.Context, companyID *uuid.UUID) (int64, error) {
|
||||
func (r *Campaign) GetActiveCount(ctx context.Context, companyID *uuid.UUID, includeTestCampaigns bool) (int64, error) {
|
||||
var c int64
|
||||
db := r.DB
|
||||
if companyID != nil {
|
||||
db = whereCompany(db, database.CAMPAIGN_TABLE, companyID)
|
||||
}
|
||||
|
||||
whereClause := "((send_start_at <= ? OR send_start_at IS NULL) AND closed_at IS NULL)"
|
||||
if !includeTestCampaigns {
|
||||
whereClause += " AND is_test = false"
|
||||
}
|
||||
|
||||
res := db.
|
||||
Model(&database.Campaign{}).
|
||||
Where(
|
||||
"((send_start_at <= ? OR send_start_at IS NULL) AND closed_at IS NULL AND is_test IS false)",
|
||||
utils.NowRFC3339UTC(),
|
||||
).
|
||||
Where(whereClause, utils.NowRFC3339UTC()).
|
||||
Count(&c)
|
||||
|
||||
return c, res.Error
|
||||
@@ -1323,17 +1335,21 @@ func (r *Campaign) GetActiveCount(ctx context.Context, companyID *uuid.UUID) (in
|
||||
|
||||
// GetUpcomingCount get the upcoming campaign count
|
||||
// if no company ID is selected it gets the global count including all companies
|
||||
func (r *Campaign) GetUpcomingCount(ctx context.Context, companyID *uuid.UUID) (int64, error) {
|
||||
func (r *Campaign) GetUpcomingCount(ctx context.Context, companyID *uuid.UUID, includeTestCampaigns bool) (int64, error) {
|
||||
var c int64
|
||||
db := r.DB
|
||||
if companyID != nil {
|
||||
db = whereCompany(db, database.CAMPAIGN_TABLE, companyID)
|
||||
}
|
||||
|
||||
whereClause := "((send_start_at > ?) AND closed_at IS NULL)"
|
||||
if !includeTestCampaigns {
|
||||
whereClause += " AND is_test = false"
|
||||
}
|
||||
|
||||
res := db.
|
||||
Model(&database.Campaign{}).
|
||||
Where(
|
||||
"((send_start_at > ?) AND closed_at IS NULL AND is_test IS false)", utils.NowRFC3339UTC(),
|
||||
).
|
||||
Where(whereClause, utils.NowRFC3339UTC()).
|
||||
Count(&c)
|
||||
|
||||
return c, res.Error
|
||||
@@ -1341,15 +1357,21 @@ func (r *Campaign) GetUpcomingCount(ctx context.Context, companyID *uuid.UUID) (
|
||||
|
||||
// GetFinishedCount get the finished campaign count
|
||||
// if no company ID is selected it gets the global count including all companies
|
||||
func (r *Campaign) GetFinishedCount(ctx context.Context, companyID *uuid.UUID) (int64, error) {
|
||||
func (r *Campaign) GetFinishedCount(ctx context.Context, companyID *uuid.UUID, includeTestCampaigns bool) (int64, error) {
|
||||
var c int64
|
||||
db := r.DB
|
||||
if companyID != nil {
|
||||
db = whereCompany(db, database.CAMPAIGN_TABLE, companyID)
|
||||
}
|
||||
|
||||
whereClause := "closed_at IS NOT NULL"
|
||||
if !includeTestCampaigns {
|
||||
whereClause += " AND is_test = false"
|
||||
}
|
||||
|
||||
res := db.
|
||||
Model(&database.Campaign{}).
|
||||
Where("closed_at IS NOT NULL AND is_test IS false").
|
||||
Where(whereClause).
|
||||
Count(&c)
|
||||
|
||||
return c, res.Error
|
||||
|
||||
@@ -736,6 +736,7 @@ func (c *Campaign) GetStats(
|
||||
ctx context.Context,
|
||||
session *model.Session,
|
||||
companyID *uuid.UUID,
|
||||
includeTestCampaigns bool,
|
||||
) (*model.CampaignsStatView, error) {
|
||||
ae := NewAuditEvent("Campaign.GetStats", session)
|
||||
if companyID != nil {
|
||||
@@ -752,15 +753,15 @@ func (c *Campaign) GetStats(
|
||||
return nil, errs.ErrAuthorizationFailed
|
||||
}
|
||||
// get stats
|
||||
active, err := c.CampaignRepository.GetActiveCount(ctx, companyID)
|
||||
active, err := c.CampaignRepository.GetActiveCount(ctx, companyID, includeTestCampaigns)
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
upcoming, err := c.CampaignRepository.GetUpcomingCount(ctx, companyID)
|
||||
upcoming, err := c.CampaignRepository.GetUpcomingCount(ctx, companyID, includeTestCampaigns)
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
finished, err := c.CampaignRepository.GetFinishedCount(ctx, companyID)
|
||||
finished, err := c.CampaignRepository.GetFinishedCount(ctx, companyID, includeTestCampaigns)
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user