@@ -18,24 +18,28 @@ import (
18
18
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
19
19
"github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
20
20
"github.com/golang/mock/gomock"
21
+ "github.com/google/uuid"
21
22
"github.com/stretchr/testify/require"
22
23
)
23
24
24
- func TestTokensService_CreatePersonalAccessTokenWithoutFeatureFlag (t * testing.T ) {
25
- experimentsClient := & experimentstest.Client {
25
+ var (
26
+ withTokenFeatureDisabled = & experimentstest.Client {
27
+ BoolMatcher : func (ctx context.Context , experiment string , defaultValue bool , attributes experiments.Attributes ) bool {
28
+ return false
29
+ },
30
+ }
31
+ withTokenFeatureEnabled = & experimentstest.Client {
26
32
BoolMatcher : func (ctx context.Context , experiment string , defaultValue bool , attributes experiments.Attributes ) bool {
27
33
return experiment == experiments .PersonalAccessTokensEnabledFlag
28
34
},
29
35
}
36
+ )
30
37
38
+ func TestTokensService_CreatePersonalAccessTokenWithoutFeatureFlag (t * testing.T ) {
31
39
user := newUser (& protocol.User {})
32
40
33
41
t .Run ("permission denied when feature flag is not enabled" , func (t * testing.T ) {
34
- serverMock , client := setupTokensService (t , & experimentstest.Client {
35
- BoolMatcher : func (ctx context.Context , experiment string , defaultValue bool , attributes experiments.Attributes ) bool {
36
- return false
37
- },
38
- })
42
+ serverMock , client := setupTokensService (t , withTokenFeatureDisabled )
39
43
40
44
serverMock .EXPECT ().GetLoggedInUser (gomock .Any ()).Return (user , nil )
41
45
@@ -46,7 +50,7 @@ func TestTokensService_CreatePersonalAccessTokenWithoutFeatureFlag(t *testing.T)
46
50
})
47
51
48
52
t .Run ("unimplemented when feature flag enabled" , func (t * testing.T ) {
49
- serverMock , client := setupTokensService (t , experimentsClient )
53
+ serverMock , client := setupTokensService (t , withTokenFeatureEnabled )
50
54
51
55
serverMock .EXPECT ().GetLoggedInUser (gomock .Any ()).Return (user , nil )
52
56
@@ -55,6 +59,53 @@ func TestTokensService_CreatePersonalAccessTokenWithoutFeatureFlag(t *testing.T)
55
59
})
56
60
}
57
61
62
+ func TestTokensService_GetPersonalAccessToken (t * testing.T ) {
63
+ user := newUser (& protocol.User {})
64
+
65
+ t .Run ("permission denied when feature flag is disabled" , func (t * testing.T ) {
66
+ serverMock , client := setupTokensService (t , withTokenFeatureDisabled )
67
+
68
+ serverMock .EXPECT ().GetLoggedInUser (gomock .Any ()).Return (user , nil )
69
+
70
+ _ , err := client .GetPersonalAccessToken (context .Background (), connect .NewRequest (& v1.GetPersonalAccessTokenRequest {
71
+ Id : uuid .New ().String (),
72
+ }))
73
+
74
+ require .Error (t , err , "This feature is currently in beta. If you would like to be part of the beta, please contact us." )
75
+ require .Equal (t , connect .CodePermissionDenied , connect .CodeOf (err ))
76
+ })
77
+
78
+ t .Run ("invalid argument when Token ID is empty" , func (t * testing.T ) {
79
+ _ , client := setupTokensService (t , withTokenFeatureEnabled )
80
+
81
+ _ , err := client .GetPersonalAccessToken (context .Background (), connect .NewRequest (& v1.GetPersonalAccessTokenRequest {}))
82
+
83
+ require .Equal (t , connect .CodeInvalidArgument , connect .CodeOf (err ))
84
+ })
85
+
86
+ t .Run ("invalid argument when Token ID is not a valid UUID" , func (t * testing.T ) {
87
+ _ , client := setupTokensService (t , withTokenFeatureEnabled )
88
+
89
+ _ , err := client .GetPersonalAccessToken (context .Background (), connect .NewRequest (& v1.GetPersonalAccessTokenRequest {
90
+ Id : "foo-bar" ,
91
+ }))
92
+
93
+ require .Equal (t , connect .CodeInvalidArgument , connect .CodeOf (err ))
94
+ })
95
+
96
+ t .Run ("unimplemented when feature flag enabled" , func (t * testing.T ) {
97
+ serverMock , client := setupTokensService (t , withTokenFeatureEnabled )
98
+
99
+ serverMock .EXPECT ().GetLoggedInUser (gomock .Any ()).Return (user , nil )
100
+
101
+ _ , err := client .GetPersonalAccessToken (context .Background (), connect .NewRequest (& v1.GetPersonalAccessTokenRequest {
102
+ Id : uuid .New ().String (),
103
+ }))
104
+
105
+ require .Equal (t , connect .CodeUnimplemented , connect .CodeOf (err ))
106
+ })
107
+ }
108
+
58
109
func setupTokensService (t * testing.T , expClient experiments.Client ) (* protocol.MockAPIInterface , v1connect.TokensServiceClient ) {
59
110
t .Helper ()
60
111
0 commit comments