|
| 1 | +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License-AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package apiv1 |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | + |
| 13 | + connect "github.com/bufbuild/connect-go" |
| 14 | + "github.com/gitpod-io/gitpod/common-go/experiments" |
| 15 | + "github.com/gitpod-io/gitpod/common-go/experiments/experimentstest" |
| 16 | + v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1" |
| 17 | + "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect" |
| 18 | + protocol "github.com/gitpod-io/gitpod/gitpod-protocol" |
| 19 | + "github.com/gitpod-io/gitpod/public-api-server/pkg/auth" |
| 20 | + "github.com/golang/mock/gomock" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + withOIDCFeatureDisabled = &experimentstest.Client{ |
| 26 | + BoolMatcher: func(ctx context.Context, experiment string, defaultValue bool, attributes experiments.Attributes) bool { |
| 27 | + return false |
| 28 | + }, |
| 29 | + } |
| 30 | + withOIDCFeatureEnabled = &experimentstest.Client{ |
| 31 | + BoolMatcher: func(ctx context.Context, experiment string, defaultValue bool, attributes experiments.Attributes) bool { |
| 32 | + return experiment == experiments.OIDCServiceEnabledFlag |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + user = newUser(&protocol.User{}) |
| 37 | +) |
| 38 | + |
| 39 | +func TestOIDCService_CreateClientConfig(t *testing.T) { |
| 40 | + t.Run("feature flag disabled returns unathorized", func(t *testing.T) { |
| 41 | + serverMock, client := setupOIDCService(t, withOIDCFeatureDisabled) |
| 42 | + |
| 43 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 44 | + serverMock.EXPECT().GetTeams(gomock.Any()).Return(teams, nil) |
| 45 | + |
| 46 | + _, err := client.CreateClientConfig(context.Background(), connect.NewRequest(&v1.CreateClientConfigRequest{})) |
| 47 | + require.Error(t, err) |
| 48 | + require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err)) |
| 49 | + }) |
| 50 | + |
| 51 | + t.Run("feature flag enabled returns unimplemented", func(t *testing.T) { |
| 52 | + serverMock, client := setupOIDCService(t, withOIDCFeatureEnabled) |
| 53 | + |
| 54 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 55 | + |
| 56 | + _, err := client.CreateClientConfig(context.Background(), connect.NewRequest(&v1.CreateClientConfigRequest{})) |
| 57 | + require.Error(t, err) |
| 58 | + require.Equal(t, connect.CodeUnimplemented, connect.CodeOf(err)) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func TestOIDCService_GetClientConfig(t *testing.T) { |
| 63 | + t.Run("feature flag disabled returns unathorized", func(t *testing.T) { |
| 64 | + serverMock, client := setupOIDCService(t, withOIDCFeatureDisabled) |
| 65 | + |
| 66 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 67 | + serverMock.EXPECT().GetTeams(gomock.Any()).Return(teams, nil) |
| 68 | + |
| 69 | + _, err := client.GetClientConfig(context.Background(), connect.NewRequest(&v1.GetClientConfigRequest{})) |
| 70 | + require.Error(t, err) |
| 71 | + require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err)) |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("feature flag enabled returns unimplemented", func(t *testing.T) { |
| 75 | + serverMock, client := setupOIDCService(t, withOIDCFeatureEnabled) |
| 76 | + |
| 77 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 78 | + |
| 79 | + _, err := client.GetClientConfig(context.Background(), connect.NewRequest(&v1.GetClientConfigRequest{})) |
| 80 | + require.Error(t, err) |
| 81 | + require.Equal(t, connect.CodeUnimplemented, connect.CodeOf(err)) |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +func TestOIDCService_ListClientConfigs(t *testing.T) { |
| 86 | + t.Run("feature flag disabled returns unathorized", func(t *testing.T) { |
| 87 | + serverMock, client := setupOIDCService(t, withOIDCFeatureDisabled) |
| 88 | + |
| 89 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 90 | + serverMock.EXPECT().GetTeams(gomock.Any()).Return(teams, nil) |
| 91 | + |
| 92 | + _, err := client.ListClientConfigs(context.Background(), connect.NewRequest(&v1.ListClientConfigsRequest{})) |
| 93 | + require.Error(t, err) |
| 94 | + require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err)) |
| 95 | + }) |
| 96 | + |
| 97 | + t.Run("feature flag enabled returns unimplemented", func(t *testing.T) { |
| 98 | + serverMock, client := setupOIDCService(t, withOIDCFeatureEnabled) |
| 99 | + |
| 100 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 101 | + |
| 102 | + _, err := client.ListClientConfigs(context.Background(), connect.NewRequest(&v1.ListClientConfigsRequest{})) |
| 103 | + require.Error(t, err) |
| 104 | + require.Equal(t, connect.CodeUnimplemented, connect.CodeOf(err)) |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +func TestOIDCService_UpdateClientConfig(t *testing.T) { |
| 109 | + t.Run("feature flag disabled returns unathorized", func(t *testing.T) { |
| 110 | + serverMock, client := setupOIDCService(t, withOIDCFeatureDisabled) |
| 111 | + |
| 112 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 113 | + serverMock.EXPECT().GetTeams(gomock.Any()).Return(teams, nil) |
| 114 | + |
| 115 | + _, err := client.UpdateClientConfig(context.Background(), connect.NewRequest(&v1.UpdateClientConfigRequest{})) |
| 116 | + require.Error(t, err) |
| 117 | + require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err)) |
| 118 | + }) |
| 119 | + |
| 120 | + t.Run("feature flag enabled returns unimplemented", func(t *testing.T) { |
| 121 | + serverMock, client := setupOIDCService(t, withOIDCFeatureEnabled) |
| 122 | + |
| 123 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 124 | + |
| 125 | + _, err := client.UpdateClientConfig(context.Background(), connect.NewRequest(&v1.UpdateClientConfigRequest{})) |
| 126 | + require.Error(t, err) |
| 127 | + require.Equal(t, connect.CodeUnimplemented, connect.CodeOf(err)) |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +func TestOIDCService_DeleteClientConfig(t *testing.T) { |
| 132 | + t.Run("feature flag disabled returns unathorized", func(t *testing.T) { |
| 133 | + serverMock, client := setupOIDCService(t, withOIDCFeatureDisabled) |
| 134 | + |
| 135 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 136 | + serverMock.EXPECT().GetTeams(gomock.Any()).Return(teams, nil) |
| 137 | + |
| 138 | + _, err := client.DeleteClientConfig(context.Background(), connect.NewRequest(&v1.DeleteClientConfigRequest{})) |
| 139 | + require.Error(t, err) |
| 140 | + require.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err)) |
| 141 | + }) |
| 142 | + |
| 143 | + t.Run("feature flag enabled returns unimplemented", func(t *testing.T) { |
| 144 | + serverMock, client := setupOIDCService(t, withOIDCFeatureEnabled) |
| 145 | + |
| 146 | + serverMock.EXPECT().GetLoggedInUser(gomock.Any()).Return(user, nil) |
| 147 | + |
| 148 | + _, err := client.DeleteClientConfig(context.Background(), connect.NewRequest(&v1.DeleteClientConfigRequest{})) |
| 149 | + require.Error(t, err) |
| 150 | + require.Equal(t, connect.CodeUnimplemented, connect.CodeOf(err)) |
| 151 | + }) |
| 152 | +} |
| 153 | + |
| 154 | +func setupOIDCService(t *testing.T, expClient experiments.Client) (*protocol.MockAPIInterface, v1connect.OIDCServiceClient) { |
| 155 | + t.Helper() |
| 156 | + |
| 157 | + ctrl := gomock.NewController(t) |
| 158 | + t.Cleanup(ctrl.Finish) |
| 159 | + |
| 160 | + serverMock := protocol.NewMockAPIInterface(ctrl) |
| 161 | + |
| 162 | + svc := NewOIDCService(&FakeServerConnPool{api: serverMock}, expClient) |
| 163 | + |
| 164 | + _, handler := v1connect.NewOIDCServiceHandler(svc, connect.WithInterceptors(auth.NewServerInterceptor())) |
| 165 | + |
| 166 | + srv := httptest.NewServer(handler) |
| 167 | + t.Cleanup(srv.Close) |
| 168 | + |
| 169 | + client := v1connect.NewOIDCServiceClient(http.DefaultClient, srv.URL, connect.WithInterceptors( |
| 170 | + auth.NewClientInterceptor("auth-token"), |
| 171 | + )) |
| 172 | + |
| 173 | + return serverMock, client |
| 174 | +} |
0 commit comments