Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Fix and test for regression for long code expiry #1511

Merged
merged 3 commits into from
Jan 5, 2021
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
2 changes: 1 addition & 1 deletion pkg/controller/codes/expire.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (c *Controller) HandleExpireAPI() http.Handler {
c.h.RenderJSON(w, http.StatusOK,
&api.ExpireCodeResponse{
ExpiresAtTimestamp: code.ExpiresAt.UTC().Unix(),
LongExpiresAtTimestamp: code.ExpiresAt.UTC().Unix(),
LongExpiresAtTimestamp: code.LongExpiresAt.UTC().Unix(),
})
})
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/controller/issueapi/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/google/exposure-notifications-verification-server/pkg/controller"
"github.com/google/exposure-notifications-verification-server/pkg/controller/issueapi"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-verification-server/pkg/sms"
)

func TestIssueOne(t *testing.T) {
Expand All @@ -40,14 +41,22 @@ func TestIssueOne(t *testing.T) {
}
ctx = controller.WithRealm(ctx, realm)

smsConfig := &database.SMSConfig{
RealmID: realm.ID,
ProviderType: sms.ProviderType(sms.ProviderTypeNoop),
}
if err := db.SaveSMSConfig(smsConfig); err != nil {
t.Fatal(err)
}

existingCode := &database.VerificationCode{
RealmID: realm.ID,
Code: "00000001",
LongCode: "00000001ABC",
Claimed: true,
TestType: "confirmed",
ExpiresAt: time.Now().Add(time.Hour),
LongExpiresAt: time.Now().Add(time.Hour),
LongExpiresAt: time.Now().Add(24 * time.Hour),
}
if err := db.SaveVerificationCode(existingCode, realm); err != nil {
t.Fatal(err)
Expand All @@ -65,6 +74,7 @@ func TestIssueOne(t *testing.T) {
request: api.IssueCodeRequest{
TestType: "confirmed",
SymptomDate: symptomDate,
Phone: "+15005550006",
},
httpStatusCode: http.StatusOK,
},
Expand Down Expand Up @@ -94,6 +104,10 @@ func TestIssueOne(t *testing.T) {
if resp.ErrorCode != tc.responseErr {
t.Errorf("did not receive expected errorCode. got %q, want %q", resp.ErrorCode, tc.responseErr)
}

if tc.responseErr == "" && tc.request.Phone != "" && resp.ExpiresAt == resp.LongExpiresAt {
t.Errorf("Long expiry should be longer than short when a phone is provided.")
}
})
}
}
3 changes: 2 additions & 1 deletion pkg/controller/issueapi/validate_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func (c *Controller) BuildVerificationCode(ctx context.Context, request *api.Iss
// Verify SMS configuration if phone was provided
var smsProvider sms.Provider
if request.Phone != "" {
smsProvider, err := realm.SMSProvider(c.db)
var err error
smsProvider, err = realm.SMSProvider(c.db)
if err != nil {
logger.Errorw("failed to get sms provider", "error", err)
return nil, &IssueResult{
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
var _ sql.Scanner = (*DurationSeconds)(nil)
var _ driver.Valuer = (*DurationSeconds)(nil)

// DurationSeconds is a custom type for writing and reating a time.Duration to be stored
// DurationSeconds is a custom type for writing and reading a time.Duration to be stored
// as seconds in the database.
type DurationSeconds struct {
Duration time.Duration
Expand Down