1515package com .rabbitmq .stream .impl ;
1616
1717import static com .rabbitmq .stream .Cli .*;
18+ import static com .rabbitmq .stream .impl .Assertions .assertThat ;
19+ import static com .rabbitmq .stream .impl .TestUtils .BrokerVersion .RABBITMQ_3_13_0 ;
1820import static org .assertj .core .api .Assertions .assertThat ;
1921import static org .assertj .core .api .Assertions .assertThatThrownBy ;
2022
2123import com .rabbitmq .stream .AuthenticationFailureException ;
2224import com .rabbitmq .stream .Constants ;
2325import com .rabbitmq .stream .StreamException ;
26+ import com .rabbitmq .stream .impl .TestUtils .BrokerVersionAtLeast ;
2427import com .rabbitmq .stream .sasl .*;
2528import java .nio .charset .StandardCharsets ;
2629import java .time .Duration ;
3235public class AuthenticationTest {
3336
3437 TestUtils .ClientFactory cf ;
38+ String brokerVersion ;
3539
3640 @ Test
3741 void authenticateShouldPassWithValidCredentials () {
@@ -120,35 +124,80 @@ void accessToNonExistingVirtualHostShouldFail() {
120124 }
121125
122126 @ Test
123- @ TestUtils . BrokerVersionAtLeast (TestUtils . BrokerVersion . RABBITMQ_3_13_0 )
124- void updateSecret () throws Exception {
127+ @ BrokerVersionAtLeast (RABBITMQ_3_13_0 )
128+ void updateSecretShouldSucceedWithNewCorrectPassword () {
125129 String username = "stream" ;
126130 String password = "stream" ;
127131 String newPassword = "new-password" ;
128132 try {
129133 addUser (username , password );
130134 setPermissions (username , "/" , "^stream.*$" );
131- Client client = cf .get (new Client .ClientParameters ().username ("stream" ).password (username ));
135+ Client client = cf .get (new Client .ClientParameters ().username (username ).password (password ));
132136 changePassword (username , newPassword );
133137 // OK
134138 client .authenticate (credentialsProvider (username , newPassword ));
135- // wrong password
136- assertThatThrownBy (() -> client .authenticate (credentialsProvider (username , "dummy" )))
137- .isInstanceOf (AuthenticationFailureException .class )
138- .hasMessageContaining (String .valueOf (Constants .RESPONSE_CODE_AUTHENTICATION_FAILURE ));
139+ assertThat (client .isOpen ()).isTrue ();
140+ client .close ();
141+ } finally {
142+ deleteUser (username );
143+ }
144+ }
145+
146+ @ Test
147+ @ BrokerVersionAtLeast (RABBITMQ_3_13_0 )
148+ void updateSecretBrokerShouldCloseConnectionWithWrongPassword () {
149+ String u = Utils .DEFAULT_USERNAME , p = u ;
150+ TestUtils .Sync closedSync = TestUtils .sync ();
151+ Client client =
152+ cf .get (
153+ new Client .ClientParameters ()
154+ .username (u )
155+ .password (p )
156+ .shutdownListener (shutdownContext -> closedSync .down ()));
157+ // wrong password
158+ assertThatThrownBy (() -> client .authenticate (credentialsProvider (u , p + "foo" )))
159+ .isInstanceOf (AuthenticationFailureException .class )
160+ .hasMessageContaining (String .valueOf (Constants .RESPONSE_CODE_AUTHENTICATION_FAILURE ));
161+ if (connectionClosedOnUpdateSecretFailure ()) {
162+ assertThat (closedSync ).completes ();
163+ assertThat (client .isOpen ()).isFalse ();
164+ }
165+ client .close ();
166+ }
167+
168+ @ Test
169+ @ BrokerVersionAtLeast (RABBITMQ_3_13_0 )
170+ void updateSecretBrokerShouldCloseConnectionWithUpdatedUsername () {
171+ String username = "stream" ;
172+ String password = "stream" ;
173+ try {
174+ addUser (username , password );
175+ setPermissions (username , "/" , "^stream.*$" );
176+ TestUtils .Sync closedSync = TestUtils .sync ();
177+ Client client =
178+ cf .get (
179+ new Client .ClientParameters ()
180+ .username (username )
181+ .password (password )
182+ .shutdownListener (shutdownContext -> closedSync .down ()));
139183 // cannot change username
140- assertThatThrownBy (() -> client .authenticate (credentialsProvider ("guest" , "guest" )))
184+ String u = Utils .DEFAULT_USERNAME , p = u ;
185+ assertThatThrownBy (() -> client .authenticate (credentialsProvider (u , p )))
141186 .isInstanceOf (StreamException .class )
142187 .hasMessageContaining (
143188 String .valueOf (Constants .RESPONSE_CODE_SASL_CANNOT_CHANGE_USERNAME ));
189+ if (connectionClosedOnUpdateSecretFailure ()) {
190+ assertThat (closedSync ).completes ();
191+ assertThat (client .isOpen ()).isFalse ();
192+ }
144193 client .close ();
145194 } finally {
146195 deleteUser (username );
147196 }
148197 }
149198
150199 @ Test
151- @ TestUtils . BrokerVersionAtLeast (TestUtils .BrokerVersion .RABBITMQ_4_0_0 )
200+ @ BrokerVersionAtLeast (TestUtils .BrokerVersion .RABBITMQ_4_0_0 )
152201 void anonymousAuthenticationShouldWork () {
153202 try (Client ignored =
154203 cf .get (
@@ -158,4 +207,8 @@ void anonymousAuthenticationShouldWork() {
158207 private static CredentialsProvider credentialsProvider (String username , String password ) {
159208 return new DefaultUsernamePasswordCredentialsProvider (username , password );
160209 }
210+
211+ private boolean connectionClosedOnUpdateSecretFailure () {
212+ return Utils .versionCompare (brokerVersion , "4.1.4" ) >= 0 ;
213+ }
161214}
0 commit comments