Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dropUser command #3866

Merged
merged 19 commits into from
Dec 20, 2023
Prev Previous commit
Next Next commit
wip
  • Loading branch information
henvic committed Dec 18, 2023
commit 6624561d4f65d8acc21aa77363e257fe5f51a59b
124 changes: 124 additions & 0 deletions integration/users/drop_user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package users

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"

"github.com/FerretDB/FerretDB/integration"
"github.com/FerretDB/FerretDB/integration/setup"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/util/testutil"
)

func TestDropUser(t *testing.T) {
t.Parallel()

ctx, collection := setup.Setup(t)
db := collection.Database()
client := db.Client()
users := client.Database("admin").Collection("system.users")

// TODO https://github.com/FerretDB/FerretDB/issues/1492
if setup.IsMongoDB(t) {
assert.NoError(t, collection.Database().RunCommand(ctx, bson.D{
{"dropAllUsersFromDatabase", 1},
}).Err())
} else {
// Erase any previously saved user in the database.
_, err := users.DeleteMany(ctx, bson.D{{"db", db.Name()}})
assert.NoError(t, err)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
}

err := db.RunCommand(ctx, bson.D{
{"createUser", "a_user"},
{"roles", bson.A{}},
{"pwd", "password"},
}).Err()
assert.NoError(t, err)

testCases := map[string]struct { //nolint:vet // for readability
payload bson.D
err *mongo.CommandError
altMessage string
expected bson.D
skip string
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
}{
"NotFound": {
payload: bson.D{
{"dropUser", "not_found_user"},
},
err: &mongo.CommandError{
Code: 11,
Name: "UserNotFound",
Message: "User 'not_found_user@TestDropUser' not found",
},
},
"Success": {
payload: bson.D{
{"dropUser", "a_user"},
},
expected: bson.D{
{
"ok", float64(1),
},
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
},
},
}

for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
if tc.skip != "" {
t.Skip(tc.skip)
}

t.Parallel()

var res bson.D
err := db.RunCommand(ctx, tc.payload).Decode(&res)
if tc.err != nil {
integration.AssertEqualAltCommandError(t, *tc.err, tc.altMessage, err)
return
}

require.NoError(t, err)

actual := integration.ConvertDocument(t, res)
actual.Remove("$clusterTime")
actual.Remove("operationTime")

expected := integration.ConvertDocument(t, tc.expected)
testutil.AssertEqual(t, expected, actual)

payload := integration.ConvertDocument(t, tc.payload)
assertUserNotFound(ctx, t, users, db.Name(), payload)
})
}
}

// assertUserNotFound checks it the user doesn't exist in the admin.system.users collection.
func assertUserNotFound(ctx context.Context, t testing.TB, users *mongo.Collection, dbName string, payload *types.Document) {
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()
err := users.FindOne(ctx, bson.D{{"user", must.NotFail(payload.Get("dropUser"))}}).Err()
require.Equal(t, mongo.ErrNoDocuments, err, `should return "no documents" error`)
}
3 changes: 3 additions & 0 deletions internal/handler/handlererrors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const (
// ErrFailedToParse indicates user input parsing failure.
ErrFailedToParse = ErrorCode(9) // FailedToParse

// ErrUserNotFound indicates an user was not found for the accessed database.
ErrUserNotFound = ErrorCode(11) // UserNotFound

// ErrUnauthorized indicates that cursor is not authorized to access another namespace.
ErrUnauthorized = ErrorCode(13) // Unauthorized

Expand Down
188 changes: 95 additions & 93 deletions internal/handler/handlererrors/errorcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading