-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathDeleteUserTests.cs
More file actions
189 lines (159 loc) · 7.44 KB
/
DeleteUserTests.cs
File metadata and controls
189 lines (159 loc) · 7.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
using System;
using System.Threading.Tasks;
using Allure.NUnit;
using NUnit.Framework;
using NUnit.Framework.Legacy;
namespace Garnet.test.Resp.ACL
{
/// <summary>
/// Tests for ACL DELUSER operations.
/// </summary>
[AllureNUnit]
[TestFixture]
class DeleteUserTests : AclTest
{
/// <summary>
/// Creates and starts the Garnet test server
/// </summary>
[SetUp]
public virtual void Setup()
{
server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, useAcl: true);
server.Start();
}
/// <summary>
/// Executes deluser command with a single user
/// </summary>
[Test]
public async Task DeleteSingleUser()
{
using var c = TestUtils.GetGarnetClientSession();
c.Connect();
// Add two new users
var response = await c.ExecuteAsync("ACL", "setuser", TestUserA, ">passwd").ConfigureAwait(false);
ClassicAssert.AreEqual("OK", response);
response = await c.ExecuteAsync("ACL", "setuser", TestUserB, ">passwd").ConfigureAwait(false);
ClassicAssert.AreEqual("OK", response);
// Verify that both users exist
string[] usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Contain(TestUserA));
Assert.That(usernames, Does.Contain(TestUserB));
// Try to delete test user A
response = await c.ExecuteAsync("ACL", "deluser", TestUserA).ConfigureAwait(false);
ClassicAssert.AreEqual("1", response);
// Ensure test user A is not listed, but test user B still exists
usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Not.Contain(TestUserA));
Assert.That(usernames, Does.Contain(TestUserB));
}
/// <summary>
/// Executes deluser command with multiple users
/// </summary>
[Test]
public async Task DeleteMultipleUser()
{
using var c = TestUtils.GetGarnetClientSession();
c.Connect();
// Add two new users
var response = await c.ExecuteAsync("ACL", "setuser", TestUserA, ">passwd").ConfigureAwait(false);
ClassicAssert.AreEqual("OK", response);
response = await c.ExecuteAsync("ACL", "setuser", TestUserB, ">passwd").ConfigureAwait(false);
ClassicAssert.AreEqual("OK", response);
// Verify that both users exist
string[] usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Contain(TestUserA));
Assert.That(usernames, Does.Contain(TestUserB));
// Try to delete both users in a single command
response = await c.ExecuteAsync("ACL", "deluser", TestUserA, TestUserB).ConfigureAwait(false);
ClassicAssert.AreEqual("2", response);
// Ensure both users have been deleted
usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Not.Contain(TestUserA));
Assert.That(usernames, Does.Not.Contain(TestUserB));
}
/// <summary>
/// Attempts to delete a non-existing user. Garnet should just ignore the command
/// </summary>
[Test]
public async Task DeleteNonexistingUser()
{
using var c = TestUtils.GetGarnetClientSession();
c.Connect();
// Add two new users
var response = await c.ExecuteAsync("ACL", "setuser", TestUserA, ">passwd").ConfigureAwait(false);
ClassicAssert.AreEqual("OK", response);
response = await c.ExecuteAsync("ACL", "setuser", TestUserB, ">passwd").ConfigureAwait(false);
ClassicAssert.AreEqual("OK", response);
// Verify that both users exist
string[] usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Contain(TestUserA));
Assert.That(usernames, Does.Contain(TestUserB));
// Try to delete both users in a single command
response = await c.ExecuteAsync("ACL", "deluser", TestUserUnknown).ConfigureAwait(false);
ClassicAssert.AreEqual("0", response);
// Ensure both users still exist
usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Contain(TestUserA));
Assert.That(usernames, Does.Contain(TestUserB));
}
/// <summary>
/// Tests that the default user cannot be deleted but is replaced by an implicit default user
/// </summary>
[Test]
public async Task DeleteDefaultUser()
{
using var c = TestUtils.GetGarnetClientSession();
c.Connect();
// Verify that default user exists
string[] usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Contain("default"));
// Try to delete the default user
try
{
var response = await c.ExecuteAsync("ACL", "deluser", "default").ConfigureAwait(false);
Assert.Fail("Attempting to delete the default user should raise an error.");
}
catch (Exception exception)
{
ClassicAssert.IsTrue(exception.Message.StartsWith("ERR"));
}
// Verify that default user still exists
usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Contain("default"));
}
/// <summary>
/// Attempts to call deluser without any usernames.
/// </summary>
[Test]
public async Task DeleteNoUser()
{
using var c = TestUtils.GetGarnetClientSession();
c.Connect();
// Add two new users
var response = await c.ExecuteAsync("ACL", "setuser", TestUserA, ">passwd").ConfigureAwait(false);
ClassicAssert.AreEqual("OK", response);
response = await c.ExecuteAsync("ACL", "setuser", TestUserB, ">passwd").ConfigureAwait(false);
ClassicAssert.AreEqual("OK", response);
// Verify that both users exist
string[] usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Contain(TestUserA));
Assert.That(usernames, Does.Contain(TestUserB));
// Try to delete both users in a single command
try
{
response = await c.ExecuteAsync("ACL", "deluser").ConfigureAwait(false);
Assert.Fail("Shouldn't succeed, DELUSER requires arguments");
}
catch (Exception e)
{
ClassicAssert.AreEqual("ERR wrong number of arguments for 'acl|deluser' command", e.Message);
}
// Ensure both users still exist
usernames = await c.ExecuteForArrayAsync("ACL", "users").ConfigureAwait(false);
Assert.That(usernames, Does.Contain(TestUserA));
Assert.That(usernames, Does.Contain(TestUserB));
}
}
}