Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/controller/issueapi/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/google/exposure-notifications-server/pkg/timeutils"
"github.com/google/exposure-notifications-verification-server/pkg/api"
"github.com/google/exposure-notifications-verification-server/pkg/controller"
"github.com/google/exposure-notifications-verification-server/pkg/database"
Expand Down Expand Up @@ -194,8 +195,8 @@ func (c *Controller) HandleIssue() http.Handler {
return
} else {
// Max date is today (UTC time) and min date is AllowedTestAge ago, truncated.
maxDate := time.Now().UTC().Truncate(24 * time.Hour)
minDate := maxDate.Add(-1 * c.config.GetAllowedSymptomAge()).Truncate(24 * time.Hour)
maxDate := timeutils.UTCMidnight(time.Now())
minDate := timeutils.Midnight(maxDate.Add(-1 * c.config.GetAllowedSymptomAge()))

symptomDate, err = validateDate(parsed, minDate, maxDate, int(request.TZOffset))
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/realmadmin/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
"testing"
"time"

"github.com/google/exposure-notifications-server/pkg/timeutils"
"github.com/google/exposure-notifications-verification-server/pkg/database"
)

func TestFormatStats(t *testing.T) {
now := time.Now().Truncate(24 * time.Hour)
yesterday := now.Add(-24 * time.Hour).Truncate(24 * time.Hour)
now := timeutils.Midnight(time.Now())
yesterday := timeutils.Midnight(now.Add(-24 * time.Hour))
tests := []struct {
data []*database.RealmUserStats
names []string
Expand Down
5 changes: 3 additions & 2 deletions pkg/database/authorized_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/google/exposure-notifications-server/pkg/base64util"
"github.com/google/exposure-notifications-server/pkg/timeutils"
"github.com/jinzhu/gorm"
)

Expand Down Expand Up @@ -205,8 +206,8 @@ func (db *Database) FindAuthorizedAppByAPIKey(apiKey string) (*AuthorizedApp, er
func (a *AuthorizedApp) Stats(db *Database, start, stop time.Time) ([]*AuthorizedAppStats, error) {
var stats []*AuthorizedAppStats

start = start.Truncate(24 * time.Hour)
stop = stop.Truncate(24 * time.Hour)
start = timeutils.UTCMidnight(start)
stop = timeutils.UTCMidnight(stop)

if err := db.db.
Model(&AuthorizedAppStats{}).
Expand Down
9 changes: 5 additions & 4 deletions pkg/database/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"
"time"

"github.com/google/exposure-notifications-server/pkg/timeutils"
"github.com/google/exposure-notifications-verification-server/pkg/digest"
"github.com/google/exposure-notifications-verification-server/pkg/sms"
"github.com/microcosm-cc/bluemonday"
Expand Down Expand Up @@ -1096,8 +1097,8 @@ func (r *Realm) DestroySigningKeyVersion(ctx context.Context, db *Database, id i
func (r *Realm) Stats(db *Database, start, stop time.Time) ([]*RealmStats, error) {
var stats []*RealmStats

start = start.Truncate(24 * time.Hour)
stop = stop.Truncate(24 * time.Hour)
start = timeutils.Midnight(start)
stop = timeutils.Midnight(stop)

if err := db.db.
Model(&RealmStats{}).
Expand Down Expand Up @@ -1183,8 +1184,8 @@ type RealmUserStats struct {

// CodesPerUser returns a set of UserStats for a given date range.
func (r *Realm) CodesPerUser(db *Database, start, stop time.Time) ([]*RealmUserStats, error) {
start = start.Truncate(time.Hour * 24)
stop = stop.Truncate(time.Hour * 24)
start = timeutils.UTCMidnight(start)
stop = timeutils.UTCMidnight(stop)
if start.After(stop) {
return nil, ErrBadDateRange
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/database/realm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package database
import (
"testing"
"time"

"github.com/google/exposure-notifications-server/pkg/timeutils"
)

func TestSMS(t *testing.T) {
Expand Down Expand Up @@ -44,8 +46,8 @@ func TestPerUserRealmStats(t *testing.T) {
db := NewTestDatabase(t)

numDays := 7
endDate := time.Now().Truncate(24 * time.Hour)
startDate := endDate.Add(time.Duration(numDays) * -24 * time.Hour).Truncate(24 * time.Hour)
endDate := timeutils.Midnight(time.Now())
startDate := timeutils.Midnight(endDate.Add(time.Duration(numDays) * -24 * time.Hour))

// Create a new realm
realm := NewRealmWithDefaults("test")
Expand Down
3 changes: 2 additions & 1 deletion pkg/database/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

"github.com/google/exposure-notifications-server/pkg/timeutils"
"github.com/google/exposure-notifications-verification-server/pkg/api"
"github.com/jinzhu/gorm"
)
Expand Down Expand Up @@ -199,7 +200,7 @@ func (db *Database) VerifyCodeAndIssueToken(realmID uint, verCode string, accept
}

// Update statistics
now := time.Now().Truncate(24 * time.Hour)
now := timeutils.Midnight(vc.CreatedAt)
sql := `
INSERT INTO realm_stats(date, realm_id, codes_claimed)
VALUES ($1, $2, 1)
Expand Down
3 changes: 2 additions & 1 deletion pkg/database/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/google/exposure-notifications-server/pkg/timeutils"
"github.com/google/exposure-notifications-verification-server/pkg/api"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestIssueToken(t *testing.T) {
t.Parallel()

codeAge := time.Hour
symptomDate := time.Now().UTC().Truncate(24 * time.Hour)
symptomDate := timeutils.UTCMidnight(time.Now())
wrongSymptomDate := symptomDate.Add(-48 * time.Hour)

acceptConfirmed := api.AcceptTypes{
Expand Down
5 changes: 3 additions & 2 deletions pkg/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"firebase.google.com/go/auth"
"github.com/google/exposure-notifications-server/pkg/timeutils"
"github.com/jinzhu/gorm"
"github.com/sethvargo/go-password/password"
)
Expand Down Expand Up @@ -204,8 +205,8 @@ func (db *Database) FindUserByEmail(email string) (*User, error) {
func (u *User) Stats(db *Database, realmID uint, start, stop time.Time) ([]*UserStats, error) {
var stats []*UserStats

start = start.Truncate(24 * time.Hour)
stop = stop.Truncate(24 * time.Hour)
start = timeutils.Midnight(start)
stop = timeutils.Midnight(stop)

if err := db.db.
Model(&UserStats{}).
Expand Down
4 changes: 2 additions & 2 deletions pkg/database/vercode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func TestStatDatesOnCreate(t *testing.T) {
{
var stats []*AuthorizedAppStats
if err := db.db.
Model(&UserStats{}).
Model(&AuthorizedAppStats{}).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it was a different bug?

Select("*").
Scan(&stats).
Error; err != nil {
Expand All @@ -386,7 +386,7 @@ func TestStatDatesOnCreate(t *testing.T) {
{
var stats []*RealmStats
if err := db.db.
Model(&UserStats{}).
Model(&RealmStats{}).
Select("*").
Scan(&stats).
Error; err != nil {
Expand Down