Romain Bertozzi | 37f3359 | 2017-03-21 16:07:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * Author: Romain Bertozzi <romain.bertozzi@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | import XCTest |
Silbino Goncalves Matado | 748f3f4 | 2017-06-08 10:10:21 -0400 | [diff] [blame^] | 22 | import RealmSwift |
| 23 | @testable import Ring |
Romain Bertozzi | 37f3359 | 2017-03-21 16:07:12 -0400 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | A test class designed to validate that the AccountModel helper runs as expected. |
| 27 | */ |
| 28 | class AccountModelHelperTests: XCTestCase { |
| 29 | |
| 30 | /// The account used for the tests. |
| 31 | var account: AccountModel? |
| 32 | |
| 33 | override func setUp() { |
| 34 | super.setUp() |
| 35 | //~ Dummy account |
| 36 | account = AccountModel(withAccountId: "identifier") |
| 37 | } |
| 38 | |
| 39 | override func tearDown() { |
| 40 | // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 41 | super.tearDown() |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | Tests that the SIP account type is properly detected. |
| 46 | */ |
| 47 | func testIsSip() { |
| 48 | var data = Dictionary<String, String>() |
| 49 | data[ConfigKey.AccountType.rawValue] = AccountType.SIP.rawValue |
| 50 | var config = AccountConfigModel(withDetails: data) |
| 51 | account?.details = config |
| 52 | |
| 53 | var helper = AccountModelHelper(withAccount: account!) |
| 54 | XCTAssertTrue(helper.isAccountSip()) |
| 55 | |
| 56 | data[ConfigKey.AccountType.rawValue] = AccountType.Ring.rawValue |
| 57 | config = AccountConfigModel(withDetails: data) |
| 58 | account?.details = config |
| 59 | |
| 60 | helper = AccountModelHelper(withAccount: account!) |
| 61 | XCTAssertFalse(helper.isAccountSip()) |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | Tests that the Ring account type is properly detected. |
| 66 | */ |
| 67 | func testIsRing() { |
| 68 | var data = Dictionary<String, String>() |
| 69 | data[ConfigKey.AccountType.rawValue] = AccountType.Ring.rawValue |
| 70 | var config = AccountConfigModel(withDetails: data) |
| 71 | account?.details = config |
| 72 | |
| 73 | var helper = AccountModelHelper(withAccount: account!) |
| 74 | XCTAssertTrue(helper.isAccountRing()) |
| 75 | |
| 76 | data[ConfigKey.AccountType.rawValue] = AccountType.SIP.rawValue |
| 77 | config = AccountConfigModel(withDetails: data) |
| 78 | account?.details = config |
| 79 | |
| 80 | helper = AccountModelHelper(withAccount: account!) |
| 81 | XCTAssertFalse(helper.isAccountRing()) |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | Tests that the account's enabled state is properly detected. |
| 86 | */ |
| 87 | func testIsEnabled() { |
| 88 | var data = Dictionary<String, String>() |
| 89 | data[ConfigKey.AccountEnable.rawValue] = "true" |
| 90 | var config = AccountConfigModel(withDetails: data) |
| 91 | account?.details = config |
| 92 | |
| 93 | var helper = AccountModelHelper(withAccount: account!) |
| 94 | XCTAssertTrue(helper.isEnabled()) |
| 95 | |
| 96 | data[ConfigKey.AccountEnable.rawValue] = "false" |
| 97 | config = AccountConfigModel(withDetails: data) |
| 98 | account?.details = config |
| 99 | helper = AccountModelHelper(withAccount: account!) |
| 100 | XCTAssertFalse(helper.isEnabled()) |
| 101 | |
| 102 | data.removeValue(forKey: ConfigKey.AccountEnable.rawValue) |
| 103 | config = AccountConfigModel(withDetails: data) |
| 104 | account?.details = config |
| 105 | helper = AccountModelHelper(withAccount: account!) |
| 106 | XCTAssertFalse(helper.isEnabled()) |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | Tests that the account's registration state is properly detected. |
| 111 | */ |
| 112 | func testRegistrationState() { |
| 113 | var data = Dictionary<String, String>() |
| 114 | data[ConfigKey.AccountRegistrationStatus.rawValue] = AccountState.Registered.rawValue |
| 115 | var config = AccountConfigModel(withDetails: data) |
| 116 | account?.volatileDetails = config |
| 117 | |
| 118 | var helper = AccountModelHelper(withAccount: account!) |
| 119 | XCTAssertEqual(helper.getRegistrationState(), AccountState.Registered.rawValue) |
| 120 | |
| 121 | data[ConfigKey.AccountRegistrationStatus.rawValue] = AccountState.Error.rawValue |
| 122 | config = AccountConfigModel(withDetails: data) |
| 123 | account?.volatileDetails = config |
| 124 | helper = AccountModelHelper(withAccount: account!) |
| 125 | XCTAssertNotEqual(helper.getRegistrationState(), AccountState.Registered.rawValue) |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | Tests that the account's error state is properly detected. |
| 130 | */ |
| 131 | func testIsInError() { |
| 132 | var data = Dictionary<String, String>() |
| 133 | data[ConfigKey.AccountRegistrationStatus.rawValue] = AccountState.Registered.rawValue |
| 134 | var config = AccountConfigModel(withDetails: data) |
| 135 | account?.volatileDetails = config |
| 136 | |
| 137 | var helper = AccountModelHelper(withAccount: account!) |
| 138 | XCTAssertFalse(helper.isInError()); |
| 139 | |
| 140 | data[ConfigKey.AccountRegistrationStatus.rawValue] = AccountState.Error.rawValue |
| 141 | config = AccountConfigModel(withDetails: data) |
| 142 | account?.volatileDetails = config |
| 143 | helper = AccountModelHelper(withAccount: account!) |
| 144 | XCTAssertTrue(helper.isInError()) |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | Tests that the account's credentials are properly inserted and retrieved. |
| 149 | */ |
| 150 | func testCredentials() { |
| 151 | let username = "username" |
| 152 | let password = "password" |
| 153 | let realm = "realm" |
| 154 | |
| 155 | var credentials = Array<Dictionary<String, String>>() |
| 156 | var credential = Dictionary<String, String>() |
| 157 | credential[ConfigKey.AccountUsername.rawValue] = username |
| 158 | credential[ConfigKey.AccountPassword.rawValue] = password |
| 159 | credential[ConfigKey.AccountRealm.rawValue] = realm |
| 160 | credentials.append(credential) |
| 161 | |
| 162 | var helper = AccountModelHelper(withAccount: account!) |
| 163 | var modifiedAccount = helper.setCredentials(credentials) |
| 164 | |
| 165 | XCTAssertEqual(modifiedAccount.credentialDetails.count, 1) |
| 166 | XCTAssertEqual(modifiedAccount.credentialDetails[0].username, username) |
| 167 | XCTAssertEqual(modifiedAccount.credentialDetails[0].password, password) |
Silbino Goncalves Matado | 748f3f4 | 2017-06-08 10:10:21 -0400 | [diff] [blame^] | 168 | XCTAssertEqual(modifiedAccount.credentialDetails[0].accountRealm, realm) |
Romain Bertozzi | 37f3359 | 2017-03-21 16:07:12 -0400 | [diff] [blame] | 169 | |
| 170 | modifiedAccount = helper.setCredentials(nil) |
| 171 | XCTAssertEqual(modifiedAccount.credentialDetails.count, 0) |
| 172 | } |
| 173 | |
| 174 | } |