@@ -23,6 +23,7 @@ import (
2323 "github.com/golang/mock/gomock"
2424 "github.com/google/uuid"
2525 "github.com/stretchr/testify/require"
26+ "google.golang.org/protobuf/types/known/timestamppb"
2627 "gorm.io/gorm"
2728)
2829
3738 return experiment == experiments .PersonalAccessTokensEnabledFlag
3839 },
3940 }
41+
42+ signer = auth .NewHS256Signer ([]byte ("my-secret" ))
4043)
4144
4245func TestTokensService_CreatePersonalAccessTokenWithoutFeatureFlag (t * testing.T ) {
@@ -47,19 +50,86 @@ func TestTokensService_CreatePersonalAccessTokenWithoutFeatureFlag(t *testing.T)
4750
4851 serverMock .EXPECT ().GetLoggedInUser (gomock .Any ()).Return (user , nil )
4952
50- _ , err := client .CreatePersonalAccessToken (context .Background (), & connect.Request [v1.CreatePersonalAccessTokenRequest ]{})
53+ _ , err := client .CreatePersonalAccessToken (context .Background (), connect .NewRequest (& v1.CreatePersonalAccessTokenRequest {
54+ Token : & v1.PersonalAccessToken {
55+ Name : "my-token" ,
56+ ExpirationTime : timestamppb .Now (),
57+ },
58+ }))
5159
5260 require .Error (t , err , "This feature is currently in beta. If you would like to be part of the beta, please contact us." )
5361 require .Equal (t , connect .CodePermissionDenied , connect .CodeOf (err ))
5462 })
5563
56- t .Run ("unimplemented when feature flag enabled" , func (t * testing.T ) {
57- serverMock , _ , client := setupTokensService (t , withTokenFeatureEnabled )
64+ t .Run ("invalid argument when name is not specified" , func (t * testing.T ) {
65+ _ , _ , client := setupTokensService (t , withTokenFeatureDisabled )
66+
67+ _ , err := client .CreatePersonalAccessToken (context .Background (), connect .NewRequest (& v1.CreatePersonalAccessTokenRequest {
68+ Token : & v1.PersonalAccessToken {},
69+ }))
70+ require .Equal (t , connect .CodeInvalidArgument , connect .CodeOf (err ))
71+ })
72+
73+ t .Run ("invalid argument when expiration time is unspecified" , func (t * testing.T ) {
74+ _ , _ , client := setupTokensService (t , withTokenFeatureDisabled )
75+
76+ _ , err := client .CreatePersonalAccessToken (context .Background (), connect .NewRequest (& v1.CreatePersonalAccessTokenRequest {
77+ Token : & v1.PersonalAccessToken {
78+ Name : "my-token" ,
79+ },
80+ }))
81+ require .Equal (t , connect .CodeInvalidArgument , connect .CodeOf (err ))
82+ })
83+
84+ t .Run ("invalid argument when expiration time is invalid" , func (t * testing.T ) {
85+ _ , _ , client := setupTokensService (t , withTokenFeatureDisabled )
86+
87+ _ , err := client .CreatePersonalAccessToken (context .Background (), connect .NewRequest (& v1.CreatePersonalAccessTokenRequest {
88+ Token : & v1.PersonalAccessToken {
89+ Name : "my-token" ,
90+ ExpirationTime : & timestamppb.Timestamp {
91+ Seconds : 253402300799 + 1 ,
92+ },
93+ },
94+ }))
95+ require .Equal (t , connect .CodeInvalidArgument , connect .CodeOf (err ))
96+ })
97+
98+ t .Run ("crates personal access token" , func (t * testing.T ) {
99+ serverMock , dbConn , client := setupTokensService (t , withTokenFeatureEnabled )
58100
59101 serverMock .EXPECT ().GetLoggedInUser (gomock .Any ()).Return (user , nil )
60102
61- _ , err := client .CreatePersonalAccessToken (context .Background (), & connect.Request [v1.CreatePersonalAccessTokenRequest ]{})
62- require .Equal (t , connect .CodeUnimplemented , connect .CodeOf (err ))
103+ token := & v1.PersonalAccessToken {
104+ Name : "my-token" ,
105+ Description : "my description" ,
106+ ExpirationTime : timestamppb .Now (),
107+ }
108+
109+ response , err := client .CreatePersonalAccessToken (context .Background (), connect .NewRequest (& v1.CreatePersonalAccessTokenRequest {
110+ Token : token ,
111+ }))
112+ require .NoError (t , err )
113+
114+ created := response .Msg .GetToken ()
115+ t .Cleanup (func () {
116+ require .NoError (t , dbConn .Where ("id = ?" , created .GetId ()).Delete (& db.PersonalAccessToken {}).Error )
117+ })
118+
119+ require .NotEmpty (t , created .GetId ())
120+ require .Equal (t , token .Name , created .GetName ())
121+ require .Equal (t , token .Description , created .GetDescription ())
122+ require .Equal (t , token .Scopes , created .GetScopes ())
123+ requireEqualProto (t , token .GetExpirationTime (), created .GetExpirationTime ())
124+
125+ // Returned token must be parseable
126+ _ , err = auth .ParsePersonalAccessToken (created .GetValue (), signer )
127+ require .NoError (t , err )
128+
129+ // token must exist in the DB, with the User ID of the requestor
130+ storedInDB , err := db .GetToken (context .Background (), dbConn , uuid .MustParse (created .GetId ()))
131+ require .NoError (t , err )
132+ require .Equal (t , user .ID , storedInDB .UserID .String ())
63133 })
64134}
65135
@@ -370,7 +440,6 @@ func setupTokensService(t *testing.T, expClient experiments.Client) (*protocol.M
370440 t .Helper ()
371441
372442 dbConn := dbtest .ConnectForTests (t )
373- signer := auth .NewHS256Signer ([]byte ("my-secret" ))
374443
375444 ctrl := gomock .NewController (t )
376445 t .Cleanup (ctrl .Finish )
0 commit comments