From 0fb1117ab0bccbd90b50ddda52e6de171367901c Mon Sep 17 00:00:00 2001 From: Alan Parra Date: Fri, 6 Dec 2024 17:09:13 -0300 Subject: [PATCH] Add basic service testing --- .../decisionv1/decision_service_test.go | 45 +++++++++++ lib/decision/decisionv1/env_test.go | 74 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 lib/decision/decisionv1/decision_service_test.go create mode 100644 lib/decision/decisionv1/env_test.go diff --git a/lib/decision/decisionv1/decision_service_test.go b/lib/decision/decisionv1/decision_service_test.go new file mode 100644 index 0000000000000..72f92fcfbe8b7 --- /dev/null +++ b/lib/decision/decisionv1/decision_service_test.go @@ -0,0 +1,45 @@ +// Teleport +// Copyright (C) 2024 Gravitational, Inc. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package decisionv1_test + +import ( + "context" + "testing" + + "github.com/gravitational/trace" + "github.com/stretchr/testify/assert" + + decisionpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/decision/v1alpha1" +) + +func TestDecisionService_notImplemented(t *testing.T) { + t.Parallel() + + env := NewTestenv(t) + decisionClient := env.DecisionClient + ctx := context.Background() + + t.Run("EvaluateSSHAccess", func(t *testing.T) { + _, err := decisionClient.EvaluateSSHAccess(ctx, &decisionpb.EvaluateSSHAccessRequest{}) + assert.ErrorAs(t, err, new(*trace.NotImplementedError), "EvaluateSSHAccess error mismatch") + }) + + t.Run("EvaluateDatabaseAccess", func(t *testing.T) { + _, err := decisionClient.EvaluateDatabaseAccess(ctx, &decisionpb.EvaluateDatabaseAccessRequest{}) + assert.ErrorAs(t, err, new(*trace.NotImplementedError), "EvaluateDatabaseAccess error mismatch") + }) +} diff --git a/lib/decision/decisionv1/env_test.go b/lib/decision/decisionv1/env_test.go new file mode 100644 index 0000000000000..6211cdb0d94ca --- /dev/null +++ b/lib/decision/decisionv1/env_test.go @@ -0,0 +1,74 @@ +// Teleport +// Copyright (C) 2024 Gravitational, Inc. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package decisionv1_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/gravitational/teleport/api/constants" + decisionpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/decision/v1alpha1" + "github.com/gravitational/teleport/api/types" + "github.com/gravitational/teleport/lib/auth" + "github.com/gravitational/teleport/lib/auth/authclient" +) + +// Testenv is a test environment for decisionv1.Service. +type Testenv struct { + TestServer *auth.TestServer + + // AuthAdminClient is an admin Auth client. + AuthAdminClient *authclient.Client + + // DecisionClient is an admin decision client. + // Created from AuthAdminClient. + DecisionClient decisionpb.DecisionServiceClient +} + +func NewTestenv(t *testing.T) *Testenv { + t.Helper() + + testServer, err := auth.NewTestServer(auth.TestServerConfig{ + Auth: auth.TestAuthServerConfig{ + Dir: t.TempDir(), + AuthPreferenceSpec: &types.AuthPreferenceSpecV2{ + SecondFactor: constants.SecondFactorOTP, // Required. + }, + }, + }) + require.NoError(t, err, "NewTestServer failed") + t.Cleanup(func() { + assert.NoError(t, + testServer.Shutdown(context.Background()), + "testServer.Shutdown failed") + }) + + adminClient, err := testServer.NewClient(auth.TestAdmin()) + require.NoError(t, err, "NewClient failed") + t.Cleanup(func() { + assert.NoError(t, adminClient.Close(), "adminClient.Close() failed") + }) + + return &Testenv{ + TestServer: testServer, + AuthAdminClient: adminClient, + DecisionClient: adminClient.DecisionClient(), + } +}