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
3 changes: 2 additions & 1 deletion pkg/database/vercode.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strings"
"time"

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

Expand Down Expand Up @@ -73,7 +74,7 @@ func (VerificationCode) TableName() string {
// to update statistics about usage. If the executions fail, an error is logged
// but the transaction continues. This is called automatically by gorm.
func (v *VerificationCode) AfterCreate(scope *gorm.Scope) {
date := v.CreatedAt.Truncate(24 * time.Hour)
date := timeutils.Midnight(v.CreatedAt)

// If the issuer was a user, update the user stats for the day.
if v.IssuingUserID != 0 {
Expand Down
103 changes: 103 additions & 0 deletions pkg/database/vercode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,106 @@ func TestPurgeVerificationCodes(t *testing.T) {
t.Fatalf("purge record count mismatch, want: 2, got: %v", count)
}
}

func TestStatDatesOnCreate(t *testing.T) {
// Please note, this test is NOT exhaustive. A better engineer would try
// all dates, and a bunch of corner cases. This is intended as a
// smokescreen.
t.Parallel()
db := NewTestDatabase(t)
db.db.LogMode(true)
fmtString := "2006-01-02"
now := time.Now()
nowStr := now.Format(fmtString)
maxAge := time.Hour

tests := []struct {
code *VerificationCode
statDate string
}{
{
&VerificationCode{
Code: "111111",
LongCode: "111111",
TestType: "negative",
ExpiresAt: now.Add(time.Second),
LongExpiresAt: now.Add(time.Second),
IssuingUserID: 100, // need for RealmUserStats
IssuingAppID: 200, // need for AuthorizedAppStats
RealmID: 300, // need for RealmStats
},
nowStr},
}

for i, test := range tests {
if err := db.SaveVerificationCode(test.code, maxAge); err != nil {
t.Errorf("[%d] error saving code: %v", i, err)
}

{
var stats []*RealmUserStats
if err := db.db.
Model(&UserStats{}).
Select("*").
Scan(&stats).
Error; err != nil {
if IsNotFound(err) {
t.Fatalf("[%d] Error grabbing user stats %v", i, err)
}
}
if len(stats) != 1 {
t.Fatalf("[%d] expected one user stat", i)
}
if stats[0].CodesIssued != uint(i+1) {
t.Errorf("[%d] expected stat.CodesIssued = %d, expected %d", i, stats[0].CodesIssued, i+1)
}
if f := stats[0].Date.Format(fmtString); f != test.statDate {
t.Errorf("[%d] expected stat.Date = %s, expected %s", i, f, test.statDate)
}
}

{
var stats []*AuthorizedAppStats
if err := db.db.
Model(&UserStats{}).
Select("*").
Scan(&stats).
Error; err != nil {
if IsNotFound(err) {
t.Fatalf("[%d] Error grabbing app stats %v", i, err)
}
}
if len(stats) != 1 {
t.Fatalf("[%d] expected one user stat", i)
}
if stats[0].CodesIssued != uint(i+1) {
t.Errorf("[%d] expected stat.CodesIssued = %d, expected %d", i, stats[0].CodesIssued, i+1)
}
if f := stats[0].Date.Format(fmtString); f != test.statDate {
t.Errorf("[%d] expected stat.Date = %s, expected %s", i, f, test.statDate)
}
}

{
var stats []*RealmStats
if err := db.db.
Model(&UserStats{}).
Select("*").
Scan(&stats).
Error; err != nil {
if IsNotFound(err) {
t.Fatalf("[%d] Error grabbing realm stats %v", i, err)
}
}
if len(stats) != 1 {
t.Fatalf("[%d] expected one user stat", i)
}
if stats[0].CodesIssued != uint(i+1) {
t.Errorf("[%d] expected stat.CodesIssued = %d, expected %d", i, stats[0].CodesIssued, i+1)
}
if f := stats[0].Date.Format(fmtString); f != test.statDate {
t.Errorf("[%d] expected stat.Date = %s, expected %s", i, f, test.statDate)
}
}
}
}
35 changes: 35 additions & 0 deletions pkg/timeutils/midnight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package timeutils defines functions to close the gaps present in Golang's
// default implementation of Time.
package timeutils

import "time"

// LocalMidnight returns the local midnight of the given time.
func LocalMidnight(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
}

// UTCMidnight converts the given time to UTC, and returns the UTC time.
func UTCMidnight(t time.Time) time.Time {
t = t.UTC()
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC)
}

// Midnight returns the midnight of the given time.
func Midnight(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}