Skip to content

Commit

Permalink
Merge branch 'develop' into fix/ci-snapshots-WPB-14950
Browse files Browse the repository at this point in the history
  • Loading branch information
netbe authored Dec 16, 2024
2 parents effc34f + f39bc3d commit 1f0a8a8
Show file tree
Hide file tree
Showing 133 changed files with 4,359 additions and 726 deletions.
1 change: 1 addition & 0 deletions WireAPI/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ let package = Package(
.product(name: "SnapshotTesting", package: "swift-snapshot-testing")
],
resources: [
.process("APIs/AccountsAPI/Resources"),
.process("APIs/BackendInfoAPI/Resources"),
.process("APIs/ConnectionsAPI/Resources"),
.process("APIs/ConversationsAPI/Resources"),
Expand Down
5 changes: 4 additions & 1 deletion WireAPI/Sources/WireAPI/APIs/AccountsAPI/AccountsAPIV7.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class AccountsAPIV7: AccountsAPIV6 {
requiringAccessToken: true
)

return try ResponseParser()
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

return try ResponseParser(decoder: decoder)
.success(code: .ok, type: UpgradeToTeamResponseV7.self)
.failure(code: .forbidden, label: "user-already-in-a-team", error: AccountsAPIError.userAlreadyInATeam)
.failure(code: .notFound, label: "not-found", error: AccountsAPIError.userNotFound)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@ struct ConversationMLSMessageAddEventDecoder {
forKey: .payload
)

let timestamp = try container.decodeIfPresent(
UTCTimeMillis.self,
forKey: .timestamp
)

return ConversationMLSMessageAddEvent(
conversationID: conversationID,
senderID: senderID,
subconversation: subconversation,
message: payload.text
message: payload.text,
timestamp: timestamp?.date
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ struct ConversationProteusMessageAddEventDecoder {
conversationID: conversationID,
senderID: senderID,
timestamp: timestamp.date,
message: .ciphertext(payload.text),
externalData: payload.data.map { .ciphertext($0) },
message: .init(encryptedMessage: payload.text),
externalData: payload.data.map { .init(encryptedMessage: $0) },
messageSenderClientID: payload.sender,
messageRecipientClientID: payload.recipient
)
Expand Down
6 changes: 3 additions & 3 deletions WireAPI/Sources/WireAPI/Models/Messaging/MessageContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import Foundation
/// The contents of a message, typically as a base-64 encoded
/// Protobuf string.

public enum MessageContent: Equatable, Codable, Sendable {
public struct MessageContent: Equatable, Codable, Sendable {

/// Encrypted message content.

case ciphertext(String)
public let encryptedMessage: String

/// Unencrypted message content.

case plaintext(String)
public var decryptedMessage: String?

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ import Foundation

public struct ConversationMLSMessageAddEvent: Equatable, Codable, Sendable {

public struct DecryptedMessage: Equatable, Codable, Sendable {

public let message: String

public let senderClientID: String?

public init(
message: String,
senderClientID: String?
) {
self.message = message
self.senderClientID = senderClientID
}
}

/// The id of the conversation.

public let conversationID: ConversationID
Expand All @@ -42,4 +57,13 @@ public struct ConversationMLSMessageAddEvent: Equatable, Codable, Sendable {

public let message: String

/// The date the message was received.

public let timestamp: Date?

/// The decrypted current message + decrypted buffered messages
/// along with the related sender client ID for each message.

public var decryptedMessages: [DecryptedMessage] = []

}
124 changes: 124 additions & 0 deletions WireAPI/Tests/WireAPITests/APIs/AccountsAPI/AccountsAPITests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//
// 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/.
//

import XCTest

@testable import WireAPI
@testable import WireAPISupport

final class AccountsAPITests: XCTestCase {

private var apiSnapshotHelper: APIServiceSnapshotHelper<any AccountsAPI>!

// MARK: - Setup

override func setUp() {
apiSnapshotHelper = APIServiceSnapshotHelper<any AccountsAPI> { apiService, apiVersion in
AccountsAPIBuilder(apiService: apiService)
.makeAPI(for: apiVersion)

}
}

override func tearDown() {
apiSnapshotHelper = nil
}

// MARK: - Request generation

func testUpgradeToTeam_V0_To_V6() async throws {
// Given
let apiService = MockAPIServiceProtocol()
let builder = AccountsAPIBuilder(apiService: apiService)

for apiVersion in [APIVersion.v0, .v1, .v2, .v3, .v4, .v5, .v6] {
let sut = builder.makeAPI(for: apiVersion)

// Then
await XCTAssertThrowsErrorAsync(AccountsAPIError.unsupportedEndpointForAPIVersion) {
try await sut.upgradeToTeam(teamName: Scaffolding.teamName)
}
}
}

func testUpgradeToTeam_Request_Generation_V7_Onwards() async throws {
// Given
let apiVersions = APIVersion.v7.andNextVersions

// Then

Check failure on line 63 in WireAPI/Tests/WireAPITests/APIs/AccountsAPI/AccountsAPITests.swift

View workflow job for this annotation

GitHub Actions / Test Results

AccountsAPITests.testUpgradeToTeam_Request_Generation_V7_Onwards() failed

failed - Snapshot "request-0-v7" does not match reference. @− "file:///Users/admin/actions-runner/_work/wire-ios/wire-ios/WireAPI/Tests/WireAPITests/APIs/AccountsAPI/__Snapshots__/AccountsAPITests/testUpgradeToTeam_Request_Generation_V7_Onwards.request-0-v7.txt" @+ "file:///Users/admin/Library/Developer/CoreSimulator/Devices/D8E2CAA5-A191-4AB5-9DD5-3CCD10A7ACCC/data/tmp/AccountsAPITests/testUpgradeToTeam_Request_Generation_V7_Onwards.request-0-v7.txt" To configure output for a custom diff tool, use 'withSnapshotTesting'. For example: withSnapshotTesting(diffTool: .ksdiff) { // ... } @@ −1,5 +1,5 @@  curl \   --request POST \   --header "Content-Type: application/json" \ − --data "{\"name\":\"iOS Team\",\"icon\":\"default\"}" \ + --data "{\"icon\":\"default\",\"name\":\"iOS Team\"}" \   "upgrade-personal-to-team"
try await apiSnapshotHelper.verifyRequest(for: apiVersions) { sut in
// When
_ = try await sut.upgradeToTeam(teamName: Scaffolding.teamName)
}
}

// MARK: - Response handling

func testUpgradeToTeam_Response_Handling_V7_Success() async throws {
// Given
let apiService = MockAPIServiceProtocol.withResponses([
(.ok, "UpgradeToTeamSuccessResponse")
])

let sut = AccountsAPIV7(apiService: apiService)

// When
let response = try await sut.upgradeToTeam(teamName: Scaffolding.teamName)

// Then
XCTAssertEqual(response, UpgradedAccountTeam(teamId: Scaffolding.teamID, teamName: Scaffolding.teamName))
}

func testUpgradeToTeam_Response_Handling_V7_User_Already_In_A_Team() async throws {
// Given
let apiService = MockAPIServiceProtocol.withResponses([
(.forbidden, "UpgradeToTeamErrorResponse_UserAlreadyInATeam")
])

let sut = AccountsAPIV7(apiService: apiService)

// Then
await XCTAssertThrowsErrorAsync(AccountsAPIError.userAlreadyInATeam) {
// When
try await sut.upgradeToTeam(teamName: Scaffolding.teamName)
}
}

func testUpgradeToTeam_Response_Handling_V7_User_Not_Found() async throws {
// Given
let apiService = MockAPIServiceProtocol.withResponses([
(.notFound, "UpgradeToTeamErrorResponse_UserNotFound")
])

let sut = AccountsAPIV7(apiService: apiService)

// Then
await XCTAssertThrowsErrorAsync(AccountsAPIError.userNotFound) {
// When
try await sut.upgradeToTeam(teamName: Scaffolding.teamName)
}
}

}

private enum Scaffolding {

static let teamName = "iOS Team"
static let teamID = UUID(uuidString: "66dc3593-4c3a-49e8-b5c3-d3d908bd7403")!

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"code": 403,
"label": "user-already-in-a-team",
"message": "Switching teams is not allowed"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"code": 404,
"label": "not-found",
"message": "User not found"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"team_id": "66dc3593-4c3a-49e8-b5c3-d3d908bd7403",
"team_name": "iOS Team"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
curl \
--request POST \
--header "Content-Type: application/json" \
--data "{\"name\":\"iOS Team\",\"icon\":\"default\"}" \
"upgrade-personal-to-team"
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ private enum Scaffolding {
conversationID: conversationID,
senderID: senderID,
timestamp: timestamp,
message: .ciphertext("foo"),
externalData: .ciphertext("bar"),
message: .init(encryptedMessage: "foo"),
externalData: .init(encryptedMessage: "bar"),
messageSenderClientID: "abc123",
messageRecipientClientID: "def456"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,8 @@ final class ConversationEventDecodingTests: XCTestCase {
conversationID: conversationID,
senderID: senderID,
subconversation: "subconversation",
message: "message"
message: "message",
timestamp: fractionalDate(from: "2024-06-04T15:03:07.598Z")
)

static let mlsWelcomeEvent = ConversationMLSWelcomeEvent(
Expand All @@ -471,8 +472,8 @@ final class ConversationEventDecodingTests: XCTestCase {
conversationID: conversationID,
senderID: senderID,
timestamp: timestamp,
message: .ciphertext("foo"),
externalData: .ciphertext("bar"),
message: .init(encryptedMessage: "foo"),
externalData: .init(encryptedMessage: "bar"),
messageSenderClientID: "abc123",
messageRecipientClientID: "def456"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"subconv": "subconversation",
"data": {
"text": "message"
}
},
"time": "2024-06-04T15:03:07.598Z"
}
Loading

0 comments on commit 1f0a8a8

Please sign in to comment.