From 8e61f828bf9e6aee23636ce7e89e16b30ccdb4cd Mon Sep 17 00:00:00 2001 From: Salim Afiune Maya Date: Fri, 27 Dec 2024 14:50:45 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20more=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Salim Afiune Maya --- test/providers/github_test.go | 60 +++++++++++++++++++++++++++++++++++ test/providers/os_test.go | 2 +- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 test/providers/github_test.go diff --git a/test/providers/github_test.go b/test/providers/github_test.go new file mode 100644 index 0000000000..c972759ad7 --- /dev/null +++ b/test/providers/github_test.go @@ -0,0 +1,60 @@ +// Copyright (c) Mondoo, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package providers + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.mondoo.com/cnquery/v11/test" +) + +func TestGithubScanFlags(t *testing.T) { + once.Do(setup) + + t.Run("github scan WITHOUT flags", func(t *testing.T) { + // NOTE this will fail but, it will load the flags and fail with the right message + r := test.NewCliTestRunner("./cnquery", "scan", "github", "repo", "foo") + err := r.Run() + require.NoError(t, err) + assert.Equal(t, 0, r.ExitCode()) + assert.NotNil(t, r.Stdout()) + assert.NotNil(t, r.Stderr()) + + assert.Contains(t, string(r.Stderr()), + "a valid GitHub authentication is required", + ) + }) + t.Run("github scan WITH flags but missing app auth key", func(t *testing.T) { + // NOTE this will fail but, it will load the flags and fail with the right message + r := test.NewCliTestRunner("./cnquery", "scan", "github", "repo", "foo", + "--app-id", "123", "--app-installation-id", "456", + ) + err := r.Run() + require.NoError(t, err) + assert.Equal(t, 1, r.ExitCode()) + assert.NotNil(t, r.Stdout()) + assert.NotNil(t, r.Stderr()) + + assert.Contains(t, string(r.Stderr()), + "could not parse private key", // expected! it means we loaded the flags + ) + }) + t.Run("github scan WITH all required flags for app auth", func(t *testing.T) { + // NOTE this will fail but, it will load the flags and fail with the right message + r := test.NewCliTestRunner("./cnquery", "scan", "github", "repo", "foo", + "--app-id", "123", "--app-installation-id", "456", "--app-private-key", "private-key.pem", + ) + err := r.Run() + require.NoError(t, err) + assert.Equal(t, 1, r.ExitCode()) + assert.NotNil(t, r.Stdout()) + assert.NotNil(t, r.Stderr()) + + assert.Contains(t, string(r.Stderr()), + "could not read private key", // expected! it means we loaded the flags + ) + }) +} diff --git a/test/providers/os_test.go b/test/providers/os_test.go index 5b4401b33f..655362cf24 100644 --- a/test/providers/os_test.go +++ b/test/providers/os_test.go @@ -20,7 +20,7 @@ var once sync.Once // setup builds cnquery locally func setup() { - // build cnspec + // build cnquery if err := exec.Command("go", "build", "../../apps/cnquery/cnquery.go").Run(); err != nil { log.Fatalf("building cnquery: %v", err) }