test(e2e): Added a test for an unauthenticated user

* test(e2e): Added a test for an unauthenticated user

* CR: spelling
pull/5672/head
dillanb-hashi 1 year ago committed by GitHub
parent 14e0b00cce
commit 32d139c45e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,97 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package base_test
import (
"context"
"encoding/json"
"net/http"
"strings"
"testing"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/stretchr/testify/require"
)
// TestCliUnauthenticatedUserAccess tests that a user that is not logged in is unable to
// run commands that require authentication
func TestCliUnauthenticatedUserAccess(t *testing.T) {
e2e.MaybeSkipTest(t)
ctx := context.Background()
boundary.AuthenticateAdminCli(t, ctx)
// Check one authenticated request
output := e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"targets", "list",
"--recursive",
),
)
require.NoError(t, output.Err, string(output.Stderr))
// Logout
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"logout",
),
)
require.NoError(t, output.Err, string(output.Stderr))
// Attempt commands that unauthenticated users can make
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"scopes", "list",
),
)
require.NoError(t, output.Err, string(output.Stderr))
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"auth-methods", "list",
),
)
require.NoError(t, output.Err, string(output.Stderr))
// Check user cannot make requests that require authentication
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"targets", "list",
"--recursive",
"-format", "json",
),
)
require.Error(t, output.Err, "Unauthenticated user was able to access private resource")
fmtError := strings.Split(string(output.Stderr), "\n")[2]
var response boundary.CliError
err := json.Unmarshal([]byte(fmtError), &response)
require.NoError(t, err)
require.Equal(t, http.StatusForbidden, int(response.Status), "Incorrect error code was returned")
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"sessions", "list",
"--recursive",
"-format", "json",
),
)
require.Error(t, output.Err, "Unauthenticated user was able to access private resource")
fmtError = strings.Split(string(output.Stderr), "\n")[2]
err = json.Unmarshal([]byte(fmtError), &response)
require.NoError(t, err)
require.Equal(t, http.StatusForbidden, int(response.Status), "Incorrect error code was returned")
output = e2e.RunCommand(ctx, "boundary",
e2e.WithArgs(
"users", "list",
"--recursive",
"-format", "json",
),
)
require.Error(t, output.Err, "Unauthenticated user was able to access private resource")
fmtError = strings.Split(string(output.Stderr), "\n")[2]
err = json.Unmarshal([]byte(fmtError), &response)
require.NoError(t, err)
require.Equal(t, http.StatusForbidden, int(response.Status), "Incorrect error code was returned")
}
Loading…
Cancel
Save