|
| 1 | +/* |
| 2 | + * Copyright 2011-Present, Redis Ltd. and Contributors |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Licensed under the MIT License. |
| 6 | + * |
| 7 | + * This file contains contributions from third-party contributors |
| 8 | + * licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | +package io.lettuce.core.commands; |
| 21 | + |
| 22 | +import io.lettuce.core.*; |
| 23 | +import io.lettuce.core.api.sync.RedisCommands; |
| 24 | + |
| 25 | +import io.lettuce.test.condition.RedisConditions; |
| 26 | +import org.junit.jupiter.api.*; |
| 27 | + |
| 28 | +import java.util.Arrays; |
| 29 | + |
| 30 | +import static io.lettuce.TestTags.INTEGRATION_TEST; |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 33 | + |
| 34 | +/** |
| 35 | + * Integration tests for ACL commands with Redis modules since Redis 8.0. |
| 36 | + * |
| 37 | + * @author M Sazzadul Hoque |
| 38 | + */ |
| 39 | +@Tag(INTEGRATION_TEST) |
| 40 | +public class ConsolidatedAclCommandIntegrationTests extends RedisContainerIntegrationTests { |
| 41 | + |
| 42 | + private static RedisClient client; |
| 43 | + |
| 44 | + private static RedisCommands<String, String> redis; |
| 45 | + |
| 46 | + @BeforeAll |
| 47 | + public static void setup() { |
| 48 | + RedisURI redisURI = RedisURI.Builder.redis("127.0.0.1").withPort(16379).build(); |
| 49 | + |
| 50 | + client = RedisClient.create(redisURI); |
| 51 | + redis = client.connect().sync(); |
| 52 | + assumeTrue(RedisConditions.of(redis).hasVersionGreaterOrEqualsTo("7.9")); |
| 53 | + } |
| 54 | + |
| 55 | + @AfterAll |
| 56 | + static void teardown() { |
| 57 | + if (client != null) { |
| 58 | + client.shutdown(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + void setUp() { |
| 64 | + redis.flushall(); |
| 65 | + redis.aclUsers().stream().filter(o -> !"default".equals(o)).forEach(redis::aclDeluser); |
| 66 | + redis.aclLogReset(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void listACLCategoriesTest() { |
| 71 | + assertThat(redis.aclCat()).containsAll(Arrays.asList(AclCategory.BLOOM, AclCategory.CUCKOO, AclCategory.CMS, |
| 72 | + AclCategory.TOPK, AclCategory.TDIGEST, AclCategory.SEARCH, AclCategory.TIMESERIES, AclCategory.JSON)); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void grantBloomCommandCatTest() { |
| 77 | + grantModuleCommandCatTest(AclCategory.BLOOM, "bloom"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void grantCuckooCommandCatTest() { |
| 82 | + grantModuleCommandCatTest(AclCategory.CUCKOO, "cuckoo"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void grantCmsCommandCatTest() { |
| 87 | + grantModuleCommandCatTest(AclCategory.CMS, "cms"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void grantTopkCommandCatTest() { |
| 92 | + grantModuleCommandCatTest(AclCategory.TOPK, "topk"); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void grantTdigestCommandCatTest() { |
| 97 | + grantModuleCommandCatTest(AclCategory.TDIGEST, "tdigest"); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void grantSearchCommandCatTest() { |
| 102 | + grantModuleCommandCatTest(AclCategory.SEARCH, "search"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void grantTimeseriesCommandCatTest() { |
| 107 | + grantModuleCommandCatTest(AclCategory.TIMESERIES, "timeseries"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void grantJsonCommandCatTest() { |
| 112 | + grantModuleCommandCatTest(AclCategory.JSON, "json"); |
| 113 | + } |
| 114 | + |
| 115 | + private void grantModuleCommandCatTest(AclCategory category, String categoryStr) { |
| 116 | + assertThat(redis.aclDeluser("foo")).isNotNull(); |
| 117 | + AclSetuserArgs args = AclSetuserArgs.Builder.on().addCategory(category); |
| 118 | + assertThat(redis.aclSetuser("foo", args)).isEqualTo("OK"); |
| 119 | + assertThat(redis.aclGetuser("foo")).contains("-@all +@" + categoryStr); |
| 120 | + assertThat(redis.aclDeluser("foo")).isNotNull(); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments