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 usersInfo command #3897

Merged
merged 57 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
5d2c18e
wip
AlekSi Dec 12, 2023
25afee7
wip
henvic Dec 12, 2023
0b7235e
wip
henvic Dec 14, 2023
575bd2b
wip
henvic Dec 15, 2023
f5e1d08
Merge branch 'main' into feat-create-user
henvic Dec 15, 2023
5fcc3b0
wip
henvic Dec 16, 2023
c66ae27
wip
henvic Dec 18, 2023
910bf7f
wip
henvic Dec 18, 2023
42cabfc
wip
henvic Dec 18, 2023
8c43dd6
Merge branch 'main' into feat-create-user
henvic Dec 18, 2023
6624561
wip
henvic Dec 18, 2023
ac21f14
wip
henvic Dec 18, 2023
3b876d2
wip
henvic Dec 18, 2023
1f83cf4
wip
rumyantseva Dec 18, 2023
13c9018
wip
henvic Dec 19, 2023
a89061f
Merge branch 'main' into feat-drop-user
henvic Dec 19, 2023
2791849
Merge branch 'main' into feat-drop-user
henvic Dec 19, 2023
bdb41e1
Merge branch 'main' into feat-drop-user
henvic Dec 19, 2023
c162803
wip
henvic Dec 19, 2023
3cdd260
Merge branch 'main' into feat-drop-user
henvic Dec 19, 2023
f75e28d
Merge branch 'main' into feat-drop-user
AlekSi Dec 19, 2023
813b2f4
wip
henvic Dec 19, 2023
e12c48f
Merge branch 'feat-drop-user' into feat-drop-all-users-from-db
henvic Dec 19, 2023
e27b488
update comments
henvic Dec 19, 2023
d4ec94e
satisfying linter
henvic Dec 19, 2023
57188be
wip
henvic Dec 21, 2023
c542e46
Merge branch 'main' into feat-usersInfo
henvic Dec 21, 2023
0cd62b3
wip
henvic Dec 21, 2023
8710ff1
wip
henvic Dec 21, 2023
c3a4c75
Merge branch 'main' into feat-usersInfo
henvic Dec 22, 2023
03fed34
wip
henvic Dec 22, 2023
0986d44
wip
henvic Dec 22, 2023
ca1d188
happy linter
henvic Dec 22, 2023
45b211f
wip
henvic Dec 22, 2023
4a89e49
maybe revert this commit
henvic Dec 22, 2023
6800d57
Merge branch 'main' into feat-usersInfo
henvic Dec 26, 2023
443c2a5
wip
henvic Dec 26, 2023
de9c14e
wip
henvic Dec 27, 2023
723dfb7
wip
henvic Dec 27, 2023
1d42afd
Merge branch 'main' into feat-usersInfo
henvic Dec 27, 2023
8d8e080
Merge branch 'main' into feat-usersInfo
henvic Dec 27, 2023
97b3097
wip
henvic Dec 27, 2023
eea7ec3
wip
henvic Dec 28, 2023
10f6f28
test
henvic Dec 28, 2023
a6726a5
Merge branch 'main' into feat-usersInfo
henvic Dec 28, 2023
78442d1
Merge branch 'main' into feat-usersInfo
henvic Dec 28, 2023
46f322d
wip
henvic Dec 29, 2023
1ed4778
Merge branch 'main' into feat-usersInfo
henvic Dec 29, 2023
04a5e0e
wip
henvic Jan 2, 2024
69e3a36
Merge branch 'main' into feat-usersInfo
henvic Jan 2, 2024
a9839a0
fix doc
henvic Jan 2, 2024
906ba81
wip
henvic Jan 2, 2024
039f3cb
Merge branch 'main' into feat-usersInfo
henvic Jan 4, 2024
1213b0e
wip
henvic Jan 4, 2024
f2e7a23
Merge branch 'main' into feat-usersInfo
henvic Jan 4, 2024
1bfcef6
Merge branch 'main' into feat-usersInfo
AlekSi Jan 4, 2024
3b98b30
Merge branch 'main' into feat-usersInfo
mergify[bot] Jan 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}

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
}{
"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),
},
},
},
}

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) {
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