@@ -10,11 +10,9 @@ import (
1010"github.com/stretchr/testify/assert"
1111"github.com/stretchr/testify/require"
1212
13- "github.com/coder/coder/v2/cli/clibase"
1413"github.com/coder/coder/v2/coderd/apikey"
1514"github.com/coder/coder/v2/coderd/database"
1615"github.com/coder/coder/v2/coderd/database/dbtime"
17- "github.com/coder/coder/v2/codersdk"
1816)
1917
2018func TestGenerate (t * testing.T ) {
@@ -30,69 +28,80 @@ func TestGenerate(t *testing.T) {
3028{
3129name :"OK" ,
3230params : 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 ,
4139},
4240},
4341{
4442name :"InvalidScope" ,
4543params : 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" ),
5452},
5553fail :true ,
5654},
5755{
5856name :"DeploymentSessionDuration" ,
5957params : 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 ,
6561LifetimeSeconds :0 ,
6662ExpiresAt : time.Time {},
6763TokenName :"hello" ,
6864RemoteAddr :"1.2.3.4" ,
6965Scope :database .APIKeyScopeApplicationConnect ,
7066},
7167},
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+ },
7281{
7382name :"DefaultIP" ,
7483params : 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 ,
8392},
8493},
8594{
8695name :"DefaultScope" ,
8796params : 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 :"" ,
96105},
97106},
98107}
@@ -131,15 +140,15 @@ func TestGenerate(t *testing.T) {
131140// Should not be a delta greater than 5 seconds.
132141assert .InDelta (t ,time .Until (tc .params .ExpiresAt ).Seconds (),key .LifetimeSeconds ,5 )
133142}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 )
135144}
136145
137146if ! tc .params .ExpiresAt .IsZero () {
138147assert .Equal (t ,tc .params .ExpiresAt .UTC (),key .ExpiresAt )
139148}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 )
141150}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 )
143152}
144153
145154if tc .params .RemoteAddr != "" {