2020-04-06 20:51:32 +02:00
package org.thoughtcrime.securesms.database.model ;
import android.app.Application ;
import androidx.annotation.NonNull ;
2020-10-06 16:21:56 +02:00
import androidx.annotation.Nullable ;
2020-04-06 20:51:32 +02:00
import androidx.test.core.app.ApplicationProvider ;
2020-07-24 17:35:44 +02:00
import com.annimon.stream.Stream ;
2020-04-06 20:51:32 +02:00
import com.google.common.collect.ImmutableMap ;
import org.junit.Before ;
2020-07-24 17:35:44 +02:00
import org.junit.Rule ;
2020-04-06 20:51:32 +02:00
import org.junit.Test ;
import org.junit.runner.RunWith ;
2020-07-24 17:35:44 +02:00
import org.powermock.core.classloader.annotations.PowerMockIgnore ;
import org.powermock.core.classloader.annotations.PrepareForTest ;
import org.powermock.modules.junit4.rule.PowerMockRule ;
2020-04-06 20:51:32 +02:00
import org.robolectric.RobolectricTestRunner ;
import org.robolectric.annotation.Config ;
import org.signal.storageservice.protos.groups.AccessControl ;
2020-05-14 18:59:34 +02:00
import org.signal.storageservice.protos.groups.local.DecryptedGroup ;
2020-04-06 20:51:32 +02:00
import org.signal.storageservice.protos.groups.local.DecryptedGroupChange ;
import org.signal.storageservice.protos.groups.local.DecryptedMember ;
import org.signal.storageservice.protos.groups.local.DecryptedPendingMember ;
2020-07-24 17:35:44 +02:00
import org.thoughtcrime.securesms.testutil.MainThreadUtil ;
import org.thoughtcrime.securesms.util.Util ;
2020-04-06 20:51:32 +02:00
import org.whispersystems.signalservice.api.util.UuidUtil ;
import java.util.Arrays ;
2020-07-24 17:35:44 +02:00
import java.util.List ;
2020-04-06 20:51:32 +02:00
import java.util.Map ;
import java.util.UUID ;
2020-07-24 17:35:44 +02:00
import static java.util.Collections.emptyList ;
2020-04-06 20:51:32 +02:00
import static java.util.Collections.singletonList ;
import static org.hamcrest.CoreMatchers.is ;
import static org.hamcrest.MatcherAssert.assertThat ;
2020-10-06 16:21:56 +02:00
import static org.junit.Assert.assertEquals ;
2020-07-24 17:35:44 +02:00
import static org.junit.Assert.assertFalse ;
2020-04-06 20:51:32 +02:00
import static org.junit.Assert.assertNotNull ;
2020-07-24 17:35:44 +02:00
import static org.junit.Assert.assertTrue ;
2020-08-03 21:12:02 +02:00
import static org.thoughtcrime.securesms.groups.v2.ChangeBuilder.changeBy ;
import static org.thoughtcrime.securesms.groups.v2.ChangeBuilder.changeByUnknown ;
2020-07-29 06:55:20 +02:00
import static org.thoughtcrime.securesms.util.StringUtil.isolateBidi ;
2020-04-06 20:51:32 +02:00
@RunWith ( RobolectricTestRunner . class )
@Config ( manifest = Config . NONE , application = Application . class )
2020-07-24 17:35:44 +02:00
@PowerMockIgnore ( { "org.mockito.*" , "org.robolectric.*" , "android.*" , "androidx.*" } )
@PrepareForTest ( Util . class )
2020-04-06 20:51:32 +02:00
public final class GroupsV2UpdateMessageProducerTest {
private UUID you ;
private UUID alice ;
private UUID bob ;
private GroupsV2UpdateMessageProducer producer ;
2020-07-24 17:35:44 +02:00
@Rule
public PowerMockRule powerMockRule = new PowerMockRule ( ) ;
2020-04-06 20:51:32 +02:00
@Before
public void setup ( ) {
you = UUID . randomUUID ( ) ;
alice = UUID . randomUUID ( ) ;
bob = UUID . randomUUID ( ) ;
GroupsV2UpdateMessageProducer . DescribeMemberStrategy describeMember = createDescriber ( ImmutableMap . of ( alice , "Alice" , bob , "Bob" ) ) ;
producer = new GroupsV2UpdateMessageProducer ( ApplicationProvider . getApplicationContext ( ) , describeMember , you ) ;
}
@Test
public void empty_change ( ) {
DecryptedGroupChange change = changeBy ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice updated the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void empty_change_by_you ( ) {
DecryptedGroupChange change = changeBy ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You updated the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void empty_change_by_unknown ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "The group was updated." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-04-06 20:51:32 +02:00
// Member additions
@Test
public void member_added_member ( ) {
DecryptedGroupChange change = changeBy ( alice )
. addMember ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice added Bob." ) ) ) ;
}
@Test
public void member_added_member_mentions_both ( ) {
DecryptedGroupChange change = changeBy ( alice )
. addMember ( bob )
. build ( ) ;
assertSingleChangeMentioning ( change , Arrays . asList ( alice , bob ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_added_member ( ) {
DecryptedGroupChange change = changeBy ( you )
. addMember ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You added Bob." ) ) ) ;
}
@Test
public void you_added_member_mentions_just_member ( ) {
DecryptedGroupChange change = changeBy ( you )
. addMember ( bob )
. build ( ) ;
assertSingleChangeMentioning ( change , singletonList ( bob ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_added_you ( ) {
DecryptedGroupChange change = changeBy ( alice )
. addMember ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice added you to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_added_you ( ) {
DecryptedGroupChange change = changeBy ( you )
. addMember ( you )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You joined the group via the group link." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_added_themselves ( ) {
DecryptedGroupChange change = changeBy ( bob )
. addMember ( bob )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob joined the group via the group link." ) ) ) ;
2020-07-24 17:35:44 +02:00
}
@Test
public void member_added_themselves_mentions_just_member ( ) {
DecryptedGroupChange change = changeBy ( bob )
. addMember ( bob )
. build ( ) ;
assertSingleChangeMentioning ( change , singletonList ( bob ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_added_you ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. addMember ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You joined the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_added_member ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. addMember ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob joined the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-08-13 20:34:05 +02:00
@Test
public void member_added_you_and_another_where_you_are_not_first ( ) {
DecryptedGroupChange change = changeBy ( bob )
. addMember ( alice )
. addMember ( you )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "Bob added you to the group." , "Bob added Alice." ) ) ) ;
}
@Test
public void unknown_member_added_you_and_another_where_you_are_not_first ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. addMember ( alice )
. addMember ( you )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "You joined the group." , "Alice joined the group." ) ) ) ;
}
@Test
public void you_added_you_and_another_where_you_are_not_first ( ) {
DecryptedGroupChange change = changeBy ( you )
. addMember ( alice )
. addMember ( you )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "You joined the group via the group link." , "You added Alice." ) ) ) ;
2020-08-13 20:34:05 +02:00
}
2020-04-06 20:51:32 +02:00
2020-07-24 17:35:44 +02:00
// Member removals
2020-04-06 20:51:32 +02:00
@Test
public void member_removed_member ( ) {
DecryptedGroupChange change = changeBy ( alice )
. deleteMember ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice removed Bob." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_removed_member ( ) {
DecryptedGroupChange change = changeBy ( you )
. deleteMember ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You removed Bob." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_removed_you ( ) {
DecryptedGroupChange change = changeBy ( alice )
. deleteMember ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice removed you from the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_removed_you ( ) {
DecryptedGroupChange change = changeBy ( you )
. deleteMember ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You left the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_removed_themselves ( ) {
DecryptedGroupChange change = changeBy ( bob )
. deleteMember ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob left the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_removed_member ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. deleteMember ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice is no longer in the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_removed_you ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. deleteMember ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You are no longer in the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-04-06 20:51:32 +02:00
// Member role modifications
@Test
public void you_make_member_admin ( ) {
DecryptedGroupChange change = changeBy ( you )
. promoteToAdmin ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You made Alice an admin." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_makes_member_admin ( ) {
DecryptedGroupChange change = changeBy ( bob )
. promoteToAdmin ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob made Alice an admin." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_makes_you_admin ( ) {
DecryptedGroupChange change = changeBy ( alice )
. promoteToAdmin ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice made you an admin." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_revoked_member_admin ( ) {
DecryptedGroupChange change = changeBy ( you )
. demoteToMember ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You revoked admin privileges from Bob." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_revokes_member_admin ( ) {
DecryptedGroupChange change = changeBy ( bob )
. demoteToMember ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob revoked admin privileges from Alice." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_revokes_your_admin ( ) {
DecryptedGroupChange change = changeBy ( alice )
. demoteToMember ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice revoked your admin privileges." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_makes_member_admin ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. promoteToAdmin ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice is now an admin." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_makes_you_admin ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. promoteToAdmin ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You are now an admin." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_revokes_member_admin ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. demoteToMember ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice is no longer an admin." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_revokes_your_admin ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. demoteToMember ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You are no longer an admin." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-04-06 20:51:32 +02:00
// Member invitation
@Test
public void you_invited_member ( ) {
DecryptedGroupChange change = changeBy ( you )
. invite ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You invited Alice to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_invited_you ( ) {
DecryptedGroupChange change = changeBy ( alice )
. invite ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice invited you to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_invited_1_person ( ) {
DecryptedGroupChange change = changeBy ( alice )
. invite ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice invited 1 person to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_invited_2_persons ( ) {
DecryptedGroupChange change = changeBy ( alice )
. invite ( bob )
. invite ( UUID . randomUUID ( ) )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice invited 2 people to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_invited_3_persons_and_you ( ) {
DecryptedGroupChange change = changeBy ( bob )
. invite ( alice )
. invite ( you )
. invite ( UUID . randomUUID ( ) )
. invite ( UUID . randomUUID ( ) )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "Bob invited you to the group." , "Bob invited 3 people to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
2020-08-06 20:06:15 +02:00
public void unknown_editor_but_known_invitee_invited_you ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. inviteBy ( you , alice )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice invited you to the group." ) ) ) ;
}
@Test
public void unknown_editor_and_unknown_inviter_invited_you ( ) {
2020-06-30 19:46:10 +02:00
DecryptedGroupChange change = changeByUnknown ( )
. invite ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You were invited to the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_invited_1_person ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. invite ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "1 person was invited to the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_invited_2_persons ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. invite ( alice )
. invite ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "2 people were invited to the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_invited_3_persons_and_you ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. invite ( alice )
. invite ( you )
. invite ( UUID . randomUUID ( ) )
. invite ( UUID . randomUUID ( ) )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "You were invited to the group." , "3 people were invited to the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-08-06 20:06:15 +02:00
@Test
public void unknown_editor_invited_3_persons_and_you_inviter_known ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. invite ( alice )
. inviteBy ( you , bob )
. invite ( UUID . randomUUID ( ) )
. invite ( UUID . randomUUID ( ) )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "Bob invited you to the group." , "3 people were invited to the group." ) ) ) ;
}
2020-08-13 20:34:05 +02:00
@Test
public void member_invited_3_persons_and_you_and_added_another_where_you_were_not_first ( ) {
DecryptedGroupChange change = changeBy ( bob )
. addMember ( alice )
. invite ( you )
. invite ( UUID . randomUUID ( ) )
. invite ( UUID . randomUUID ( ) )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "Bob invited you to the group." , "Bob added Alice." , "Bob invited 2 people to the group." ) ) ) ;
}
@Test
public void unknown_editor_but_known_invitee_invited_you_and_added_another_where_you_were_not_first ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. addMember ( bob )
. inviteBy ( you , alice )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "Alice invited you to the group." , "Bob joined the group." ) ) ) ;
}
@Test
public void unknown_editor_and_unknown_inviter_invited_you_and_added_another_where_you_were_not_first ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. addMember ( alice )
. invite ( you )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "You were invited to the group." , "Alice joined the group." ) ) ) ;
}
2020-04-06 20:51:32 +02:00
// Member invitation revocation
@Test
public void member_uninvited_1_person ( ) {
DecryptedGroupChange change = changeBy ( alice )
. uninvite ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice revoked an invitation to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_uninvited_2_people ( ) {
DecryptedGroupChange change = changeBy ( alice )
. uninvite ( bob )
. uninvite ( UUID . randomUUID ( ) )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice revoked 2 invitations to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_uninvited_1_person ( ) {
DecryptedGroupChange change = changeBy ( you )
. uninvite ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You revoked an invitation to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_uninvited_2_people ( ) {
DecryptedGroupChange change = changeBy ( you )
. uninvite ( bob )
. uninvite ( UUID . randomUUID ( ) )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You revoked 2 invitations to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void pending_member_declines_invite ( ) {
DecryptedGroupChange change = changeBy ( bob )
. uninvite ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Someone declined an invitation to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_decline_invite ( ) {
DecryptedGroupChange change = changeBy ( you )
. uninvite ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You declined the invitation to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_revokes_your_invite ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. uninvite ( you )
. build ( ) ;
2020-08-06 20:06:15 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "An admin revoked your invitation to the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_revokes_1_invite ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. uninvite ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "An invitation to the group was revoked." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_revokes_2_invites ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. uninvite ( bob )
. uninvite ( UUID . randomUUID ( ) )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "2 invitations to the group were revoked." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_revokes_yours_and_three_other_invites ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. uninvite ( bob )
. uninvite ( you )
. uninvite ( UUID . randomUUID ( ) )
. uninvite ( UUID . randomUUID ( ) )
. build ( ) ;
2020-08-06 20:06:15 +02:00
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "An admin revoked your invitation to the group." , "3 invitations to the group were revoked." ) ) ) ;
}
@Test
public void your_invite_was_revoked_by_known_member ( ) {
DecryptedGroupChange change = changeBy ( bob )
. uninvite ( you )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob revoked your invitation to the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-04-06 20:51:32 +02:00
// Promote pending members
@Test
public void member_accepts_invite ( ) {
DecryptedGroupChange change = changeBy ( bob )
. promote ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob accepted an invitation to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_accept_invite ( ) {
DecryptedGroupChange change = changeBy ( you )
. promote ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You accepted the invitation to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_promotes_pending_member ( ) {
DecryptedGroupChange change = changeBy ( bob )
. promote ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob added invited member Alice." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_promote_pending_member ( ) {
DecryptedGroupChange change = changeBy ( you )
. promote ( bob )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You added invited member Bob." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void member_promotes_you ( ) {
DecryptedGroupChange change = changeBy ( bob )
. promote ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob added you to the group." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_added_by_invite ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. promote ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You joined the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
@Test
public void unknown_promotes_pending_member ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. promote ( alice )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice joined the group." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-04-06 20:51:32 +02:00
// Title change
@Test
public void member_changes_title ( ) {
DecryptedGroupChange change = changeBy ( alice )
. title ( "New title" )
. build ( ) ;
2020-07-29 06:55:20 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice changed the group name to \"" + isolateBidi ( "New title" ) + "\"." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_change_title ( ) {
DecryptedGroupChange change = changeBy ( you )
. title ( "Title 2" )
. build ( ) ;
2020-07-29 06:55:20 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You changed the group name to \"" + isolateBidi ( "Title 2" ) + "\"." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_changed_title ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. title ( "Title 3" )
. build ( ) ;
2020-07-29 06:55:20 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "The group name has changed to \"" + isolateBidi ( "Title 3" ) + "\"." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-04-06 20:51:32 +02:00
// Avatar change
@Test
public void member_changes_avatar ( ) {
DecryptedGroupChange change = changeBy ( alice )
. avatar ( "Avatar1" )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice changed the group avatar." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_change_avatar ( ) {
DecryptedGroupChange change = changeBy ( you )
. avatar ( "Avatar2" )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You changed the group avatar." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_changed_avatar ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. avatar ( "Avatar3" )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "The group avatar has been changed." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-04-06 20:51:32 +02:00
// Timer change
@Test
public void member_changes_timer ( ) {
DecryptedGroupChange change = changeBy ( bob )
. timer ( 10 )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob set the disappearing message timer to 10 seconds." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_change_timer ( ) {
DecryptedGroupChange change = changeBy ( you )
. timer ( 60 )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You set the disappearing message timer to 1 minute." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_change_timer ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. timer ( 120 )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "The disappearing message timer has been set to 2 minutes." ) ) ) ;
}
@Test
public void unknown_change_timer_mentions_no_one ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. timer ( 120 )
. build ( ) ;
assertSingleChangeMentioning ( change , emptyList ( ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-04-06 20:51:32 +02:00
// Attribute access change
@Test
public void member_changes_attribute_access ( ) {
DecryptedGroupChange change = changeBy ( bob )
. attributeAccess ( AccessControl . AccessRequired . MEMBER )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob changed who can edit group info to \"All members\"." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_changed_attribute_access ( ) {
DecryptedGroupChange change = changeBy ( you )
. attributeAccess ( AccessControl . AccessRequired . ADMINISTRATOR )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You changed who can edit group info to \"Only admins\"." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_changed_attribute_access ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. attributeAccess ( AccessControl . AccessRequired . ADMINISTRATOR )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Who can edit group info has been changed to \"Only admins\"." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-04-06 20:51:32 +02:00
// Membership access change
@Test
public void member_changes_membership_access ( ) {
DecryptedGroupChange change = changeBy ( alice )
. membershipAccess ( AccessControl . AccessRequired . ADMINISTRATOR )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice changed who can edit group membership to \"Only admins\"." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
@Test
public void you_changed_membership_access ( ) {
DecryptedGroupChange change = changeBy ( you )
. membershipAccess ( AccessControl . AccessRequired . MEMBER )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You changed who can edit group membership to \"All members\"." ) ) ) ;
2020-04-06 20:51:32 +02:00
}
2020-06-30 19:46:10 +02:00
@Test
public void unknown_changed_membership_access ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. membershipAccess ( AccessControl . AccessRequired . ADMINISTRATOR )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Who can edit group membership has been changed to \"Only admins\"." ) ) ) ;
2020-06-30 19:46:10 +02:00
}
2020-08-18 19:26:09 +02:00
// Group link access change
@Test
public void you_changed_group_link_access_to_any ( ) {
DecryptedGroupChange change = changeBy ( you )
. inviteLinkAccess ( AccessControl . AccessRequired . ANY )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You turned on the group link with admin approval off." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void you_changed_group_link_access_to_administrator_approval ( ) {
DecryptedGroupChange change = changeBy ( you )
. inviteLinkAccess ( AccessControl . AccessRequired . ADMINISTRATOR )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You turned on the group link with admin approval on." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void you_turned_off_group_link_access ( ) {
DecryptedGroupChange change = changeBy ( you )
. inviteLinkAccess ( AccessControl . AccessRequired . UNSATISFIABLE )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You turned off the group link." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void member_changed_group_link_access_to_any ( ) {
DecryptedGroupChange change = changeBy ( alice )
. inviteLinkAccess ( AccessControl . AccessRequired . ANY )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice turned on the group link with admin approval off." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void member_changed_group_link_access_to_administrator_approval ( ) {
DecryptedGroupChange change = changeBy ( bob )
. inviteLinkAccess ( AccessControl . AccessRequired . ADMINISTRATOR )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob turned on the group link with admin approval on." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void member_turned_off_group_link_access ( ) {
DecryptedGroupChange change = changeBy ( alice )
. inviteLinkAccess ( AccessControl . AccessRequired . UNSATISFIABLE )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice turned off the group link." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void unknown_changed_group_link_access_to_any ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. inviteLinkAccess ( AccessControl . AccessRequired . ANY )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "The group link has been turned on with admin approval off." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void unknown_changed_group_link_access_to_administrator_approval ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. inviteLinkAccess ( AccessControl . AccessRequired . ADMINISTRATOR )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "The group link has been turned on with admin approval on." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void unknown_turned_off_group_link_access ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. inviteLinkAccess ( AccessControl . AccessRequired . UNSATISFIABLE )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "The group link has been turned off." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
2020-10-06 16:21:56 +02:00
// Group link with known previous group state
@Test
public void group_link_access_from_unknown_to_administrator ( ) {
assertEquals ( "You turned on the group link with admin approval on." , describeGroupLinkChange ( you , AccessControl . AccessRequired . UNKNOWN , AccessControl . AccessRequired . ADMINISTRATOR ) ) ;
assertEquals ( "Alice turned on the group link with admin approval on." , describeGroupLinkChange ( alice , AccessControl . AccessRequired . UNKNOWN , AccessControl . AccessRequired . ADMINISTRATOR ) ) ;
assertEquals ( "The group link has been turned on with admin approval on." , describeGroupLinkChange ( null , AccessControl . AccessRequired . UNKNOWN , AccessControl . AccessRequired . ADMINISTRATOR ) ) ;
}
@Test
public void group_link_access_from_administrator_to_unsatisfiable ( ) {
assertEquals ( "You turned off the group link." , describeGroupLinkChange ( you , AccessControl . AccessRequired . ADMINISTRATOR , AccessControl . AccessRequired . UNSATISFIABLE ) ) ;
assertEquals ( "Bob turned off the group link." , describeGroupLinkChange ( bob , AccessControl . AccessRequired . ADMINISTRATOR , AccessControl . AccessRequired . UNSATISFIABLE ) ) ;
assertEquals ( "The group link has been turned off." , describeGroupLinkChange ( null , AccessControl . AccessRequired . ADMINISTRATOR , AccessControl . AccessRequired . UNSATISFIABLE ) ) ;
}
@Test
public void group_link_access_from_unsatisfiable_to_administrator ( ) {
assertEquals ( "You turned on the group link with admin approval on." , describeGroupLinkChange ( you , AccessControl . AccessRequired . UNSATISFIABLE , AccessControl . AccessRequired . ADMINISTRATOR ) ) ;
assertEquals ( "Alice turned on the group link with admin approval on." , describeGroupLinkChange ( alice , AccessControl . AccessRequired . UNSATISFIABLE , AccessControl . AccessRequired . ADMINISTRATOR ) ) ;
assertEquals ( "The group link has been turned on with admin approval on." , describeGroupLinkChange ( null , AccessControl . AccessRequired . UNSATISFIABLE , AccessControl . AccessRequired . ADMINISTRATOR ) ) ;
}
@Test
public void group_link_access_from_administrator_to_any ( ) {
assertEquals ( "You turned off admin approval for the group link." , describeGroupLinkChange ( you , AccessControl . AccessRequired . ADMINISTRATOR , AccessControl . AccessRequired . ANY ) ) ;
assertEquals ( "Bob turned off admin approval for the group link." , describeGroupLinkChange ( bob , AccessControl . AccessRequired . ADMINISTRATOR , AccessControl . AccessRequired . ANY ) ) ;
assertEquals ( "The admin approval for the group link has been turned off." , describeGroupLinkChange ( null , AccessControl . AccessRequired . ADMINISTRATOR , AccessControl . AccessRequired . ANY ) ) ;
}
@Test
public void group_link_access_from_any_to_administrator ( ) {
assertEquals ( "You turned on admin approval for the group link." , describeGroupLinkChange ( you , AccessControl . AccessRequired . ANY , AccessControl . AccessRequired . ADMINISTRATOR ) ) ;
assertEquals ( "Bob turned on admin approval for the group link." , describeGroupLinkChange ( bob , AccessControl . AccessRequired . ANY , AccessControl . AccessRequired . ADMINISTRATOR ) ) ;
assertEquals ( "The admin approval for the group link has been turned on." , describeGroupLinkChange ( null , AccessControl . AccessRequired . ANY , AccessControl . AccessRequired . ADMINISTRATOR ) ) ;
}
private String describeGroupLinkChange ( @Nullable UUID editor , @NonNull AccessControl . AccessRequired fromAccess , AccessControl . AccessRequired toAccess ) {
DecryptedGroup previousGroupState = DecryptedGroup . newBuilder ( )
. setAccessControl ( AccessControl . newBuilder ( )
. setAddFromInviteLink ( fromAccess ) )
. build ( ) ;
DecryptedGroupChange change = ( editor ! = null ? changeBy ( editor ) : changeByUnknown ( ) ) . inviteLinkAccess ( toAccess )
. build ( ) ;
List < String > strings = describeChange ( previousGroupState , change ) ;
assertEquals ( 1 , strings . size ( ) ) ;
return strings . get ( 0 ) ;
}
2020-08-18 19:26:09 +02:00
// Group link reset
@Test
public void you_reset_group_link ( ) {
DecryptedGroupChange change = changeBy ( you )
. resetGroupLink ( )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You reset the group link." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void member_reset_group_link ( ) {
DecryptedGroupChange change = changeBy ( alice )
. resetGroupLink ( )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice reset the group link." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void unknown_reset_group_link ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. resetGroupLink ( )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "The group link has been reset." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
/ * *
* When the group link is turned on and reset in the same change , assume this is the first time
* the link password it being set and do not show reset message .
* /
@Test
public void member_changed_group_link_access_to_on_and_reset ( ) {
DecryptedGroupChange change = changeBy ( alice )
. inviteLinkAccess ( AccessControl . AccessRequired . ANY )
. resetGroupLink ( )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice turned on the group link with admin approval off." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
/ * *
* When the group link is turned on and reset in the same change , assume this is the first time
* the link password it being set and do not show reset message .
* /
@Test
public void you_changed_group_link_access_to_on_and_reset ( ) {
DecryptedGroupChange change = changeBy ( you )
. inviteLinkAccess ( AccessControl . AccessRequired . ADMINISTRATOR )
. resetGroupLink ( )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "You turned on the group link with admin approval on." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void you_changed_group_link_access_to_off_and_reset ( ) {
DecryptedGroupChange change = changeBy ( you )
. inviteLinkAccess ( AccessControl . AccessRequired . UNSATISFIABLE )
. resetGroupLink ( )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( Arrays . asList ( "You turned off the group link." , "You reset the group link." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
// Group link request
@Test
public void you_requested_to_join_the_group ( ) {
DecryptedGroupChange change = changeBy ( you )
. requestJoin ( )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "You sent a request to join the group." ) ) ) ;
}
@Test
public void member_requested_to_join_the_group ( ) {
DecryptedGroupChange change = changeBy ( bob )
. requestJoin ( )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob requested to join via the group link." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void unknown_requested_to_join_the_group ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. requestJoin ( alice )
. build ( ) ;
2020-09-03 23:06:41 +02:00
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice requested to join via the group link." ) ) ) ;
2020-08-18 19:26:09 +02:00
}
@Test
public void member_approved_your_join_request ( ) {
DecryptedGroupChange change = changeBy ( bob )
. approveRequest ( you )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "Bob approved your request to join the group." ) ) ) ;
}
@Test
public void member_approved_another_join_request ( ) {
DecryptedGroupChange change = changeBy ( alice )
. approveRequest ( bob )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice approved a request to join the group from Bob." ) ) ) ;
}
2020-08-26 17:51:25 +02:00
@Test
public void you_approved_another_join_request ( ) {
DecryptedGroupChange change = changeBy ( you )
. approveRequest ( alice )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "You approved a request to join the group from Alice." ) ) ) ;
}
2020-08-18 19:26:09 +02:00
@Test
public void unknown_approved_your_join_request ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. approveRequest ( you )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "Your request to join the group has been approved." ) ) ) ;
}
@Test
public void unknown_approved_another_join_request ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. approveRequest ( bob )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "A request to join the group from Bob has been approved." ) ) ) ;
}
@Test
public void member_denied_another_join_request ( ) {
DecryptedGroupChange change = changeBy ( alice )
. denyRequest ( bob )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice denied a request to join the group from Bob." ) ) ) ;
}
@Test
public void member_denied_your_join_request ( ) {
DecryptedGroupChange change = changeBy ( alice )
. denyRequest ( you )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "Your request to join the group has been denied by an admin." ) ) ) ;
}
2020-08-26 17:51:25 +02:00
@Test
public void you_cancelled_your_join_request ( ) {
DecryptedGroupChange change = changeBy ( you )
. denyRequest ( you )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "You canceled your request to join the group." ) ) ) ;
}
@Test
public void member_cancelled_their_join_request ( ) {
DecryptedGroupChange change = changeBy ( alice )
. denyRequest ( alice )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "Alice canceled their request to join the group." ) ) ) ;
}
2020-08-18 19:26:09 +02:00
@Test
public void unknown_denied_your_join_request ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. denyRequest ( you )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "Your request to join the group has been denied by an admin." ) ) ) ;
}
@Test
public void unknown_denied_another_join_request ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. denyRequest ( bob )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( singletonList ( "A request to join the group from Bob has been denied." ) ) ) ;
}
2020-04-06 20:51:32 +02:00
// Multiple changes
@Test
public void multiple_changes ( ) {
DecryptedGroupChange change = changeBy ( alice )
. addMember ( bob )
. membershipAccess ( AccessControl . AccessRequired . MEMBER )
. title ( "Title" )
2020-08-13 20:34:05 +02:00
. addMember ( you )
2020-04-06 20:51:32 +02:00
. timer ( 300 )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( Arrays . asList (
2020-08-13 20:34:05 +02:00
"Alice added you to the group." ,
2020-04-06 20:51:32 +02:00
"Alice added Bob." ,
2020-07-29 06:55:20 +02:00
"Alice changed the group name to \"" + isolateBidi ( "Title" ) + "\"." ,
2020-04-06 20:51:32 +02:00
"Alice set the disappearing message timer to 5 minutes." ,
"Alice changed who can edit group membership to \"All members\"." ) ) ) ;
}
2020-07-24 21:27:39 +02:00
@Test
public void multiple_changes_leave_and_promote ( ) {
DecryptedGroupChange change = changeBy ( alice )
. deleteMember ( alice )
. promoteToAdmin ( bob )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( Arrays . asList (
"Alice made Bob an admin." ,
"Alice left the group." ) ) ) ;
}
@Test
public void multiple_changes_leave_and_promote_by_unknown ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. deleteMember ( alice )
. promoteToAdmin ( bob )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( Arrays . asList (
"Bob is now an admin." ,
"Alice is no longer in the group." ) ) ) ;
}
2020-06-30 19:46:10 +02:00
@Test
public void multiple_changes_by_unknown ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. addMember ( bob )
. membershipAccess ( AccessControl . AccessRequired . MEMBER )
. title ( "Title 2" )
. avatar ( "Avatar 1" )
. timer ( 600 )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeChange ( change ) , is ( Arrays . asList (
2020-06-30 19:46:10 +02:00
"Bob joined the group." ,
2020-07-29 06:55:20 +02:00
"The group name has changed to \"" + isolateBidi ( "Title 2" ) + "\"." ,
2020-06-30 19:46:10 +02:00
"The group avatar has been changed." ,
"The disappearing message timer has been set to 10 minutes." ,
"Who can edit group membership has been changed to \"All members\"." ) ) ) ;
}
2020-07-24 21:27:39 +02:00
@Test
public void multiple_changes_join_and_leave_by_unknown ( ) {
DecryptedGroupChange change = changeByUnknown ( )
. addMember ( alice )
. promoteToAdmin ( alice )
. deleteMember ( alice )
. title ( "Updated title" )
. build ( ) ;
assertThat ( describeChange ( change ) , is ( Arrays . asList (
"Alice joined the group." ,
"Alice is now an admin." ,
2020-07-29 06:55:20 +02:00
"The group name has changed to \"" + isolateBidi ( "Updated title" ) + "\"." ,
2020-07-24 21:27:39 +02:00
"Alice is no longer in the group." ) ) ) ;
}
2020-05-14 18:59:34 +02:00
// Group state without a change record
2020-09-05 15:09:31 +02:00
@Test
public void you_created_a_group_change_not_found ( ) {
DecryptedGroup group = newGroupBy ( you , 0 )
. build ( ) ;
assertThat ( describeNewGroup ( group ) , is ( "You joined the group." ) ) ;
}
2020-05-14 18:59:34 +02:00
@Test
public void you_created_a_group ( ) {
DecryptedGroup group = newGroupBy ( you , 0 )
. build ( ) ;
2020-09-05 15:09:31 +02:00
DecryptedGroupChange change = changeBy ( you )
. addMember ( alice )
. addMember ( you )
. addMember ( bob )
. title ( "New title" )
. build ( ) ;
assertThat ( describeNewGroup ( group , change ) , is ( "You created the group." ) ) ;
}
@Test
public void alice_created_a_group_change_not_found ( ) {
DecryptedGroup group = newGroupBy ( alice , 0 )
. member ( you )
. build ( ) ;
assertThat ( describeNewGroup ( group ) , is ( "You joined the group." ) ) ;
2020-05-14 18:59:34 +02:00
}
@Test
public void alice_created_a_group ( ) {
DecryptedGroup group = newGroupBy ( alice , 0 )
. member ( you )
. build ( ) ;
2020-09-05 15:09:31 +02:00
DecryptedGroupChange change = changeBy ( alice )
. addMember ( you )
. addMember ( alice )
. addMember ( bob )
. title ( "New title" )
. build ( ) ;
assertThat ( describeNewGroup ( group , change ) , is ( "Alice added you to the group." ) ) ;
2020-05-14 18:59:34 +02:00
}
@Test
public void alice_created_a_group_above_zero ( ) {
DecryptedGroup group = newGroupBy ( alice , 1 )
. member ( you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeNewGroup ( group ) , is ( "You joined the group." ) ) ;
2020-05-14 18:59:34 +02:00
}
@Test
public void you_were_invited_to_a_group ( ) {
DecryptedGroup group = newGroupBy ( alice , 0 )
. invite ( bob , you )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeNewGroup ( group ) , is ( "Bob invited you to the group." ) ) ;
2020-05-14 18:59:34 +02:00
}
@Test
public void describe_a_group_you_are_not_in ( ) {
DecryptedGroup group = newGroupBy ( alice , 1 )
. build ( ) ;
2020-07-24 17:35:44 +02:00
assertThat ( describeNewGroup ( group ) , is ( "Group updated." ) ) ;
}
private @NonNull List < String > describeChange ( @NonNull DecryptedGroupChange change ) {
2020-10-06 16:21:56 +02:00
return describeChange ( null , change ) ;
}
private @NonNull List < String > describeChange ( @Nullable DecryptedGroup previousGroupState ,
@NonNull DecryptedGroupChange change )
{
2020-07-24 17:35:44 +02:00
MainThreadUtil . setMainThread ( false ) ;
2020-10-06 16:21:56 +02:00
return Stream . of ( producer . describeChanges ( previousGroupState , change ) )
2020-07-24 17:35:44 +02:00
. map ( UpdateDescription : : getString )
. toList ( ) ;
2020-05-14 18:59:34 +02:00
}
2020-07-24 17:35:44 +02:00
private @NonNull String describeNewGroup ( @NonNull DecryptedGroup group ) {
2020-09-05 15:09:31 +02:00
return describeNewGroup ( group , DecryptedGroupChange . getDefaultInstance ( ) ) ;
}
private @NonNull String describeNewGroup ( @NonNull DecryptedGroup group , @NonNull DecryptedGroupChange groupChange ) {
2020-07-24 17:35:44 +02:00
MainThreadUtil . setMainThread ( false ) ;
2020-09-05 15:09:31 +02:00
return producer . describeNewGroup ( group , groupChange ) . getString ( ) ;
2020-07-24 17:35:44 +02:00
}
private static GroupStateBuilder newGroupBy ( UUID foundingMember , int revision ) {
2020-05-14 18:59:34 +02:00
return new GroupStateBuilder ( foundingMember , revision ) ;
}
2020-07-24 17:35:44 +02:00
private void assertSingleChangeMentioning ( DecryptedGroupChange change , List < UUID > expectedMentions ) {
2020-10-06 16:21:56 +02:00
List < UpdateDescription > changes = producer . describeChanges ( null , change ) ;
2020-07-24 17:35:44 +02:00
assertThat ( changes . size ( ) , is ( 1 ) ) ;
UpdateDescription description = changes . get ( 0 ) ;
assertThat ( description . getMentioned ( ) , is ( expectedMentions ) ) ;
if ( expectedMentions . isEmpty ( ) ) {
assertTrue ( description . isStringStatic ( ) ) ;
} else {
assertFalse ( description . isStringStatic ( ) ) ;
}
}
2020-05-14 18:59:34 +02:00
private static class GroupStateBuilder {
private final DecryptedGroup . Builder builder ;
2020-05-29 19:35:40 +02:00
GroupStateBuilder ( @NonNull UUID foundingMember , int revision ) {
2020-05-14 18:59:34 +02:00
builder = DecryptedGroup . newBuilder ( )
2020-05-29 19:35:40 +02:00
. setRevision ( revision )
2020-05-14 18:59:34 +02:00
. addMembers ( DecryptedMember . newBuilder ( )
. setUuid ( UuidUtil . toByteString ( foundingMember ) ) ) ;
}
GroupStateBuilder invite ( @NonNull UUID inviter , @NonNull UUID invitee ) {
builder . addPendingMembers ( DecryptedPendingMember . newBuilder ( )
. setUuid ( UuidUtil . toByteString ( invitee ) )
. setAddedByUuid ( UuidUtil . toByteString ( inviter ) ) ) ;
return this ;
}
GroupStateBuilder member ( @NonNull UUID member ) {
builder . addMembers ( DecryptedMember . newBuilder ( )
. setUuid ( UuidUtil . toByteString ( member ) ) ) ;
return this ;
}
public DecryptedGroup build ( ) {
return builder . build ( ) ;
}
}
2020-05-02 00:13:23 +02:00
private static @NonNull GroupsV2UpdateMessageProducer . DescribeMemberStrategy createDescriber ( @NonNull Map < UUID , String > map ) {
2020-04-06 20:51:32 +02:00
return uuid - > {
String name = map . get ( uuid ) ;
assertNotNull ( name ) ;
return name ;
} ;
}
}