diff --git a/OmiseGOTests/CodingTests/DecodeTests.swift b/OmiseGOTests/CodingTests/DecodeTests.swift index ea92723..f669527 100644 --- a/OmiseGOTests/CodingTests/DecodeTests.swift +++ b/OmiseGOTests/CodingTests/DecodeTests.swift @@ -277,6 +277,14 @@ class DecodeTests: XCTestCase { XCTAssertTrue(decodedData.balances[0].token.encryptedMetadata.isEmpty) XCTAssertEqual(decodedData.balances[0].token.createdAt, try "2018-01-01T00:00:00Z".toDate()) XCTAssertEqual(decodedData.balances[0].token.updatedAt, try "2018-01-01T00:00:00Z".toDate()) + XCTAssertEqual(decodedData.name, "primary") + XCTAssertEqual(decodedData.identifier, "primary") + XCTAssertEqual(decodedData.userId!, "cec34607-0761-4a59-8357-18963e42a1aa") + XCTAssertEqual(decodedData.user!.id, "cec34607-0761-4a59-8357-18963e42a1aa") + XCTAssertEqual(decodedData.accountId, nil) + XCTAssertEqual(decodedData.account, nil) + XCTAssertTrue(decodedData.metadata.isEmpty) + XCTAssertTrue(decodedData.encryptedMetadata.isEmpty) } catch let thrownError { XCTFail(thrownError.localizedDescription) } diff --git a/OmiseGOTests/FixtureTests/Fixtures/fixture/me.list_wallets.json b/OmiseGOTests/FixtureTests/Fixtures/fixture/me.list_wallets.json index 6c2f50c..d6a22af 100644 --- a/OmiseGOTests/FixtureTests/Fixtures/fixture/me.list_wallets.json +++ b/OmiseGOTests/FixtureTests/Fixtures/fixture/me.list_wallets.json @@ -38,7 +38,24 @@ "updated_at": "2018-01-01T00:00:00Z" } } - ] + ], + "name": "primary", + "identifier": "primary", + "user_id": "cec34607-0761-4a59-8357-18963e42a1aa", + "user": { + "id": "cec34607-0761-4a59-8357-18963e42a1aa", + "provider_user_id": "wijf-fbancomw-dqwjudb", + "username": "john.doe@example.com", + "socket_topic": "user:cec34607-0761-4a59-8357-18963e42a1aa", + "metadata": {}, + "encrypted_metadata": {}, + "created_at": "2018-01-01T00:00:00Z", + "updated_at": "2018-01-01T00:00:00Z" + }, + "account_id": null, + "account": null, + "metadata": {}, + "encrypted_metadata": {} } ] } diff --git a/OmiseGOTests/FixtureTests/Fixtures/objects/wallet.json b/OmiseGOTests/FixtureTests/Fixtures/objects/wallet.json index 128c10c..043dd4f 100644 --- a/OmiseGOTests/FixtureTests/Fixtures/objects/wallet.json +++ b/OmiseGOTests/FixtureTests/Fixtures/objects/wallet.json @@ -14,5 +14,22 @@ "updated_at": "2018-01-01T00:00:00Z" } } - ] + ], + "name": "primary", + "identifier": "primary", + "user_id": "cec34607-0761-4a59-8357-18963e42a1aa", + "user": { + "id": "cec34607-0761-4a59-8357-18963e42a1aa", + "provider_user_id": "wijf-fbancomw-dqwjudb", + "username": "john.doe@example.com", + "socket_topic": "user:cec34607-0761-4a59-8357-18963e42a1aa", + "metadata": {}, + "encrypted_metadata": {}, + "created_at": "2018-01-01T00:00:00Z", + "updated_at": "2018-01-01T00:00:00Z" + }, + "account_id": null, + "account": null, + "metadata": {}, + "encrypted_metadata": {} } diff --git a/OmiseGOTests/FixtureTests/WalletFixtureTests.swift b/OmiseGOTests/FixtureTests/WalletFixtureTests.swift index e78f00c..e1ed6fa 100644 --- a/OmiseGOTests/FixtureTests/WalletFixtureTests.swift +++ b/OmiseGOTests/FixtureTests/WalletFixtureTests.swift @@ -33,7 +33,6 @@ class WalletFixtureTests: FixtureTestCase { XCTAssertTrue(balance1.token.encryptedMetadata.isEmpty) XCTAssertEqual(balance1.token.createdAt, "2018-01-01T00:00:00Z".toDate()) XCTAssertEqual(balance1.token.updatedAt, "2018-01-01T00:00:00Z".toDate()) - XCTAssertEqual(balance2.amount, 133700) XCTAssertEqual(balance2.token.id, "KNC:123") XCTAssertEqual(balance2.token.symbol, "KNC") @@ -42,6 +41,15 @@ class WalletFixtureTests: FixtureTestCase { XCTAssertTrue(balance2.token.encryptedMetadata.isEmpty) XCTAssertEqual(balance2.token.createdAt, "2018-01-01T00:00:00Z".toDate()) XCTAssertEqual(balance2.token.updatedAt, "2018-01-01T00:00:00Z".toDate()) + + XCTAssertEqual(wallet.name, "primary") + XCTAssertEqual(wallet.identifier, "primary") + XCTAssertEqual(wallet.userId!, "cec34607-0761-4a59-8357-18963e42a1aa") + XCTAssertEqual(wallet.user!.id, "cec34607-0761-4a59-8357-18963e42a1aa") + XCTAssertEqual(wallet.accountId, nil) + XCTAssertEqual(wallet.account, nil) + XCTAssertTrue(wallet.metadata.isEmpty) + XCTAssertTrue(wallet.encryptedMetadata.isEmpty) case .fail(error: let error): XCTFail("\(error)") } diff --git a/OmiseGOTests/Helpers/StubGenerator.swift b/OmiseGOTests/Helpers/StubGenerator.swift index 2892571..1fe3ada 100644 --- a/OmiseGOTests/Helpers/StubGenerator.swift +++ b/OmiseGOTests/Helpers/StubGenerator.swift @@ -26,12 +26,28 @@ class StubGenerator { } class func wallet(address: String? = nil, - balances: [Balance]? = nil) + balances: [Balance]? = nil, + name: String? = nil, + identifier: String? = nil, + userId: String? = nil, + user: User? = nil, + accountId: String? = nil, + account: Account? = nil, + metadata: [String: Any] = [:], + encryptedMetadata: [String: Any] = [:]) -> Wallet { let v: Wallet = self.stub(forResource: "wallet") return Wallet( address: address ?? v.address, - balances: [self.balance()] + balances: [self.balance()], + name: name ?? v.name, + identifier: identifier ?? v.identifier, + userId: userId ?? v.userId, + user: user ?? v.user, + accountId: accountId ?? v.accountId, + account: account ?? v.account, + metadata: metadata, + encryptedMetadata: encryptedMetadata ) } diff --git a/Source/Models/Wallet.swift b/Source/Models/Wallet.swift index 9436a22..8dc6773 100644 --- a/Source/Models/Wallet.swift +++ b/Source/Models/Wallet.swift @@ -7,12 +7,60 @@ // /// Represents a wallet containing a list of balances -public struct Wallet: Retrievable, Decodable { +public struct Wallet: Retrievable { - /// The address of the balances + /// The address of the wallet public let address: String /// The list of balances associated with that address public let balances: [Balance] + /// The name of the wallet + public let name: String + /// The identifier of the wallet + public let identifier: String + /// The id of the user associated to this wallet if it's a user wallet. Nil if it's an account wallet. + public let userId: String? + /// The user associated to this wallet if it's a user wallet. Nil if it's an account wallet. + public let user: User? + /// The id of the account associated to this wallet if it's an account wallet. Nil if it's a user wallet. + public let accountId: String? + /// The account associated to this wallet if it's an account wallet. Nil if it's a user wallet. + public let account: Account? + /// Any additional metadata that need to be stored as a dictionary + public let metadata: [String: Any] + /// Any additional encrypted metadata that need to be stored as a dictionary + public let encryptedMetadata: [String: Any] +} + +extension Wallet: Decodable { + + private enum CodingKeys: String, CodingKey { + case id + case address + case balances + case name + case identifier + case userId = "user_id" + case user + case accountId = "account_id" + case account + case metadata + case encryptedMetadata = "encrypted_metadata" + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + address = try container.decode(String.self, forKey: .address) + balances = try container.decode([Balance].self, forKey: .balances) + name = try container.decode(String.self, forKey: .name) + identifier = try container.decode(String.self, forKey: .identifier) + userId = try container.decodeIfPresent(String.self, forKey: .userId) + user = try container.decodeIfPresent(User.self, forKey: .user) + accountId = try container.decodeIfPresent(String.self, forKey: .accountId) + account = try container.decodeIfPresent(Account.self, forKey: .account) + metadata = try container.decode([String: Any].self, forKey: .metadata) + encryptedMetadata = try container.decode([String: Any].self, forKey: .encryptedMetadata) + } + } extension Wallet: Listable {