UserStatusIntegrationTest.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.jivesoftware.smack.packet.Presence;
  21. import org.jivesoftware.smackx.muc.packet.MUCUser;

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

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

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

  40.     /**
  41.      * Verifies that when a member gets its membership removed in an open room, the appropriate event listener is invoked.
  42.      *
  43.      * @throws Exception On unexpected results
  44.      */
  45.     @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\".")
  46.     public void testMembershipRevokedInOpenRoom() throws Exception {
  47.         // Setup test fixture.
  48.         final EntityBareJid mucAddress = getRandomRoom("smack-inttest-userstatus-membership-revoked-membersonly");
  49.         final MultiUserChat mucAsSeenByOwner = mucManagerOne.getMultiUserChat(mucAddress);
  50.         final MultiUserChat mucAsSeenByTarget = mucManagerTwo.getMultiUserChat(mucAddress);

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

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

  57.             final SimpleResultSyncPoint targetSeesRevoke = new SimpleResultSyncPoint();
  58.             mucAsSeenByTarget.addUserStatusListener(new UserStatusListener() {
  59.                 @Override
  60.                 public void membershipRevoked() {
  61.                     targetSeesRevoke.signal();
  62.                 }
  63.             });

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

  66.             // Verify result.
  67.             assertResult(targetSeesRevoke, "Expected '" + conTwo.getUser() + "' (using nickname '" + mucAddressTarget.getResourcepart() + "') to be notified that their membership status was removed by '" + conOne.getUser() + "' (using nickname '" + mucAddressOwner.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-userstatus-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 targetSeesRevoke = new SimpleResultSyncPoint();
  91.             final SimpleResultSyncPoint targetSeesRemove = new SimpleResultSyncPoint();
  92.             mucAsSeenByTarget.addUserStatusListener(new UserStatusListener() {
  93.                 @Override
  94.                 public void removed(MUCUser mucUser, Presence presence) {
  95.                     targetSeesRemove.signal();
  96.                 }

  97.                 @Override
  98.                 public void membershipRevoked() {
  99.                     targetSeesRevoke.signal();
  100.                 }
  101.             });

  102.             // Execute system under test.
  103.             mucAsSeenByOwner.revokeMembership(conTwo.getUser().asBareJid());

  104.             // Verify result.
  105.             assertResult(targetSeesRemove, "Expected '" + conTwo.getUser() + "' (using nickname '" + mucAddressTarget.getResourcepart() + "') to be notified that it is removed from '" + mucAddress + "' which is a member-only room, as their membership status was removed by '" + conOne.getUser() + "' (using nickname '" + mucAddressOwner.getResourcepart() + "') (but did not).");
  106.             assertResult(targetSeesRevoke, "Expected '" + conTwo.getUser() + "' (using nickname '" + mucAddressTarget.getResourcepart() + "') to be notified that their membership status was removed by '" + conOne.getUser() + "' (using nickname '" + mucAddressOwner.getResourcepart() + "') in '" + mucAddress + "' (but did not).");
  107.         } finally {
  108.             // Clean up test fixture.
  109.             tryDestroy(mucAsSeenByOwner);
  110.         }
  111.     }
  112. }