@@ -10,11 +10,9 @@ import (
10
10
"github.com/stretchr/testify/assert"
11
11
"github.com/stretchr/testify/require"
12
12
13
- "github.com/coder/coder/v2/cli/clibase"
14
13
"github.com/coder/coder/v2/coderd/apikey"
15
14
"github.com/coder/coder/v2/coderd/database"
16
15
"github.com/coder/coder/v2/coderd/database/dbtime"
17
- "github.com/coder/coder/v2/codersdk"
18
16
)
19
17
20
18
func TestGenerate (t * testing.T ) {
@@ -30,69 +28,80 @@ func TestGenerate(t *testing.T) {
30
28
{
31
29
name : "OK" ,
32
30
params : apikey.CreateParams {
33
- UserID : uuid .New (),
34
- LoginType : database .LoginTypeOIDC ,
35
- DeploymentValues : & codersdk. DeploymentValues {} ,
36
- ExpiresAt : time .Now ().Add (time .Hour ),
37
- LifetimeSeconds : int64 (time .Hour .Seconds ()),
38
- TokenName : "hello" ,
39
- RemoteAddr : "1.2.3.4" ,
40
- Scope : database .APIKeyScopeApplicationConnect ,
31
+ UserID : uuid .New (),
32
+ LoginType : database .LoginTypeOIDC ,
33
+ DefaultLifetime : time . Duration ( 0 ) ,
34
+ ExpiresAt : time .Now ().Add (time .Hour ),
35
+ LifetimeSeconds : int64 (time .Hour .Seconds ()),
36
+ TokenName : "hello" ,
37
+ RemoteAddr : "1.2.3.4" ,
38
+ Scope : database .APIKeyScopeApplicationConnect ,
41
39
},
42
40
},
43
41
{
44
42
name : "InvalidScope" ,
45
43
params : apikey.CreateParams {
46
- UserID : uuid .New (),
47
- LoginType : database .LoginTypeOIDC ,
48
- DeploymentValues : & codersdk. DeploymentValues {} ,
49
- ExpiresAt : time .Now ().Add (time .Hour ),
50
- LifetimeSeconds : int64 (time .Hour .Seconds ()),
51
- TokenName : "hello" ,
52
- RemoteAddr : "1.2.3.4" ,
53
- Scope : database .APIKeyScope ("test" ),
44
+ UserID : uuid .New (),
45
+ LoginType : database .LoginTypeOIDC ,
46
+ DefaultLifetime : time . Duration ( 0 ) ,
47
+ ExpiresAt : time .Now ().Add (time .Hour ),
48
+ LifetimeSeconds : int64 (time .Hour .Seconds ()),
49
+ TokenName : "hello" ,
50
+ RemoteAddr : "1.2.3.4" ,
51
+ Scope : database .APIKeyScope ("test" ),
54
52
},
55
53
fail : true ,
56
54
},
57
55
{
58
56
name : "DeploymentSessionDuration" ,
59
57
params : apikey.CreateParams {
60
- UserID : uuid .New (),
61
- LoginType : database .LoginTypeOIDC ,
62
- DeploymentValues : & codersdk.DeploymentValues {
63
- SessionDuration : clibase .Duration (time .Hour ),
64
- },
58
+ UserID : uuid .New (),
59
+ LoginType : database .LoginTypeOIDC ,
60
+ DefaultLifetime : time .Hour ,
65
61
LifetimeSeconds : 0 ,
66
62
ExpiresAt : time.Time {},
67
63
TokenName : "hello" ,
68
64
RemoteAddr : "1.2.3.4" ,
69
65
Scope : database .APIKeyScopeApplicationConnect ,
70
66
},
71
67
},
68
+ {
69
+ name : "LifetimeSeconds" ,
70
+ params : apikey.CreateParams {
71
+ UserID : uuid .New (),
72
+ LoginType : database .LoginTypeOIDC ,
73
+ DefaultLifetime : time .Duration (0 ),
74
+ LifetimeSeconds : int64 (time .Hour .Seconds ()),
75
+ ExpiresAt : time.Time {},
76
+ TokenName : "hello" ,
77
+ RemoteAddr : "1.2.3.4" ,
78
+ Scope : database .APIKeyScopeApplicationConnect ,
79
+ },
80
+ },
72
81
{
73
82
name : "DefaultIP" ,
74
83
params : apikey.CreateParams {
75
- UserID : uuid .New (),
76
- LoginType : database .LoginTypeOIDC ,
77
- DeploymentValues : & codersdk. DeploymentValues {} ,
78
- ExpiresAt : time .Now ().Add (time .Hour ),
79
- LifetimeSeconds : int64 (time .Hour .Seconds ()),
80
- TokenName : "hello" ,
81
- RemoteAddr : "" ,
82
- Scope : database .APIKeyScopeApplicationConnect ,
84
+ UserID : uuid .New (),
85
+ LoginType : database .LoginTypeOIDC ,
86
+ DefaultLifetime : time . Duration ( 0 ) ,
87
+ ExpiresAt : time .Now ().Add (time .Hour ),
88
+ LifetimeSeconds : int64 (time .Hour .Seconds ()),
89
+ TokenName : "hello" ,
90
+ RemoteAddr : "" ,
91
+ Scope : database .APIKeyScopeApplicationConnect ,
83
92
},
84
93
},
85
94
{
86
95
name : "DefaultScope" ,
87
96
params : apikey.CreateParams {
88
- UserID : uuid .New (),
89
- LoginType : database .LoginTypeOIDC ,
90
- DeploymentValues : & codersdk. DeploymentValues {} ,
91
- ExpiresAt : time .Now ().Add (time .Hour ),
92
- LifetimeSeconds : int64 (time .Hour .Seconds ()),
93
- TokenName : "hello" ,
94
- RemoteAddr : "1.2.3.4" ,
95
- Scope : "" ,
97
+ UserID : uuid .New (),
98
+ LoginType : database .LoginTypeOIDC ,
99
+ DefaultLifetime : time . Duration ( 0 ) ,
100
+ ExpiresAt : time .Now ().Add (time .Hour ),
101
+ LifetimeSeconds : int64 (time .Hour .Seconds ()),
102
+ TokenName : "hello" ,
103
+ RemoteAddr : "1.2.3.4" ,
104
+ Scope : "" ,
96
105
},
97
106
},
98
107
}
@@ -131,15 +140,15 @@ func TestGenerate(t *testing.T) {
131
140
// Should not be a delta greater than 5 seconds.
132
141
assert .InDelta (t , time .Until (tc .params .ExpiresAt ).Seconds (), key .LifetimeSeconds , 5 )
133
142
} else {
134
- assert .Equal (t , int64 (tc .params .DeploymentValues . SessionDuration . Value () .Seconds ()), key .LifetimeSeconds )
143
+ assert .Equal (t , int64 (tc .params .DefaultLifetime .Seconds ()), key .LifetimeSeconds )
135
144
}
136
145
137
146
if ! tc .params .ExpiresAt .IsZero () {
138
147
assert .Equal (t , tc .params .ExpiresAt .UTC (), key .ExpiresAt )
139
148
} else if tc .params .LifetimeSeconds > 0 {
140
- assert .WithinDuration (t , dbtime .Now ().Add (time .Duration (tc .params .LifetimeSeconds )), key .ExpiresAt , time .Second * 5 )
149
+ assert .WithinDuration (t , dbtime .Now ().Add (time .Duration (tc .params .LifetimeSeconds )* time . Second ), key .ExpiresAt , time .Second * 5 )
141
150
} else {
142
- assert .WithinDuration (t , dbtime .Now ().Add (tc .params .DeploymentValues . SessionDuration . Value () ), key .ExpiresAt , time .Second * 5 )
151
+ assert .WithinDuration (t , dbtime .Now ().Add (tc .params .DefaultLifetime ), key .ExpiresAt , time .Second * 5 )
143
152
}
144
153
145
154
if tc .params .RemoteAddr != "" {
0 commit comments