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 |
| 22 | |
| 23 | /** |
| 24 | A test class designed to validate that the AccountModel helper runs as expected. |
| 25 | */ |
| 26 | class AccountModelHelperTests: XCTestCase { |
| 27 | |
| 28 | /// The account used for the tests. |
| 29 | var account: AccountModel? |
| 30 | |
| 31 | override func setUp() { |
| 32 | super.setUp() |
| 33 | //~ Dummy account |
| 34 | account = AccountModel(withAccountId: "identifier") |
| 35 | } |
| 36 | |
| 37 | override func tearDown() { |
| 38 | // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 39 | super.tearDown() |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | Tests that the SIP account type is properly detected. |
| 44 | */ |
| 45 | func testIsSip() { |
| 46 | var data = Dictionary<String, String>() |
| 47 | data[ConfigKey.AccountType.rawValue] = AccountType.SIP.rawValue |
| 48 | var config = AccountConfigModel(withDetails: data) |
| 49 | account?.details = config |
| 50 | |
| 51 | var helper = AccountModelHelper(withAccount: account!) |
| 52 | XCTAssertTrue(helper.isAccountSip()) |
| 53 | |
| 54 | data[ConfigKey.AccountType.rawValue] = AccountType.Ring.rawValue |
| 55 | config = AccountConfigModel(withDetails: data) |
| 56 | account?.details = config |
| 57 | |
| 58 | helper = AccountModelHelper(withAccount: account!) |
| 59 | XCTAssertFalse(helper.isAccountSip()) |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | Tests that the Ring account type is properly detected. |
| 64 | */ |
| 65 | func testIsRing() { |
| 66 | var data = Dictionary<String, String>() |
| 67 | data[ConfigKey.AccountType.rawValue] = AccountType.Ring.rawValue |
| 68 | var config = AccountConfigModel(withDetails: data) |
| 69 | account?.details = config |
| 70 | |
| 71 | var helper = AccountModelHelper(withAccount: account!) |
| 72 | XCTAssertTrue(helper.isAccountRing()) |
| 73 | |
| 74 | data[ConfigKey.AccountType.rawValue] = AccountType.SIP.rawValue |
| 75 | config = AccountConfigModel(withDetails: data) |
| 76 | account?.details = config |
| 77 | |
| 78 | helper = AccountModelHelper(withAccount: account!) |
| 79 | XCTAssertFalse(helper.isAccountRing()) |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | Tests that the account's enabled state is properly detected. |
| 84 | */ |
| 85 | func testIsEnabled() { |
| 86 | var data = Dictionary<String, String>() |
| 87 | data[ConfigKey.AccountEnable.rawValue] = "true" |
| 88 | var config = AccountConfigModel(withDetails: data) |
| 89 | account?.details = config |
| 90 | |
| 91 | var helper = AccountModelHelper(withAccount: account!) |
| 92 | XCTAssertTrue(helper.isEnabled()) |
| 93 | |
| 94 | data[ConfigKey.AccountEnable.rawValue] = "false" |
| 95 | config = AccountConfigModel(withDetails: data) |
| 96 | account?.details = config |
| 97 | helper = AccountModelHelper(withAccount: account!) |
| 98 | XCTAssertFalse(helper.isEnabled()) |
| 99 | |
| 100 | data.removeValue(forKey: ConfigKey.AccountEnable.rawValue) |
| 101 | config = AccountConfigModel(withDetails: data) |
| 102 | account?.details = config |
| 103 | helper = AccountModelHelper(withAccount: account!) |
| 104 | XCTAssertFalse(helper.isEnabled()) |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | Tests that the account's registration state is properly detected. |
| 109 | */ |
| 110 | func testRegistrationState() { |
| 111 | var data = Dictionary<String, String>() |
| 112 | data[ConfigKey.AccountRegistrationStatus.rawValue] = AccountState.Registered.rawValue |
| 113 | var config = AccountConfigModel(withDetails: data) |
| 114 | account?.volatileDetails = config |
| 115 | |
| 116 | var helper = AccountModelHelper(withAccount: account!) |
| 117 | XCTAssertEqual(helper.getRegistrationState(), AccountState.Registered.rawValue) |
| 118 | |
| 119 | data[ConfigKey.AccountRegistrationStatus.rawValue] = AccountState.Error.rawValue |
| 120 | config = AccountConfigModel(withDetails: data) |
| 121 | account?.volatileDetails = config |
| 122 | helper = AccountModelHelper(withAccount: account!) |
| 123 | XCTAssertNotEqual(helper.getRegistrationState(), AccountState.Registered.rawValue) |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | Tests that the account's error state is properly detected. |
| 128 | */ |
| 129 | func testIsInError() { |
| 130 | var data = Dictionary<String, String>() |
| 131 | data[ConfigKey.AccountRegistrationStatus.rawValue] = AccountState.Registered.rawValue |
| 132 | var config = AccountConfigModel(withDetails: data) |
| 133 | account?.volatileDetails = config |
| 134 | |
| 135 | var helper = AccountModelHelper(withAccount: account!) |
| 136 | XCTAssertFalse(helper.isInError()); |
| 137 | |
| 138 | data[ConfigKey.AccountRegistrationStatus.rawValue] = AccountState.Error.rawValue |
| 139 | config = AccountConfigModel(withDetails: data) |
| 140 | account?.volatileDetails = config |
| 141 | helper = AccountModelHelper(withAccount: account!) |
| 142 | XCTAssertTrue(helper.isInError()) |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | Tests that the account's credentials are properly inserted and retrieved. |
| 147 | */ |
| 148 | func testCredentials() { |
| 149 | let username = "username" |
| 150 | let password = "password" |
| 151 | let realm = "realm" |
| 152 | |
| 153 | var credentials = Array<Dictionary<String, String>>() |
| 154 | var credential = Dictionary<String, String>() |
| 155 | credential[ConfigKey.AccountUsername.rawValue] = username |
| 156 | credential[ConfigKey.AccountPassword.rawValue] = password |
| 157 | credential[ConfigKey.AccountRealm.rawValue] = realm |
| 158 | credentials.append(credential) |
| 159 | |
| 160 | var helper = AccountModelHelper(withAccount: account!) |
| 161 | var modifiedAccount = helper.setCredentials(credentials) |
| 162 | |
| 163 | XCTAssertEqual(modifiedAccount.credentialDetails.count, 1) |
| 164 | XCTAssertEqual(modifiedAccount.credentialDetails[0].username, username) |
| 165 | XCTAssertEqual(modifiedAccount.credentialDetails[0].password, password) |
| 166 | XCTAssertEqual(modifiedAccount.credentialDetails[0].realm, realm) |
| 167 | |
| 168 | modifiedAccount = helper.setCredentials(nil) |
| 169 | XCTAssertEqual(modifiedAccount.credentialDetails.count, 0) |
| 170 | } |
| 171 | |
| 172 | } |