Skip to content

Commit

Permalink
feat: harden ClientCapabilityDTO for api v7 [WPB-14882] (#3151)
Browse files Browse the repository at this point in the history
* feat: harden ClientCapabilityDTO for api v7

* add tests

* detekt

* detekt

* chore: disable folders update handler (#3161)

* fix: Login to second device does not have MLS capabilities RC (#3165)

* feat: add usecase to get team url (WPB-14872) (#3157) (#3168)

* feat: add usecase to get team url

* feat:detekt

* feat: detekt

* feat: detekt

(cherry picked from commit abcd037)

* chore: upgrade to cc3, fixes new_transaction in paralell (#3170)

* test: add tests for old format parser in capabilities (#3173)

* Update libs.versions.toml

* Update UserPropertiesEventReceiver.kt

* Update UserPropertiesEventReceiver.kt

* Update UserPropertiesEventReceiverTest.kt

* fix

* fix tests

* missing imports

* fix circular dependency

* Trigger CI

* remove not needed dependency

---------

Co-authored-by: Jakub Żerko <[email protected]>
Co-authored-by: Yamil Medina <[email protected]>
Co-authored-by: boris <[email protected]>
Co-authored-by: Oussama Hassine <[email protected]>
  • Loading branch information
5 people authored Jan 9, 2025
1 parent f506f90 commit 5788184
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ enum class DeviceType {
Unknown;
}

enum class ClientCapability {
LegalHoldImplicitConsent;
sealed class ClientCapability {
data object LegalHoldImplicitConsent : ClientCapability()
data class Unknown(val name: String) : ClientCapability()
}

data class OtherUserClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,12 @@ class ClientMapper(

private fun toClientCapabilityDTO(clientCapability: ClientCapability): ClientCapabilityDTO = when (clientCapability) {
ClientCapability.LegalHoldImplicitConsent -> ClientCapabilityDTO.LegalHoldImplicitConsent
is ClientCapability.Unknown -> ClientCapabilityDTO.Unknown(clientCapability.name)
}

private fun fromClientCapabilityDTO(clientCapabilityDTO: ClientCapabilityDTO): ClientCapability = when (clientCapabilityDTO) {
ClientCapabilityDTO.LegalHoldImplicitConsent -> ClientCapability.LegalHoldImplicitConsent
is ClientCapabilityDTO.Unknown -> ClientCapability.Unknown(clientCapabilityDTO.name)
}

fun fromOtherUsersClientsDTO(otherUsersClients: List<ClientEntity>): List<OtherUserClient> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2164,14 +2164,15 @@ class UserSessionScope internal constructor(
kaliumLogger = userScopedLogger,
)

val getTeamUrlUseCase: GetTeamUrlUseCase by lazy {
GetTeamUrlUseCase(
val getTeamUrlUseCase: GetTeamUrlUseCase
get() = GetTeamUrlUseCase(
userId,
authenticationScope.serverConfigRepository,
)
}

private val inCallReactionsRepository: InCallReactionsRepository = InCallReactionsDataSource()
private val inCallReactionsRepository: InCallReactionsRepository by lazy {
InCallReactionsDataSource()
}

/**
* This will start subscribers of observable work per user session, as long as the user is logged in.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class UserPropertiesEventReceiverTest {
suspend fun withUpdateConversationFolders() = apply {
coEvery {
conversationFolderRepository.updateConversationFolders(any())
}.returns(Either.Right(Unit))
}.returns(Either.Right(Unit))
}

fun arrange() = this to userPropertiesEventReceiver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import com.wire.kalium.network.api.authenticated.client.ClientCapabilityDTO
import com.wire.kalium.network.api.authenticated.client.ClientDTO
import com.wire.kalium.network.api.authenticated.client.ClientTypeDTO
import com.wire.kalium.network.api.authenticated.client.DeviceTypeDTO
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

object ClientResponseJson {
private val jsonProvider = { serializable: ClientDTO ->
Expand All @@ -36,7 +38,7 @@ object ClientResponseJson {
| "cookie": "${serializable.cookie}",
| "model": "${serializable.model}",
| "capabilities": [
| "${serializable.capabilities[0]}"
| ${Json.encodeToString(serializable.capabilities[0])}
| ],
| "mls_public_keys": ${serializable.mlsPublicKeys}
|}
Expand All @@ -57,9 +59,9 @@ object ClientResponseJson {
| "model": "${serializable.model}",
| "capabilities": {
| "capabilities": [
| "${serializable.capabilities[0]}"
| ${Json.encodeToString(serializable.capabilities[0])}
| ]
| }
| },
| "mls_public_keys": ${serializable.mlsPublicKeys}
|}
""".trimMargin()
Expand Down
1 change: 0 additions & 1 deletion network-model/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ kotlin {

// KTX
implementation(libs.ktxDateTime)

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import com.wire.kalium.network.api.authenticated.client.DeviceTypeDTO.Unknown
import com.wire.kalium.network.api.authenticated.prekey.PreKeyDTO
import com.wire.kalium.network.api.model.MLSPublicKey
import com.wire.kalium.network.api.model.UserId
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

@Serializable
data class RegisterClientRequest(
Expand Down Expand Up @@ -86,12 +92,33 @@ enum class DeviceTypeDTO {
}
}

@Serializable
enum class ClientCapabilityDTO {
@Serializable(with = ClientCapabilityDTOSerializer::class)
sealed class ClientCapabilityDTO {
@SerialName("legalhold-implicit-consent")
LegalHoldImplicitConsent {
override fun toString(): String {
return "legalhold-implicit-consent"
data object LegalHoldImplicitConsent : ClientCapabilityDTO()
data class Unknown(val name: String) : ClientCapabilityDTO()
}

object ClientCapabilityDTOSerializer : KSerializer<ClientCapabilityDTO> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor(
serialName = "ClientCapabilityDTO",
kind = PrimitiveKind.STRING
)

override fun serialize(encoder: Encoder, value: ClientCapabilityDTO) {
when (value) {
is ClientCapabilityDTO.LegalHoldImplicitConsent ->
encoder.encodeString("legalhold-implicit-consent")

is ClientCapabilityDTO.Unknown ->
encoder.encodeString(value.name)
}
}

override fun deserialize(decoder: Decoder): ClientCapabilityDTO {
return when (val value = decoder.decodeString()) {
"legalhold-implicit-consent" -> ClientCapabilityDTO.LegalHoldImplicitConsent
else -> ClientCapabilityDTO.Unknown(value)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.network.api.authenticated.client

import kotlinx.serialization.SerializationException
import kotlinx.serialization.json.Json
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith


class ClientCapabilityDTOSerializerTest {

private val json = Json { ignoreUnknownKeys = true }

@Test
fun serialize_legal_hold_implicit_consent() {
val capability = ClientCapabilityDTO.LegalHoldImplicitConsent
val result = json.encodeToString(ClientCapabilityDTO.serializer(), capability)
assertEquals("\"legalhold-implicit-consent\"", result)
}

@Test
fun serialize_unknown_capability() {
val capability = ClientCapabilityDTO.Unknown("custom-capability")
val result = json.encodeToString(ClientCapabilityDTO.serializer(), capability)
assertEquals("\"custom-capability\"", result)
}

@Test
fun deserialize_legal_hold_implicit_consent() {
val jsonString = "\"legalhold-implicit-consent\""
val result = json.decodeFromString(ClientCapabilityDTO.serializer(), jsonString)
assertEquals(ClientCapabilityDTO.LegalHoldImplicitConsent, result)
}

@Test
fun deserialize_unknown_capability() {
val jsonString = "\"unknown-capability\""
val result = json.decodeFromString(ClientCapabilityDTO.serializer(), jsonString)
assertEquals(ClientCapabilityDTO.Unknown("unknown-capability"), result)
}

@Test
fun deserialization_fails_on_invalid_json_format() {
val invalidJson = "12345" // Invalid format for a string
assertFailsWith<SerializationException> {
json.decodeFromString(ClientCapabilityDTO.serializer(), invalidJson)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ internal class ClientApiV0Test : ApiTest() {
@Test
fun givenAValidRegisterClientRequest_whenCallingTheRegisterClientEndpointWithOldFormat_theRequestShouldBeConfiguredCorrectly() =
runTest {

val rowJson = VALID_REGISTER_CLIENT_OLD_RESPONSE.rawJson
val data = VALID_REGISTER_CLIENT_OLD_RESPONSE.serializableData

val networkClient = mockAuthenticatedNetworkClient(
VALID_REGISTER_CLIENT_OLD_RESPONSE.rawJson,
rowJson,
statusCode = HttpStatusCode.Created,
assertion = {
assertPost()
Expand All @@ -77,7 +81,7 @@ internal class ClientApiV0Test : ApiTest() {
val clientApi: ClientApi = ClientApiV0(networkClient)
val response = clientApi.registerClient(REGISTER_CLIENT_REQUEST.serializableData)
assertTrue(response.isSuccessful())
assertEquals(VALID_REGISTER_CLIENT_OLD_RESPONSE.serializableData, response.value)
assertEquals(data, response.value)
}

@Test
Expand Down

0 comments on commit 5788184

Please sign in to comment.