blob: d2009bf1d5c6859a5a405331f8e97910bf88462a [file] [log] [blame]
Romain Bertozzi37f33592017-03-21 16:07:12 -04001/*
Sébastien Blin77c737a2019-01-02 17:34:46 -05002 * Copyright (C) 2017-2019 Savoir-faire Linux Inc.
Romain Bertozzi37f33592017-03-21 16:07:12 -04003 *
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
21import XCTest
Silbino Goncalves Matado748f3f42017-06-08 10:10:21 -040022import RealmSwift
23@testable import Ring
Romain Bertozzi37f33592017-03-21 16:07:12 -040024
25/**
26 A test class designed to validate that the AccountModel helper runs as expected.
27 */
28class 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() {
Thibault Wittemberg14b092a2017-07-04 18:13:26 -040048 var data = [String: String]()
49 data[ConfigKey.accountType.rawValue] = AccountType.sip.rawValue
Romain Bertozzi37f33592017-03-21 16:07:12 -040050 var config = AccountConfigModel(withDetails: data)
51 account?.details = config
52
53 var helper = AccountModelHelper(withAccount: account!)
54 XCTAssertTrue(helper.isAccountSip())
55
Thibault Wittemberg14b092a2017-07-04 18:13:26 -040056 data[ConfigKey.accountType.rawValue] = AccountType.ring.rawValue
Romain Bertozzi37f33592017-03-21 16:07:12 -040057 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() {
Thibault Wittemberg14b092a2017-07-04 18:13:26 -040068 var data = [String: String]()
69 data[ConfigKey.accountType.rawValue] = AccountType.ring.rawValue
Romain Bertozzi37f33592017-03-21 16:07:12 -040070 var config = AccountConfigModel(withDetails: data)
71 account?.details = config
72
73 var helper = AccountModelHelper(withAccount: account!)
74 XCTAssertTrue(helper.isAccountRing())
75
Thibault Wittemberg14b092a2017-07-04 18:13:26 -040076 data[ConfigKey.accountType.rawValue] = AccountType.sip.rawValue
Romain Bertozzi37f33592017-03-21 16:07:12 -040077 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() {
Thibault Wittemberg14b092a2017-07-04 18:13:26 -040088 var data = [String: String]()
89 data[ConfigKey.accountEnable.rawValue] = "true"
Romain Bertozzi37f33592017-03-21 16:07:12 -040090 var config = AccountConfigModel(withDetails: data)
91 account?.details = config
92
93 var helper = AccountModelHelper(withAccount: account!)
94 XCTAssertTrue(helper.isEnabled())
95
Thibault Wittemberg14b092a2017-07-04 18:13:26 -040096 data[ConfigKey.accountEnable.rawValue] = "false"
Romain Bertozzi37f33592017-03-21 16:07:12 -040097 config = AccountConfigModel(withDetails: data)
98 account?.details = config
99 helper = AccountModelHelper(withAccount: account!)
100 XCTAssertFalse(helper.isEnabled())
101
Thibault Wittemberg14b092a2017-07-04 18:13:26 -0400102 data.removeValue(forKey: ConfigKey.accountEnable.rawValue)
Romain Bertozzi37f33592017-03-21 16:07:12 -0400103 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() {
Thibault Wittemberg14b092a2017-07-04 18:13:26 -0400113 var data = [String: String]()
114 data[ConfigKey.accountRegistrationStatus.rawValue] = AccountState.registered.rawValue
Romain Bertozzi37f33592017-03-21 16:07:12 -0400115 var config = AccountConfigModel(withDetails: data)
116 account?.volatileDetails = config
117
118 var helper = AccountModelHelper(withAccount: account!)
Thibault Wittemberg14b092a2017-07-04 18:13:26 -0400119 XCTAssertEqual(helper.getRegistrationState(), AccountState.registered.rawValue)
Romain Bertozzi37f33592017-03-21 16:07:12 -0400120
Thibault Wittemberg14b092a2017-07-04 18:13:26 -0400121 data[ConfigKey.accountRegistrationStatus.rawValue] = AccountState.error.rawValue
Romain Bertozzi37f33592017-03-21 16:07:12 -0400122 config = AccountConfigModel(withDetails: data)
123 account?.volatileDetails = config
124 helper = AccountModelHelper(withAccount: account!)
Thibault Wittemberg14b092a2017-07-04 18:13:26 -0400125 XCTAssertNotEqual(helper.getRegistrationState(), AccountState.registered.rawValue)
Romain Bertozzi37f33592017-03-21 16:07:12 -0400126 }
127
128 /**
129 Tests that the account's error state is properly detected.
130 */
131 func testIsInError() {
Thibault Wittemberg14b092a2017-07-04 18:13:26 -0400132 var data = [String: String]()
133 data[ConfigKey.accountRegistrationStatus.rawValue] = AccountState.registered.rawValue
Romain Bertozzi37f33592017-03-21 16:07:12 -0400134 var config = AccountConfigModel(withDetails: data)
135 account?.volatileDetails = config
136
137 var helper = AccountModelHelper(withAccount: account!)
Thibault Wittemberg14b092a2017-07-04 18:13:26 -0400138 XCTAssertFalse(helper.isInError())
Romain Bertozzi37f33592017-03-21 16:07:12 -0400139
Thibault Wittemberg14b092a2017-07-04 18:13:26 -0400140 data[ConfigKey.accountRegistrationStatus.rawValue] = AccountState.error.rawValue
Romain Bertozzi37f33592017-03-21 16:07:12 -0400141 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
Thibault Wittemberg14b092a2017-07-04 18:13:26 -0400155 var credentials = [[String: String]]()
156 var credential = [String: String]()
157 credential[ConfigKey.accountUsername.rawValue] = username
158 credential[ConfigKey.accountPassword.rawValue] = password
159 credential[ConfigKey.accountRealm.rawValue] = realm
Romain Bertozzi37f33592017-03-21 16:07:12 -0400160 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 Matado748f3f42017-06-08 10:10:21 -0400168 XCTAssertEqual(modifiedAccount.credentialDetails[0].accountRealm, realm)
Romain Bertozzi37f33592017-03-21 16:07:12 -0400169
170 modifiedAccount = helper.setCredentials(nil)
171 XCTAssertEqual(modifiedAccount.credentialDetails.count, 0)
172 }
173
174}