From 0409484bd499812610ff4fe3fe133f6be113cf67 Mon Sep 17 00:00:00 2001
From: MM53 <28218664+MM53@users.noreply.github.com>
Date: Mon, 25 Sep 2023 16:50:17 +0200
Subject: [PATCH 1/4] fix: Add missing type conversion when reading device
 requests from etcd

Signed-off-by: MM53 <28218664+MM53@users.noreply.github.com>
---
 storage/etcd/etcd.go  |  7 +++++--
 storage/etcd/types.go | 11 +++++++++++
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/storage/etcd/etcd.go b/storage/etcd/etcd.go
index 13e815ec8d..0343ade96d 100644
--- a/storage/etcd/etcd.go
+++ b/storage/etcd/etcd.go
@@ -577,8 +577,11 @@ func (c *conn) CreateDeviceRequest(d storage.DeviceRequest) error {
 func (c *conn) GetDeviceRequest(userCode string) (r storage.DeviceRequest, err error) {
 	ctx, cancel := context.WithTimeout(context.Background(), defaultStorageTimeout)
 	defer cancel()
-	err = c.getKey(ctx, keyID(deviceRequestPrefix, userCode), &r)
-	return r, err
+	var dr DeviceRequest
+	if err = c.getKey(ctx, keyID(deviceRequestPrefix, userCode), &dr); err == nil {
+		r = toStorageDeviceRequest(dr)
+	}
+	return
 }
 
 func (c *conn) listDeviceRequests(ctx context.Context) (requests []DeviceRequest, err error) {
diff --git a/storage/etcd/types.go b/storage/etcd/types.go
index 91199ab655..b945376e41 100644
--- a/storage/etcd/types.go
+++ b/storage/etcd/types.go
@@ -277,6 +277,17 @@ func fromStorageDeviceRequest(d storage.DeviceRequest) DeviceRequest {
 	}
 }
 
+func toStorageDeviceRequest(d DeviceRequest) storage.DeviceRequest {
+	return storage.DeviceRequest{
+		UserCode:     d.UserCode,
+		DeviceCode:   d.DeviceCode,
+		ClientID:     d.ClientID,
+		ClientSecret: d.ClientSecret,
+		Scopes:       d.Scopes,
+		Expiry:       d.Expiry,
+	}
+}
+
 // DeviceToken is a mirrored struct from storage with JSON struct tags
 type DeviceToken struct {
 	DeviceCode          string    `json:"device_code"`

From 441973c8181d292c9a0fc632f897256a0badaec8 Mon Sep 17 00:00:00 2001
From: MM53 <28218664+MM53@users.noreply.github.com>
Date: Mon, 25 Sep 2023 16:51:42 +0200
Subject: [PATCH 2/4] Extend conformance tests to verify stored device requests
 match the input

Signed-off-by: MM53 <28218664+MM53@users.noreply.github.com>
---
 storage/conformance/conformance.go | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/storage/conformance/conformance.go b/storage/conformance/conformance.go
index 71a2e181e8..16712a64de 100644
--- a/storage/conformance/conformance.go
+++ b/storage/conformance/conformance.go
@@ -8,6 +8,7 @@ import (
 	"time"
 
 	"github.com/kylelemons/godebug/pretty"
+	"github.com/stretchr/testify/require"
 	"golang.org/x/crypto/bcrypt"
 	jose "gopkg.in/square/go-jose.v2"
 
@@ -991,6 +992,13 @@ func testDeviceRequestCRUD(t *testing.T, s storage.Storage) {
 	err := s.CreateDeviceRequest(d1)
 	mustBeErrAlreadyExists(t, "device request", err)
 
+	got, err := s.GetDeviceRequest(d1.UserCode)
+	if err != nil {
+		t.Fatalf("failed to get device request: %v", err)
+	}
+
+	require.Equal(t, d1, got)
+
 	// No manual deletes for device requests, will be handled by garbage collection routines
 	// see testGC
 }

From d55f780393646e4daafbe9f47eed880b845dbe6d Mon Sep 17 00:00:00 2001
From: MM53 <28218664+MM53@users.noreply.github.com>
Date: Tue, 26 Sep 2023 12:58:16 +0200
Subject: [PATCH 3/4] Round expiry to next second to avoid mismatches in tests

Signed-off-by: MM53 <28218664+MM53@users.noreply.github.com>
---
 storage/conformance/conformance.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/storage/conformance/conformance.go b/storage/conformance/conformance.go
index 16712a64de..2f791cdee4 100644
--- a/storage/conformance/conformance.go
+++ b/storage/conformance/conformance.go
@@ -981,7 +981,7 @@ func testDeviceRequestCRUD(t *testing.T, s storage.Storage) {
 		ClientID:     "client1",
 		ClientSecret: "secret1",
 		Scopes:       []string{"openid", "email"},
-		Expiry:       neverExpire,
+		Expiry:       neverExpire.Round(time.Second),
 	}
 
 	if err := s.CreateDeviceRequest(d1); err != nil {

From 8e35c618a84cd89721324fde74e28f0a50a96f42 Mon Sep 17 00:00:00 2001
From: MM53 <28218664+MM53@users.noreply.github.com>
Date: Fri, 20 Oct 2023 08:46:28 +0200
Subject: [PATCH 4/4] Set postgres timezone to UTC

Signed-off-by: MM53 <28218664+MM53@users.noreply.github.com>
---
 .github/workflows/ci.yaml | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index c0a2e8f314..9417229709 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -16,12 +16,16 @@ jobs:
     services:
       postgres:
         image: postgres:10.8
+        env:
+          TZ: UTC
         ports:
           - 5432
         options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
 
       postgres-ent:
         image: postgres:10.8
+        env:
+          TZ: UTC
         ports:
           - 5432
         options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5