ParticipantStatusIntegrationTest.java

  1. /**
  2.  *
  3.  * Copyright 2024 Guus der Kinderen
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.muc;

  18. import org.jivesoftware.smack.SmackException;
  19. import org.jivesoftware.smack.XMPPException;

  20. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  21. import org.igniterealtime.smack.inttest.TestNotPossibleException;
  22. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
  23. import org.igniterealtime.smack.inttest.annotations.SpecificationReference;
  24. import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
  25. import org.jxmpp.jid.EntityBareJid;
  26. import org.jxmpp.jid.EntityFullJid;
  27. import org.jxmpp.jid.impl.JidCreate;
  28. import org.jxmpp.jid.parts.Resourcepart;
  29. import org.jxmpp.stringprep.XmppStringprepException;

  30. /**
  31.  * Tests that verify the correct functionality of Smack's {@link ParticipantStatusListener}.
  32.  */
  33. @SpecificationReference(document = "XEP-0045", version = "1.34.6")
  34. public class ParticipantStatusIntegrationTest extends AbstractMultiUserChatIntegrationTest {

  35.     public ParticipantStatusIntegrationTest(SmackIntegrationTestEnvironment environment) throws SmackException.NoResponseException, XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, TestNotPossibleException, MultiUserChatException.MucAlreadyJoinedException, MultiUserChatException.MissingMucCreationAcknowledgeException, XmppStringprepException, MultiUserChatException.NotAMucServiceException {
  36.         super(environment);
  37.     }

  38.     /**
  39.      * Verifies that when a member gets its membership removed in an open room, the appropriate event listener is invoked.
  40.      *
  41.      * @throws Exception On unexpected results
  42.      */
  43.     @SmackIntegrationTest(section = "9.4", quote = "An admin might want to revoke a user's membership [...] The service MUST then send updated presence from this individual to all occupants, indicating the loss of membership by sending a presence element that contains an <x/> element qualified by the 'http://jabber.org/protocol/muc#user' namespace and containing an <item/> child with the 'affiliation' attribute set to a value of \"none\".")
  44.     public void testMembershipRevokedInOpenRoom() throws Exception {
  45.         // Setup test fixture.
  46.         final EntityBareJid mucAddress = getRandomRoom("smack-inttest-participantstatus-membership-revoked-open");
  47.         final MultiUserChat mucAsSeenByOwner = mucManagerOne.getMultiUserChat(mucAddress);
  48.         final MultiUserChat mucAsSeenByTarget = mucManagerTwo.getMultiUserChat(mucAddress);

  49.         final EntityFullJid mucAddressOwner = JidCreate.entityFullFrom(mucAddress, Resourcepart.from("owner-" + randomString));
  50.         final EntityFullJid mucAddressTarget = JidCreate.entityFullFrom(mucAddress, Resourcepart.from("target-" + randomString));

  51.         createMuc(mucAsSeenByOwner, mucAddressOwner.getResourcepart());
  52.         try {
  53.             mucAsSeenByOwner.grantMembership(conTwo.getUser().asBareJid());
  54.             mucAsSeenByTarget.join(mucAddressTarget.getResourcepart());

  55.             final SimpleResultSyncPoint ownerSeesRevoke = new SimpleResultSyncPoint();
  56.             mucAsSeenByOwner.addParticipantStatusListener(new ParticipantStatusListener() {
  57.                 @Override
  58.                 public void membershipRevoked(EntityFullJid participant) {
  59.                     if (mucAddressTarget.equals(participant)) {
  60.                         ownerSeesRevoke.signal();
  61.                     }
  62.                 }
  63.             });

  64.             // Execute system under test.
  65.             mucAsSeenByOwner.revokeMembership(conTwo.getUser().asBareJid());

  66.             // Verify result.
  67.             assertResult(ownerSeesRevoke, "Expected '" + conOne.getUser() + "' to be notified of the revocation of membership of '" + conTwo.getUser() + "' (using nickname '" + mucAddressTarget.getResourcepart() + "') in '" + mucAddress + "' (but did not).");
  68.         } finally {
  69.             // Clean up test fixture.
  70.             tryDestroy(mucAsSeenByOwner);
  71.         }
  72.     }

  73.     /**
  74.      * Verifies that when a member gets its membership removed in a members-only room, the appropriate event listeners are invoked.
  75.      *
  76.      * @throws Exception On unexpected results
  77.      */
  78.     @SmackIntegrationTest(section = "9.4", quote = "An admin might want to revoke a user's membership [...] If the room is members-only, the service MUST remove the user from the room, including a status code of 321 to indicate that the user was removed because of an affiliation change, and inform all remaining occupants")
  79.     public void testMembershipRevokedInMemberOnlyRoom() throws Exception {
  80.         // Setup test fixture.
  81.         final EntityBareJid mucAddress = getRandomRoom("smack-inttest-participantstatus-membership-revoked-membersonly");
  82.         final MultiUserChat mucAsSeenByOwner = mucManagerOne.getMultiUserChat(mucAddress);
  83.         final MultiUserChat mucAsSeenByTarget = mucManagerTwo.getMultiUserChat(mucAddress);

  84.         final EntityFullJid mucAddressOwner = JidCreate.entityFullFrom(mucAddress, Resourcepart.from("owner-" + randomString));
  85.         final EntityFullJid mucAddressTarget = JidCreate.entityFullFrom(mucAddress, Resourcepart.from("target-" + randomString));

  86.         createMembersOnlyMuc(mucAsSeenByOwner, mucAddressOwner.getResourcepart());
  87.         try {
  88.             mucAsSeenByOwner.grantMembership(conTwo.getUser().asBareJid());
  89.             mucAsSeenByTarget.join(mucAddressTarget.getResourcepart());

  90.             final SimpleResultSyncPoint ownerSeesRevoke = new SimpleResultSyncPoint();
  91.             final SimpleResultSyncPoint ownerSeesDeparture = new SimpleResultSyncPoint();
  92.             mucAsSeenByOwner.addParticipantStatusListener(new ParticipantStatusListener() {
  93.                 @Override
  94.                 public void membershipRevoked(EntityFullJid participant) {
  95.                     if (mucAddressTarget.equals(participant)) {
  96.                         ownerSeesRevoke.signal();
  97.                     }
  98.                 }

  99.                 @Override
  100.                 public void parted(EntityFullJid participant) {
  101.                     if (mucAddressTarget.equals(participant)) {
  102.                         ownerSeesDeparture.signal();
  103.                     }
  104.                 }
  105.             });

  106.             // Execute system under test.
  107.             mucAsSeenByOwner.revokeMembership(conTwo.getUser().asBareJid());

  108.             // Verify result.
  109.             assertResult(ownerSeesRevoke, "Expected '" + conOne.getUser() + "' to be notified of the revocation of membership of '" + conTwo.getUser() + "' (using nickname '" + mucAddressTarget.getResourcepart() + "') in '" + mucAddress + "' (but did not).");
  110.             assertResult(ownerSeesDeparture, "Expected '" + conOne.getUser() + "' to be notified of '" + conTwo.getUser() + "' (using nickname '" + mucAddressTarget.getResourcepart() + "') departing '" + mucAddress + "' (but did not).");
  111.         } finally {
  112.             // Clean up test fixture.
  113.             tryDestroy(mucAsSeenByOwner);
  114.         }
  115.     }
  116. }