@@ -17,11 +17,13 @@ import (
17
17
"github.com/google/uuid"
18
18
"github.com/stretchr/testify/assert"
19
19
"github.com/stretchr/testify/require"
20
+ "golang.org/x/oauth2"
20
21
21
22
"cdr.dev/slog"
22
23
"cdr.dev/slog/sloggers/slogtest"
23
24
"github.com/coder/coder/agent"
24
25
"github.com/coder/coder/coderd/coderdtest"
26
+ "github.com/coder/coder/coderd/database"
25
27
"github.com/coder/coder/coderd/gitauth"
26
28
"github.com/coder/coder/codersdk"
27
29
"github.com/coder/coder/provisioner/echo"
@@ -884,6 +886,72 @@ func TestWorkspaceAgentsGitAuth(t *testing.T) {
884
886
resp = gitAuthCallback (t , "github" , client )
885
887
require .Equal (t , http .StatusTemporaryRedirect , resp .StatusCode )
886
888
})
889
+
890
+ t .Run ("ExpiredNoRefresh" , func (t * testing.T ) {
891
+ t .Parallel ()
892
+ client := coderdtest .New (t , & coderdtest.Options {
893
+ IncludeProvisionerDaemon : true ,
894
+ GitAuthConfigs : []* gitauth.Config {{
895
+ OAuth2Config : & oauth2Config {
896
+ token : & oauth2.Token {
897
+ AccessToken : "token" ,
898
+ RefreshToken : "something" ,
899
+ Expiry : database .Now ().Add (- time .Hour ),
900
+ },
901
+ },
902
+ ID : "github" ,
903
+ Regex : regexp .MustCompile (`github\.com` ),
904
+ Type : codersdk .GitProviderGitHub ,
905
+ NoRefresh : true ,
906
+ }},
907
+ })
908
+ user := coderdtest .CreateFirstUser (t , client )
909
+ authToken := uuid .NewString ()
910
+ version := coderdtest .CreateTemplateVersion (t , client , user .OrganizationID , & echo.Responses {
911
+ Parse : echo .ParseComplete ,
912
+ ProvisionPlan : echo .ProvisionComplete ,
913
+ ProvisionApply : []* proto.Provision_Response {{
914
+ Type : & proto.Provision_Response_Complete {
915
+ Complete : & proto.Provision_Complete {
916
+ Resources : []* proto.Resource {{
917
+ Name : "example" ,
918
+ Type : "aws_instance" ,
919
+ Agents : []* proto.Agent {{
920
+ Id : uuid .NewString (),
921
+ Auth : & proto.Agent_Token {
922
+ Token : authToken ,
923
+ },
924
+ }},
925
+ }},
926
+ },
927
+ },
928
+ }},
929
+ })
930
+ template := coderdtest .CreateTemplate (t , client , user .OrganizationID , version .ID )
931
+ coderdtest .AwaitTemplateVersionJob (t , client , version .ID )
932
+ workspace := coderdtest .CreateWorkspace (t , client , user .OrganizationID , template .ID )
933
+ coderdtest .AwaitWorkspaceBuildJob (t , client , workspace .LatestBuild .ID )
934
+
935
+ agentClient := codersdk .New (client .URL )
936
+ agentClient .SetSessionToken (authToken )
937
+
938
+ token , err := agentClient .WorkspaceAgentGitAuth (context .Background (), "github.com/asd/asd" , false )
939
+ require .NoError (t , err )
940
+ require .NotEmpty (t , token .URL )
941
+
942
+ // In the configuration, we set our OAuth provider
943
+ // to return an expired token. Coder consumes this
944
+ // and stores it.
945
+ resp := gitAuthCallback (t , "github" , client )
946
+ require .Equal (t , http .StatusTemporaryRedirect , resp .StatusCode )
947
+
948
+ // Because the token is expired and `NoRefresh` is specified,
949
+ // a redirect URL should be returned again.
950
+ token , err = agentClient .WorkspaceAgentGitAuth (context .Background (), "github.com/asd/asd" , false )
951
+ require .NoError (t , err )
952
+ require .NotEmpty (t , token .URL )
953
+ })
954
+
887
955
t .Run ("FullFlow" , func (t * testing.T ) {
888
956
t .Parallel ()
889
957
client := coderdtest .New (t , & coderdtest.Options {
0 commit comments