Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit ae12eef7 authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

Add UnifiedAccountId

parent b82ffdda
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
package net.thunderbird.feature.account

import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
import net.thunderbird.core.architecture.model.Id

/**
 * Constant for the unified account ID.
 *
 * This ID is used to identify the unified account, which is a special account for aggregation purposes.
 *
 * The unified account ID is represented by a nil UUID (all zeros).
 *
 * See [RFC 4122 Section 4.1.7](https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.7) for more details on nil UUIDs.
 */
@OptIn(ExperimentalUuidApi::class)
val UnifiedAccountId: AccountId = Id(Uuid.NIL)

/**
 * Extension property to check if an [AccountId] is the unified account ID.
 */
val AccountId.isUnified: Boolean
    get() = this == UnifiedAccountId

/**
 * Ensures that the [AccountId] is not the unified account ID.
 */
fun AccountId.requireReal(): AccountId {
    check(!isUnified) { "Operation not allowed on unified account" }
    return this
}
+41 −0
Original line number Diff line number Diff line
package net.thunderbird.feature.account

import assertk.assertFailure
import assertk.assertThat
import assertk.assertions.hasMessage
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isTrue
import kotlin.test.Test

class UnifiedAccountIdTest {

    @Test
    fun `unified account id is nil uuid`() {
        assertThat(UnifiedAccountId.asRaw()).isEqualTo("00000000-0000-0000-0000-000000000000")
    }

    @Test
    fun `isUnified returns true for unified account id`() {
        assertThat(UnifiedAccountId.isUnified).isTrue()
    }

    @Test
    fun `isUnified returns false for non-unified account id`() {
        val nonUnifiedAccountId = AccountIdFactory.of("123e4567-e89b-12d3-a456-426614174000")
        assertThat(nonUnifiedAccountId.isUnified).isFalse()
    }

    @Test
    fun `requireReal returns the same id if not unified`() {
        val nonUnifiedAccountId = AccountIdFactory.of("123e4567-e89b-12d3-a456-426614174000")
        assertThat(nonUnifiedAccountId.requireReal()).isEqualTo(nonUnifiedAccountId)
    }

    @Test
    fun `requireReal throws exception if unified`() {
        assertFailure {
            UnifiedAccountId.requireReal()
        }.hasMessage("Operation not allowed on unified account")
    }
}