forked from canonical/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_write_test.go
55 lines (45 loc) · 1.72 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
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package fosite_test
import (
"context"
"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",
}}
rw := httptest.NewRecorder()
ar := &Request{}
resp := &DeviceResponse{}
resp.SetUserCode("AAAA")
resp.SetDeviceCode("BBBB")
resp.SetInterval(int(
oauth2.Config.GetDeviceAuthTokenPollingInterval(context.TODO()).Round(time.Second).Seconds(),
))
resp.SetExpiresIn(int64(
time.Now().Round(time.Second).Add(oauth2.Config.GetDeviceAndUserCodeLifespan(context.TODO())).Second(),
))
resp.SetVerificationURI(oauth2.Config.GetDeviceVerificationURL(context.TODO()))
resp.SetVerificationURIComplete(
oauth2.Config.GetDeviceVerificationURL(context.TODO()) + "?user_code=" + resp.GetUserCode(),
)
oauth2.WriteDeviceResponse(context.Background(), rw, ar, resp)
assert.Equal(t, 200, rw.Code)
wroteDeviceResponse := DeviceResponse{}
err := wroteDeviceResponse.FromJson(rw.Body)
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)
}