|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2024, by Samuel Williams. |
| 5 | + |
| 6 | +require 'async/redis/client' |
| 7 | +require 'async/redis/protocol/authenticated' |
| 8 | +require 'sus/fixtures/async' |
| 9 | + |
| 10 | +describe Async::Redis::Protocol::Authenticated do |
| 11 | + include Sus::Fixtures::Async::ReactorContext |
| 12 | + |
| 13 | + let(:endpoint) {Async::Redis.local_endpoint} |
| 14 | + let(:credentials) {["testuser", "testpassword"]} |
| 15 | + let(:protocol) {subject.new(credentials)} |
| 16 | + let(:client) {Async::Redis::Client.new(endpoint, protocol: protocol)} |
| 17 | + |
| 18 | + before do |
| 19 | + # Setup ACL user with limited permissions for testing. |
| 20 | + admin_client = Async::Redis::Client.new(endpoint) |
| 21 | + admin_client.call("ACL", "SETUSER", "testuser", "on", ">" + credentials[1], "+ping", "+auth") |
| 22 | + ensure |
| 23 | + admin_client.close |
| 24 | + end |
| 25 | + |
| 26 | + after do |
| 27 | + # Cleanup ACL user after tests. |
| 28 | + admin_client = Async::Redis::Client.new(endpoint) |
| 29 | + admin_client.call("ACL", "DELUSER", "testuser") |
| 30 | + admin_client.close |
| 31 | + end |
| 32 | + |
| 33 | + it "can authenticate and send allowed commands" do |
| 34 | + response = client.call("PING") |
| 35 | + expect(response).to be == "PONG" |
| 36 | + end |
| 37 | + |
| 38 | + it "rejects commands not allowed by ACL" do |
| 39 | + expect do |
| 40 | + client.call("SET", "key", "value") |
| 41 | + end.to raise_exception(Protocol::Redis::ServerError, message: be =~ /NOPERM/) |
| 42 | + end |
| 43 | +end |
0 commit comments