55"database/sql"
66"encoding/json"
77"net/url"
8+ "strings"
89"sync/atomic"
910"testing"
1011"time"
@@ -15,6 +16,7 @@ import (
1516"golang.org/x/oauth2"
1617
1718"cdr.dev/slog/sloggers/slogtest"
19+ "github.com/coder/coder/cli/clibase"
1820"github.com/coder/coder/coderd/audit"
1921"github.com/coder/coder/coderd/database"
2022"github.com/coder/coder/coderd/database/dbfake"
@@ -61,6 +63,7 @@ func TestAcquireJob(t *testing.T) {
6163Auditor :mockAuditor (),
6264TemplateScheduleStore :testTemplateScheduleStore (),
6365Tracer :trace .NewNoopTracerProvider ().Tracer ("noop" ),
66+ DeploymentValues :& codersdk.DeploymentValues {},
6467}
6568job ,err := srv .AcquireJob (context .Background (),nil )
6669require .NoError (t ,err )
@@ -102,6 +105,10 @@ func TestAcquireJob(t *testing.T) {
102105t .Parallel ()
103106srv := setup (t ,false )
104107gitAuthProvider := "github"
108+ // Set the max session token lifetime so we can assert we
109+ // create an API key with an expiration within the bounds of the
110+ // deployment config.
111+ srv .DeploymentValues .MaxTokenLifetime = clibase .Duration (time .Hour )
105112srv .GitAuthConfigs = []* gitauth.Config {{
106113ID :gitAuthProvider ,
107114OAuth2Config :& testutil.OAuth2Config {},
@@ -216,6 +223,16 @@ func TestAcquireJob(t *testing.T) {
216223got ,err := json .Marshal (job .Type )
217224require .NoError (t ,err )
218225
226+ // Validate that a session token is generated during the job.
227+ sessionToken := job .Type .(* proto.AcquiredJob_WorkspaceBuild_ ).WorkspaceBuild .Metadata .CoderSessionToken
228+ require .NotEmpty (t ,sessionToken )
229+ toks := strings .Split (sessionToken ,"-" )
230+ require .Len (t ,toks ,2 ,"invalid api key" )
231+ key ,err := srv .Database .GetAPIKeyByID (ctx ,toks [0 ])
232+ require .NoError (t ,err )
233+ require .Equal (t ,int64 (srv .DeploymentValues .MaxTokenLifetime .Value ().Seconds ()),key .LifetimeSeconds )
234+ require .WithinDuration (t ,time .Now ().Add (srv .DeploymentValues .MaxTokenLifetime .Value ()),key .ExpiresAt ,time .Minute )
235+
219236want ,err := json .Marshal (& proto.AcquiredJob_WorkspaceBuild_ {
220237WorkspaceBuild :& proto.AcquiredJob_WorkspaceBuild {
221238WorkspaceBuildId :build .ID .String (),
@@ -247,6 +264,7 @@ func TestAcquireJob(t *testing.T) {
247264WorkspaceOwnerId :user .ID .String (),
248265TemplateName :template .Name ,
249266TemplateVersion :version .Name ,
267+ CoderSessionToken :sessionToken ,
250268},
251269},
252270})
@@ -1205,6 +1223,7 @@ func setup(t *testing.T, ignoreLogErrors bool) *provisionerdserver.Server {
12051223Auditor :mockAuditor (),
12061224TemplateScheduleStore :testTemplateScheduleStore (),
12071225Tracer :trace .NewNoopTracerProvider ().Tracer ("noop" ),
1226+ DeploymentValues :& codersdk.DeploymentValues {},
12081227}
12091228}
12101229