Fix ending poll power level condition.

This commit is contained in:
Onuray Sahin 2022-05-17 15:44:39 +03:00
parent 4c079cc0ac
commit edd35872f3
3 changed files with 16 additions and 4 deletions

1
changelog.d/6074.bugfix Normal file
View file

@ -0,0 +1 @@
Poll refactoring with unit tests

View file

@ -140,8 +140,10 @@ class DefaultPollAggregationProcessor @Inject constructor() : PollAggregationPro
aggregatedPollSummaryEntity.sourceEvents.add(event.eventId)
}
val myVote = existingVotes.find { it.userId == session.myUserId }?.option
val newSumModel = PollSummaryContent(
myVote = vote,
myVote = myVote,
votes = existingVotes,
votesSummary = newVotesSummary,
totalVotes = totalVotes,
@ -159,7 +161,7 @@ class DefaultPollAggregationProcessor @Inject constructor() : PollAggregationPro
val pollOwnerId = getPollEvent(session, roomId, pollEventId)?.root?.senderId
val isPollOwner = pollOwnerId == event.senderId
if (!isPollOwner || !powerLevelsHelper.isUserAbleToRedact(event.senderId ?: "")) {
if (!isPollOwner && !powerLevelsHelper.isUserAbleToRedact(event.senderId ?: "")) {
Timber.v("## Received poll.end event $pollEventId but user ${event.senderId} doesn't have enough power level in room $roomId")
return false
}

View file

@ -196,6 +196,7 @@ class PollAggregationProcessorTest {
fun setup() {
mockEventAnnotationsSummaryEntity()
mockRoom(A_ROOM_ID, AN_EVENT_ID)
every { session.myUserId } returns A_USER_ID_1
}
@Test
@ -259,10 +260,18 @@ class PollAggregationProcessorTest {
}
@Test
fun `given a poll end event without redaction power level is not processed by poll aggregator`() {
fun `given a poll end event without redaction power level of event owner is processed by poll aggregator`() {
every { realm.instance.createObject(PollResponseAggregatedSummaryEntity::class.java) } returns PollResponseAggregatedSummaryEntity()
val powerLevelsHelper = mockRedactionPowerLevels(A_USER_ID_1, false)
pollAggregationProcessor.handlePollEndEvent(session, powerLevelsHelper, realm.instance, A_POLL_END_EVENT).shouldBeFalse()
pollAggregationProcessor.handlePollEndEvent(session, powerLevelsHelper, realm.instance, A_POLL_END_EVENT).shouldBeTrue()
}
@Test
fun `given a poll end event without redaction power level of non event owner is not processed by poll aggregator`() {
every { realm.instance.createObject(PollResponseAggregatedSummaryEntity::class.java) } returns PollResponseAggregatedSummaryEntity()
val powerLevelsHelper = mockRedactionPowerLevels("another-sender-id", false)
val event = A_POLL_END_EVENT.copy(senderId = "another-sender-id")
pollAggregationProcessor.handlePollEndEvent(session, powerLevelsHelper, realm.instance, event).shouldBeFalse()
}
private inline fun <reified T : RealmModel> RealmQuery<T>.givenEqualTo(fieldName: String, value: String, result: RealmQuery<T>) {