-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathdevice_write_test.go
61 lines (50 loc) · 1.75 KB
/
device_write_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright © 2025 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package fosite_test
import (
"context"
"encoding/json"
"io"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
. "github.com/ory/fosite"
)
func TestWriteDeviceUserResponse(t *testing.T) {
oauth2 := &Fosite{Config: &Config{
DeviceAndUserCodeLifespan: time.Minute,
DeviceAuthTokenPollingInterval: time.Minute,
DeviceVerificationURL: "http://ory.sh",
}}
ctx := context.Background()
rw := httptest.NewRecorder()
ar := &DeviceRequest{}
resp := &DeviceResponse{}
resp.SetUserCode("AAAA")
resp.SetDeviceCode("BBBB")
resp.SetInterval(int(
oauth2.Config.GetDeviceAuthTokenPollingInterval(ctx).Round(time.Second).Seconds(),
))
resp.SetExpiresIn(int64(
oauth2.Config.GetDeviceAndUserCodeLifespan(ctx),
))
resp.SetVerificationURI(oauth2.Config.GetDeviceVerificationURL(ctx))
resp.SetVerificationURIComplete(
oauth2.Config.GetDeviceVerificationURL(ctx) + "?user_code=" + resp.GetUserCode(),
)
oauth2.WriteDeviceResponse(context.Background(), rw, ar, resp)
assert.Equal(t, 200, rw.Code)
body, err := io.ReadAll(rw.Body)
require.NoError(t, err)
wroteDeviceResponse := DeviceResponse{}
err = json.Unmarshal(body, &wroteDeviceResponse)
require.NoError(t, err)
assert.Equal(t, resp.GetUserCode(), wroteDeviceResponse.UserCode)
assert.Equal(t, resp.GetDeviceCode(), wroteDeviceResponse.DeviceCode)
assert.Equal(t, resp.GetVerificationURI(), wroteDeviceResponse.VerificationURI)
assert.Equal(t, resp.GetVerificationURIComplete(), wroteDeviceResponse.VerificationURIComplete)
assert.Equal(t, resp.GetInterval(), wroteDeviceResponse.Interval)
assert.Equal(t, resp.GetExpiresIn(), wroteDeviceResponse.ExpiresIn)
}