-
Notifications
You must be signed in to change notification settings - Fork 27
/
MockLogins.swift
153 lines (131 loc) · 5.59 KB
/
MockLogins.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Shared
public class MockLogins: BrowserLogins, SyncableLogins {
private var cache = [Login]()
public init(files: FileAccessor) {
}
public func getLoginsForProtectionSpace(protectionSpace: NSURLProtectionSpace) -> Deferred<Maybe<Cursor<LoginData>>> {
let cursor = ArrayCursor(data: cache.filter({ login in
return login.protectionSpace.host == protectionSpace.host
}).sort({ (loginA, loginB) -> Bool in
return loginA.timeLastUsed > loginB.timeLastUsed
}).map({ login in
return login as LoginData
}))
return Deferred(value: Maybe(success: cursor))
}
public func getLoginsForProtectionSpace(protectionSpace: NSURLProtectionSpace, withUsername username: String?) -> Deferred<Maybe<Cursor<LoginData>>> {
let cursor = ArrayCursor(data: cache.filter({ login in
return login.protectionSpace.host == protectionSpace.host &&
login.username == username
}).sort({ (loginA, loginB) -> Bool in
return loginA.timeLastUsed > loginB.timeLastUsed
}).map({ login in
return login as LoginData
}))
return Deferred(value: Maybe(success: cursor))
}
public func getLoginDataForGUID(guid: GUID) -> Deferred<Maybe<Login>> {
if let login = (cache.filter { $0.guid == guid }).first {
return deferMaybe(login)
} else {
return deferMaybe(LoginDataError(description: "Login for GUID \(guid) not found"))
}
}
public func getAllLogins() -> Deferred<Maybe<Cursor<Login>>> {
let cursor = ArrayCursor(data: cache.sort({ (loginA, loginB) -> Bool in
return loginA.hostname > loginB.hostname
}))
return Deferred(value: Maybe(success: cursor))
}
public func searchLoginsWithQuery(query: String?) -> Deferred<Maybe<Cursor<Login>>> {
let cursor = ArrayCursor(data: cache.filter({ login in
var checks = [Bool]()
if let query = query {
checks.append(login.username?.contains(query) ?? false)
checks.append(login.password.contains(query))
checks.append(login.hostname.contains(query))
}
return checks.contains(true)
}).sort({ (loginA, loginB) -> Bool in
return loginA.hostname > loginB.hostname
}))
return Deferred(value: Maybe(success: cursor))
}
// This method is only here for testing
public func getUsageDataForLoginByGUID(guid: GUID) -> Deferred<Maybe<LoginUsageData>> {
let res = cache.filter({ login in
return login.guid == guid
}).sort({ (loginA, loginB) -> Bool in
return loginA.timeLastUsed > loginB.timeLastUsed
})[0] as LoginUsageData
return Deferred(value: Maybe(success: res))
}
public func addLogin(login: LoginData) -> Success {
if let _ = cache.indexOf(login as! Login) {
return deferMaybe(LoginDataError(description: "Already in the cache"))
}
cache.append(login as! Login)
return succeed()
}
public func updateLoginByGUID(guid: GUID, new: LoginData, significant: Bool) -> Success {
// TODO
return succeed()
}
public func getModifiedLoginsToUpload() -> Deferred<Maybe<[Login]>> {
// TODO
return deferMaybe([])
}
public func getDeletedLoginsToUpload() -> Deferred<Maybe<[GUID]>> {
// TODO
return deferMaybe([])
}
public func updateLogin(login: LoginData) -> Success {
if let index = cache.indexOf(login as! Login) {
cache[index].timePasswordChanged = NSDate.nowMicroseconds()
return succeed()
}
return deferMaybe(LoginDataError(description: "Password wasn't cached yet. Can't update"))
}
public func addUseOfLoginByGUID(guid: GUID) -> Success {
if let login = cache.filter({ $0.guid == guid }).first {
login.timeLastUsed = NSDate.nowMicroseconds()
return succeed()
}
return deferMaybe(LoginDataError(description: "Password wasn't cached yet. Can't update"))
}
public func removeLoginByGUID(guid: GUID) -> Success {
let filtered = cache.filter { $0.guid != guid }
if filtered.count == cache.count {
return deferMaybe(LoginDataError(description: "Can not remove a password that wasn't stored"))
}
cache = filtered
return succeed()
}
public func removeLoginsWithGUIDs(guids: [GUID]) -> Success {
return walk(guids) { guid in
self.removeLoginByGUID(guid)
}
}
public func removeAll() -> Success {
cache.removeAll(keepCapacity: false)
return succeed()
}
public func hasSyncedLogins() -> Deferred<Maybe<Bool>> {
return deferMaybe(true)
}
// TODO
public func deleteByGUID(guid: GUID, deletedAt: Timestamp) -> Success { return succeed() }
public func applyChangedLogin(upstream: ServerLogin) -> Success { return succeed() }
public func markAsSynchronized(_: [GUID], modified: Timestamp) -> Deferred<Maybe<Timestamp>> { return deferMaybe(0) }
public func markAsDeleted(guids: [GUID]) -> Success { return succeed() }
public func onRemovedAccount() -> Success { return succeed() }
}
extension MockLogins: ResettableSyncStorage {
public func resetClient() -> Success {
return succeed()
}
}