mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-03 08:50:42 +02:00
add test
This commit is contained in:
parent
37eb800950
commit
263aba50c1
2 changed files with 46 additions and 12 deletions
|
@ -60,16 +60,8 @@ func (ur *UsageReporter) Run(ctx context.Context, client databroker.DataBrokerSe
|
|||
}
|
||||
|
||||
func (ur *UsageReporter) report(ctx context.Context, records []usageReporterRecord) error {
|
||||
req := cluster.ReportUsageRequest{}
|
||||
for _, record := range records {
|
||||
u := cluster.ReportUsageUser{
|
||||
LastSignedInAt: record.lastSignedInAt,
|
||||
PseudonymousId: cryptutil.Pseudonymize(ur.organizationID, record.userID),
|
||||
}
|
||||
if record.userEmail != "" {
|
||||
u.PseudonymousEmail = cryptutil.Pseudonymize(ur.organizationID, record.userEmail)
|
||||
}
|
||||
req.Users = append(req.Users, u)
|
||||
req := cluster.ReportUsageRequest{
|
||||
Users: convertUsageReporterRecords(ur.organizationID, records),
|
||||
}
|
||||
return backoff.Retry(func() error {
|
||||
log.Debug(ctx).Int("updated-users", len(req.Users)).Msg("reporting usage")
|
||||
|
@ -181,6 +173,21 @@ func (ur *UsageReporter) onUpdateUser(u *user.User) {
|
|||
}
|
||||
}
|
||||
|
||||
func convertUsageReporterRecords(organizationID string, records []usageReporterRecord) []cluster.ReportUsageUser {
|
||||
var users []cluster.ReportUsageUser
|
||||
for _, record := range records {
|
||||
u := cluster.ReportUsageUser{
|
||||
LastSignedInAt: record.lastSignedInAt,
|
||||
PseudonymousId: cryptutil.Pseudonymize(organizationID, record.userID),
|
||||
}
|
||||
if record.userEmail != "" {
|
||||
u.PseudonymousEmail = cryptutil.Pseudonymize(organizationID, record.userEmail)
|
||||
}
|
||||
users = append(users, u)
|
||||
}
|
||||
return users
|
||||
}
|
||||
|
||||
func latest(t1, t2 time.Time) time.Time {
|
||||
if t2.After(t1) {
|
||||
return t2
|
||||
|
|
|
@ -1,7 +1,34 @@
|
|||
package usagereporter
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
func Test_report(t *testing.T) {
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/pomerium/pomerium/pkg/zero/cluster"
|
||||
)
|
||||
|
||||
func Test_convertUsageReporterRecords(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tm1 := time.Date(2024, time.September, 11, 11, 56, 0, 0, time.UTC)
|
||||
|
||||
assert.Empty(t, convertUsageReporterRecords("XXX", nil))
|
||||
assert.Equal(t, []cluster.ReportUsageUser{{
|
||||
LastSignedInAt: tm1,
|
||||
PseudonymousId: "T9V1yL/UueF/LVuF6XjoSNde0INElXG10zKepmyPke8=",
|
||||
PseudonymousEmail: "8w5rtnZyv0EGkpHmTlkmupgb1jCzn/IxGCfvpdGGnvI=",
|
||||
}}, convertUsageReporterRecords("XXX", []usageReporterRecord{{
|
||||
userID: "ID",
|
||||
userEmail: "EMAIL@example.com",
|
||||
lastSignedInAt: tm1,
|
||||
}}))
|
||||
assert.Equal(t, []cluster.ReportUsageUser{{
|
||||
LastSignedInAt: tm1,
|
||||
PseudonymousId: "T9V1yL/UueF/LVuF6XjoSNde0INElXG10zKepmyPke8=",
|
||||
}}, convertUsageReporterRecords("XXX", []usageReporterRecord{{
|
||||
userID: "ID",
|
||||
lastSignedInAt: tm1,
|
||||
}}), "should leave empty email")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue